*/
export function crypto_verify_decodepubkey (pub: StaticArray<u8>): ge_p3 | null {
// fail if public key cannot be decoded
- return (ge_frombytes_negate_vartime(A, pub) != 0) ? null : A
+ return (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe[0], t_fe[1], t_fe[2]) != 0) ? null : A
}
/**
blake2b.init().update(sig, 32).update(pub, KEY_BYTELENGTH).update(M, BLOCKHASH_BYTELENGTH).digest(h)
sc_reduce(h)
- ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
+ ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe[0])
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)
blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
sc_reduce(h)
- ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
+ ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe[0])
ge_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1)
return ge_has_small_order(check, t_fe[0], t_u8x32)
blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
sc_reduce(h)
- ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S)
+ ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe[0])
ge_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1)
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])
if (ge_is_canonical(pub) != 0) return false
// fail if public key cannot be decoded
- if (ge_frombytes_negate_vartime(A, pub) != 0) return false
+ if (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe[0], t_fe[1], t_fe[2]) != 0) return false
// fail if public key `k` is small order
if (ge_has_small_order(A, t_fe[0], t_u8x32) != 0) return false
/**
* Inversion - sets `out` = 0 if `z` == 0
*/
-export function fe_invert (out: FieldElement, z: FieldElement, t0: FieldElement, t1: FieldElement, t2: FieldElement, t3: FieldElement): void {
- fe_sq(t0, z)
- fe_sq(t1, t0)
- fe_sq(t1, t1)
- fe_mul(t1, z, t1)
- fe_mul(t0, t0, t1)
- fe_sq(t2, t0)
- fe_mul(t1, t1, t2)
- fe_sq(t2, t1)
+export function fe_invert (out: FieldElement, z: FieldElement, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_fe_3: FieldElement): void {
+ fe_sq(t_fe_0, z)
+ fe_sq(t_fe_1, t_fe_0)
+ fe_sq(t_fe_1, t_fe_1)
+ fe_mul(t_fe_1, z, t_fe_1)
+ fe_mul(t_fe_0, t_fe_0, t_fe_1)
+ fe_sq(t_fe_2, t_fe_0)
+ fe_mul(t_fe_1, t_fe_1, t_fe_2)
+ fe_sq(t_fe_2, t_fe_1)
for (let i = 1; i < 5; ++i) {
- fe_sq(t2, t2)
+ fe_sq(t_fe_2, t_fe_2)
}
- fe_mul(t1, t2, t1)
- fe_sq(t2, t1)
+ fe_mul(t_fe_1, t_fe_2, t_fe_1)
+ fe_sq(t_fe_2, t_fe_1)
for (let i = 1; i < 10; ++i) {
- fe_sq(t2, t2)
+ fe_sq(t_fe_2, t_fe_2)
}
- fe_mul(t2, t2, t1)
- fe_sq(t3, t2)
+ fe_mul(t_fe_2, t_fe_2, t_fe_1)
+ fe_sq(t_fe_3, t_fe_2)
for (let i = 1; i < 20; ++i) {
- fe_sq(t3, t3)
+ fe_sq(t_fe_3, t_fe_3)
}
- fe_mul(t2, t3, t2)
+ fe_mul(t_fe_2, t_fe_3, t_fe_2)
for (let i = 1; i < 11; ++i) {
- fe_sq(t2, t2)
+ fe_sq(t_fe_2, t_fe_2)
}
- fe_mul(t1, t2, t1)
- fe_sq(t2, t1)
+ fe_mul(t_fe_1, t_fe_2, t_fe_1)
+ fe_sq(t_fe_2, t_fe_1)
for (let i = 1; i < 50; ++i) {
- fe_sq(t2, t2)
+ fe_sq(t_fe_2, t_fe_2)
}
- fe_mul(t2, t2, t1)
- fe_sq(t3, t2)
+ fe_mul(t_fe_2, t_fe_2, t_fe_1)
+ fe_sq(t_fe_3, t_fe_2)
for (let i = 1; i < 100; ++i) {
- fe_sq(t3, t3)
+ fe_sq(t_fe_3, t_fe_3)
}
- fe_mul(t2, t3, t2)
+ fe_mul(t_fe_2, t_fe_3, t_fe_2)
for (let i = 1; i < 51; ++i) {
- fe_sq(t2, t2)
+ fe_sq(t_fe_2, t_fe_2)
}
- fe_mul(t1, t2, t1)
+ fe_mul(t_fe_1, t_fe_2, t_fe_1)
for (let i = 1; i < 6; ++i) {
- fe_sq(t1, t1)
+ fe_sq(t_fe_1, t_fe_1)
}
- fe_mul(out, t1, t0)
+ fe_mul(out, t_fe_1, t_fe_0)
}
/**
* Preconditions:
* |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
*/
-export function fe_iszero (f: FieldElement, u8x32_scratch: StaticArray<u8>, t: FieldElement): u8 {
- fe_tobytes(u8x32_scratch, f, t)
+export function fe_iszero (f: FieldElement, t_u8x32: StaticArray<u8>, t: FieldElement): u8 {
+ fe_tobytes(t_u8x32, f, t)
let d: i32 = 0
for (let i: i32 = 0; i < 32; i++) {
- d |= u8x32_scratch[i]
+ d |= t_u8x32[i]
}
d &= 255
return u8(((d - 1) >> 8) & 1)
* returns z^((p-5)/8) = z^(2²⁵²-3)
* used to compute square roots since we have p=5 (mod 8); see Cohen and Frey.
*/
-export function fe_pow22523 (out: FieldElement, z: FieldElement, t0: FieldElement, t1: FieldElement, t2: FieldElement): void {
- fe_sq(t0, z)
- fe_sq(t1, t0)
- fe_sq(t1, t1)
- fe_mul(t1, z, t1)
- fe_mul(t0, t0, t1)
- fe_sq(t0, t0)
- fe_mul(t0, t1, t0)
- fe_sq(t1, t0)
+export function fe_pow22523 (out: FieldElement, z: FieldElement, t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement): void {
+ fe_sq(t_fe_0, z)
+ fe_sq(t_fe_1, t_fe_0)
+ fe_sq(t_fe_1, t_fe_1)
+ fe_mul(t_fe_1, z, t_fe_1)
+ fe_mul(t_fe_0, t_fe_0, t_fe_1)
+ fe_sq(t_fe_0, t_fe_0)
+ fe_mul(t_fe_0, t_fe_1, t_fe_0)
+ fe_sq(t_fe_1, t_fe_0)
for (let i = 1; i < 5; ++i) {
- fe_sq(t1, t1)
+ fe_sq(t_fe_1, t_fe_1)
}
- fe_mul(t0, t1, t0)
- fe_sq(t1, t0)
+ fe_mul(t_fe_0, t_fe_1, t_fe_0)
+ fe_sq(t_fe_1, t_fe_0)
for (let i = 1; i < 10; ++i) {
- fe_sq(t1, t1)
+ fe_sq(t_fe_1, t_fe_1)
}
- fe_mul(t1, t1, t0)
- fe_sq(t2, t1)
+ fe_mul(t_fe_1, t_fe_1, t_fe_0)
+ fe_sq(t_fe_2, t_fe_1)
for (let i = 1; i < 20; ++i) {
- fe_sq(t2, t2)
+ fe_sq(t_fe_2, t_fe_2)
}
- fe_mul(t1, t2, t1)
+ fe_mul(t_fe_1, t_fe_2, t_fe_1)
for (let i = 1; i < 11; ++i) {
- fe_sq(t1, t1)
+ fe_sq(t_fe_1, t_fe_1)
}
- fe_mul(t0, t1, t0)
- fe_sq(t1, t0)
+ fe_mul(t_fe_0, t_fe_1, t_fe_0)
+ fe_sq(t_fe_1, t_fe_0)
for (let i = 1; i < 50; ++i) {
- fe_sq(t1, t1)
+ fe_sq(t_fe_1, t_fe_1)
}
- fe_mul(t1, t1, t0)
- fe_sq(t2, t1)
+ fe_mul(t_fe_1, t_fe_1, t_fe_0)
+ fe_sq(t_fe_2, t_fe_1)
for (let i = 1; i < 100; ++i) {
- fe_sq(t2, t2)
+ fe_sq(t_fe_2, t_fe_2)
}
- fe_mul(t1, t2, t1)
+ fe_mul(t_fe_1, t_fe_2, t_fe_1)
for (let i = 1; i < 51; ++i) {
- fe_sq(t1, t1)
+ fe_sq(t_fe_1, t_fe_1)
}
- fe_mul(t0, t1, t0)
- fe_sq(t0, t0)
- fe_sq(t0, t0)
- fe_mul(out, t0, z)
+ fe_mul(t_fe_0, t_fe_1, t_fe_0)
+ fe_sq(t_fe_0, t_fe_0)
+ fe_sq(t_fe_0, t_fe_0)
+ fe_mul(out, t_fe_0, z)
}
/**
*
* Goal: Output h0+...+2²³⁰ h9.
*/
-export function fe_tobytes (s: StaticArray<u8>, h: FieldElement, t: FieldElement): void {
- fe_reduce(t, h)
- const t_ptr: usize = changetype<usize>(t)
+export function fe_tobytes (s: StaticArray<u8>, h: FieldElement, t_fe: FieldElement): void {
+ fe_reduce(t_fe, h)
+ const t_ptr: usize = changetype<usize>(t_fe)
const t0 = load<i32>(t_ptr, 0)
const t1 = load<i32>(t_ptr, 4)
const t2 = load<i32>(t_ptr, 8)
const ge_frombytes_negate_vartime_vxx: FieldElement = fe()
const ge_frombytes_negate_vartime_m_root_check: FieldElement = fe()
const ge_frombytes_negate_vartime_p_root_check: FieldElement = fe()
-const ge_frombytes_negate_vartime_u8x32_scratch = new StaticArray<u8>(32)
-const ge_frombytes_negate_vartime_fe_scratch_0: FieldElement = fe()
-const ge_frombytes_negate_vartime_fe_scratch_1: FieldElement = fe()
-const ge_frombytes_negate_vartime_fe_scratch_2: FieldElement = fe()
/**
* Variable-time point negation converted from byte representation.
*
*
* @returns {i32} 0 if `s` decodes to a valid point, else -1
*/
-export function ge_frombytes_negate_vartime (h: ge_p3, s: StaticArray<u8>): i32 {
+export function ge_frombytes_negate_vartime (h: ge_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 v3 = ge_frombytes_negate_vartime_v3
const m_root_check = ge_frombytes_negate_vartime_m_root_check
const p_root_check = ge_frombytes_negate_vartime_p_root_check
- const u8x32_scratch = ge_frombytes_negate_vartime_u8x32_scratch
- const fe_scratch_0 = ge_frombytes_negate_vartime_fe_scratch_0
- const fe_scratch_1 = ge_frombytes_negate_vartime_fe_scratch_1
- const fe_scratch_2 = ge_frombytes_negate_vartime_fe_scratch_2
-
fe_frombytes(h.Y, s)
fe_1(h.Z)
fe_mul(h.X, h.X, u)
// x = (uv⁷)^((q-5)/8)
- fe_pow22523(h.X, h.X, fe_scratch_0, fe_scratch_1, fe_scratch_2)
+ fe_pow22523(h.X, h.X, t_fe_0, t_fe_1, t_fe_2)
// x = uv³((uv⁷)^((q-5)/8))
fe_mul(h.X, h.X, v3)
// m = vx²-u
fe_sub(m_root_check, vxx, u)
- if (fe_iszero(m_root_check, u8x32_scratch, fe_scratch_0) == 0) {
+ if (fe_iszero(m_root_check, t_u8x32, t_fe_0) == 0) {
// p = vx²+u
fe_add(p_root_check, vxx, u)
- if (fe_iszero(p_root_check, u8x32_scratch, fe_scratch_0) == 0) {
+ if (fe_iszero(p_root_check, t_u8x32, t_fe_0) == 0) {
return -1
}
fe_mul(h.X, h.X, fe_sqrtm1)
}
- if (fe_isnegative(h.X, fe_scratch_0) == (s[31] >> 7)) {
+ if (fe_isnegative(h.X, t_fe_0) == (s[31] >> 7)) {
fe_neg(h.X, h.X)
}
fe_mul(h.T, h.X, h.Y)
* @returns {i32} 0 if `s` decodes to a valid point, else -1
*/
export function ge_frombytes (h: ge_p3, s: StaticArray<u8>,
- t0_fe: FieldElement, t1_fe: FieldElement, t2_fe: FieldElement, t_u8x32: StaticArray<u8>
+ t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_u8x32: StaticArray<u8>
): i32 {
const u = ge_frombytes_u
const v = ge_frombytes_v
/* x = u((uv)^((q-5)/8)) */
fe_mul(h.X, u, v)
- fe_pow22523(h.X, h.X, t0_fe, t1_fe, t2_fe)
+ fe_pow22523(h.X, h.X, t_fe_0, t_fe_1, t_fe_2)
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, t_u8x32, t0_fe)
+ has_m_root = fe_iszero(m_root_check, t_u8x32, t_fe_0)
// p = vx²+u
fe_add(p_root_check, vxx, u)
- has_p_root = fe_iszero(p_root_check, t_u8x32, t0_fe)
+ has_p_root = fe_iszero(p_root_check, t_u8x32, t_fe_0)
// 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, t0_fe) ^ (s[31] >> 7))
+ fe_cmov(h.X, negx, fe_isnegative(h.X, t_fe_0) ^ (s[31] >> 7))
fe_mul(h.T, h.X, h.Y)
return i32(has_m_root | has_p_root) - 1
* @param a 32-byte input buffer for scalar
*/
export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u8>,
- t0: FieldElement, t1: FieldElement, t2: FieldElement, t3: FieldElement, t4: FieldElement, t5: FieldElement, t6: FieldElement
+ t_fe_0: FieldElement, t_fe_1: FieldElement, t_fe_2: FieldElement, t_fe_3: FieldElement, t_fe_4: FieldElement, t_fe_5: FieldElement, t_fe_6: FieldElement
): void {
const h = ge_scalarmult_base_tobytes_h
const e = ge_scalarmult_base_tobytes_e
ge_p1p1_to_p3(h, r)
}
- ge_p3_dbl(r, h, t0)
+ ge_p3_dbl(r, h, t_fe_0)
ge_p1p1_to_p2(s, r)
- ge_p2_dbl(r, s, t0)
+ ge_p2_dbl(r, s, t_fe_0)
ge_p1p1_to_p2(s, r)
- ge_p2_dbl(r, s, t0)
+ ge_p2_dbl(r, s, t_fe_0)
ge_p1p1_to_p2(s, r)
- ge_p2_dbl(r, s, t0)
+ ge_p2_dbl(r, s, t_fe_0)
ge_p1p1_to_p3(h, r)
// base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
}
ge_p3_tobytes(S, h,
- t0, t1, t2, t3, t4, t5, t6
+ t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6
)
// clear sensitive data
ge_precomp_0(t)
ge_p3_0(h)
e.fill(0)
- t0.fill(0)
- t1.fill(0)
- t2.fill(0)
- t3.fill(0)
- t4.fill(0)
- t5.fill(0)
- t6.fill(0)
+ t_fe_0.fill(0)
+ t_fe_1.fill(0)
+ t_fe_2.fill(0)
+ t_fe_3.fill(0)
+ t_fe_4.fill(0)
+ t_fe_5.fill(0)
+ t_fe_6.fill(0)
}
const ge_double_scalarmult_vartime_aslide: StaticArray<i8> = new StaticArray<i8>(256)
const ge_double_scalarmult_vartime_t: ge_p1p1 = new ge_p1p1()
const ge_double_scalarmult_vartime_u: ge_p3 = new ge_p3()
const ge_double_scalarmult_vartime_A2: ge_p3 = new ge_p3()
-const ge_double_scalarmult_vartime_fe_scratch: FieldElement = fe()
/**
* `r = a[A] + b[B]`
*
*
* Variable-time; only used for signature verification.
*/
-export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>, A: ge_p3, b: StaticArray<u8>): void {
+export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>, A: ge_p3, b: StaticArray<u8>, t_fe: FieldElement): void {
const Bi = base2
const aslide = ge_double_scalarmult_vartime_aslide
const bslide = ge_double_scalarmult_vartime_bslide
const t = ge_double_scalarmult_vartime_t
const u = ge_double_scalarmult_vartime_u
const A2 = ge_double_scalarmult_vartime_A2
- const fe_scratch = ge_double_scalarmult_vartime_fe_scratch
let i: i32 = 0
slide_vartime(aslide, a)
ge_p3_to_cached(Ai[0], A)
- ge_p3_dbl(t, A, fe_scratch)
+ ge_p3_dbl(t, A, t_fe)
ge_p1p1_to_p3(A2, t)
- ge_add_cached(t, A2, Ai[0], fe_scratch)
+ ge_add_cached(t, A2, Ai[0], t_fe)
ge_p1p1_to_p3(u, t)
ge_p3_to_cached(Ai[1], u)
- ge_add_cached(t, A2, Ai[1], fe_scratch)
+ ge_add_cached(t, A2, Ai[1], t_fe)
ge_p1p1_to_p3(u, t)
ge_p3_to_cached(Ai[2], u)
- ge_add_cached(t, A2, Ai[2], fe_scratch)
+ ge_add_cached(t, A2, Ai[2], t_fe)
ge_p1p1_to_p3(u, t)
ge_p3_to_cached(Ai[3], u)
- ge_add_cached(t, A2, Ai[3], fe_scratch)
+ ge_add_cached(t, A2, Ai[3], t_fe)
ge_p1p1_to_p3(u, t)
ge_p3_to_cached(Ai[4], u)
- ge_add_cached(t, A2, Ai[4], fe_scratch)
+ ge_add_cached(t, A2, Ai[4], t_fe)
ge_p1p1_to_p3(u, t)
ge_p3_to_cached(Ai[5], u)
- ge_add_cached(t, A2, Ai[5], fe_scratch)
+ ge_add_cached(t, A2, Ai[5], t_fe)
ge_p1p1_to_p3(u, t)
ge_p3_to_cached(Ai[6], u)
- ge_add_cached(t, A2, Ai[6], fe_scratch)
+ ge_add_cached(t, A2, Ai[6], t_fe)
ge_p1p1_to_p3(u, t)
ge_p3_to_cached(Ai[7], u)
}
for (; i >= 0; --i) {
- ge_p2_dbl(t, p, fe_scratch)
+ ge_p2_dbl(t, p, t_fe)
if (aslide[i] > 0) {
ge_p1p1_to_p3(u, t)
- ge_add_cached(t, u, Ai[aslide[i] / 2], fe_scratch)
+ ge_add_cached(t, u, Ai[aslide[i] / 2], t_fe)
} else if (aslide[i] < 0) {
ge_p1p1_to_p3(u, t)
ge_sub_cached(t, u, Ai[(-aslide[i]) / 2])