*/
export function fe_1 (h: FieldElement): void {
fe_0(h)
- h[0] = 1
+ store<i32>(changetype<usize>(h), 1, 0)
}
/**
* @param g FieldElement summand source
*/
export function fe_add (h: FieldElement, f: FieldElement, g: FieldElement): void {
- const h_ptr = changetype<usize>(h)
- const f_ptr = changetype<usize>(f)
- const g_ptr = changetype<usize>(g)
- v128.store(h_ptr, v128.add<i32>(v128.load(f_ptr, 0), v128.load(g_ptr, 0)), 0)
- v128.store(h_ptr, v128.add<i32>(v128.load(f_ptr, 16), v128.load(g_ptr, 16)), 16)
- v128.store_lane<i64>(h_ptr, v128.add<i32>(v128.load(f_ptr, 32), v128.load(g_ptr, 32)), 0, 32)
+ const f0 = v128.load(changetype<usize>(f), 0), f1 = v128.load(changetype<usize>(f), 16), f2 = v128.load(changetype<usize>(f), 32)
+ const g0 = v128.load(changetype<usize>(g), 0), g1 = v128.load(changetype<usize>(g), 16), g2 = v128.load(changetype<usize>(g), 32)
+ const h0 = v128.add<i32>(f0, g0), h1 = v128.add<i32>(f1, g1), h2 = v128.add<i32>(f2, g2)
+ v128.store(changetype<usize>(h), h0, 0)
+ v128.store(changetype<usize>(h), h1, 16)
+ v128.store_lane<i64>(changetype<usize>(h), h2, 0, 32)
}
/**
* @param {u64} b Copy `g` into `f` if 1, copy `f` into `f` if 0
*/
export function fe_cmov (f: FieldElement, g: FieldElement, b: u64): void {
- const f_ptr = changetype<usize>(f)
- const g_ptr = changetype<usize>(g)
const c = v128.splat<u64>(0 - b)
- v128.store(f_ptr, v128.bitselect(v128.load(g_ptr), v128.load(f_ptr, 0), c))
- v128.store(f_ptr, v128.bitselect(v128.load(g_ptr, 16), v128.load(f_ptr, 16), c), 16)
- v128.store_lane<u64>(f_ptr, v128.bitselect(v128.load(g_ptr, 32), v128.load(f_ptr, 32), c), 0, 32)
+ const f0 = v128.load(changetype<usize>(f), 0), f1 = v128.load(changetype<usize>(f), 16), f2 = v128.load(changetype<usize>(f), 32)
+ const g0 = v128.load(changetype<usize>(g), 0), g1 = v128.load(changetype<usize>(g), 16), g2 = v128.load(changetype<usize>(g), 32)
+ const h0 = v128.bitselect(g0, f0, c), h1 = v128.bitselect(g1, f1, c), h2 = v128.bitselect(g2, f2, c)
+ v128.store(changetype<usize>(f), h0, 0)
+ v128.store(changetype<usize>(f), h1, 16)
+ v128.store_lane<u64>(changetype<usize>(f), h2, 0, 32)
}
/**
* @param f FieldElement summand source
*/
export function fe_dbl (h: FieldElement, f: FieldElement): void {
- const h_ptr = changetype<usize>(h)
- const f_ptr = changetype<usize>(f)
- v128.store(h_ptr, v128.shl<i32>(v128.load(f_ptr, 0), 1), 0)
- v128.store(h_ptr, v128.shl<i32>(v128.load(f_ptr, 16), 1), 16)
- v128.store_lane<i64>(h_ptr, v128.shl<i32>(v128.load(f_ptr, 32), 1), 0, 32)
+ const f0 = v128.load(changetype<usize>(f), 0), f1 = v128.load(changetype<usize>(f), 16), f2 = v128.load(changetype<usize>(f), 32)
+ const h0 = v128.shl<i32>(f0, 1), h1 = v128.shl<i32>(f1, 1), h2 = v128.shl<i32>(f2, 1)
+ v128.store(changetype<usize>(h), h0, 0)
+ v128.store(changetype<usize>(h), h1, 16)
+ v128.store_lane<i64>(changetype<usize>(h), h2, 0, 32)
}
/**
h9 += carry8
h8 -= carry8 << 26
- const h_ptr: usize = changetype<usize>(h)
- store<i32>(h_ptr, h0, 0)
- store<i32>(h_ptr, h1, 4)
- store<i32>(h_ptr, h2, 8)
- store<i32>(h_ptr, h3, 12)
- store<i32>(h_ptr, h4, 16)
- store<i32>(h_ptr, h5, 20)
- store<i32>(h_ptr, h6, 24)
- store<i32>(h_ptr, h7, 28)
- store<i32>(h_ptr, h8, 32)
- store<i32>(h_ptr, h9, 36)
+ store<i32>(changetype<usize>(h), h0, 0)
+ store<i32>(changetype<usize>(h), h1, 4)
+ store<i32>(changetype<usize>(h), h2, 8)
+ store<i32>(changetype<usize>(h), h3, 12)
+ store<i32>(changetype<usize>(h), h4, 16)
+ store<i32>(changetype<usize>(h), h5, 20)
+ store<i32>(changetype<usize>(h), h6, 24)
+ store<i32>(changetype<usize>(h), h7, 28)
+ store<i32>(changetype<usize>(h), h8, 32)
+ store<i32>(changetype<usize>(h), h9, 36)
}
/**
*/
export function fe_isnegative (f: FieldElement, t: FieldElement): u8 {
fe_reduce(t, f)
- return u8(t[0] & 1)
+ return u8(load<i32>(changetype<usize>(t), 0) & 1)
}
/**
* Preconditions:
* |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
*/
-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 |= t_u8x32[i]
- }
- d &= 255
- return u8(((d - 1) >> 8) & 1)
+export function fe_iszero (f: FieldElement, t_fe: FieldElement, t_u8x32: StaticArray<u8>): u8 {
+ fe_tobytes(t_u8x32, f, t_fe)
+ const t0 = v128.load(changetype<usize>(t_u8x32), 0)
+ const t1 = v128.load(changetype<usize>(t_u8x32), 16)
+ const d = v128.or(t0, t1)
+ return u8(v128.any_true(d)) ^ 1
}
/**
* @param g FieldElement multiplicand source
*/
export function fe_mul (h: FieldElement, f: FieldElement, g: FieldElement): void {
- const h_ptr: usize = changetype<usize>(h)
- const f_ptr: usize = changetype<usize>(f)
- const g_ptr: usize = changetype<usize>(g)
- const g0123: v128 = v128.load(g_ptr, 0)
- const g4567: v128 = v128.load(g_ptr, 16)
- const g89xx: v128 = v128.load(g_ptr, 32)
-
// mask to select even lane from first vector and odd lane from second
const m: v128 = i32x4(-1, 0, -1, 0)
// mask to double odd-on-odd indexes
const odds: v128 = i32x4(0, -1, 0, -1)
+ const g0123: v128 = v128.load(changetype<usize>(g), 0)
+ const g4567: v128 = v128.load(changetype<usize>(g), 16)
+ const g89xx: v128 = v128.load(changetype<usize>(g), 32)
+
// factor of 19 for wrap from h9 to h0
const g0123_19: v128 = i32x4.mul(g0123, i32x4.splat(19))
const g4567_19: v128 = i32x4.mul(g4567, i32x4.splat(19))
const g89xx_19: v128 = i32x4.mul(g89xx, i32x4.splat(19))
// f[0]
- let f_i: v128 = v128.load_splat<i32>(f_ptr, 0 << 2)
+ let f_i: v128 = v128.load_splat<i32>(changetype<usize>(f), 0 << 2)
let h01: v128 = i64x2.extmul_low_i32x4_s(f_i, g0123)
let h23: v128 = i64x2.extmul_high_i32x4_s(f_i, g0123)
let h89: v128 = i64x2.extmul_low_i32x4_s(f_i, g89xx)
// f[1]
- f_i = v128.load_splat<i32>(f_ptr, 1 << 2)
+ f_i = v128.load_splat<i32>(changetype<usize>(f), 1 << 2)
f_i = v128.add<i32>(f_i, v128.and(f_i, odds))
let h12: v128 = i64x2.extmul_low_i32x4_s(f_i, g0123)
let h90: v128 = i64x2.extmul_low_i32x4_s(f_i, v128.bitselect(g89xx, g89xx_19, m))
// f[2]
- f_i = v128.load_splat<i32>(f_ptr, 2 << 2)
+ f_i = v128.load_splat<i32>(changetype<usize>(f), 2 << 2)
let t0: v128 = i64x2.extmul_low_i32x4_s(f_i, g0123)
let t1: v128 = i64x2.extmul_high_i32x4_s(f_i, g0123)
h01 = i64x2.add(h01, t4)
// f[3]
- f_i = v128.load_splat<i32>(f_ptr, 3 << 2)
+ f_i = v128.load_splat<i32>(changetype<usize>(f), 3 << 2)
f_i = v128.add<i32>(f_i, v128.and(f_i, odds))
t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
h12 = i64x2.add(h12, t4)
// f[4]
- f_i = v128.load_splat<i32>(f_ptr, 4 << 2)
+ f_i = v128.load_splat<i32>(changetype<usize>(f), 4 << 2)
t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
t1 = i64x2.extmul_high_i32x4_s(f_i, g0123)
h23 = i64x2.add(h23, t4)
// f[5]
- f_i = v128.load_splat<i32>(f_ptr, 5 << 2)
+ f_i = v128.load_splat<i32>(changetype<usize>(f), 5 << 2)
f_i = v128.add<i32>(f_i, v128.and(f_i, odds))
t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
h34 = i64x2.add(h34, t4)
// f[6]
- f_i = v128.load_splat<i32>(f_ptr, 6 << 2)
+ f_i = v128.load_splat<i32>(changetype<usize>(f), 6 << 2)
t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
t1 = i64x2.extmul_high_i32x4_s(f_i, g0123)
h45 = i64x2.add(h45, t4)
// f[7]
- f_i = v128.load_splat<i32>(f_ptr, 7 << 2)
+ f_i = v128.load_splat<i32>(changetype<usize>(f), 7 << 2)
f_i = v128.add<i32>(f_i, v128.and(f_i, odds))
t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
h56 = i64x2.add(h56, t4)
// f[8]
- f_i = v128.load_splat<i32>(f_ptr, 8 << 2)
+ f_i = v128.load_splat<i32>(changetype<usize>(f), 8 << 2)
t0 = i64x2.extmul_low_i32x4_s(f_i, g0123)
t1 = i64x2.extmul_high_i32x4_s(f_i, g0123_19)
h67 = i64x2.add(h67, t4)
// f[9]
- f_i = v128.load_splat<i32>(f_ptr, 9 << 2)
+ f_i = v128.load_splat<i32>(changetype<usize>(f), 9 << 2)
f_i = v128.add<i32>(f_i, v128.and(f_i, odds))
t0 = i64x2.extmul_low_i32x4_s(f_i, v128.bitselect(g0123, g0123_19, m))
h1 += carry0
// assign results to output
- store<i32>(h_ptr, h0, 0)
- store<i32>(h_ptr, h1, 4)
- store<i32>(h_ptr, h2, 8)
- store<i32>(h_ptr, h3, 12)
- store<i32>(h_ptr, h4, 16)
- store<i32>(h_ptr, h5, 20)
- store<i32>(h_ptr, h6, 24)
- store<i32>(h_ptr, h7, 28)
- store<i32>(h_ptr, h8, 32)
- store<i32>(h_ptr, h9, 36)
+ store<i32>(changetype<usize>(h), h0, 0)
+ store<i32>(changetype<usize>(h), h1, 4)
+ store<i32>(changetype<usize>(h), h2, 8)
+ store<i32>(changetype<usize>(h), h3, 12)
+ store<i32>(changetype<usize>(h), h4, 16)
+ store<i32>(changetype<usize>(h), h5, 20)
+ store<i32>(changetype<usize>(h), h6, 24)
+ store<i32>(changetype<usize>(h), h7, 28)
+ store<i32>(changetype<usize>(h), h8, 32)
+ store<i32>(changetype<usize>(h), h9, 36)
}
/**
* @param {FieldElement} f operand source
*/
export function fe_neg (h: FieldElement, f: FieldElement): void {
- const h_ptr = changetype<usize>(h)
- const f_ptr = changetype<usize>(f)
- v128.store(h_ptr, v128.neg<i32>(v128.load(f_ptr)))
- v128.store(h_ptr, v128.neg<i32>(v128.load(f_ptr, 16)), 16)
- v128.store_lane<i64>(h_ptr, v128.neg<i32>(v128.load(f_ptr, 32)), 0, 32)
+ const f0 = v128.load(changetype<usize>(f), 0), f1 = v128.load(changetype<usize>(f), 16), f2 = v128.load(changetype<usize>(f), 32)
+ const h0 = v128.neg<i32>(f0), h1 = v128.neg<i32>(f1), h2 = v128.neg<i32>(f2)
+ v128.store(changetype<usize>(h), h0, 0)
+ v128.store(changetype<usize>(h), h1, 16)
+ v128.store_lane<i64>(changetype<usize>(h), h2, 0, 32)
}
/**
* @param f FieldElement base source
*/
export function fe_sq (h: FieldElement, f: FieldElement): void {
- const f_ptr: usize = changetype<usize>(f)
- const f0: i64 = i64(load<i32>(f_ptr, 0))
- const f1: i64 = i64(load<i32>(f_ptr, 4))
- const f2: i64 = i64(load<i32>(f_ptr, 8))
- const f3: i64 = i64(load<i32>(f_ptr, 12))
- const f4: i64 = i64(load<i32>(f_ptr, 16))
- const f5: i64 = i64(load<i32>(f_ptr, 20))
- const f6: i64 = i64(load<i32>(f_ptr, 24))
- const f7: i64 = i64(load<i32>(f_ptr, 28))
- const f8: i64 = i64(load<i32>(f_ptr, 32))
- const f9: i64 = i64(load<i32>(f_ptr, 36))
+ const f0: i64 = i64(load<i32>(changetype<usize>(f), 0))
+ const f1: i64 = i64(load<i32>(changetype<usize>(f), 4))
+ const f2: i64 = i64(load<i32>(changetype<usize>(f), 8))
+ const f3: i64 = i64(load<i32>(changetype<usize>(f), 12))
+ const f4: i64 = i64(load<i32>(changetype<usize>(f), 16))
+ const f5: i64 = i64(load<i32>(changetype<usize>(f), 20))
+ const f6: i64 = i64(load<i32>(changetype<usize>(f), 24))
+ const f7: i64 = i64(load<i32>(changetype<usize>(f), 28))
+ const f8: i64 = i64(load<i32>(changetype<usize>(f), 32))
+ const f9: i64 = i64(load<i32>(changetype<usize>(f), 36))
const f0_2: i64 = f0 * 2
const f1_2: i64 = f1 * 2
* @param g FieldElement subtrahend source
*/
export function fe_sub (h: FieldElement, f: FieldElement, g: FieldElement): void {
- const f_ptr = changetype<usize>(f)
- const g_ptr = changetype<usize>(g)
- const h_ptr = changetype<usize>(h)
- v128.store(h_ptr, v128.sub<i32>(v128.load(f_ptr, 0), v128.load(g_ptr, 0)), 0)
- v128.store(h_ptr, v128.sub<i32>(v128.load(f_ptr, 16), v128.load(g_ptr, 16)), 16)
- v128.store_lane<i64>(h_ptr, v128.sub<i32>(v128.load(f_ptr, 32), v128.load(g_ptr, 32)), 0, 32)
+ const f0 = v128.load(changetype<usize>(f), 0), f1 = v128.load(changetype<usize>(f), 16), f2 = v128.load(changetype<usize>(f), 32)
+ const g0 = v128.load(changetype<usize>(g), 0), g1 = v128.load(changetype<usize>(g), 16), g2 = v128.load(changetype<usize>(g), 32)
+ const h0 = v128.sub<i32>(f0, g0), h1 = v128.sub<i32>(f1, g1), h2 = v128.sub<i32>(f2, g2)
+ v128.store(changetype<usize>(h), h0, 0)
+ v128.store(changetype<usize>(h), h1, 16)
+ v128.store_lane<i64>(changetype<usize>(h), h2, 0, 32)
}
/**
*/
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 t3 = load<i32>(t_ptr, 12)
- const t4 = load<i32>(t_ptr, 16)
- const t5 = load<i32>(t_ptr, 20)
- const t6 = load<i32>(t_ptr, 24)
- const t7 = load<i32>(t_ptr, 28)
- const t8 = load<i32>(t_ptr, 32)
- const t9 = load<i32>(t_ptr, 36)
-
- const s_ptr: usize = changetype<usize>(s)
- store<u8>(s_ptr, t0 >> 0, 0)
- store<u8>(s_ptr, t0 >> 8, 1)
- store<u8>(s_ptr, t0 >> 16, 2)
- store<u8>(s_ptr, (t0 >> 24) | (t1 << 2), 3)
- store<u8>(s_ptr, t1 >> 6, 4)
- store<u8>(s_ptr, t1 >> 14, 5)
- store<u8>(s_ptr, (t1 >> 22) | (t2 << 3), 6)
- store<u8>(s_ptr, t2 >> 5, 7)
- store<u8>(s_ptr, t2 >> 13, 8)
- store<u8>(s_ptr, (t2 >> 21) | (t3 << 5), 9)
- store<u8>(s_ptr, t3 >> 3, 10)
- store<u8>(s_ptr, t3 >> 11, 11)
- store<u8>(s_ptr, (t3 >> 19) | (t4 << 6), 12)
- store<u8>(s_ptr, t4 >> 2, 13)
- store<u8>(s_ptr, t4 >> 10, 14)
- store<u8>(s_ptr, t4 >> 18, 15)
- store<u8>(s_ptr, t5 >> 0, 16)
- store<u8>(s_ptr, t5 >> 8, 17)
- store<u8>(s_ptr, t5 >> 16, 18)
- store<u8>(s_ptr, (t5 >> 24) | (t6 << 1), 19)
- store<u8>(s_ptr, t6 >> 7, 20)
- store<u8>(s_ptr, t6 >> 15, 21)
- store<u8>(s_ptr, (t6 >> 23) | (t7 << 3), 22)
- store<u8>(s_ptr, t7 >> 5, 23)
- store<u8>(s_ptr, t7 >> 13, 24)
- store<u8>(s_ptr, (t7 >> 21) | (t8 << 4), 25)
- store<u8>(s_ptr, t8 >> 4, 26)
- store<u8>(s_ptr, t8 >> 12, 27)
- store<u8>(s_ptr, (t8 >> 20) | (t9 << 6), 28)
- store<u8>(s_ptr, t9 >> 2, 29)
- store<u8>(s_ptr, t9 >> 10, 30)
- store<u8>(s_ptr, t9 >> 18, 31)
+ const t0 = load<i32>(changetype<usize>(t_fe), 0)
+ const t1 = load<i32>(changetype<usize>(t_fe), 4)
+ const t2 = load<i32>(changetype<usize>(t_fe), 8)
+ const t3 = load<i32>(changetype<usize>(t_fe), 12)
+ const t4 = load<i32>(changetype<usize>(t_fe), 16)
+ const t5 = load<i32>(changetype<usize>(t_fe), 20)
+ const t6 = load<i32>(changetype<usize>(t_fe), 24)
+ const t7 = load<i32>(changetype<usize>(t_fe), 28)
+ const t8 = load<i32>(changetype<usize>(t_fe), 32)
+ const t9 = load<i32>(changetype<usize>(t_fe), 36)
+
+ store<u8>(changetype<usize>(s), t0 >> 0, 0)
+ store<u8>(changetype<usize>(s), t0 >> 8, 1)
+ store<u8>(changetype<usize>(s), t0 >> 16, 2)
+ store<u8>(changetype<usize>(s), (t0 >> 24) | (t1 << 2), 3)
+ store<u8>(changetype<usize>(s), t1 >> 6, 4)
+ store<u8>(changetype<usize>(s), t1 >> 14, 5)
+ store<u8>(changetype<usize>(s), (t1 >> 22) | (t2 << 3), 6)
+ store<u8>(changetype<usize>(s), t2 >> 5, 7)
+ store<u8>(changetype<usize>(s), t2 >> 13, 8)
+ store<u8>(changetype<usize>(s), (t2 >> 21) | (t3 << 5), 9)
+ store<u8>(changetype<usize>(s), t3 >> 3, 10)
+ store<u8>(changetype<usize>(s), t3 >> 11, 11)
+ store<u8>(changetype<usize>(s), (t3 >> 19) | (t4 << 6), 12)
+ store<u8>(changetype<usize>(s), t4 >> 2, 13)
+ store<u8>(changetype<usize>(s), t4 >> 10, 14)
+ store<u8>(changetype<usize>(s), t4 >> 18, 15)
+ store<u8>(changetype<usize>(s), t5 >> 0, 16)
+ store<u8>(changetype<usize>(s), t5 >> 8, 17)
+ store<u8>(changetype<usize>(s), t5 >> 16, 18)
+ store<u8>(changetype<usize>(s), (t5 >> 24) | (t6 << 1), 19)
+ store<u8>(changetype<usize>(s), t6 >> 7, 20)
+ store<u8>(changetype<usize>(s), t6 >> 15, 21)
+ store<u8>(changetype<usize>(s), (t6 >> 23) | (t7 << 3), 22)
+ store<u8>(changetype<usize>(s), t7 >> 5, 23)
+ store<u8>(changetype<usize>(s), t7 >> 13, 24)
+ store<u8>(changetype<usize>(s), (t7 >> 21) | (t8 << 4), 25)
+ store<u8>(changetype<usize>(s), t8 >> 4, 26)
+ store<u8>(changetype<usize>(s), t8 >> 12, 27)
+ store<u8>(changetype<usize>(s), (t8 >> 20) | (t9 << 6), 28)
+ store<u8>(changetype<usize>(s), t9 >> 2, 29)
+ store<u8>(changetype<usize>(s), t9 >> 10, 30)
+ store<u8>(changetype<usize>(s), t9 >> 18, 31)
}