import { Blake2b } from './ed25519/blake2b'
import { ge_scalarmult_base_tobytes } from './ed25519/ge'
-import { clear_t, t_p3 } from './ed25519/t'
import { clamp } from './ed25519/utils'
const PRIVATEKEY_BYTES: i32 = 32
clamp(s)
// Perform fixed-base scalar multiplication `[s]B`, output to public key `A`
- ge_scalarmult_base_tobytes(A, s, t_p3)
+ ge_scalarmult_base_tobytes(A, s)
s.fill(0)
- clear_t()
}
import { Blake2b } from './ed25519/blake2b'
import { ge_scalarmult_base_tobytes } from './ed25519/ge'
import { sc_muladd, sc_reduce } from './ed25519/sc'
-import { clear_t, t_p3 } from './ed25519/t'
import { clamp } from './ed25519/utils'
// crypto_hash function
sc_reduce(r)
// Perform fixed-base scalar multiplication `[r]B`, output to point `R`
- ge_scalarmult_base_tobytes(R, r, t_p3)
- clear_t()
+ ge_scalarmult_base_tobytes(R, r)
// Compute challenge hash `blake2b(R || A || M)`, output to `k`
blake2b.init().update(R, 32).update(A, 32).update(M, mlen).digest(k)
import { ge_accept_small_order, ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate, ge_is_canonical, ge_reject_small_order } from './ed25519/ge'
import { p3, p3_0, p3_sub_p3, p3_tobytes, p_cached } from './ed25519/p'
import { sc_is_canonical, sc_reduce } from './ed25519/sc'
-import { clear_t, t_fe } from './ed25519/t'
import { equalbytes } from './ed25519/utils'
// crypto_hash function
const sb_ah = new p3()
const check = new p3()
const check_r = new StaticArray<u8>(32)
-const t_p3 = new p3()
-const t_u8x32 = new StaticArray<u8>(32)
/**
* Verify public key `pub` can be decoded to point `A`. If so, builds a cache
* @returns null if public key fails to decode, else return pointer to Ai
*/
export function crypto_verify_decodepubkey (pub: StaticArray<u8>): StaticArray<p_cached> | null {
- const b = ge_frombytes_negate(A, pub, t_fe, t_u8x32)
- clear_t()
+ const b = ge_frombytes_negate(A, pub)
if (b != 0) {
p3_0(A)
return null
}
- ge_cache_odd_multiples(Ai, A, t_p3, t_fe)
+ ge_cache_odd_multiples(Ai, A)
return Ai
}
sc_reduce(h)
ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S)
- p3_tobytes(check_r, sb_ah, t_p3)
- clear_t()
+ p3_tobytes(check_r, sb_ah)
return equalbytes(sig, check_r, KEY_BYTELENGTH)
}
blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
sc_reduce(h)
- ge_cache_odd_multiples(Ai, A, t_p3, t_fe)
+ ge_cache_odd_multiples(Ai, A)
ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S)
- p3_sub_p3(check, expected_r, sb_ah, t_fe)
+ p3_sub_p3(check, expected_r, sb_ah)
// after cofactoring, `check` should be small order and not malformed in order
// to pass, unlike `verify_pubkey()` and `verify_signature()` below
- const b = ge_accept_small_order(check, t_fe, t_u8x32)
- clear_t()
+ const b = ge_accept_small_order(check)
if (b != 0) return 0
return 1
blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
sc_reduce(h)
- ge_cache_odd_multiples(Ai, A, t_p3, t_fe)
+ ge_cache_odd_multiples(Ai, A)
ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S)
- p3_tobytes(check_r, sb_ah, t_p3)
- clear_t()
+ p3_tobytes(check_r, sb_ah)
return equalbytes(sig, check_r, KEY_BYTELENGTH)
}
}
// fail if public key cannot be decoded
- if (ge_frombytes_negate(A, pub, t_fe, t_u8x32) != 0) {
+ if (ge_frombytes_negate(A, pub) != 0) {
p3_0(A)
- clear_t()
return false
}
// fail if public key `k` is small order
- if (ge_reject_small_order(A, t_fe, t_u8x32) != 0) {
+ if (ge_reject_small_order(A) != 0) {
p3_0(A)
- clear_t()
return false
}
- clear_t()
return true
}
}
// fail if nonce `R` does not decode to a valid curve point
- if (ge_frombytes(expected_r, sig, t_fe, t_u8x32) != 0) {
- clear_t()
+ if (ge_frombytes(expected_r, sig) != 0) {
return false
}
// fail if nonce `R` is small order
- if (ge_reject_small_order(expected_r, t_fe, t_u8x32) != 0) {
- clear_t()
+ if (ge_reject_small_order(expected_r) != 0) {
return false
}
- clear_t()
return true
}
*/
import { load_3, load_4 } from './utils'
+const t_fe = fe()
+const t_u8x32 = new StaticArray<u8>(32)
+
/**
* Ed25519 curve constant √-1
* = 2⁽ᵖ⁻¹⁾⸍⁴ mod `p`
* 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(load<i32>(changetype<usize>(t), 0) & 1)
+export function fe_isnegative (f: FieldElement): u8 {
+ fe_reduce(t_fe, f)
+ const isNeg = u8(load<i32>(changetype<usize>(t_fe), 0) & 1)
+ fe_0(t_fe)
+ return isNeg
}
/**
* Preconditions:
* |f| bounded by 1.1x2²⁶,1.1x2²⁵,1.1x2²⁶,1.1x2²⁵,etc.
*/
-export function fe_iszero (f: FieldElement, t_fe: FieldElement, t_u8x32: StaticArray<u8>): u8 {
- fe_tobytes(t_u8x32, f, t_fe)
+export function fe_iszero (f: FieldElement): u8 {
+ fe_tobytes(t_u8x32, f)
const t_ptr: usize = changetype<usize>(t_u8x32)
const t0 = v128.load(t_ptr, 0 << 4)
const t1 = v128.load(t_ptr, 1 << 4)
const d = v128.or(t0, t1)
+ v128.store(t_ptr, i64x2.splat(0), 0 << 4)
+ v128.store(t_ptr, i64x2.splat(0), 1 << 4)
return u8(v128.any_true(d)) ^ 1
}
*
* Goal: Output h0+...+2²³⁰ h9.
*/
-export function fe_tobytes (s: StaticArray<u8>, h: FieldElement, t_fe: FieldElement): void {
+export function fe_tobytes (s: StaticArray<u8>, h: FieldElement): void {
fe_reduce(t_fe, h)
const t_ptr: usize = changetype<usize>(t_fe)
const t8 = load<i32>(t_ptr, 8 << 2)
const t9 = load<i32>(t_ptr, 9 << 2)
+ fe_0(t_fe)
+
const s_ptr: usize = changetype<usize>(s)
store<u8>(s_ptr, t0 >> 0, 0)
store<u8>(s_ptr, t0 >> 8, 1)
import { p1p1, p1p1_0, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_cached, p3_add_cached_output_cached, p3_add_precomp, p3_dbl, p3_dbl_to_p3, p3_sub_cached, p3_sub_precomp, p3_to_cached, p3_tobytes, p_cached, p_precomp, precomp_0 } from './p'
import { fe_pow22523 } from './pow22523'
+const t_p3 = new p3()
+
function ge_cmov8_base (t: p_precomp, bp: usize, b: i8): void {
const isbneg: i8 = (b >> 7) & 1 /* 0: positive; 1: negative */
const babs: i8 = b - (((-isbneg) & b) * (i8(1) << 1))
*
* @returns {i32} 0 if `s` decodes to a valid point, else -1
*/
-export function ge_frombytes (h: p3, s: StaticArray<u8>, t_fe_0: FieldElement, t_u8x32: StaticArray<u8>): i32 {
+export function ge_frombytes (h: p3, s: StaticArray<u8>): i32 {
const u = ge_frombytes_u
const v = ge_frombytes_v
const vx2 = ge_frombytes_vx2
// p = vx²+u
// m = vx²-u
fe_addsub(p_root_check, m_root_check, vx2, u)
- has_p_root = fe_iszero(p_root_check, t_fe_0, t_u8x32)
- has_m_root = fe_iszero(m_root_check, t_fe_0, t_u8x32)
+ has_p_root = fe_iszero(p_root_check)
+ has_m_root = fe_iszero(m_root_check)
// x = x√-1 if vx² = -u, else x
fe_cmov(h.X, x_sqrtm1, 1 - has_m_root)
// negx = -x
fe_neg(negx, h.X)
// x = |x|
- fe_cmov(h.X, negx, fe_isnegative(h.X, t_fe_0) ^ (s[31] >> 7))
+ fe_cmov(h.X, negx, fe_isnegative(h.X) ^ (s[31] >> 7))
// Create point projection
fe_mul(h.T, h.X, h.Y)
*
* @returns {i32} 0 if `s` decodes to a valid point, else -1
*/
-export function ge_frombytes_negate (h: p3, s: StaticArray<u8>, t_fe_0: FieldElement, t_u8x32: StaticArray<u8>): i32 {
- const check = ge_frombytes(h, s, t_fe_0, t_u8x32)
+export function ge_frombytes_negate (h: p3, s: StaticArray<u8>): i32 {
+ const check = ge_frombytes(h, s)
fe_neg(h.X, h.X)
fe_neg(h.T, h.T)
return check
* @param {p3} p
* @returns 1 if `p` is malformed (`Z = 0`, or `X = Y = 0`), else 0
*/
-function ge_is_malformed (p: p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
- const x_iszero = fe_iszero(p.X, t_fe, t_u8x32)
- const y_iszero = fe_iszero(p.Y, t_fe, t_u8x32)
- const z_iszero = fe_iszero(p.Z, t_fe, t_u8x32)
+function ge_is_malformed (p: p3): i32 {
+ const x_iszero = fe_iszero(p.X)
+ const y_iszero = fe_iszero(p.Y)
+ const z_iszero = fe_iszero(p.Z)
return ((x_iszero & y_iszero) | z_iszero)
}
* @param {p3} p
* @returns 1 if `p` has small order, else 0
*/
-function ge_has_small_order (p: p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
+function ge_has_small_order (p: p3): i32 {
const y_sqrtm1 = ge_has_small_order_y_sqrtm1
const c = ge_has_small_order_c
- const x_iszero = fe_iszero(p.X, t_fe, t_u8x32)
- const y_iszero = fe_iszero(p.Y, t_fe, t_u8x32)
+ const x_iszero = fe_iszero(p.X)
+ const y_iszero = fe_iszero(p.Y)
fe_mul_sqrtm1(y_sqrtm1, p.Y)
fe_sub(c, y_sqrtm1, p.X)
- const y_sqrtm1_minusx = fe_iszero(c, t_fe, t_u8x32)
+ const y_sqrtm1_minusx = fe_iszero(c)
fe_add(c, y_sqrtm1, p.X)
- const y_sqrtm1_plusx = fe_iszero(c, t_fe, t_u8x32)
+ const y_sqrtm1_plusx = fe_iszero(c)
return x_iszero | y_iszero | y_sqrtm1_minusx | y_sqrtm1_plusx
}
* @param {p3} p
* @returns 0 if `p` is not malformed and has small order, else -1
*/
-export function ge_accept_small_order (p: p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
- const malformed = ge_is_malformed(p, t_fe, t_u8x32)
- const small_order = ge_has_small_order(p, t_fe, t_u8x32)
+export function ge_accept_small_order (p: p3): i32 {
+ const malformed = ge_is_malformed(p)
+ const small_order = ge_has_small_order(p)
return 0 - (malformed | (small_order ^ 1))
}
* @param {p3} p
* @returns 0 if `p` is not malformed and not small order, else -1
*/
-export function ge_reject_small_order (p: p3, t_fe: FieldElement, t_u8x32: StaticArray<u8>): i32 {
- const malformed = ge_is_malformed(p, t_fe, t_u8x32)
- const small_order = ge_has_small_order(p, t_fe, t_u8x32)
+export function ge_reject_small_order (p: p3): i32 {
+ const malformed = ge_is_malformed(p)
+ const small_order = ge_has_small_order(p)
return 0 - (malformed | small_order)
}
* @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>, t_p3: p3): void {
+export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u8>): void {
const h = ge_scalarmult_base_tobytes_h
const e = ge_scalarmult_base_tobytes_e
const r = ge_scalarmult_base_tobytes_r
p1p1_to_p3(h, r)
}
- p3_tobytes(S, h, t_p3)
+ p3_tobytes(S, h)
// clear sensitive data
p1p1_0(r)
* Computes a lookup table for multiples of the odd limbs of an extended
* projective point so it can be cached and reused for performance.
*/
-export function ge_cache_odd_multiples (Ai: StaticArray<p_cached>, A: p3, t_p3: p3, t_fe: FieldElement): void {
+export function ge_cache_odd_multiples (Ai: StaticArray<p_cached>, A: p3): void {
p3_to_cached(Ai[0], A)
- p3_dbl_to_p3(t_p3, A, t_fe)
+ p3_dbl_to_p3(t_p3, A)
for (let i = 0; i < 7; i++) {
- p3_add_cached_output_cached(Ai[i + 1], t_p3, Ai[i], t_fe)
+ p3_add_cached_output_cached(Ai[i + 1], t_p3, Ai[i])
}
+ p3_0(t_p3)
}
const ge_double_scalarmult_vartime_aslide: StaticArray<i8> = new StaticArray<i8>(256)
xy2d: FieldElement = fe()
}
+const t_fe = fe()
+const t_p3 = new p3()
+
export function p2_0 (h: p2): void {
fe_0(h.X)
fe_1(h.Y)
* T = 2Z₁² - (Y₁²-X₁²)
* ```
*/
-export function p3_dbl_to_p3 (r: p3, p: p3, t_fe: FieldElement): void {
+export function p3_dbl_to_p3 (r: p3, p: p3): void {
fe_add(r.X, p.X, p.Y)
fe_sq_pair_dbl(r.X, r.X, r.T, p.Z)
fe_sq_pair(r.Z, p.X, r.Y, p.Y)
fe_mul_pair(r.X, r.X, t_fe, r.T, r.X, r.Y)
fe_mul_pair(r.Y, r.Y, r.Z, r.Z, r.Z, t_fe)
+
+ fe_0(t_fe)
}
/**
* T2d = 2X₁Y₁d
* ```
*/
-export function p3_add_cached_output_cached (r: p_cached, p: p3, q: p_cached, t_fe: FieldElement): void {
+export function p3_add_cached_output_cached (r: p_cached, p: p3, q: p_cached): void {
// p3 + cached
fe_addsub(r.YplusX, r.YminusX, p.Y, p.X)
fe_mul_pair(r.YplusX, r.YplusX, q.YplusX, r.YminusX, r.YminusX, q.YminusX)
fe_mul_pair_2d(r.YminusX, r.YminusX, t_fe, r.T2d, r.YminusX, r.YplusX)
fe_mul_pair(r.YplusX, r.YplusX, r.Z, r.Z, r.Z, t_fe)
fe_addsub(r.YplusX, r.YminusX, r.YplusX, r.YminusX)
+
+ fe_0(t_fe)
}
/**
* T = X₃Y₃ = ((Y₁+X₁)(Y₂-X₂) - (Y₁-X₁)(Y₂+X₂))((Y₁+X₁)(Y₂-X₂) + (Y₁-X₁)(Y₂+X₂))
* ```
*/
-export function p3_sub_p3 (r: p3, p: p3, q: p3, t_fe: FieldElement): void {
+export function p3_sub_p3 (r: p3, p: p3, q: p3): void {
fe_addsub(r.X, r.Y, p.Y, p.X) // X = Y₁+X₁, Y = Y₁-X₁
fe_addsub(r.Z, r.T, q.Y, q.X) // Z = Y₂+X₂, T = Y₂-X₂
fe_mul_pair(r.X, r.X, r.T, r.Y, r.Y, r.Z) // X = (Y₁+X₁)(Y₂-X₂), Y = (Y₁-X₁)(Y₂+X₂)
fe_mul_pair(r.Y, r.Y, r.Z, r.T, r.X, r.Y)
fe_mul_pair(r.X, r.X, t_fe, r.Z, r.Z, t_fe)
+
+ fe_0(t_fe)
}
/**
fe_addsub(r.T, r.Z, r.Z, r.T)
}
-export function p3_tobytes (s: StaticArray<u8>, h: p3, t_p3: p3): void {
+export function p3_tobytes (s: StaticArray<u8>, h: p3): void {
fe_invert(t_p3.T, h.Z, t_p3.X, t_p3.Y, t_p3.Z)
fe_mul_pair(t_p3.X, h.X, t_p3.T, t_p3.Y, h.Y, t_p3.T)
- fe_tobytes(s, t_p3.Y, t_p3.T)
- s[31] ^= fe_isnegative(t_p3.X, t_p3.T) << 7
+ fe_tobytes(s, t_p3.Y)
+ s[31] ^= fe_isnegative(t_p3.X) << 7
+ p3_0(t_p3)
}
+++ /dev/null
-//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
-//! SPDX-License-Identifier: GPL-3.0-or-later
-
-/**
- * Shared buffers for holding temporary values. Only import in `crypto_derive`,
- * `crypto_sign`, and `crypto_verify`; they pass scratch buffers to lower
- * functions as `t_` parameters, and they clear all buffers when done with them.
- *
- * Beware nested calls. `crypto_sign` calls `crypto_derive` which overwrites all
- * temporary buffers; this specific call is OK because they never use the
- * buffers at the same time. However, never pass a buffer to a called function
- * if it presently holds a value in the current function.
- */
-import { fe, fe_0 } from './fe'
-import { p3, p3_0 } from './p'
-
-export const t_fe = fe()
-export const t_p3 = new p3()
-
-export function clear_t (): void {
- fe_0(t_fe)
- p3_0(t_p3)
-}
//! SPDX-FileCopyrightText: 2013-2026 Frank Denis <j@pureftpd.org>
//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! SPDX-License-Identifier: ISC
+/**
+ * Functions intended for use by any other arbitrary module in this Ed25519
+ * implementation. As such, it should never contain any imports to avoid any
+ * circular dependencies.
+ */
/**
* Clears the 3 least significant bits to guarantee multiple of 8 and prevent
const FE_PAIR: i32 = 3 // bytes i and j both set to m
const check_fe_iszero_f = fe()
-const check_fe_iszero_t_fe = fe()
-const check_fe_iszero_t_u8x32 = new StaticArray<u8>(KEY_BYTELENGTH)
function fe_case (shape: i32, m: i32, i: i32 = 0, j: i32 = 0): i32 {
return m | (i << 8) | (j << 13) | (shape << 18)
fe_frombytes(check_fe_iszero_f, actual)
}
- const result = fe_iszero(check_fe_iszero_f, check_fe_iszero_t_fe, check_fe_iszero_t_u8x32)
+ const result = fe_iszero(check_fe_iszero_f)
if (zero && result != 1) {
raise(ERROR_FE_ISZERO_SELFTEST_XPASS, c, result)
}