//! SPDX-License-Identifier: GPL-3.0-or-later
import { Blake2b } from './ed25519/blake2b'
+import { FieldElement, fe } from './ed25519/fe'
import { ge_scalarmult_base_tobytes } from './ed25519/ge'
import { clamp } from './ed25519/utils'
const h: StaticArray<u8> = new StaticArray<u8>(SECRETKEY_BYTES)
const s: StaticArray<u8> = new StaticArray<u8>(PRIVATEKEY_BYTES)
+// temporary buffers
+const t = StaticArray.fromArray<FieldElement>([fe(), fe(), fe(), fe(), fe(), fe(), fe()])
+
/**
* Hash seed to 32-byte scalar `a`, clamp it, then point-multiply it by the
* Ed25519 base point. The Nano specification uses BLAKE2b as the hash function
clamp(s)
// Perform fixed-base scalar multiplication `[s]B`, output to public key `A`
- ge_scalarmult_base_tobytes(A, s)
+ ge_scalarmult_base_tobytes(A, s, t[0], t[1], t[2], t[3], t[4], t[5], t[6])
s.fill(0)
}
import { KEY_BYTELENGTH } from './constants'
import { crypto_derive } from './crypto_derive'
import { Blake2b } from './ed25519/blake2b'
+import { FieldElement, fe } from './ed25519/fe'
import { ge_scalarmult_base_tobytes } from './ed25519/ge'
import { sc_muladd, sc_reduce } from './ed25519/sc'
import { clamp } from './ed25519/utils'
const R = new StaticArray<u8>(64)
const k = new StaticArray<u8>(64)
const S = new StaticArray<u8>(64)
+const t = StaticArray.fromArray<FieldElement>([fe(), fe(), fe(), fe(), fe(), fe(), fe()])
/**
* Sign a message with a private key. The Nano specification uses BLAKE2b as the
* hash function instead of SHA-512 specified by RFC 8032.
sc_reduce(r)
// Perform fixed-base scalar multiplication `[r]B`, output to point `R`
- ge_scalarmult_base_tobytes(R, r)
+ ge_scalarmult_base_tobytes(R, r, t[0], t[1], t[2], t[3], t[4], t[5], t[6])
// Compute challenge hash `blake2b(R || A || M)`, output to `k`
blake2b.init().update(R, 32).update(A, 32).update(M, mlen).digest(k)
/**
* Inversion - sets `out` = 0 if `z` == 0
*/
-export function fe_invert (out: FieldElement, z: FieldElement, fe_scratch_0: FieldElement, fe_scratch_1: FieldElement, fe_scratch_2: FieldElement, fe_scratch_3: FieldElement): void {
- const t0: FieldElement = fe_scratch_0
- const t1: FieldElement = fe_scratch_1
- const t2: FieldElement = fe_scratch_2
- const t3: FieldElement = fe_scratch_3
+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)
* Preconditions:
* |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
*/
-export function fe_isnegative (f: FieldElement, fe_scratch: FieldElement): u8 {
- fe_reduce(fe_scratch, f)
- return u8(fe_scratch[0] & 1)
+export function fe_isnegative (f: FieldElement, t: FieldElement): u8 {
+ fe_reduce(t, f)
+ return u8(t[0] & 1)
}
/**
* Preconditions:
* |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
*/
-export function fe_iszero (f: FieldElement, u8x32_scratch: StaticArray<u8>, fe_scratch: FieldElement): u8 {
- fe_tobytes(u8x32_scratch, f, fe_scratch)
+export function fe_iszero (f: FieldElement, u8x32_scratch: StaticArray<u8>, t: FieldElement): u8 {
+ fe_tobytes(u8x32_scratch, f, t)
let d: i32 = 0
for (let i: i32 = 0; i < 32; i++) {
d |= u8x32_scratch[i]
* 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, fe_scratch_0: FieldElement, fe_scratch_1: FieldElement, fe_scratch_2: FieldElement): void {
- const t0 = fe_scratch_0
- const t1 = fe_scratch_1
- const t2 = fe_scratch_2
-
+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)
*
* Goal: Output h0+...+2²³⁰ h9.
*/
-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)
+export function fe_tobytes (s: StaticArray<u8>, h: FieldElement, t: FieldElement): void {
+ fe_reduce(t, h)
+ const t_ptr: usize = changetype<usize>(t)
const t0 = load<i32>(t_ptr, 0)
const t1 = load<i32>(t_ptr, 4)
const t2 = load<i32>(t_ptr, 8)
const ge_has_small_order_y_sqrtm1: FieldElement = fe()
const ge_has_small_order_c: FieldElement = fe()
-const ge_has_small_order_u8x32_scratch = new StaticArray<u8>(32)
-const ge_has_small_order_fe_scratch: 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
const y_sqrtm1 = ge_has_small_order_y_sqrtm1
const c = ge_has_small_order_c
- const u8x32_scratch = ge_has_small_order_u8x32_scratch
- const fe_scratch = ge_has_small_order_fe_scratch
+ 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, u8x32_scratch, fe_scratch)
- ret |= fe_iszero(p.Y, u8x32_scratch, fe_scratch)
- ret |= fe_iszero(p.Z, u8x32_scratch, fe_scratch)
+ ret |= fe_iszero(p.X, t_u8x32, t_fe)
+ ret |= fe_iszero(p.Y, t_u8x32, t_fe)
+ ret |= fe_iszero(p.Z, t_u8x32, t_fe)
fe_mul(y_sqrtm1, p.Y, fe_sqrtm1)
fe_sub(c, y_sqrtm1, p.X)
- ret |= fe_iszero(c, u8x32_scratch, fe_scratch)
+ ret |= fe_iszero(c, t_u8x32, t_fe)
fe_add(c, y_sqrtm1, p.X)
- ret |= fe_iszero(c, u8x32_scratch, fe_scratch)
+ ret |= fe_iszero(c, t_u8x32, t_fe)
return ret
}
* @param S 32-byte output buffer for scalar * base point
* @param a 32-byte input buffer for scalar
*/
-export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u8>): void {
+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
+): void {
const h = ge_scalarmult_base_tobytes_h
const e = ge_scalarmult_base_tobytes_e
const r = ge_scalarmult_base_tobytes_r
const s = ge_scalarmult_base_tobytes_s
const t = ge_scalarmult_base_tobytes_t
- const fe_scratch_0 = ge_scalarmult_base_tobytes_fe_scratch_0
- const fe_scratch_1 = ge_scalarmult_base_tobytes_fe_scratch_1
- const fe_scratch_2 = ge_scalarmult_base_tobytes_fe_scratch_2
- const fe_scratch_3 = ge_scalarmult_base_tobytes_fe_scratch_3
- const fe_scratch_4 = ge_scalarmult_base_tobytes_fe_scratch_4
- const fe_scratch_5 = ge_scalarmult_base_tobytes_fe_scratch_5
- const fe_scratch_6 = ge_scalarmult_base_tobytes_fe_scratch_6
for (let i = 0; i < 32; ++i) {
e[(i << 1) + 0] = (a[i] >> 0) & 15
ge_p1p1_to_p3(h, r)
}
- ge_p3_dbl(r, h, fe_scratch_0)
+ ge_p3_dbl(r, h, t0)
ge_p1p1_to_p2(s, r)
- ge_p2_dbl(r, s, fe_scratch_0)
+ ge_p2_dbl(r, s, t0)
ge_p1p1_to_p2(s, r)
- ge_p2_dbl(r, s, fe_scratch_0)
+ ge_p2_dbl(r, s, t0)
ge_p1p1_to_p2(s, r)
- ge_p2_dbl(r, s, fe_scratch_0)
+ ge_p2_dbl(r, s, t0)
ge_p1p1_to_p3(h, r)
// base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
}
ge_p3_tobytes(S, h,
- fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3, fe_scratch_4, fe_scratch_5, fe_scratch_6
+ t0, t1, t2, t3, t4, t5, t6
)
// clear sensitive data
ge_precomp_0(t)
ge_p3_0(h)
e.fill(0)
- fe_scratch_0.fill(0)
- fe_scratch_1.fill(0)
- fe_scratch_2.fill(0)
- fe_scratch_3.fill(0)
- fe_scratch_4.fill(0)
- fe_scratch_5.fill(0)
- fe_scratch_6.fill(0)
+ t0.fill(0)
+ t1.fill(0)
+ t2.fill(0)
+ t3.fill(0)
+ t4.fill(0)
+ t5.fill(0)
+ t6.fill(0)
}
const ge_double_scalarmult_vartime_aslide: StaticArray<i8> = new StaticArray<i8>(256)
/**
* r = 2 * p
*/
-export function ge_p2_dbl (r: ge_p1p1, p: ge_p2, fe_scratch: FieldElement): void {
+export function ge_p2_dbl (r: ge_p1p1, p: ge_p2, t: FieldElement): void {
fe_sq(r.X, p.X)
fe_sq(r.Z, p.Y)
fe_sq2(r.T, p.Z)
fe_add(r.Y, p.X, p.Y)
- fe_sq(fe_scratch, r.Y)
+ fe_sq(t, r.Y)
fe_add(r.Y, r.Z, r.X)
fe_sub(r.Z, r.Z, r.X)
- fe_sub(r.X, fe_scratch, r.Y)
+ fe_sub(r.X, t, r.Y)
fe_sub(r.T, r.T, r.Z)
}
/**
* r = 2 * p
*/
-export function ge_p3_dbl (r: ge_p1p1, p: ge_p3, fe_scratch: FieldElement): void {
+export function ge_p3_dbl (r: ge_p1p1, p: ge_p3, t: FieldElement): void {
fe_sq(r.X, p.X)
fe_sq(r.Z, p.Y)
fe_sq2(r.T, p.Z)
fe_add(r.Y, p.X, p.Y)
- fe_sq(fe_scratch, r.Y)
+ fe_sq(t, r.Y)
fe_add(r.Y, r.Z, r.X)
fe_sub(r.Z, r.Z, r.X)
- fe_sub(r.X, fe_scratch, r.Y)
+ fe_sub(r.X, t, r.Y)
fe_sub(r.T, r.T, r.Z)
}
/**
* r = p + q
*/
-export function ge_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached, fe_scratch: FieldElement): void {
- const t0 = fe_scratch
+export function ge_add_cached (r: ge_p1p1, p: ge_p3, q: ge_cached, t: FieldElement): void {
fe_add(r.X, p.Y, p.X)
fe_sub(r.Y, p.Y, p.X)
fe_mul(r.Z, r.X, q.YplusX)
fe_mul(r.Y, r.Y, q.YminusX)
fe_mul(r.T, q.T2d, p.T)
fe_mul(r.X, p.Z, q.Z)
- fe_dbl(t0, r.X)
+ fe_dbl(t, r.X)
fe_sub(r.X, r.Z, r.Y)
fe_add(r.Y, r.Z, r.Y)
- fe_add(r.Z, t0, r.T)
- fe_sub(r.T, t0, r.T)
+ fe_add(r.Z, t, r.T)
+ fe_sub(r.T, t, r.T)
}
/**
}
/* r = p-q */
-export function ge_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, fe_scratch: FieldElement, ge_cached_scratch: ge_cached, ge_p1p1_scratch: ge_p1p1): void {
+export function ge_sub_p3 (r: ge_p3, p: ge_p3, q: ge_p3, t_fe: FieldElement, t_ge_cached: ge_cached, t_ge_p1p1: ge_p1p1): void {
fe_neg(r.X, q.X)
fe_copy(r.Y, q.Y)
fe_copy(r.Z, q.Z)
fe_neg(r.T, q.T)
- ge_p3_to_cached(ge_cached_scratch, r)
- ge_add_cached(ge_p1p1_scratch, p, ge_cached_scratch, fe_scratch)
- ge_p1p1_to_p3(r, ge_p1p1_scratch)
+ ge_p3_to_cached(t_ge_cached, r)
+ ge_add_cached(t_ge_p1p1, p, t_ge_cached, t_fe)
+ ge_p1p1_to_p3(r, t_ge_p1p1)
}
/**
}
export function ge_p3_tobytes (s: StaticArray<u8>, h: ge_p3,
- reciprocal_scratch: FieldElement,
- x_scratch: FieldElement,
- y_scratch: FieldElement,
- fe_scratch_0: FieldElement,
- fe_scratch_1: FieldElement,
- fe_scratch_2: FieldElement,
- fe_scratch_3: FieldElement
+ reciprocal: FieldElement, x: FieldElement, y: FieldElement,
+ t0: FieldElement, t1: FieldElement, t2: FieldElement, t3: FieldElement
): void {
- fe_invert(reciprocal_scratch, h.Z, fe_scratch_0, fe_scratch_1, fe_scratch_2, fe_scratch_3)
- fe_mul(x_scratch, h.X, reciprocal_scratch)
- fe_mul(y_scratch, h.Y, reciprocal_scratch)
- fe_tobytes(s, y_scratch, fe_scratch_0)
- s[31] ^= fe_isnegative(x_scratch, fe_scratch_0) << 7
+ fe_invert(reciprocal, h.Z, t0, t1, t2, t3)
+ fe_mul(x, h.X, reciprocal)
+ fe_mul(y, h.Y, reciprocal)
+ fe_tobytes(s, y, t0)
+ s[31] ^= fe_isnegative(x, t0) << 7
}