import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants'
import { Blake2b } from './ed25519/blake2b'
-import { fe } from './ed25519/fe'
+import { FieldElement, fe } from './ed25519/fe'
import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_has_small_order, ge_is_canonical } from './ed25519/ge'
import { ge_cached, ge_p1p1, ge_p3, ge_p3_tobytes, ge_sub_p3 } from './ed25519/p'
import { sc_is_canonical, sc_reduce } from './ed25519/sc'
const sb_ah = new ge_p3()
const check = new ge_p3()
const check_r = new StaticArray<u8>(32)
-const ge_cached_scratch = new ge_cached()
-const ge_p1p1_scratch = new ge_p1p1()
-const fe_scratch_0 = fe()
-const fe_scratch_1 = fe()
-const fe_scratch_2 = fe()
-const fe_scratch_3 = fe()
-const fe_scratch_4 = fe()
-const fe_scratch_5 = fe()
-const fe_scratch_6 = fe()
+const t_ge_cached = new ge_cached()
+const t_ge_p1p1 = new ge_p1p1()
+const t_u8x32 = new StaticArray<u8>(32)
+const t_fe = StaticArray.fromArray<FieldElement>([fe(), fe(), fe(), fe(), fe(), fe(), fe()])
/**
* Verify public key `pub` can be decoded.
sc_reduce(h)
ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
- ge_p3_tobytes(check_r, sb_ah,
- fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3, fe_scratch_4, fe_scratch_5, fe_scratch_6
- )
+ ge_p3_tobytes(check_r, sb_ah, t_fe[0], t_fe[1], t_fe[2], t_fe[3], t_fe[4], t_fe[5], t_fe[6])
return equalbytes(sig, check_r, KEY_BYTELENGTH)
}
sc_reduce(h)
ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
- ge_sub_p3(check, expected_r, sb_ah, fe_scratch_0, ge_cached_scratch, ge_p1p1_scratch)
+ ge_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1)
- return ge_has_small_order(check)
+ return ge_has_small_order(check, t_fe[0], t_u8x32)
}
/**
sc_reduce(h)
ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
- ge_sub_p3(check, expected_r, sb_ah, fe_scratch_0, ge_cached_scratch, ge_p1p1_scratch)
+ ge_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1)
- ge_p3_tobytes(check_r, sb_ah,
- fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3, fe_scratch_4, fe_scratch_5, fe_scratch_6
- )
+ ge_p3_tobytes(check_r, sb_ah, t_fe[0], t_fe[1], t_fe[2], t_fe[3], t_fe[4], t_fe[5], t_fe[6])
return equalbytes(sig, check_r, KEY_BYTELENGTH)
}
if (ge_frombytes_negate_vartime(A, pub) != 0) return false
// fail if public key `k` is small order
- if (ge_has_small_order(A) != 0) return false
+ if (ge_has_small_order(A, t_fe[0], t_u8x32) != 0) return false
return true
}
if (ge_is_canonical(sig) != 0) return false
// fail if nonce `R` does not decode to a valid curve point
- if (ge_frombytes(expected_r, sig) != 0) return false
+ if (ge_frombytes(expected_r, sig, t_fe[0], t_fe[1], t_fe[2], t_u8x32) != 0) return false
// fail if nonce `R` is small order
- if (ge_has_small_order(expected_r) != 0) return false
+ if (ge_has_small_order(expected_r, t_fe[0], t_u8x32) != 0) return false
return true
}
const ge_frombytes_p_root_check: FieldElement = fe()
const ge_frombytes_negx: FieldElement = fe()
const ge_frombytes_x_sqrtm1: FieldElement = fe()
-const ge_frombytes_u8x32_scratch = new StaticArray<u8>(32)
-const ge_frombytes_fe_scratch_0: FieldElement = fe()
-const ge_frombytes_fe_scratch_1: FieldElement = fe()
-const ge_frombytes_fe_scratch_2: FieldElement = fe()
/**
* Constant-time point conversion from byte representation.
*
* @returns {i32} 0 if `s` decodes to a valid point, else -1
*/
-export function ge_frombytes (h: ge_p3, s: StaticArray<u8>): i32 {
+export function ge_frombytes (h: ge_p3, s: StaticArray<u8>,
+ t0_fe: FieldElement, t1_fe: FieldElement, t2_fe: FieldElement, t_u8x32: StaticArray<u8>
+): i32 {
const u = ge_frombytes_u
const v = ge_frombytes_v
const vxx = ge_frombytes_vxx
const negx = ge_frombytes_negx
const x_sqrtm1 = ge_frombytes_x_sqrtm1
- const u8x32_scratch = ge_frombytes_u8x32_scratch
- const fe_scratch_0 = ge_frombytes_fe_scratch_0
- const fe_scratch_1 = ge_frombytes_fe_scratch_1
- const fe_scratch_2 = ge_frombytes_fe_scratch_2
-
let has_m_root: u8
let has_p_root: u8
/* x = u((uv)^((q-5)/8)) */
fe_mul(h.X, u, v)
- fe_pow22523(h.X, h.X, fe_scratch_0, fe_scratch_1, fe_scratch_2)
+ fe_pow22523(h.X, h.X, t0_fe, t1_fe, t2_fe)
fe_mul(h.X, u, h.X)
// vxx = vx²
// m = vx²-u
fe_sub(m_root_check, vxx, u)
- has_m_root = fe_iszero(m_root_check, u8x32_scratch, fe_scratch_0)
+ has_m_root = fe_iszero(m_root_check, t_u8x32, t0_fe)
// p = vx²+u
fe_add(p_root_check, vxx, u)
- has_p_root = fe_iszero(p_root_check, u8x32_scratch, fe_scratch_0)
+ has_p_root = fe_iszero(p_root_check, t_u8x32, t0_fe)
// x√-1
fe_mul(x_sqrtm1, h.X, fe_sqrtm1)
fe_cmov(h.X, x_sqrtm1, 1 - has_m_root)
fe_neg(negx, h.X)
- fe_cmov(h.X, negx, fe_isnegative(h.X, fe_scratch_0) ^ (s[31] >> 7))
+ fe_cmov(h.X, negx, fe_isnegative(h.X, t0_fe) ^ (s[31] >> 7))
fe_mul(h.T, h.X, h.Y)
return i32(has_m_root | has_p_root) - 1
const ge_has_small_order_y_sqrtm1: FieldElement = fe()
const ge_has_small_order_c: FieldElement = fe()
-const ge_has_small_order_t_u8x32 = new StaticArray<u8>(32)
-const ge_has_small_order_t_fe: FieldElement = fe()
/**
* @param {ge_p3} p
* @returns 1 if `p` has small order, else 0
*/
-export function ge_has_small_order (p: ge_p3): i32 {
+export function ge_has_small_order (p: ge_p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
const y_sqrtm1 = ge_has_small_order_y_sqrtm1
const c = ge_has_small_order_c
- const t_u8x32 = ge_has_small_order_t_u8x32
- const t_fe = ge_has_small_order_t_fe
-
let ret: i32 = 0
ret |= fe_iszero(p.X, t_u8x32, t_fe)
const ge_scalarmult_base_tobytes_r: ge_p1p1 = new ge_p1p1()
const ge_scalarmult_base_tobytes_s: ge_p2 = new ge_p2()
const ge_scalarmult_base_tobytes_t: ge_precomp = new ge_precomp()
-const ge_scalarmult_base_tobytes_fe_scratch_0: FieldElement = fe()
-const ge_scalarmult_base_tobytes_fe_scratch_1: FieldElement = fe()
-const ge_scalarmult_base_tobytes_fe_scratch_2: FieldElement = fe()
-const ge_scalarmult_base_tobytes_fe_scratch_3: FieldElement = fe()
-const ge_scalarmult_base_tobytes_fe_scratch_4: FieldElement = fe()
-const ge_scalarmult_base_tobytes_fe_scratch_5: FieldElement = fe()
-const ge_scalarmult_base_tobytes_fe_scratch_6: FieldElement = fe()
/**
* `h = a * B` (with precomputation)
*