From: Chris Duncan Date: Wed, 4 Nov 2026 14:01:31 +0000 (-0800) Subject: Reorganize group op modules. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=361bbd435901f94403ec05f3e086d322e5a110c7;p=nano25519.git Reorganize group op modules. --- diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge/ge.ts similarity index 63% rename from src/assembly/ed25519/ge.ts rename to src/assembly/ed25519/ge/ge.ts index 6b54467..0e45dd9 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge/ge.ts @@ -17,60 +17,10 @@ * - p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T * - precomp (Duif): (y+x,y-x,2dxy) */ -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_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 './z' - -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)) - - // identity: (1, 1, 0) - // yplusx = 1 - let p0 = i32x4(1, 0, 0, 0), p1 = i32x4(0, 0, 0, 0), p2 = i32x4(0, 0, 0, 0) - // yminusx = 1 - let m0 = i32x4(1, 0, 0, 0), m1 = i32x4(0, 0, 0, 0), m2 = i32x4(0, 0, 0, 0) - // xy2d = 0 - let d0 = i32x4(0, 0, 0, 0), d1 = i32x4(0, 0, 0, 0), d2 = i32x4(0, 0, 0, 0) - - // base pointer offset += 144n = (4 bytes * 12 values * 3 fields) * n - for (let i: i8 = 1; i <= 8; i++, bp += 144) { - const isbabs: i8 = (((babs ^ i) - 1) >> 7) & 1 /* xor == 0: yes; xor == 1..255: no */ - const c = v128.splat(0 - u64(isbabs)) - p0 = v128.bitselect(v128.load(bp), p0, c) - p1 = v128.bitselect(v128.load(bp, 16), p1, c) - p2 = v128.bitselect(v128.load(bp, 32), p2, c) - m0 = v128.bitselect(v128.load(bp, 48), m0, c) - m1 = v128.bitselect(v128.load(bp, 64), m1, c) - m2 = v128.bitselect(v128.load(bp, 80), m2, c) - d0 = v128.bitselect(v128.load(bp, 96), d0, c) - d1 = v128.bitselect(v128.load(bp, 112), d1, c) - d2 = v128.bitselect(v128.load(bp, 128), d2, c) - } - - // -(x, y) == (-x, y); for -x, y+x <-> y-x and negate xy2d - const n = v128.splat(0 - u64(isbneg)) - const tp = changetype(t.yplusx) - const tm = changetype(t.yminusx) - const td = changetype(t.xy2d) - - // y+x = y-x if n, else y+x - v128.store(tp, v128.bitselect(m0, p0, n)) - v128.store(tp, v128.bitselect(m1, p1, n), 16) - v128.store_lane(tp, v128.bitselect(m2, p2, n), 0, 32) - - // y-x = y+x if n, else y-x - v128.store(tm, v128.bitselect(p0, m0, n)) - v128.store(tm, v128.bitselect(p1, m1, n), 16) - v128.store_lane(tm, v128.bitselect(p2, m2, n), 0, 32) - - // xy2d = -xy2d if n, else xy2d - v128.store(td, v128.bitselect(v128.neg(d0), d0, n)) - v128.store(td, v128.bitselect(v128.neg(d1), d1, n), 16) - v128.store_lane(td, v128.bitselect(v128.neg(d2), d2, n), 0, 32) -} +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_sq, fe_sub } from '../fe' +import { p1p1, 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_sub_cached, p3_sub_precomp, p3_to_cached, p_cached } from '../p' +import { fe_pow22523 } from '../z' const ge_frombytes_u: FieldElement = fe() const ge_frombytes_v: FieldElement = fe() @@ -227,87 +177,6 @@ export function ge_is_canonical (s: StaticArray): i32 { return 0 - (c & d & 1) } -const ge_scalarmult_base_tobytes_h: p3 = new p3() -const ge_scalarmult_base_tobytes_e: StaticArray = new StaticArray(64) -const ge_scalarmult_base_tobytes_r: p1p1 = new p1p1() -const ge_scalarmult_base_tobytes_s: p2 = new p2() -const ge_scalarmult_base_tobytes_t: p_precomp = new p_precomp() -/** - * `h = a * B` (with precomputation) - * - * where - * - * `a = 256⁰a[0] + 256¹a[1] + ... + 256³¹a[31]` - * - * B is the Ed25519 base point `(x,⅘)` with `x` positive. As bytes: - * - * `0x5866666666666666666666666666666666666666666666666666666666666666` - * - * Preconditions: - * - `a[31] ≤ 127` - * - * @param S 32-byte output buffer for scalar * base point - * @param a 32-byte input buffer for scalar - */ -export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray): void { - const h = ge_scalarmult_base_tobytes_h - const e = ge_scalarmult_base_tobytes_e - const r = ge_scalarmult_base_tobytes_r - const s = ge_scalarmult_base_tobytes_s - const t = ge_scalarmult_base_tobytes_t - - for (let i = 0; i < 32; ++i) { - e[(i << 1) + 0] = (a[i] >> 0) & 15 - e[(i << 1) + 1] = (a[i] >> 4) & 15 - } - /* each e[i] is between 0 and 15 */ - /* e[63] is between 0 and 7 */ - - let carry: i8 = 0 - for (let i = 0; i < 63; ++i) { - e[i] += carry - carry = e[i] + 8 - carry >>= 4 - e[i] -= carry * (i8(1) << 4) - } - e[63] += carry - /* each e[i] is between -8 and 8 */ - - p3_0(h) - - // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes - for (let i = 1, bp = 0; i < 64; i += 2, bp += 1152) { - ge_cmov8_base(t, base + bp, load(changetype(e) + i)) - p3_add_precomp(r, h, t) - p1p1_to_p3(h, r) - } - - p3_dbl(r, h) - p1p1_to_p2(s, r) - p2_dbl(r, s) - p1p1_to_p2(s, r) - p2_dbl(r, s) - p1p1_to_p2(s, r) - p2_dbl(r, s) - p1p1_to_p3(h, r) - - // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes - for (let i = 0, bp = 0; i < 64; i += 2, bp += 1152) { - ge_cmov8_base(t, base + bp, load(changetype(e) + i)) - p3_add_precomp(r, h, t) - p1p1_to_p3(h, r) - } - - p3_tobytes(S, h) - - // clear sensitive data - p1p1_0(r) - p2_0(s) - precomp_0(t) - p3_0(h) - e.fill(0) -} - /** * Computes a lookup table for multiples of the odd limbs of an extended * projective point so it can be cached and reused for performance. diff --git a/src/assembly/ed25519/ge/index.ts b/src/assembly/ed25519/ge/index.ts new file mode 100644 index 0000000..0deb953 --- /dev/null +++ b/src/assembly/ed25519/ge/index.ts @@ -0,0 +1,21 @@ +//! SPDX-FileCopyrightText: 2013-2026 Frank Denis +//! SPDX-FileCopyrightText: 2026 Chris Duncan +//! SPDX-License-Identifier: ISC + +/** + * ge means group element. + * + * Here the group is the set of pairs (x,y) of field elements satisfying + * + * -x² + y² = 1 + dx²y² + * + * where d = -121665/121666. + * + * Representations: + * - p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z + * - p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT + * - p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T + * - precomp (Duif): (y+x,y-x,2dxy) +*/ +export { 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 './ge' +export { ge_scalarmult_base_tobytes } from './scalarmult_base' diff --git a/src/assembly/ed25519/ge/scalarmult_base.ts b/src/assembly/ed25519/ge/scalarmult_base.ts new file mode 100644 index 0000000..e1e60ca --- /dev/null +++ b/src/assembly/ed25519/ge/scalarmult_base.ts @@ -0,0 +1,131 @@ +//! SPDX-FileCopyrightText: 2013-2026 Frank Denis +//! SPDX-FileCopyrightText: 2026 Chris Duncan +//! SPDX-License-Identifier: ISC + +import { base } from '../base' +import { p1p1, p1p1_0, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_precomp, p3_dbl, p3_tobytes, p_precomp, precomp_0 } from '../p' + +const h: p3 = new p3() +const e: StaticArray = new StaticArray(64) +const r: p1p1 = new p1p1() +const s: p2 = new p2() +const t: p_precomp = new p_precomp() + +/** + * `h = a * B` (with precomputation) + * + * where + * + * `a = 256⁰a[0] + 256¹a[1] + ... + 256³¹a[31]` + * + * B is the Ed25519 base point `(x,⅘)` with `x` positive. As bytes: + * + * `0x5866666666666666666666666666666666666666666666666666666666666666` + * + * Preconditions: + * - `a[31] ≤ 127` + * + * @param S 32-byte output buffer for scalar * base point + * @param a 32-byte input buffer for scalar + */ +export function ge_scalarmult_base_tobytes (S: StaticArray, a: StaticArray): void { + for (let i = 0; i < 32; ++i) { + e[(i << 1) + 0] = (a[i] >> 0) & 15 + e[(i << 1) + 1] = (a[i] >> 4) & 15 + } + /* each e[i] is between 0 and 15 */ + /* e[63] is between 0 and 7 */ + + let carry: i8 = 0 + for (let i = 0; i < 63; ++i) { + e[i] += carry + carry = e[i] + 8 + carry >>= 4 + e[i] -= carry * (i8(1) << 4) + } + e[63] += carry + /* each e[i] is between -8 and 8 */ + + p3_0(h) + + // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes + for (let i = 1, bp = 0; i < 64; i += 2, bp += 1152) { + ge_cmov8_base(t, base + bp, load(changetype(e) + i)) + p3_add_precomp(r, h, t) + p1p1_to_p3(h, r) + } + + p3_dbl(r, h) + p1p1_to_p2(s, r) + p2_dbl(r, s) + p1p1_to_p2(s, r) + p2_dbl(r, s) + p1p1_to_p2(s, r) + p2_dbl(r, s) + p1p1_to_p3(h, r) + + // base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes + for (let i = 0, bp = 0; i < 64; i += 2, bp += 1152) { + ge_cmov8_base(t, base + bp, load(changetype(e) + i)) + p3_add_precomp(r, h, t) + p1p1_to_p3(h, r) + } + + p3_tobytes(S, h) + + // clear sensitive data + p1p1_0(r) + p2_0(s) + precomp_0(t) + p3_0(h) + e.fill(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)) + + // identity: (1, 1, 0) + // yplusx = 1 + let p0 = i32x4(1, 0, 0, 0), p1 = i32x4(0, 0, 0, 0), p2 = i32x4(0, 0, 0, 0) + // yminusx = 1 + let m0 = i32x4(1, 0, 0, 0), m1 = i32x4(0, 0, 0, 0), m2 = i32x4(0, 0, 0, 0) + // xy2d = 0 + let d0 = i32x4(0, 0, 0, 0), d1 = i32x4(0, 0, 0, 0), d2 = i32x4(0, 0, 0, 0) + + // base pointer offset += 144n = (4 bytes * 12 values * 3 fields) * n + for (let i: i8 = 1; i <= 8; i++, bp += 144) { + const isbabs: i8 = (((babs ^ i) - 1) >> 7) & 1 /* xor == 0: yes; xor == 1..255: no */ + const c = v128.splat(0 - u64(isbabs)) + p0 = v128.bitselect(v128.load(bp), p0, c) + p1 = v128.bitselect(v128.load(bp, 16), p1, c) + p2 = v128.bitselect(v128.load(bp, 32), p2, c) + m0 = v128.bitselect(v128.load(bp, 48), m0, c) + m1 = v128.bitselect(v128.load(bp, 64), m1, c) + m2 = v128.bitselect(v128.load(bp, 80), m2, c) + d0 = v128.bitselect(v128.load(bp, 96), d0, c) + d1 = v128.bitselect(v128.load(bp, 112), d1, c) + d2 = v128.bitselect(v128.load(bp, 128), d2, c) + } + + // -(x, y) == (-x, y); for -x, y+x <-> y-x and negate xy2d + const n = v128.splat(0 - u64(isbneg)) + const tp = changetype(t.yplusx) + const tm = changetype(t.yminusx) + const td = changetype(t.xy2d) + + // y+x = y-x if n, else y+x + v128.store(tp, v128.bitselect(m0, p0, n)) + v128.store(tp, v128.bitselect(m1, p1, n), 16) + v128.store_lane(tp, v128.bitselect(m2, p2, n), 0, 32) + + // y-x = y+x if n, else y-x + v128.store(tm, v128.bitselect(p0, m0, n)) + v128.store(tm, v128.bitselect(p1, m1, n), 16) + v128.store_lane(tm, v128.bitselect(p2, m2, n), 0, 32) + + // xy2d = -xy2d if n, else xy2d + v128.store(td, v128.bitselect(v128.neg(d0), d0, n)) + v128.store(td, v128.bitselect(v128.neg(d1), d1, n), 16) + v128.store_lane(td, v128.bitselect(v128.neg(d2), d2, n), 0, 32) +}