]> git.codecow.com Git - nano25519.git/commitdiff
Move power function to its own module so it can have its own temporary buffers.
authorChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 06:39:02 +0000 (22:39 -0800)
committerChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 06:39:02 +0000 (22:39 -0800)
src/assembly/crypto_verify.ts
src/assembly/ed25519/fe.ts
src/assembly/ed25519/ge.ts
src/assembly/ed25519/pow22523.ts [new file with mode: 0644]

index 6c0d5f6a2f91becbf762a43949522396c3a74354..b8c206ea093422b11035ededfafa6f490b6088c2 100644 (file)
@@ -6,7 +6,7 @@ import { Blake2b } from './ed25519/blake2b'
 import { ge_accept_small_order, ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate, ge_is_canonical, ge_reject_small_order } from './ed25519/ge'
 import { p3, p3_0, p3_sub_p3, p3_tobytes, p_cached } from './ed25519/p'
 import { sc_is_canonical, sc_reduce } from './ed25519/sc'
-import { clear_t, t_fe_0, t_fe_1, t_fe_2 } from './ed25519/t'
+import { clear_t, t_fe_0 } from './ed25519/t'
 import { equalbytes } from './ed25519/utils'
 
 // crypto_hash function
@@ -40,7 +40,7 @@ const t_u8x32 = new StaticArray<u8>(32)
  * @returns null if public key fails to decode, else return pointer to Ai
  */
 export function crypto_verify_decodepubkey (pub: StaticArray<u8>): StaticArray<p_cached> | null {
-       const b = ge_frombytes_negate(A, pub, t_fe_0, t_fe_1, t_fe_2, t_u8x32)
+       const b = ge_frombytes_negate(A, pub, t_fe_0, t_u8x32)
        clear_t()
        if (b != 0) {
                p3_0(A)
@@ -162,7 +162,7 @@ export function verify_pubkey (pub: StaticArray<u8>): boolean {
        }
 
        // fail if public key cannot be decoded
-       if (ge_frombytes_negate(A, pub, t_fe_0, t_fe_1, t_fe_2, t_u8x32) != 0) {
+       if (ge_frombytes_negate(A, pub, t_fe_0, t_u8x32) != 0) {
                p3_0(A)
                clear_t()
                return false
@@ -200,7 +200,7 @@ export function verify_signature (sig: StaticArray<u8>): boolean {
        }
 
        // fail if nonce `R` does not decode to a valid curve point
-       if (ge_frombytes(expected_r, sig, t_fe_0, t_fe_1, t_fe_2, t_u8x32) != 0) {
+       if (ge_frombytes(expected_r, sig, t_fe_0, t_u8x32) != 0) {
                clear_t()
                return false
        }
index ab380953b0b5486040ebdbf8088d5ccd8045f02d..0a608ab9799d085a96a897ef92aea9ca82f8433f 100644 (file)
@@ -1210,44 +1210,6 @@ export function fe_neg (h: FieldElement, f: FieldElement): void {
        v128.store_lane<i64>(h_ptr, v128.neg<i32>(f2), 0, 2 << 4)
 }
 
-/**
- * returns z^((p-5)/8) = z^(2²⁵²-3)
- * used to compute square roots since we have p=5 (mod 8); see Cohen and Frey.
- */
-export function fe_pow22523 (out: FieldElement, z: FieldElement, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement): void {
-       fe_sq(t_fe_0, z)
-       fe_sq_n(t_fe_1, t_fe_0, 2)
-       fe_mul(t_fe_1, t_fe_1, z)
-       fe_mul(t_fe_0, t_fe_1, t_fe_0)
-
-       fe_sq(t_fe_0, t_fe_0)
-       fe_mul(t_fe_0, t_fe_1, t_fe_0)
-
-       fe_sq_n(t_fe_1, t_fe_0, 5)
-       fe_mul(t_fe_0, t_fe_1, t_fe_0)
-
-       fe_sq_n(t_fe_1, t_fe_0, 10)
-       fe_mul(t_fe_1, t_fe_1, t_fe_0)
-
-       fe_sq_n(t_fe_2, t_fe_1, 20)
-       fe_mul(t_fe_1, t_fe_2, t_fe_1)
-
-       fe_sq_n(t_fe_1, t_fe_1, 10)
-       fe_mul(t_fe_0, t_fe_1, t_fe_0)
-
-       fe_sq_n(t_fe_1, t_fe_0, 50)
-       fe_mul(t_fe_1, t_fe_1, t_fe_0)
-
-       fe_sq_n(t_fe_2, t_fe_1, 100)
-       fe_mul(t_fe_1, t_fe_2, t_fe_1)
-
-       fe_sq_n(t_fe_1, t_fe_1, 50)
-       fe_mul(t_fe_0, t_fe_1, t_fe_0)
-
-       fe_sq_n(t_fe_0, t_fe_0, 2)
-       fe_mul(out, t_fe_0, z)
-}
-
 /**
  * Preconditions:
  * |h| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
index 942b3a98c3ea63ad0e41fe941a1f240c4fa48e54..a4c05c213dfb5bb0860fc2ed7e00f7f55e03d3f2 100644 (file)
@@ -19,8 +19,9 @@
 */
 import { base } from './base'
 import { base2 } from './base2'
-import { FieldElement, fe, fe_1, fe_add, fe_addsub, fe_cmov, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_mul_d, fe_mul_pair_sqrtm1, fe_mul_sqrtm1, fe_neg, fe_pow22523, fe_sq, fe_sub } from './fe'
+import { FieldElement, fe, fe_1, fe_add, fe_addsub, fe_cmov, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_mul_d, fe_mul_pair_sqrtm1, fe_mul_sqrtm1, fe_neg, fe_sq, fe_sub } from './fe'
 import { p1p1, p1p1_0, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_cached, p3_add_cached_output_cached, p3_add_precomp, p3_dbl, p3_dbl_to_p3, p3_sub_cached, p3_sub_precomp, p3_to_cached, p3_tobytes, p_cached, p_precomp, precomp_0 } from './p'
+import { fe_pow22523 } from './pow22523'
 
 function ge_cmov8_base (t: p_precomp, bp: usize, b: i8): void {
        const isbneg: i8 = (b >> 7) & 1 /* 0: positive; 1: negative */
@@ -83,9 +84,7 @@ const ge_frombytes_x_sqrtm1: FieldElement = fe()
  *
  * @returns {i32} 0 if `s` decodes to a valid point, else -1
  */
-export function ge_frombytes (h: p3, s: StaticArray<u8>,
-       t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_u8x32: StaticArray<u8>
-): i32 {
+export function ge_frombytes (h: p3, s: StaticArray<u8>, t_fe_0: FieldElement, t_u8x32: StaticArray<u8>): i32 {
        const u = ge_frombytes_u
        const v = ge_frombytes_v
        const vx2 = ge_frombytes_vx2
@@ -112,7 +111,7 @@ export function ge_frombytes (h: p3, s: StaticArray<u8>,
        // x = uv
        fe_mul(h.X, u, v)
        // x = (uv)⁽𐞥⁻⁵⁾⸍⁸
-       fe_pow22523(h.X, h.X, t_fe_0, t_fe_1, t_fe_2)
+       fe_pow22523(h.X, h.X)
        // x = u(uv)⁽𐞥⁻⁵⁾⸍⁸
        fe_mul(h.X, u, h.X)
 
@@ -146,10 +145,8 @@ export function ge_frombytes (h: p3, s: StaticArray<u8>,
  *
  * @returns {i32} 0 if `s` decodes to a valid point, else -1
  */
-export function ge_frombytes_negate (h: p3, s: StaticArray<u8>,
-       t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_u8x32: StaticArray<u8>
-): i32 {
-       const check = ge_frombytes(h, s, t_fe_0, t_fe_1, t_fe_2, t_u8x32)
+export function ge_frombytes_negate (h: p3, s: StaticArray<u8>, t_fe_0: FieldElement, t_u8x32: StaticArray<u8>): i32 {
+       const check = ge_frombytes(h, s, t_fe_0, t_u8x32)
        fe_neg(h.X, h.X)
        fe_neg(h.T, h.T)
        return check
diff --git a/src/assembly/ed25519/pow22523.ts b/src/assembly/ed25519/pow22523.ts
new file mode 100644 (file)
index 0000000..c2a487e
--- /dev/null
@@ -0,0 +1,47 @@
+//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
+//! SPDX-License-Identifier: GPL-3.0-or-later
+
+import { FieldElement, fe, fe_mul, fe_sq, fe_sq_n } from './fe'
+
+const a = fe()
+const b = fe()
+const c = fe()
+
+/**
+ * `z⁽ᵖ⁻⁵⁾⸍⁸ = z^(2²⁵²-3)`
+ *
+ * Used to compute square roots since we have p=5 (mod 8); see Cohen and Frey.
+ */
+export function fe_pow22523 (out: FieldElement, z: FieldElement): void {
+       fe_sq(a, z)
+       fe_sq_n(b, a, 2)
+       fe_mul(b, b, z)
+       fe_mul(a, b, a)
+
+       fe_sq(a, a)
+       fe_mul(a, b, a)
+
+       fe_sq_n(b, a, 5)
+       fe_mul(a, b, a)
+
+       fe_sq_n(b, a, 10)
+       fe_mul(b, b, a)
+
+       fe_sq_n(c, b, 20)
+       fe_mul(b, c, b)
+
+       fe_sq_n(b, b, 10)
+       fe_mul(a, b, a)
+
+       fe_sq_n(b, a, 50)
+       fe_mul(b, b, a)
+
+       fe_sq_n(c, b, 100)
+       fe_mul(b, c, b)
+
+       fe_sq_n(b, b, 50)
+       fe_mul(a, b, a)
+
+       fe_sq_n(a, a, 2)
+       fe_mul(out, a, z)
+}