From: Chris Duncan Date: Tue, 3 Nov 2026 06:39:02 +0000 (-0800) Subject: Move power function to its own module so it can have its own temporary buffers. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=1ff409eb2f611729dcd571c1248e279f4ac8dc1d;p=nano25519.git Move power function to its own module so it can have its own temporary buffers. --- diff --git a/src/assembly/crypto_verify.ts b/src/assembly/crypto_verify.ts index 6c0d5f6..b8c206e 100644 --- a/src/assembly/crypto_verify.ts +++ b/src/assembly/crypto_verify.ts @@ -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(32) * @returns null if public key fails to decode, else return pointer to Ai */ export function crypto_verify_decodepubkey (pub: StaticArray): StaticArray | 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): 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): 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 } diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index ab38095..0a608ab 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -1210,44 +1210,6 @@ export function fe_neg (h: FieldElement, f: FieldElement): void { v128.store_lane(h_ptr, v128.neg(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. diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index 942b3a9..a4c05c2 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -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, - t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_u8x32: StaticArray -): i32 { +export function ge_frombytes (h: p3, s: StaticArray, t_fe_0: FieldElement, t_u8x32: StaticArray): 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, // 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, * * @returns {i32} 0 if `s` decodes to a valid point, else -1 */ -export function ge_frombytes_negate (h: p3, s: StaticArray, - t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_u8x32: StaticArray -): 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, t_fe_0: FieldElement, t_u8x32: StaticArray): 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 index 0000000..c2a487e --- /dev/null +++ b/src/assembly/ed25519/pow22523.ts @@ -0,0 +1,47 @@ +//! SPDX-FileCopyrightText: 2026 Chris Duncan +//! 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) +}