From 1d6a91c6a6ff21bf5261bac827c1e826488cd03e Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 29 Oct 2026 20:08:45 -0700 Subject: [PATCH] Apply same preloaded vectors of Ed25519 constant d to fe_mul like 2d. --- src/assembly/ed25519/fe.ts | 43 +++++++++++++++++++++++++++++++++++++- src/assembly/ed25519/ge.ts | 21 +++---------------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/assembly/ed25519/fe.ts b/src/assembly/ed25519/fe.ts index db72962..bc9af58 100644 --- a/src/assembly/ed25519/fe.ts +++ b/src/assembly/ed25519/fe.ts @@ -17,6 +17,38 @@ */ import { load_3, load_4 } from './utils' +/** + * √-1 + */ +export const fe_sqrtm1: FieldElement = StaticArray.fromArray([ + -32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482, 0, 0 +]) + +/** + * Ed25519 curve constant d + * = −121665 / 121666 + * = 37095705934669439343138083508754565189542113879843219016388785533085940283555 + * = [-10913610, 13857413, -15372611, 6949391, 114729, -8787816, -6275908, -3247719, -18696448, -12055116, 0, 0] + * + * Shuffled for multiplication as the following, where `x` is a `FieldElement` placeholder for 0: + * + * `0123, 4567, 89xx, 9012, 3456, 78xx` + * + * Shuffles are also multiplied by 19 for limb wrapping, except limb 0 which does not wrap. + */ +const d_0123 = i32x4(-10913610, 13857413, -15372611, 6949391) +const d_4567 = i32x4(114729, -8787816, -6275908, -3247719) +const d_89xx = i32x4(-18696448, -12055116, 0, 0) +const d_9012 = i32x4(-12055116, -10913610, 13857413, -15372611) +const d_3456 = i32x4(6949391, 114729, -8787816, -6275908) +const d_78xx = i32x4(-3247719, -18696448, 0, 0) +const d_0123_19 = i32x4(-207358590, 263290847, -292079609, 132038429) +const d_4567_19 = i32x4(2179851, -166968504, -119242252, -61706661) +const d_89xx_19 = i32x4(-355232512, -229047204, 0, 0) +const d_9012_19 = i32x4(-229047204, -10913610, 263290847, -292079609) +const d_3456_19 = i32x4(132038429, 2179851, -166968504, -119242252) +const d_78xx_19 = i32x4(-61706661, -355232512, 0, 0) + /** * Ed25519 curve constant 2d * = −121665 / 60833 @@ -28,7 +60,6 @@ import { load_3, load_4 } from './utils' * `0123, 4567, 89xx, 9012, 3456, 78xx` * * Shuffles are also multiplied by 19 for limb wrapping, except limb 0 which does not wrap. - * */ const d2_0123 = i32x4(-21827239, -5839606, -30745221, 13898782) const d2_4567 = i32x4(229458, 15978800, -12551817, -6495438) @@ -593,6 +624,16 @@ export function fe_mul (f: FieldElement, a: FieldElement, b: FieldElement): void fe_extmul(f, a, b0123, b4567, b89xx, b9012, b3456, b78xx, b0123_19, b4567_19, b89xx_19, b9012_19, b3456_19, b78xx_19) } +/** + * Multiply a FieldElement with the Ed25519 constant `d` and store the product. + * + * @param f FieldElement product destination + * @param a FieldElement multiplicand source + */ +export function fe_mul_d (f: FieldElement, a: FieldElement): void { + fe_extmul(f, a, d_0123, d_4567, d_89xx, d_9012, d_3456, d_78xx, d_0123_19, d_4567_19, d_89xx_19, d_9012_19, d_3456_19, d_78xx_19) +} + /** * Multiply a FieldElement with the Ed25519 constant `2d` and store the product. * diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index 74bfb97..0b9ead6 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -19,24 +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_pair, 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, fe_neg, fe_pow22523, fe_sq, fe_sqrtm1, fe_sub } from './fe' import { p_cached, p1p1, p1p1_0, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_cached, p3_add_cached_to_cached, p3_add_precomp, p3_dbl, p3_dbl_to_p3, p3_sub_cached, p3_sub_precomp, p3_to_cached, p3_tobytes, p_precomp, precomp_0 } from './p' -/** - * √-1 - */ -const fe_sqrtm1: FieldElement = StaticArray.fromArray([ - -32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482, 0, 0 -]) - -/** - * d = −121665 / 121666 - * = 37095705934669439343138083508754565189542113879843219016388785533085940283555 - */ -const ed25519_d: FieldElement = StaticArray.fromArray([ - -10913610, 13857413, -15372611, 6949391, 114729, -8787816, -6275908, -3247719, -18696448, -12055116, 0, 0 -]) - function ge_cmov8_base (t: p_precomp, bp: usize, b: i8): void { const isbneg: i8 = (b >> 7) & 1 /* 0: positive; 1: negative */ const babs: i8 = b - (((-isbneg) & b) * (i8(1) << 1)) @@ -115,7 +100,7 @@ export function ge_frombytes_negate_vartime (h: p3, s: StaticArray, // u = y²-1 // v = dy²+1 fe_sq(u, h.Y) - fe_mul(v, u, ed25519_d) + fe_mul_d(v, u) u[0] -= 1 v[0] += 1 @@ -190,7 +175,7 @@ export function ge_frombytes (h: p3, s: StaticArray, // u = y²-1 // v = dy²+1 fe_sq(u, h.Y) - fe_mul(v, u, ed25519_d) + fe_mul_d(v, u) u[0] -= 1 v[0] += 1 -- 2.52.0