import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants'
import { Blake2b } from './ed25519/blake2b'
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_dbl_to_p3, p3_sub_p3, p3_tobytes, p_cached } from './ed25519/p'
+import { ge_double_scalarmult_vartime_to_cached } from './ed25519/ge/double_scalarmult'
+import { p3, p3_0, p3_dbl_to_p3, p3_sub_cached_to_p3, p3_tobytes, p_cached } from './ed25519/p'
import { sc_is_canonical, sc_reduce } from './ed25519/sc'
import { equalbytes } from './ed25519/utils'
const expected_r = new p3()
const h = new StaticArray<u8>(64)
const sb_ah = new p3()
+const sb_ah_cached = new p_cached()
const check = new p3()
const check_r = new StaticArray<u8>(32)
p3_dbl_to_p3(A2, A)
ge_cache_odd_multiples(Ai, A2, A)
- ge_double_scalarmult_vartime_to_p3(sb_ah, h, Ai, S)
- p3_sub_p3(check, expected_r, sb_ah)
+ ge_double_scalarmult_vartime_to_cached(sb_ah_cached, h, Ai, S)
+ p3_sub_cached_to_p3(check, expected_r, sb_ah_cached)
// after cofactoring, `check` should be small order and not malformed in order
// to pass, unlike `verify_pubkey()` and `verify_signature()` below
* - precomp (Duif): (y+x,y-x,2dxy)
*/
import { base2 as Bi } from '../constants/base2'
-import { p1p1, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_0, p3_add_cached, p3_add_precomp, p3_sub_cached, p3_sub_precomp, p_cached } from '../p'
+import { p1p1, p1p1_0, p1p1_to_cached, p1p1_to_p2, p1p1_to_p3, p2, p2_0, p2_dbl, p3, p3_add_cached, p3_add_precomp, p3_sub_cached, p3_sub_precomp, p_cached } from '../p'
const aslide: StaticArray<i8> = new StaticArray<i8>(256)
const bslide: StaticArray<i8> = new StaticArray<i8>(256)
* Variable-time; only used for signature verification.
*/
export function ge_double_scalarmult_vartime_to_p3 (r: p3, a: StaticArray<u8>, Ai: StaticArray<p_cached>, b: StaticArray<u8>): void {
- p3_0(r)
+ ge_double_scalarmult_vartime_to_p1p1(t, a, Ai, b)
+ p1p1_to_p3(r, t)
+ p1p1_0(t)
+}
+
+export function ge_double_scalarmult_vartime_to_cached (r: p_cached, a: StaticArray<u8>, Ai: StaticArray<p_cached>, b: StaticArray<u8>): void {
+ ge_double_scalarmult_vartime_to_p1p1(t, a, Ai, b)
+ p1p1_to_cached(r, t)
+ p1p1_0(t)
+}
+function ge_double_scalarmult_vartime_to_p1p1 (r: p1p1, a: StaticArray<u8>, Ai: StaticArray<p_cached>, b: StaticArray<u8>): void {
slide_vartime(aslide, a)
slide_vartime(bslide, b)
}
while (i >= 0) {
- p2_dbl(t, p)
+ p2_dbl(r, p)
const aslidei = aslide[i]
if (aslidei > 0) {
- p1p1_to_p3(u, t)
+ p1p1_to_p3(u, r)
p3_add_cached(t, u, Ai[aslidei >> 1])
} else if (aslidei < 0) {
- p1p1_to_p3(u, t)
- p3_sub_cached(t, u, Ai[(-aslidei) >> 1])
+ p1p1_to_p3(u, r)
+ p3_sub_cached(r, u, Ai[(-aslidei) >> 1])
}
const bslidei = bslide[i]
if (bslidei > 0) {
- p1p1_to_p3(u, t)
- p3_add_precomp(t, u, Bi[bslidei >> 1])
+ p1p1_to_p3(u, r)
+ p3_add_precomp(r, u, Bi[bslidei >> 1])
} else if (bslidei < 0) {
- p1p1_to_p3(u, t)
- p3_sub_precomp(t, u, Bi[(-bslidei) >> 1])
+ p1p1_to_p3(u, r)
+ p3_sub_precomp(r, u, Bi[(-bslidei) >> 1])
}
if (i > 0) {
- p1p1_to_p2(p, t)
- } else {
- p1p1_to_p3(r, t)
+ p1p1_to_p2(p, r)
}
i--
}
//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! SPDX-License-Identifier: ISC
-import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_isnegative, fe_mul, fe_mul_2d, fe_mul_pair, fe_mul_pair_2d, fe_mul_pair_dbl, fe_mul_pair_dbl_2d, fe_sq_pair, fe_sq_pair_dbl, fe_sub, fe_tobytes } from './fe'
+import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_copy, fe_dbl, fe_isnegative, fe_mul, fe_mul_2d, fe_mul_pair, fe_mul_pair_2d, fe_mul_pair_dbl, fe_sq_pair, fe_sq_pair_dbl, fe_sub, fe_tobytes } from './fe'
import { fe_invert } from './z'
/**
xy2d: FieldElement = fe()
}
-const t_p3 = new p3()
+const t = new p1p1()
export function p2_0 (h: p2): void {
fe_0(h.X)
fe_sub(r.T, r.T, r.Z)
}
+/**
+ * `r = 2p`
+ *
+ * ```
+ * X = (X₁+Y₁)² - (Y₁²+X₁²)
+ * Y = Y₁² + X₁²
+ * Z = Y₁² - X₁²
+ * T = 2Z₁² - (Y₁²-X₁²)
+ * ```
+ */
+export function p3_dbl (r: p1p1, 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_addsub(r.Y, r.Z, r.Y, r.Z)
+ fe_sub(r.X, r.X, r.Y)
+ fe_sub(r.T, r.T, r.Z)
+}
+
/**
* `r = 2p`
*
* ```
*/
export function p3_dbl_to_p3 (r: p3, p: p3): void {
- fe_add(t_p3.X, p.X, p.Y)
- fe_sq_pair_dbl(t_p3.X, t_p3.X, t_p3.T, p.Z)
- fe_sq_pair(t_p3.Z, p.X, t_p3.Y, p.Y)
- fe_addsub(t_p3.Y, t_p3.Z, t_p3.Y, t_p3.Z)
- fe_sub(t_p3.X, t_p3.X, t_p3.Y)
- fe_sub(t_p3.T, t_p3.T, t_p3.Z)
-
- fe_mul_pair(r.X, t_p3.X, t_p3.T, r.T, t_p3.X, t_p3.Y)
- fe_mul_pair(r.Y, t_p3.Y, t_p3.Z, r.Z, t_p3.Z, t_p3.T)
-
- p3_0(t_p3)
+ p3_dbl(t, p)
+ p1p1_to_p3(r, t)
+ p1p1_0(t)
}
/**
* ```
*/
export function p3_dbl_to_p2 (r: p2, p: p3): void {
- fe_add(t_p3.T, p.X, p.Y)
- fe_sq_pair_dbl(t_p3.X, t_p3.T, t_p3.T, p.Z)
- fe_sq_pair(t_p3.Z, p.X, t_p3.Y, p.Y)
- fe_addsub(t_p3.Y, t_p3.Z, t_p3.Y, t_p3.Z)
- fe_sub(t_p3.X, t_p3.X, t_p3.Y)
- fe_sub(t_p3.T, t_p3.T, t_p3.Z)
-
- fe_mul_pair(r.X, t_p3.X, t_p3.T, r.Y, t_p3.Y, t_p3.Z)
- fe_mul(r.Z, t_p3.Z, t_p3.T)
-
- p3_0(t_p3)
+ p3_dbl(t, p)
+ p1p1_to_p2(r, t)
+ p1p1_0(t)
}
/**
* T2d = 2X₁Y₁d
* ```
*/
-export function p3_add_cached_output_cached (r: p_cached, p: p3, q: p_cached): void {
- // p3 + cached
- fe_addsub(t_p3.X, t_p3.Y, p.Y, p.X)
- fe_mul_pair(t_p3.X, t_p3.X, q.YplusX, t_p3.Y, t_p3.Y, q.YminusX)
- fe_mul_pair_dbl(t_p3.Z, p.Z, q.Z, t_p3.T, q.T2d, p.T)
- fe_addsub(t_p3.X, t_p3.Y, t_p3.X, t_p3.Y)
- fe_addsub(t_p3.Z, t_p3.T, t_p3.Z, t_p3.T)
-
- // p1p1 -> cached
- fe_mul_pair_2d(t_p3.Y, t_p3.Y, t_p3.T, r.T2d, t_p3.Y, t_p3.X)
- fe_mul_pair(t_p3.X, t_p3.X, t_p3.Z, r.Z, t_p3.Z, t_p3.T)
- fe_addsub(r.YplusX, r.YminusX, t_p3.X, t_p3.Y)
-
- p3_0(t_p3)
+export function p3_add_cached_to_cached (r: p_cached, p: p3, q: p_cached): void {
+ p3_add_cached(t, p, q)
+ p1p1_to_cached(r, t)
+ p1p1_0(t)
}
/**
* 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): void {
- fe_addsub(t_p3.X, t_p3.Y, p.Y, p.X) // X = Y₁+X₁, Y = Y₁-X₁
- fe_addsub(t_p3.Z, t_p3.T, q.Y, q.X) // Z = Y₂+X₂, T = Y₂-X₂
- fe_mul_pair(t_p3.X, t_p3.X, t_p3.T, t_p3.Y, t_p3.Y, t_p3.Z) // X = (Y₁+X₁)(Y₂-X₂), Y = (Y₁-X₁)(Y₂+X₂)
- fe_mul_pair_dbl_2d(t_p3.Z, p.Z, q.Z, t_p3.T, p.T, q.T) // Z = 2Z₁Z₂, T = 2T₁T₂d
- fe_addsub(t_p3.Y, t_p3.X, t_p3.X, t_p3.Y) // Y = (Y₁+X₁)(Y₂-X₂) + (Y₁-X₁)(Y₂+X₂), X = (Y₁+X₁)(Y₂-X₂) - (Y₁-X₁)(Y₂+X₂)
- fe_addsub(t_p3.T, t_p3.Z, t_p3.Z, t_p3.T) // t = 2Z₁Z₂ + 2T₁T₂d, Z = 2Z₁Z₂ - 2T₁T₂d
-
- fe_mul_pair(r.Y, t_p3.Y, t_p3.Z, r.T, t_p3.X, t_p3.Y)
- fe_mul_pair(r.X, t_p3.X, t_p3.T, r.Z, t_p3.Z, t_p3.T)
-
- p3_0(t_p3)
+export function p3_sub_cached_to_p3 (r: p3, p: p3, q: p_cached): void {
+ p3_sub_cached(t, p, q)
+ p1p1_to_p3(r, t)
+ p1p1_0(t)
}
/**
* ```
*/
export function p3_sub_cached (r: p1p1, p: p3, q: p_cached): void {
- fe_addsub(r.X, r.Y, p.Y, p.X)
- fe_mul_pair(r.X, r.X, q.YminusX, r.Y, r.Y, q.YplusX)
- fe_mul_pair_dbl(r.Z, p.Z, q.Z, r.T, q.T2d, p.T)
+ fe_addsub(r.X, r.Y, p.Y, p.X) // X = Y₁+X₁, Y = Y₁-X₁
+ fe_mul_pair(r.X, r.X, q.YminusX, r.Y, r.Y, q.YplusX) // X = (Y₁+X₁)(Y₂-X₂), Y = (Y₁-X₁)(Y₂+X₂)
+ fe_mul_pair_dbl(r.Z, p.Z, q.Z, r.T, q.T2d, p.T) // Z = 2Z₁Z₂, T = 2T₁T₂d
fe_addsub(r.Y, r.X, r.X, r.Y)
fe_addsub(r.T, r.Z, r.Z, r.T)
}
}
export function p3_tobytes (s: StaticArray<u8>, h: p3): void {
- fe_invert(t_p3.T, h.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)
- s[31] ^= fe_isnegative(t_p3.X) << 7
- p3_0(t_p3)
+ fe_invert(t.T, h.Z)
+ fe_mul_pair(t.X, h.X, t.T, t.Y, h.Y, t.T)
+ fe_tobytes(s, t.Y)
+ s[31] ^= fe_isnegative(t.X) << 7
+ p1p1_0(t)
}
import './env'
import { FieldElement, fe, fe_0, fe_1, fe_add, fe_addsub, fe_cmov, fe_copy, fe_dbl, fe_frombytes, fe_isnegative, fe_iszero, fe_mul, fe_mul_2d, fe_mul_d, fe_mul_pair, fe_mul_pair_2d, fe_mul_pair_dbl, fe_mul_pair_dbl_2d, fe_mul_pair_sqrtm1, fe_mul_sqrtm1, fe_neg, fe_reduce, fe_sq, fe_sq_n, fe_sq_pair, fe_sq_pair_dbl, fe_sub, fe_tobytes } from './ed25519/fe'
import { ge_cache_odd_multiples, ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate, ge_accept_small_order, ge_reject_small_order, ge_is_canonical, ge_scalarmult_base_tobytes } from './ed25519/ge'
-import { p3, p3_dbl_to_p3, p3_sub_p3, p3_tobytes, p_cached } from './ed25519/p'
+import { p3, p3_dbl_to_p3, p3_sub_cached_to_p3, p3_tobytes, p_cached } from './ed25519/p'
import { fe_invert, fe_pow22523 } from './ed25519/z'
import { sc_is_canonical, sc_muladd, sc_reduce } from './ed25519/sc'
import { verify_pubkey, verify_signature } from './crypto_verify'
ge_cache_odd_multiples(AI, A2, PA)
ge_double_scalarmult_vartime_to_p3(PB, U8A, AI, U8B)
}
-export function p3_sub_p3_ (): void { p3_sub_p3(PC, PA, PB) }
+export function p3_sub_cached_to_p3_ (): void { p3_sub_cached_to_p3(PC, PA, PB) }
export function verify_pubkey_ (): i32 { return verify_pubkey(U8A) ? 1 : 0 }
export function verify_signature_ (): i32 { return verify_signature(U8W) ? 1 : 0 }
`
// R - ([a]A + [b]B), as verify() computes it, with R decoded so Z = 1
const R = mul(B, randomInt(L))
setPoint(PA, R)
- x.p3_sub_p3_()
+ x.p3_sub_cached_to_p3()
const diff = getPoint(PC)
- note('p3_sub_p3 of the double scalar multiplication', equal(diff, add(R, negate(want))) && isExtended(diff), k)
+ note('p3_sub_cached_to_p3 of the double scalar multiplication', equal(diff, add(R, negate(want))) && isExtended(diff), k)
}
// both scalars zero right after a call that left a point in r
setPoint(PA, mul(B, 12345n))