]> git.codecow.com Git - nano25519.git/commitdiff
Reduce module size by sharing temp buffers among all three crypto_ functions.
authorChris Duncan <chris@codecow.com>
Fri, 9 Oct 2026 16:37:41 +0000 (09:37 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 9 Oct 2026 16:37:41 +0000 (09:37 -0700)
src/assembly/crypto_derive.ts
src/assembly/crypto_sign.ts
src/assembly/crypto_verify.ts
src/assembly/ed25519/ge.ts
src/assembly/ed25519/t.ts [new file with mode: 0644]
src/assembly/env.ts

index 539d069f8b3492ea08b86d459b78cf381d16418e..d38e072b19f0f37fa6c7b3f61580c676ee08d836 100644 (file)
@@ -2,8 +2,8 @@
 //! 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
@@ -17,9 +17,6 @@ const blake2b = new Blake2b()
 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
@@ -43,6 +40,6 @@ export function crypto_derive (A: StaticArray<u8>, key: StaticArray<u8>): void {
        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)
 }
index 2e0aee75eabdccda6c35ceee6875a048895a3d53..10c459b1d72facf1012782a4e8db647642109a51 100644 (file)
@@ -4,9 +4,9 @@
 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
@@ -21,7 +21,7 @@ const r = new StaticArray<u8>(64)
 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.
@@ -69,7 +69,7 @@ export function crypto_sign (RS: StaticArray<u8>, M: StaticArray<u8>, mlen: i32,
        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)
index 2977e746a298a52f380e8c27f8c4af2daf0935ba..506a8ecda27151d22bb65bf776559e23b311c8a1 100644 (file)
@@ -3,10 +3,10 @@
 
 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
@@ -23,7 +23,6 @@ const check_r = new StaticArray<u8>(32)
 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.
@@ -31,7 +30,7 @@ const t_fe = StaticArray.fromArray<FieldElement>([fe(), fe(), fe(), fe(), fe(),
  */
 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
 }
 
 /**
@@ -61,8 +60,8 @@ export function crypto_verify_donna (sig: StaticArray<u8>, M: StaticArray<u8>, 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)
 }
@@ -88,10 +87,10 @@ export function crypto_verify_sodium (sig: StaticArray<u8>, M: StaticArray<u8>,
        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)
 }
 
 /**
@@ -116,10 +115,10 @@ export function crypto_verify_strict (sig: StaticArray<u8>, M: StaticArray<u8>,
        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)
 }
 
@@ -134,10 +133,10 @@ function verify_pubkey (pub: StaticArray<u8>): boolean {
        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
 }
@@ -156,10 +155,10 @@ function verify_signature (sig: StaticArray<u8>): boolean {
        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
 }
index 0d43594582f7dabb903af128c5f5a8ba33a74973..a0bbfe65f3a35ab72594ec029f5c2f721d838c49 100644 (file)
@@ -315,7 +315,7 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
 
        // 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)
        }
@@ -331,7 +331,7 @@ export function ge_scalarmult_base_tobytes (S: StaticArray<u8>, a: StaticArray<u
 
        // 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)
        }
@@ -440,20 +440,22 @@ export function ge_double_scalarmult_vartime_to_p3 (r: ge_p3, a: StaticArray<u8>
        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)
diff --git a/src/assembly/ed25519/t.ts b/src/assembly/ed25519/t.ts
new file mode 100644 (file)
index 0000000..4e4afcd
--- /dev/null
@@ -0,0 +1,18 @@
+//! 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()
index cbb09dccf84f0f5cf2c8d1cb04584fd178e3e3a0..72366e05fb94e0f126345a518c19cca899c74e7f 100644 (file)
@@ -1,8 +1,6 @@
 //! 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`.
@@ -15,7 +13,7 @@ import { ERROR_UNKNOWN, } from './errors'
 @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
 
@@ -28,7 +26,7 @@ export function abort (message: usize, fileName: usize, row: u32, col: u32): voi
        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)