]> git.codecow.com Git - nano25519.git/commitdiff
Encode errors for passing among WASM functions and out to host.
authorChris Duncan <chris@codecow.com>
Sat, 29 Aug 2026 21:43:54 +0000 (14:43 -0700)
committerChris Duncan <chris@codecow.com>
Sat, 29 Aug 2026 21:43:54 +0000 (14:43 -0700)
src/assembly/crypto_sign.ts
src/assembly/errors.ts [new file with mode: 0644]
src/assembly/index.ts

index 55e2a8ae339c03691fb6a7404bf2fcf30ab27c26..a6605ce7567dd77382adca526aaae51d9852297d 100644 (file)
@@ -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<u8>, M: StaticArray<u8>, 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 (file)
index 0000000..2dd6be1
--- /dev/null
@@ -0,0 +1,9 @@
+//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
+//! 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
index a277617315719bf60c60526ee5d77537f60638f2..67fbdc586ae558812b5177b4c34684b273e5b842 100644 (file)
@@ -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