]> git.codecow.com Git - nano25519.git/commitdiff
Call byte decoding instead of implementing twice.
authorChris Duncan <chris@codecow.com>
Fri, 30 Oct 2026 21:43:33 +0000 (14:43 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 30 Oct 2026 21:43:33 +0000 (14:43 -0700)
src/assembly/ed25519/ge.ts

index 4eb86063b83986f98685171e6b91e4ad27182a58..663eef17f282a93f7c07a2a95cc50182650a2ac5 100644 (file)
@@ -79,7 +79,7 @@ const ge_frombytes_p_root_check: FieldElement = fe()
 const ge_frombytes_negx: FieldElement = fe()
 const ge_frombytes_x_sqrtm1: FieldElement = fe()
 /**
- * Constant-time point conversion from byte representation.
+ * Constant-time decoding from bytes to a point.
  *
  * @returns {i32} 0 if `s` decodes to a valid point, else -1
  */
@@ -141,71 +141,18 @@ export function ge_frombytes (h: p3, s: StaticArray<u8>,
        return i32(has_m_root | has_p_root) - 1
 }
 
-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()
 /**
- * Variable-time point negation converted from byte representation.
- *
- * Compiler optimization of vartime function is OK.
+ * Decode the byte representation of a point and negate it.
  *
  * @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
 ): 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
-
-       // Decode bytes to y-coordinate
-       fe_frombytes(h.Y, s)
-       fe_1(h.Z)
-
-       // u = y²
-       fe_sq(u, h.Y)
-       // v = dy²
-       fe_mul_d(v, u)
-       // u = y²-1
-       u[0] -= 1
-       // v = dy²+1
-       v[0] += 1
-
-       // 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)
-       // x = u(uv)⁽𐞥⁻⁵⁾⸍⁸
-       fe_mul(h.X, u, h.X)
-
-       // vx2 = x²
-       fe_sq(vx2, h.X)
-       // vx2 = vx²
-       fe_mul(vx2, vx2, v)
-
-       // 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)
-       }
-
-       // x = |x|
-       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 0
+       const check = ge_frombytes(h, s, t_fe_0, t_fe_1, t_fe_2, t_u8x32)
+       fe_neg(h.X, h.X)
+       fe_neg(h.T, h.T)
+       return check
 }
 
 /**