//! 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 { t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6 } from './ed25519/t'
import { clamp } from './ed25519/utils'
const PRIVATEKEY_BYTES: i32 = 32
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, t[0], t[1], t[2], t[3], t[4], t[5], t[6])
+ ge_scalarmult_base_tobytes(A, s, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_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 { t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6 } from './ed25519/t'
import { clamp } from './ed25519/utils'
// crypto_hash function
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, t[0], t[1], t[2], t[3], t[4], t[5], t[6])
+ ge_scalarmult_base_tobytes(R, r, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
// Compute challenge hash `blake2b(R || A || M)`, output to `k`
blake2b.init().update(R, 32).update(A, 32).update(M, mlen).digest(k)
import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants'
import { Blake2b } from './ed25519/blake2b'
-import { FieldElement, fe } from './ed25519/fe'
import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_has_small_order, ge_is_canonical } from './ed25519/ge'
import { ge_cached, ge_p1p1, ge_p3, ge_p3_tobytes, ge_sub_p3 } from './ed25519/p'
import { sc_is_canonical, sc_reduce } from './ed25519/sc'
+import { t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6 } from './ed25519/t'
import { equalbytes } from './ed25519/utils'
// crypto_hash function
const t_ge_cached = new ge_cached()
const t_ge_p1p1 = new ge_p1p1()
const t_u8x32 = new StaticArray<u8>(32)
-const t_fe = StaticArray.fromArray<FieldElement>([fe(), fe(), fe(), fe(), fe(), fe(), fe()])
/**
* Verify public key `pub` can be decoded.
*/
export function crypto_verify_decodepubkey (pub: StaticArray<u8>): ge_p3 | null {
// fail if public key cannot be decoded
- return (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe[0], t_fe[1], t_fe[2]) != 0) ? null : A
+ return (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe_0, t_fe_1, t_fe_2) != 0) ? null : A
}
/**
blake2b.init().update(sig, 32).update(pub, KEY_BYTELENGTH).update(M, BLOCKHASH_BYTELENGTH).digest(h)
sc_reduce(h)
- ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe[0])
- ge_p3_tobytes(check_r, sb_ah, t_fe[0], t_fe[1], t_fe[2], t_fe[3], t_fe[4], t_fe[5], t_fe[6])
+ ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
+ ge_p3_tobytes(check_r, sb_ah, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
return equalbytes(sig, check_r, KEY_BYTELENGTH)
}
blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
sc_reduce(h)
- ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe[0])
- ge_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1)
+ ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
+ ge_sub_p3(check, expected_r, sb_ah, t_fe_0, t_ge_cached, t_ge_p1p1)
- return ge_has_small_order(check, t_fe[0], t_u8x32)
+ return ge_has_small_order(check, t_fe_0, t_u8x32)
}
/**
blake2b.init().update(sig, 32).update(pub, 32).update(M, mlen).digest(h)
sc_reduce(h)
- ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe[0])
- ge_sub_p3(check, expected_r, sb_ah, t_fe[0], t_ge_cached, t_ge_p1p1)
+ ge_double_scalarmult_vartime_to_p3(sb_ah, h, A, S, t_fe_0)
+ ge_sub_p3(check, expected_r, sb_ah, t_fe_0, t_ge_cached, t_ge_p1p1)
- ge_p3_tobytes(check_r, sb_ah, t_fe[0], t_fe[1], t_fe[2], t_fe[3], t_fe[4], t_fe[5], t_fe[6])
+ ge_p3_tobytes(check_r, sb_ah, t_fe_0, t_fe_1, t_fe_2, t_fe_3, t_fe_4, t_fe_5, t_fe_6)
return equalbytes(sig, check_r, KEY_BYTELENGTH)
}
if (ge_is_canonical(pub) != 0) return false
// fail if public key cannot be decoded
- if (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe[0], t_fe[1], t_fe[2]) != 0) return false
+ if (ge_frombytes_negate_vartime(A, pub, t_u8x32, t_fe_0, t_fe_1, t_fe_2) != 0) return false
// fail if public key `k` is small order
- if (ge_has_small_order(A, t_fe[0], t_u8x32) != 0) return false
+ if (ge_has_small_order(A, t_fe_0, t_u8x32) != 0) return false
return true
}
if (ge_is_canonical(sig) != 0) return false
// fail if nonce `R` does not decode to a valid curve point
- if (ge_frombytes(expected_r, sig, t_fe[0], t_fe[1], t_fe[2], t_u8x32) != 0) return false
+ if (ge_frombytes(expected_r, sig, t_fe_0, t_fe_1, t_fe_2, t_u8x32) != 0) return false
// fail if nonce `R` is small order
- if (ge_has_small_order(expected_r, t_fe[0], t_u8x32) != 0) return false
+ if (ge_has_small_order(expected_r, t_fe_0, t_u8x32) != 0) return false
return true
}
// base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
for (let i = 1, bp = 0; i < 64; i += 2, bp += 1152) {
- ge_cmov8_base(t, base + bp, e[i])
+ ge_cmov8_base(t, base + bp, load<i8>(changetype<usize>(e) + i))
ge_add_precomp(r, h, t)
ge_p1p1_to_p3(h, r)
}
// base pointer offset = 4 bytes * 12 values * 3 fe * 8 ge = 1152 bytes
for (let i = 0, bp = 0; i < 64; i += 2, bp += 1152) {
- ge_cmov8_base(t, base + bp, e[i])
+ ge_cmov8_base(t, base + bp, load<i8>(changetype<usize>(e) + i))
ge_add_precomp(r, h, t)
ge_p1p1_to_p3(h, r)
}
for (; i >= 0; --i) {
ge_p2_dbl(t, p, t_fe)
- if (aslide[i] > 0) {
+ const aslidei = aslide[i]
+ if (aslidei > 0) {
ge_p1p1_to_p3(u, t)
- ge_add_cached(t, u, Ai[aslide[i] / 2], t_fe)
- } else if (aslide[i] < 0) {
+ ge_add_cached(t, u, Ai[aslidei >> 1], t_fe)
+ } else if (aslidei < 0) {
ge_p1p1_to_p3(u, t)
- ge_sub_cached(t, u, Ai[(-aslide[i]) / 2])
+ ge_sub_cached(t, u, Ai[(-aslidei) >> 1])
}
- if (bslide[i] > 0) {
+ const bslidei = bslide[i]
+ if (bslidei > 0) {
ge_p1p1_to_p3(u, t)
- ge_add_precomp(t, u, Bi[bslide[i] / 2])
- } else if (bslide[i] < 0) {
+ ge_add_precomp(t, u, Bi[bslidei >> 1])
+ } else if (bslidei < 0) {
ge_p1p1_to_p3(u, t)
- ge_sub_precomp(t, u, Bi[(-bslide[i]) / 2])
+ ge_sub_precomp(t, u, Bi[(-bslidei) >> 1])
}
ge_p1p1_to_p2(p, t)
--- /dev/null
+//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
+//! SPDX-License-Identifier: GPL-3.0-or-later
+
+/**
+ * Shared buffers for holding temporary values. To ensure buffers are not
+ * overwritten between dependent calls, these should only be imported from the
+ * highest call site (i.e. crypto_derive/crypto_sign/crypto_verify) and passed
+ * down to lower functions as parameter arguments.
+ */
+import { fe } from './fe'
+
+export const t_fe_0 = fe()
+export const t_fe_1 = fe()
+export const t_fe_2 = fe()
+export const t_fe_3 = fe()
+export const t_fe_4 = fe()
+export const t_fe_5 = fe()
+export const t_fe_6 = fe()
//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
//! SPDX-License-Identifier: GPL-3.0-or-later
-import { ERROR_UNKNOWN, } from './errors'
-
/**
* The module's only import, installed as AssemblyScript's `abort` by
* `use: ["abort=src/assembly/env/abort"]` in `asconfig.json`.
@external('env', 'abort')
declare function env_abort (code: i32, line: i32, detail: i32, context: i32): void
-let errorCode: i32 = ERROR_UNKNOWN
+let errorCode: i32 = 0
let errorDetail: i32 = 0
let errorContext: i32 = 0
const code = errorCode
const detail = errorDetail
const context = errorContext
- errorCode = ERROR_UNKNOWN
+ errorCode = 0
errorDetail = 0
errorContext = 0
env_abort(code, i32(row), detail, context)