]> git.codecow.com Git - nano25519.git/commitdiff
Move field element inversion into pow22523 module for better code reuse.
authorChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 14:39:14 +0000 (06:39 -0800)
committerChris Duncan <chris@codecow.com>
Tue, 3 Nov 2026 14:39:14 +0000 (06:39 -0800)
src/assembly/ed25519/fe.ts
src/assembly/ed25519/p.ts
src/assembly/ed25519/pow22523.ts

index 0a84e6d58c44f3aa74231f03501bc5be345df3a6..4ffa4c2baca2bcaa5baad5486d4392212d96cf3e 100644 (file)
@@ -351,43 +351,6 @@ export function fe_frombytes (h: FieldElement, s: StaticArray<u8>): void {
        store<i32>(h_ptr, h9, 9 << 2)
 }
 
-/**
- * Inversion - sets `out` = 0 if `z` == 0
- */
-export function fe_invert (out: FieldElement, z: FieldElement, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement): void {
-       fe_sq(out, z)
-       fe_sq_n(t_fe_0, out, 2)
-       fe_mul(t_fe_0, z, t_fe_0)
-       fe_mul(out, out, t_fe_0)
-
-       fe_sq(t_fe_1, out)
-       fe_mul(t_fe_0, t_fe_0, t_fe_1)
-
-       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, 5)
-       fe_mul(out, t_fe_0, out)
-}
-
 /**
  * return 1 if f is in {1,3,5,...,q-2}
  * return 0 if f is in {0,2,4,...,q-1}
index d58ac26e910f2897fd5405acdc92870afb050319..352a4a61f277a1468ecb729b2ef69d041afd6be5 100644 (file)
@@ -2,7 +2,8 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: ISC
 
-import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_invert, fe_isnegative, fe_mul, fe_mul_2d, fe_mul_pair, fe_mul_pair_2d, fe_mul_pair_dbl, fe_mul_pair_dbl_2d, fe_sq_pair, fe_sq_pair_dbl, fe_sub, fe_tobytes } from './fe'
+import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_isnegative, fe_mul, fe_mul_2d, fe_mul_pair, fe_mul_pair_2d, fe_mul_pair_dbl, fe_mul_pair_dbl_2d, fe_sq_pair, fe_sq_pair_dbl, fe_sub, fe_tobytes } from './fe'
+import { fe_invert } from './pow22523'
 
 /**
  * Base class for building branded subclasses of coordinate systems.
@@ -364,7 +365,7 @@ export function p3_sub_precomp (r: p1p1, p: p3, q: p_precomp): void {
 }
 
 export function p3_tobytes (s: StaticArray<u8>, h: p3): void {
-       fe_invert(t_p3.T, h.Z, t_p3.X, t_p3.Y, t_p3.Z)
+       fe_invert(t_p3.T, h.Z)
        fe_mul_pair(t_p3.X, h.X, t_p3.T, t_p3.Y, h.Y, t_p3.T)
        fe_tobytes(s, t_p3.Y)
        s[31] ^= fe_isnegative(t_p3.X) << 7
index c2a487eb8ec90dcb72830f38066d16b294667f66..015b476a346d84102c604eefe2c2a204190e4c94 100644 (file)
@@ -6,19 +6,18 @@ import { FieldElement, fe, fe_mul, fe_sq, fe_sq_n } from './fe'
 const a = fe()
 const b = fe()
 const c = fe()
+const t = fe()
 
 /**
- * `z⁽ᵖ⁻⁵⁾⸍⁸ = z^(2²⁵²-3)`
- *
- * Used to compute square roots since we have p=5 (mod 8); see Cohen and Frey.
+ * Holds z¹¹ in `t` for final multiplication step of inversion.
  */
-export function fe_pow22523 (out: FieldElement, z: FieldElement): void {
+function fe_pow22501 (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_mul(t, b, a)
 
-       fe_sq(a, a)
+       fe_sq(a, t)
        fe_mul(a, b, a)
 
        fe_sq_n(b, a, 5)
@@ -40,8 +39,31 @@ export function fe_pow22523 (out: FieldElement, z: FieldElement): void {
        fe_mul(b, c, b)
 
        fe_sq_n(b, b, 50)
-       fe_mul(a, b, a)
+       fe_mul(out, b, a)
+}
 
-       fe_sq_n(a, a, 2)
-       fe_mul(out, a, z)
+/**
+ * Inversion - sets `out` = 0 if `z` == 0
+ *
+ * Depends on `pow22501` setting `t = z¹¹`. Do not change this assignment.
+ */
+export function fe_invert (out: FieldElement, z: FieldElement): void {
+       fe_pow22501(out, z)
+       fe_sq_n(out, out, 5)
+       fe_mul(out, t, out)
+}
+
+/**
+ * `z⁽ᵖ⁻⁵⁾⸍⁸ = z^(2²⁵²-3)`
+ *
+ * Used to compute square roots since we have p = 5 (mod 8); see Cohen and Frey.
+ *
+ * Does not need intermediate `z¹¹` result, so `t` is overwritable and thus
+ * reused for intermediate `pow22501` result since `out` can and does alias `z`
+ * and must not be used until the very last output step.
+ */
+export function fe_pow22523 (out: FieldElement, z: FieldElement): void {
+       fe_pow22501(t, z)
+       fe_sq_n(t, t, 2)
+       fe_mul(out, t, z)
 }