From: Chris Duncan Date: Sat, 29 Aug 2026 21:43:54 +0000 (-0700) Subject: Encode errors for passing among WASM functions and out to host. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=ab7c8b8ab30c74794be267a4a8a74245fa2ba310;p=nano25519.git Encode errors for passing among WASM functions and out to host. --- diff --git a/src/assembly/crypto_sign.ts b/src/assembly/crypto_sign.ts index 55e2a8a..a6605ce 100644 --- a/src/assembly/crypto_sign.ts +++ b/src/assembly/crypto_sign.ts @@ -4,6 +4,7 @@ import { KEY_BYTELENGTH } from '.' import { Blake2b } from './blake2b' import { crypto_derive } from './crypto_derive' +import { errors } from './errors' import { ge_scalarmult_base_tobytes } from './ge' import { sc_muladd, sc_reduce } from './sc' import { clamp } from './utils' @@ -46,7 +47,7 @@ export function crypto_sign (RS: StaticArray, M: StaticArray, mlen: i32, for (let i = 0; i < KEY_BYTELENGTH; i++) { c |= A[i] ^ pub[i] } - if (c != 0) return -1 + if (c != 0) return 1 // Hash private key to `h` blake2b.init().update(prv, KEY_BYTELENGTH).digest(h) diff --git a/src/assembly/errors.ts b/src/assembly/errors.ts new file mode 100644 index 0000000..2dd6be1 --- /dev/null +++ b/src/assembly/errors.ts @@ -0,0 +1,9 @@ +//! SPDX-FileCopyrightText: 2026 Chris Duncan +//! SPDX-License-Identifier: GPL-3.0-or-later + +export const errors = [ + null, + 'Invalid public key', + 'Invalid message length', + 'Invalid block count' +] \ No newline at end of file diff --git a/src/assembly/index.ts b/src/assembly/index.ts index a277617..67fbdc5 100644 --- a/src/assembly/index.ts +++ b/src/assembly/index.ts @@ -4,6 +4,7 @@ import { crypto_derive } from './crypto_derive' import { crypto_sign } from './crypto_sign' import { crypto_verify_decodepubkey, crypto_verify_relaxed, crypto_verify_strict } from './crypto_verify' +import { errors } from './errors' export const BLOCKHASH_BYTELENGTH: i32 = 32 export const KEY_BYTELENGTH: i32 = 32 @@ -121,7 +122,7 @@ export function sign (mlen: i32): void { INPUT_PRV.fill(0) INPUT_PUB.fill(0) INPUT_MSG.fill(0) - throw new Error('Invalid message length') + throw new Error(errors[2]) } // Copy input buffers to local parameters, then clear input buffer @@ -139,7 +140,7 @@ export function sign (mlen: i32): void { // Clear signature output buffer if signing failed if (result != 0) { OUTPUT_SIGN.fill(0) - throw new Error('Failed to sign, check keypair and message are valid') + throw new Error(errors[result]) } } @@ -161,7 +162,7 @@ export function verify (mlen: i32): void { if (mlen < 0 || mlen > MAX_MESSAGE_BYTELENGTH) { INPUT_MSG.fill(0) INPUT_PUB.fill(0) - throw new Error('Invalid message length') + throw new Error(errors[2]) } // Copy input buffer to local parameters, then clear input buffer @@ -209,7 +210,7 @@ export function verify_blocks (count: i32): void { if (count < 1 || MAX_VERIFY_BLOCKS < count) { INPUT_MSG.fill(0) INPUT_PUB.fill(0) - throw new Error('Invalid block count') + throw new Error(errors[3]) } // Copy public key input buffer to local parameter, then clear input buffer