From 38c3e3ff79356d47c384f9e01e468f609816fe84 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 4 Sep 2026 11:18:36 -0700 Subject: [PATCH] Move constants out of main WASM file to eliminate possible import loops. --- src/assembly/constants.ts | 23 ++++++++++++++++++++--- src/assembly/crypto_sign.ts | 2 +- src/assembly/crypto_verify.ts | 2 +- src/assembly/index.ts | 19 ++++++------------- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/assembly/constants.ts b/src/assembly/constants.ts index 290c016..41a9b55 100644 --- a/src/assembly/constants.ts +++ b/src/assembly/constants.ts @@ -3,17 +3,34 @@ import { FieldElement } from './fe' -/* sqrt(-1) */ +/** + * √-1 + */ export const fe_sqrtm1: FieldElement = StaticArray.fromArray([ -32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482, 0, 0 ]) -/* 37095705934669439343138083508754565189542113879843219016388785533085940283555 */ +/** + * d = −121665 / 121666 + * = 37095705934669439343138083508754565189542113879843219016388785533085940283555 + */ export const ed25519_d: FieldElement = StaticArray.fromArray([ -10913610, 13857413, -15372611, 6949391, 114729, -8787816, -6275908, -3247719, -18696448, -12055116, 0, 0 ]) -/* 16295367250680780974490674513165176452449235426866156013048779062215315747161 */ +/** + * 2d = −121665 / 60833 + * = 16295367250680780974490674513165176452449235426866156013048779062215315747161 + */ export const ed25519_d2: FieldElement = StaticArray.fromArray([ -21827239, -5839606, -30745221, 13898782, 229458, 15978800, -12551817, -6495438, 29715968, 9444199, 0, 0 ]) + +const PAGE_BYTELENGTH: i32 = 1 << 16 +export const BLOCKHASH_BYTELENGTH: i32 = 32 +export const KEY_BYTELENGTH: i32 = 32 +export const SIGNATURE_BYTELENGTH: i32 = 64 +export const MAX_VERIFY_BLOCKS: i32 = 64 +export const SIGNEDBLOCK_BYTELENGTH: i32 = BLOCKHASH_BYTELENGTH + SIGNATURE_BYTELENGTH +export const MAX_VERIFY_BLOCKS_BYTELENGTH: i32 = MAX_VERIFY_BLOCKS * SIGNEDBLOCK_BYTELENGTH +export const MAX_MESSAGE_BYTELENGTH: i32 = MAX_VERIFY_BLOCKS_BYTELENGTH > PAGE_BYTELENGTH ? MAX_VERIFY_BLOCKS_BYTELENGTH : PAGE_BYTELENGTH diff --git a/src/assembly/crypto_sign.ts b/src/assembly/crypto_sign.ts index cfc9523..c3429da 100644 --- a/src/assembly/crypto_sign.ts +++ b/src/assembly/crypto_sign.ts @@ -1,8 +1,8 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { KEY_BYTELENGTH } from '.' import { Blake2b } from './blake2b' +import { KEY_BYTELENGTH } from './constants' import { crypto_derive } from './crypto_derive' import { ge_scalarmult_base_tobytes } from './ge' import { sc_muladd, sc_reduce } from './sc' diff --git a/src/assembly/crypto_verify.ts b/src/assembly/crypto_verify.ts index c0f9d06..09f1b39 100644 --- a/src/assembly/crypto_verify.ts +++ b/src/assembly/crypto_verify.ts @@ -1,8 +1,8 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from '.' import { Blake2b } from './blake2b' +import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH } from './constants' import { ge_double_scalarmult_vartime_to_p3, ge_frombytes, ge_frombytes_negate_vartime, ge_has_small_order, ge_is_canonical } from './ge' import { ge_p3, ge_p3_tobytes, ge_sub_p3 } from './p' import { sc_is_canonical, sc_reduce } from './sc' diff --git a/src/assembly/index.ts b/src/assembly/index.ts index 7cb2fc3..16d1fea 100644 --- a/src/assembly/index.ts +++ b/src/assembly/index.ts @@ -1,21 +1,14 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { crypto_derive } from './crypto_derive' -import { crypto_sign } from './crypto_sign' -import { crypto_verify_decodepubkey, crypto_verify_donna, crypto_verify_sodium } from './crypto_verify' -import { errors } from './errors' +import { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, MAX_VERIFY_BLOCKS_BYTELENGTH, SIGNATURE_BYTELENGTH, SIGNEDBLOCK_BYTELENGTH, } from './constants' +import { crypto_derive, } from './crypto_derive' +import { crypto_sign, } from './crypto_sign' +import { crypto_verify_decodepubkey, crypto_verify_donna, crypto_verify_sodium, } from './crypto_verify' +import { errors, } from './errors' import './tests' -export const BLOCKHASH_BYTELENGTH: i32 = 32 -export const KEY_BYTELENGTH: i32 = 32 -export const SIGNATURE_BYTELENGTH: i32 = 64 - -const PAGE_BYTELENGTH: i32 = 1 << 16 -const SIGNEDBLOCK_BYTELENGTH: i32 = BLOCKHASH_BYTELENGTH + SIGNATURE_BYTELENGTH -export const MAX_VERIFY_BLOCKS: i32 = 64 -export const MAX_VERIFY_BLOCKS_BYTELENGTH: i32 = MAX_VERIFY_BLOCKS * SIGNEDBLOCK_BYTELENGTH -export const MAX_MESSAGE_BYTELENGTH: i32 = MAX_VERIFY_BLOCKS_BYTELENGTH > PAGE_BYTELENGTH ? MAX_VERIFY_BLOCKS_BYTELENGTH : PAGE_BYTELENGTH +export { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, MAX_VERIFY_BLOCKS_BYTELENGTH, SIGNATURE_BYTELENGTH } // Static I/O buffers const INPUT_MSG = new StaticArray(MAX_MESSAGE_BYTELENGTH) -- 2.52.0