*
* `n = 2⁰a[0] + 2²⁶a[1] + 2⁷⁷a[2] ... + 2²³⁰a[9]`
*/
-import { load_3, load_4, sodium_is_zero } from './utils'
+import { load_3, load_4 } from './utils'
/**
* 10x 25- and 26-bit array of values representing a large integer `n mod p`. In
* Preconditions:
* |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
*/
-export function fe_isnegative (f: FieldElement, t: FieldElement): u8 {
- fe_reduce(t, f)
- return u8(t[0] & 1)
+export function fe_isnegative (f: FieldElement, fe_scratch: FieldElement): u8 {
+ fe_reduce(fe_scratch, f)
+ return u8(fe_scratch[0] & 1)
}
/**
* Preconditions:
* |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
*/
-export function fe_iszero (s: StaticArray<u8>, f: FieldElement, t: FieldElement): u8 {
- fe_tobytes(s, f, t)
- return sodium_is_zero(s, 32)
+export function fe_iszero (f: FieldElement, u8x32_scratch: StaticArray<u8>, fe_scratch: FieldElement): u8 {
+ fe_tobytes(u8x32_scratch, f, fe_scratch)
+ let d: i32 = 0
+ for (let i: i32 = 0; i < 32; i++) {
+ d |= u8x32_scratch[i]
+ }
+ d &= 255
+ return u8(((d - 1) >> 8) & 1)
}
/**
*
* 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, fe_scratch: FieldElement): void {
+ fe_reduce(fe_scratch, h)
+ const t_ptr: usize = changetype<usize>(fe_scratch)
const t0 = load<i32>(t_ptr, 0)
const t1 = load<i32>(t_ptr, 4)
const t2 = load<i32>(t_ptr, 8)
// m = vx²-u
fe_sub(m_root_check, vxx, u)
- if (fe_iszero(u8x32_scratch, m_root_check, fe_scratch) == 0) {
+ if (fe_iszero(m_root_check, u8x32_scratch, fe_scratch) == 0) {
// p = vx²+u
fe_add(p_root_check, vxx, u)
- if (fe_iszero(u8x32_scratch, p_root_check, fe_scratch) == 0) {
+ if (fe_iszero(p_root_check, u8x32_scratch, fe_scratch) == 0) {
return -1
}
fe_mul(h.X, h.X, fe_sqrtm1)
// m = vx²-u
fe_sub(m_root_check, vxx, u)
- has_m_root = fe_iszero(u8x32_scratch, m_root_check, fe_scratch)
+ has_m_root = fe_iszero(m_root_check, u8x32_scratch, fe_scratch)
// p = vx²+u
fe_add(p_root_check, vxx, u)
- has_p_root = fe_iszero(u8x32_scratch, p_root_check, fe_scratch)
+ has_p_root = fe_iszero(p_root_check, u8x32_scratch, fe_scratch)
// x√-1
fe_mul(x_sqrtm1, h.X, fe_sqrtm1)
const fe_scratch = ge_has_small_order_fe_scratch
let ret: i32 = 0
- ret |= fe_iszero(u8x32_scratch, p.X, fe_scratch)
- ret |= fe_iszero(u8x32_scratch, p.Y, fe_scratch)
- ret |= fe_iszero(u8x32_scratch, p.Z, fe_scratch)
+ ret |= fe_iszero(p.X, u8x32_scratch, fe_scratch)
+ ret |= fe_iszero(p.Y, u8x32_scratch, fe_scratch)
+ ret |= fe_iszero(p.Z, u8x32_scratch, fe_scratch)
fe_mul(y_sqrtm1, p.Y, fe_sqrtm1)
fe_sub(c, y_sqrtm1, p.X)
- ret |= fe_iszero(u8x32_scratch, c, fe_scratch)
+ ret |= fe_iszero(c, u8x32_scratch, fe_scratch)
fe_add(c, y_sqrtm1, p.X)
- ret |= fe_iszero(u8x32_scratch, c, fe_scratch)
+ ret |= fe_iszero(c, u8x32_scratch, fe_scratch)
return ret
}
fe_mul(r.T2d, p.T, ed25519_d2)
}
-const ge_p3_tobytes_t: FieldElement = fe()
+const ge_p3_tobytes_fe_scratch: FieldElement = fe()
const ge_p3_tobytes_recip: FieldElement = fe()
const ge_p3_tobytes_x: FieldElement = fe()
const ge_p3_tobytes_y: FieldElement = fe()
export function ge_p3_tobytes (s: StaticArray<u8>, h: ge_p3): void {
- const t = ge_p3_tobytes_t
+ const fe_scratch = ge_p3_tobytes_fe_scratch
const recip = ge_p3_tobytes_recip
const x = ge_p3_tobytes_x
const y = ge_p3_tobytes_y
fe_invert(recip, h.Z)
fe_mul(x, h.X, recip)
fe_mul(y, h.Y, recip)
- fe_tobytes(s, y, t)
- s[31] ^= fe_isnegative(x, t) << 7
+ fe_tobytes(s, y, fe_scratch)
+ s[31] ^= fe_isnegative(x, fe_scratch) << 7
}
const ge_p2_dbl_t: FieldElement = fe()