]> git.codecow.com Git - nano25519.git/commitdiff
Rearrange functions.
authorChris Duncan <chris@codecow.com>
Fri, 30 Oct 2026 21:40:31 +0000 (14:40 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 30 Oct 2026 21:40:31 +0000 (14:40 -0700)
src/assembly/ed25519/ge.ts

index 2adc9d534828878aec62fb0359e8f7a7cf73a266..4eb86063b83986f98685171e6b91e4ad27182a58 100644 (file)
@@ -71,26 +71,32 @@ function ge_cmov8_base (t: p_precomp, bp: usize, b: i8): void {
        v128.store_lane<u64>(td, v128.bitselect(v128.neg<i32>(d2), d2, n), 0, 32)
 }
 
-const ge_frombytes_negate_vartime_u: FieldElement = fe()
-const ge_frombytes_negate_vartime_v: FieldElement = fe()
-const ge_frombytes_negate_vartime_vx2: FieldElement = fe()
-const ge_frombytes_negate_vartime_check: FieldElement = fe()
+const ge_frombytes_u: FieldElement = fe()
+const ge_frombytes_v: FieldElement = fe()
+const ge_frombytes_vx2: FieldElement = fe()
+const ge_frombytes_m_root_check: FieldElement = fe()
+const ge_frombytes_p_root_check: FieldElement = fe()
+const ge_frombytes_negx: FieldElement = fe()
+const ge_frombytes_x_sqrtm1: FieldElement = fe()
 /**
- * Variable-time point negation converted from byte representation.
- *
- * Compiler optimization of vartime function is OK.
+ * Constant-time point conversion from byte representation.
  *
  * @returns {i32} 0 if `s` decodes to a valid point, else -1
  */
-export function ge_frombytes_negate_vartime (h: p3, s: StaticArray<u8>,
-       t_u8x32: StaticArray<u8>, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement
+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 {
-       const u = ge_frombytes_negate_vartime_u
-       const v = ge_frombytes_negate_vartime_v
-       const vx2 = ge_frombytes_negate_vartime_vx2
-       const check = ge_frombytes_negate_vartime_check
+       const u = ge_frombytes_u
+       const v = ge_frombytes_v
+       const vx2 = ge_frombytes_vx2
+       const m_root_check = ge_frombytes_m_root_check
+       const p_root_check = ge_frombytes_p_root_check
+       const negx = ge_frombytes_negx
+       const x_sqrtm1 = ge_frombytes_x_sqrtm1
+
+       let has_m_root: u8
+       let has_p_root: u8
 
-       // Decode bytes to y-coordinate
        fe_frombytes(h.Y, s)
        fe_1(h.Z)
 
@@ -112,58 +118,49 @@ export function ge_frombytes_negate_vartime (h: p3, s: StaticArray<u8>,
 
        // vx2 = x²
        fe_sq(vx2, h.X)
-       // vx2 = vx²
-       fe_mul(vx2, vx2, v)
+       // vx2 = vx², x_sqrtm1 = x√-1
+       fe_mul_pair_sqrtm1(vx2, vx2, v, x_sqrtm1, h.X)
 
+       // p = vx²+u
        // m = vx²-u
-       fe_sub(check, vx2, u)
-       if (fe_iszero(check, t_fe_0, t_u8x32) == 0) {
-               // p = vx²+u
-               fe_add(check, vx2, u)
-               if (fe_iszero(check, t_fe_0, t_u8x32) == 0) {
-                       // Reject invalid point
-                       return -1
-               }
-               // x = x√-1
-               fe_mul_sqrtm1(h.X, h.X)
-       }
+       fe_addsub(p_root_check, m_root_check, vx2, u)
+       has_p_root = fe_iszero(p_root_check, t_fe_0, t_u8x32)
+       has_m_root = fe_iszero(m_root_check, t_fe_0, t_u8x32)
 
+       // x = x√-1 if vx² = -u, else x
+       fe_cmov(h.X, x_sqrtm1, 1 - has_m_root)
+
+       // negx = -x
+       fe_neg(negx, h.X)
        // x = |x|
-       if (fe_isnegative(h.X, t_fe_0) == (s[31] >> 7)) {
-               fe_neg(h.X, h.X)
-       }
+       fe_cmov(h.X, negx, fe_isnegative(h.X, t_fe_0) ^ (s[31] >> 7))
+
        // Create point projection
        fe_mul(h.T, h.X, h.Y)
 
-       return 0
+       return i32(has_m_root | has_p_root) - 1
 }
 
-const ge_frombytes_u: FieldElement = fe()
-const ge_frombytes_v: FieldElement = fe()
-const ge_frombytes_vx2: FieldElement = fe()
-const ge_frombytes_m_root_check: FieldElement = fe()
-const ge_frombytes_p_root_check: FieldElement = fe()
-const ge_frombytes_negx: FieldElement = fe()
-const ge_frombytes_x_sqrtm1: FieldElement = fe()
+const ge_frombytes_negate_vartime_u: FieldElement = fe()
+const ge_frombytes_negate_vartime_v: FieldElement = fe()
+const ge_frombytes_negate_vartime_vx2: FieldElement = fe()
+const ge_frombytes_negate_vartime_check: FieldElement = fe()
 /**
- * Constant-time point conversion from byte representation.
+ * Variable-time point negation converted from byte representation.
+ *
+ * Compiler optimization of vartime function is OK.
  *
  * @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>
+export function ge_frombytes_negate_vartime (h: p3, s: StaticArray<u8>,
+       t_u8x32: StaticArray<u8>, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement
 ): i32 {
-       const u = ge_frombytes_u
-       const v = ge_frombytes_v
-       const vx2 = ge_frombytes_vx2
-       const m_root_check = ge_frombytes_m_root_check
-       const p_root_check = ge_frombytes_p_root_check
-       const negx = ge_frombytes_negx
-       const x_sqrtm1 = ge_frombytes_x_sqrtm1
-
-       let has_m_root: u8
-       let has_p_root: u8
+       const u = ge_frombytes_negate_vartime_u
+       const v = ge_frombytes_negate_vartime_v
+       const vx2 = ge_frombytes_negate_vartime_vx2
+       const check = ge_frombytes_negate_vartime_check
 
+       // Decode bytes to y-coordinate
        fe_frombytes(h.Y, s)
        fe_1(h.Z)
 
@@ -185,27 +182,30 @@ export function ge_frombytes (h: p3, s: StaticArray<u8>,
 
        // vx2 = x²
        fe_sq(vx2, h.X)
-       // vx2 = vx², x_sqrtm1 = x√-1
-       fe_mul_pair_sqrtm1(vx2, vx2, v, x_sqrtm1, h.X)
+       // vx2 = vx²
+       fe_mul(vx2, vx2, v)
 
-       // p = vx²+u
        // m = vx²-u
-       fe_addsub(p_root_check, m_root_check, vx2, u)
-       has_p_root = fe_iszero(p_root_check, t_fe_0, t_u8x32)
-       has_m_root = fe_iszero(m_root_check, t_fe_0, t_u8x32)
-
-       // x = x√-1 if vx² = -u, else x
-       fe_cmov(h.X, x_sqrtm1, 1 - has_m_root)
+       fe_sub(check, vx2, u)
+       if (fe_iszero(check, t_fe_0, t_u8x32) == 0) {
+               // p = vx²+u
+               fe_add(check, vx2, u)
+               if (fe_iszero(check, t_fe_0, t_u8x32) == 0) {
+                       // Reject invalid point
+                       return -1
+               }
+               // x = x√-1
+               fe_mul_sqrtm1(h.X, h.X)
+       }
 
-       // negx = -x
-       fe_neg(negx, h.X)
        // x = |x|
-       fe_cmov(h.X, negx, fe_isnegative(h.X, t_fe_0) ^ (s[31] >> 7))
-
+       if (fe_isnegative(h.X, t_fe_0) == (s[31] >> 7)) {
+               fe_neg(h.X, h.X)
+       }
        // Create point projection
        fe_mul(h.T, h.X, h.Y)
 
-       return i32(has_m_root | has_p_root) - 1
+       return 0
 }
 
 /**