From d79d37a0ae960d08b40f394d1126c2cea5a7af8a Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 5 Oct 2026 22:59:16 -0700 Subject: [PATCH] Update canonical group element tests. --- src/assembly/ed25519/ge.ts | 4 +-- src/assembly/env.ts | 8 +++-- src/assembly/errors.ts | 18 ++++++++-- src/assembly/index.ts | 4 +-- src/assembly/tests.ts | 72 ++++++++++++++++++++------------------ src/lib/errors.ts | 36 +++++++++++-------- 6 files changed, 84 insertions(+), 58 deletions(-) diff --git a/src/assembly/ed25519/ge.ts b/src/assembly/ed25519/ge.ts index d8337df..6c0f400 100644 --- a/src/assembly/ed25519/ge.ts +++ b/src/assembly/ed25519/ge.ts @@ -271,13 +271,13 @@ export function ge_has_small_order (p: ge_p3): i32 { * @returns 0 if `s < p`, else -1 */ export function ge_is_canonical (s: StaticArray): i32 { - // setup failure if any bits of `y` are set to 0 + // setup success if any bits of `y` are set to 0 let c: i32 = (s[31] & 0x7f) ^ 0x7f for (let i = 30; i > 0; i--) { c |= s[i] ^ 0xff } c = (c - 1) >> 8 - // setup failure if low bits of `y` are greater than or equal to -19 + // setup success if low bits of `y` are less than -19 const d: i32 = (0xec - s[0]) >> 8 return 0 - (c & d & 1) } diff --git a/src/assembly/env.ts b/src/assembly/env.ts index 5f53c2f..e2b22db 100644 --- a/src/assembly/env.ts +++ b/src/assembly/env.ts @@ -18,19 +18,22 @@ declare function env_abort (code: i32, line: i32, detail: i32, context: i32): vo let errorCode: i32 = ERROR_UNKNOWN let errorDetail: i32 = 0 let errorContext: i32 = 0 +let errorLine: i32 = 0 /** * Reads the pending code, resets it, and hands it to the host. Resetting * *before* the call matters: `env_abort` throws, so nothing after it runs, and * a stale code would otherwise be attributed to the next uncoded abort. */ -export function abort (message: usize, fileName: usize, line: u32, col: u32): void { +export function abort (message: usize, fileName: usize, row: u32, col: u32): void { const code = errorCode const detail = errorDetail const context = errorContext + const line = errorLine || row errorCode = ERROR_UNKNOWN errorDetail = 0 errorContext = 0 + errorLine = 0 env_abort(code, i32(line), detail, context) } @@ -45,9 +48,10 @@ export function abort (message: usize, fileName: usize, line: u32, col: u32): vo */ //@ts-expect-error @inline -export function raise (code: i32, detail: i32 = 0, context: i32 = 0): void { +export function raise (code: i32, detail: i32 = 0, context: i32 = 0, line: i32 = 0): void { errorCode = code errorDetail = detail errorContext = context + errorLine = line throw new Error() } diff --git a/src/assembly/errors.ts b/src/assembly/errors.ts index 7110503..9103112 100644 --- a/src/assembly/errors.ts +++ b/src/assembly/errors.ts @@ -41,6 +41,18 @@ export const BUFFER_OUTPUT_VERIFY: i32 = 6 * - `SELFTEST_XFAIL_ONE`: `b` is `(position << 8) | mask` * - `SELFTEST_XFAIL_PAIR`: `b` is `(position << 8) | position`, both `^= 0x80` */ -export const ERROR_SELFTEST_XPASS: i32 = 6 -export const ERROR_SELFTEST_XFAIL_ONE: i32 = 7 -export const ERROR_SELFTEST_XFAIL_PAIR: i32 = 8 +export const ERROR_EQUALBYTES_SELFTEST_XPASS: i32 = 6 +export const ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE: i32 = 7 +export const ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR: i32 = 8 + +/** + * `ge_is_canonical` start-function guard. `a` is the vector index (0 `zeros`, + * 1 `ones`, 2 `spread`); `b` encodes the corruption the comparison failed to + * detect with 0 for `SELFTEST_XPASS`. + * + * - `SELFTEST_XFAIL_ONE`: `b` is `(position << 8) | mask` + * - `SELFTEST_XFAIL_PAIR`: `b` is `(position << 8) | position`, both `^= 0x80` + */ +export const ERROR_GE_IS_CANONICAL_SELFTEST_XPASS: i32 = 9 +export const ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL: i32 = 10 +export const ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL_PAIR: i32 = 11 diff --git a/src/assembly/index.ts b/src/assembly/index.ts index b82b2d0..82f42c8 100644 --- a/src/assembly/index.ts +++ b/src/assembly/index.ts @@ -6,10 +6,10 @@ 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 { raise, } from './env' -import { BUFFER_INPUT_MSG, BUFFER_INPUT_PRV, BUFFER_INPUT_PUB, BUFFER_INPUT_SIG, BUFFER_OUTPUT_DERIVE, BUFFER_OUTPUT_SIGN, BUFFER_OUTPUT_VERIFY, ERROR_BYTE_OUT_OF_RANGE, ERROR_INDEX_OUT_OF_RANGE, ERROR_INVALID_BLOCK_COUNT, ERROR_INVALID_MESSAGE_LENGTH, ERROR_INVALID_PUBLIC_KEY, ERROR_SELFTEST_XFAIL_ONE, ERROR_SELFTEST_XFAIL_PAIR, ERROR_SELFTEST_XPASS, ERROR_UNKNOWN, } from './errors' +import { BUFFER_INPUT_MSG, BUFFER_INPUT_PRV, BUFFER_INPUT_PUB, BUFFER_INPUT_SIG, BUFFER_OUTPUT_DERIVE, BUFFER_OUTPUT_SIGN, BUFFER_OUTPUT_VERIFY, ERROR_BYTE_OUT_OF_RANGE, ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE, ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR, ERROR_EQUALBYTES_SELFTEST_XPASS, ERROR_INDEX_OUT_OF_RANGE, ERROR_INVALID_BLOCK_COUNT, ERROR_INVALID_MESSAGE_LENGTH, ERROR_INVALID_PUBLIC_KEY, ERROR_UNKNOWN, } from './errors' import './tests' -export { BLOCKHASH_BYTELENGTH, BUFFER_INPUT_MSG, BUFFER_INPUT_PRV, BUFFER_INPUT_PUB, BUFFER_INPUT_SIG, BUFFER_OUTPUT_DERIVE, BUFFER_OUTPUT_SIGN, BUFFER_OUTPUT_VERIFY, ERROR_BYTE_OUT_OF_RANGE, ERROR_INDEX_OUT_OF_RANGE, ERROR_INVALID_BLOCK_COUNT, ERROR_INVALID_MESSAGE_LENGTH, ERROR_INVALID_PUBLIC_KEY, ERROR_SELFTEST_XFAIL_ONE, ERROR_SELFTEST_XFAIL_PAIR, ERROR_SELFTEST_XPASS, ERROR_UNKNOWN, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, MAX_VERIFY_BLOCKS_BYTELENGTH, SIGNATURE_BYTELENGTH } +export { BLOCKHASH_BYTELENGTH, BUFFER_INPUT_MSG, BUFFER_INPUT_PRV, BUFFER_INPUT_PUB, BUFFER_INPUT_SIG, BUFFER_OUTPUT_DERIVE, BUFFER_OUTPUT_SIGN, BUFFER_OUTPUT_VERIFY, ERROR_BYTE_OUT_OF_RANGE, ERROR_INDEX_OUT_OF_RANGE, ERROR_INVALID_BLOCK_COUNT, ERROR_INVALID_MESSAGE_LENGTH, ERROR_INVALID_PUBLIC_KEY, ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE as ERROR_SELFTEST_XFAIL_ONE, ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR as ERROR_SELFTEST_XFAIL_PAIR, ERROR_EQUALBYTES_SELFTEST_XPASS as ERROR_SELFTEST_XPASS, ERROR_UNKNOWN, 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) diff --git a/src/assembly/tests.ts b/src/assembly/tests.ts index 5a81812..0175179 100644 --- a/src/assembly/tests.ts +++ b/src/assembly/tests.ts @@ -5,7 +5,7 @@ import { KEY_BYTELENGTH } from './constants' import { ge_is_canonical } from './ed25519/ge' import { equalbytes } from './ed25519/utils' import { raise, } from './env' -import { ERROR_SELFTEST_XFAIL_ONE, ERROR_SELFTEST_XFAIL_PAIR, ERROR_SELFTEST_XPASS, } from './errors' +import { ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE, ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR, ERROR_EQUALBYTES_SELFTEST_XPASS, ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL, ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL_PAIR, ERROR_GE_IS_CANONICAL_SELFTEST_XPASS } from './errors' /** * Tests run on initialization to ensure build is correct. @@ -15,21 +15,21 @@ import { ERROR_SELFTEST_XFAIL_ONE, ERROR_SELFTEST_XFAIL_PAIR, ERROR_SELFTEST_XPA * for a path that runs once and then traps. The host reconstructs the text * from `vector` and `detail` — see `src/lib/errors.ts`. */ -function xpass_equalbytes (vector: i32, expected: StaticArray, actual: StaticArray): void { +function xpass_equalbytes (vector: i32, expected: StaticArray, actual: StaticArray, line: i32 = 0): void { if (!equalbytes(expected, actual, KEY_BYTELENGTH)) { - raise(ERROR_SELFTEST_XPASS, vector, 0) + raise(ERROR_EQUALBYTES_SELFTEST_XPASS, vector, 0, line) } } -function xfail_equalbytes_one (vector: i32, position: i32, mask: i32, expected: StaticArray, actual: StaticArray): void { +function xfail_equalbytes_one (vector: i32, position: i32, mask: i32, expected: StaticArray, actual: StaticArray, line: i32 = 0): void { if (equalbytes(expected, actual, KEY_BYTELENGTH)) { - raise(ERROR_SELFTEST_XFAIL_ONE, vector, (position << 8) | mask) + raise(ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE, vector, (position << 8) | mask, line) } } -function xfail_equalbytes_pair (vector: i32, i: i32, j: i32, expected: StaticArray, actual: StaticArray): void { +function xfail_equalbytes_pair (vector: i32, i: i32, j: i32, expected: StaticArray, actual: StaticArray, line: i32 = 0): void { if (equalbytes(expected, actual, KEY_BYTELENGTH)) { - raise(ERROR_SELFTEST_XFAIL_PAIR, vector, (i << 8) | j) + raise(ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR, vector, (i << 8) | j, line) } } @@ -78,56 +78,60 @@ for (let v = 0; v < expecteds.length; v++) { } } -function xpass_ge_is_canonical (vector: i32, data: StaticArray): void { - if (!ge_is_canonical(data)) { - raise(ERROR_SELFTEST_XPASS, vector, 0) +function xpass_ge_is_canonical (vector: i32, data: StaticArray, line: i32 = 0): void { + if (ge_is_canonical(data) != 0) { + raise(ERROR_GE_IS_CANONICAL_SELFTEST_XPASS, vector, 0, line) } } -function xfail_ge_is_canonical (vector: i32, t: i32, data: StaticArray): void { - if (ge_is_canonical(data)) { - raise(ERROR_SELFTEST_XFAIL_ONE, vector, t) +function xfail_ge_is_canonical (vector: i32, position: i32, t: i32, data: StaticArray, line: i32 = 0): void { + if (ge_is_canonical(data) != -1) { + raise(ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL, vector, (position << 8) | t, line) } } -function xfail_ge_is_canonical_pair (vector: i32, i: i32, j: i32, data: StaticArray): void { - if (ge_is_canonical(data)) { - raise(ERROR_SELFTEST_XFAIL_PAIR, vector, (i << 8) | j) +function xfail_ge_is_canonical_pair (vector: i32, i: i32, j: i32, data: StaticArray, line: i32 = 0): void { + if (ge_is_canonical(data) != -1) { + raise(ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL_PAIR, vector, (i << 8) | j, line) } } /** * ge_is_canonical must report "canonical" for every `s` less than `p` and * "non-canonical" for every `s` equal to or greater than `p`. - * - * - `zeros`: lower boundary with every bit cleared - * - `ones`: upper boundary with every bit set - * - `spread`: all bytes distinct from each other and setting each bit to 0 or 1 */ +memory.copy(changetype(actual), changetype(zeros), KEY_BYTELENGTH) +xpass_ge_is_canonical(0, actual, 104) -// Vector index is the error detail: 0 zeros, 1 ones, 2 spread -const expected_ge_is_canonical = [zeros, ones, spread,] +memory.copy(changetype(actual), changetype(ones), KEY_BYTELENGTH) +xfail_ge_is_canonical(1, 31, 0, actual, 107) -for (let v = 0; v < expected_ge_is_canonical.length; v++) { - const expected = expected_ge_is_canonical[v] - memory.copy(changetype(actual), changetype(expected), KEY_BYTELENGTH) - xpass_ge_is_canonical(v, actual) - - // verify all 19 non-canonical encodings fail - for (let i = 0; i <= 19; i++) { - actual[31] -= u8(i) - xfail_ge_is_canonical(v, i, actual) - actual[31] = expected[i] - } +// verify all 19 non-canonical encodings fail +for (let i = 1; i <= 19; i++) { + actual[0] = u8(0x100 - i) + xfail_ge_is_canonical(1, 0, i, actual, 112) +} +// verify canonical encodings per byte +for (let v = 0; v < expecteds.length; v++) { + const expected = expecteds[v] + memory.copy(changetype(actual), changetype(expected), KEY_BYTELENGTH) for (let i = 0; i < KEY_BYTELENGTH; i++) { + for (let j = 1; j < 256; j++) { + actual[i] ^= u8(j) + if (i === 0) actual[i] &= 0xec + xpass_ge_is_canonical(v, actual, 124) + actual[i] = expected[i] + } + // verify |= was not replaced by ^= or += due to a typo // invisible to single byte checks, caught only by comparing byte pairs for (let j = i + 1; j < KEY_BYTELENGTH; j++) { actual[i] ^= 128 actual[j] ^= 128 - xfail_ge_is_canonical_pair(v, i, j, actual) + if (i === 0) actual[i] &= 0xec + xpass_ge_is_canonical(v, actual, 134) actual[i] = expected[i] actual[j] = expected[j] } diff --git a/src/lib/errors.ts b/src/lib/errors.ts index 0723dca..d62f91a 100644 --- a/src/lib/errors.ts +++ b/src/lib/errors.ts @@ -17,8 +17,8 @@ Object.defineProperty(Nano25519WasmError, 'name', { value: 'Nano25519WasmError' * so the two cannot drift silently. * * The table is keyed by number rather than read from the module because the - * handler has to exist *before* instantiation: the `equalbytes` guard runs in - * the start function, so it can abort while `exports` is still undefined. + * handler has to exist *before* instantiation: the guards run in the start + * function so that they can abort while `exports` is still undefined. */ export const errorCodes: Record = { ERROR_UNKNOWN: 0, @@ -27,9 +27,12 @@ export const errorCodes: Record = { ERROR_INVALID_BLOCK_COUNT: 3, ERROR_INDEX_OUT_OF_RANGE: 4, ERROR_BYTE_OUT_OF_RANGE: 5, - ERROR_SELFTEST_XPASS: 6, - ERROR_SELFTEST_XFAIL_ONE: 7, - ERROR_SELFTEST_XFAIL_PAIR: 8, + ERROR_EQUALBYTES_SELFTEST_XPASS: 6, + ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE: 7, + ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR: 8, + ERROR_GE_IS_CANONICAL_SELFTEST_XPASS: 9, + ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL: 10, + ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL_PAIR: 11 } const VECTORS = ['zeros', 'ones', 'spread'] @@ -50,24 +53,24 @@ function vector (index: number, length: number): number[] { return bytes } -function selftest (code: number, v: number, detail: number, length: number): string { +function selftest (code: number, line: number, v: number, detail: number, length: number): string { const name = VECTORS[v] ?? `vector ${v}` const expected = vector(v, length) const actual = vector(v, length) - if (code === errorCodes.ERROR_SELFTEST_XFAIL_ONE) { + if (code === errorCodes.ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE || code === errorCodes.ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL) { const position = (detail >> 8) & 0xff const mask = detail & 0xff actual[position] ^= mask - return `equalbytes build error from "${name}": expected fail [${expected}]; actual pass [${actual}] (byte ${position} ^ 0x${mask.toString(16).padStart(2, '0')})` + return `line ${line}: code ${code} build error from "${name}": expected fail [${expected}]; actual pass [${actual}] (byte ${position} ^ 0x${mask.toString(16).padStart(2, '0')})` } - if (code === errorCodes.ERROR_SELFTEST_XFAIL_PAIR) { + if (code === errorCodes.ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR || code === errorCodes.ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL_PAIR) { const i = (detail >> 8) & 0xff const j = detail & 0xff actual[i] ^= 0x80 actual[j] ^= 0x80 - return `equalbytes build error from "${name}": expected fail [${expected}]; actual pass [${actual}] (bytes ${i} and ${j} ^ 0x80)` + return `line ${line}: code ${code} build error from "${name}": expected fail [${expected}]; actual pass [${actual}] (bytes ${i} and ${j} ^ 0x80)` } - return `equalbytes build error from "${name}": expected pass [${expected}]; actual fail` + return `line ${line}: code ${code} build error from "${name}": expected pass [${expected}]; actual fail` } /** @@ -88,10 +91,13 @@ function describe (code: number, line: number, detail: number, context: number): return `Buffer index out of range for ${BUFFERS[detail] ?? `buffer ${detail}`}: ${context}` case errorCodes.ERROR_BYTE_OUT_OF_RANGE: return `Byte value out of range for ${BUFFERS[detail] ?? `buffer ${detail}`}: ${context} is not in [0, 255]` - case errorCodes.ERROR_SELFTEST_XPASS: - case errorCodes.ERROR_SELFTEST_XFAIL_ONE: - case errorCodes.ERROR_SELFTEST_XFAIL_PAIR: - return selftest(code, detail, context, 32) + case errorCodes.ERROR_EQUALBYTES_SELFTEST_XPASS: + case errorCodes.ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE: + case errorCodes.ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR: + case errorCodes.ERROR_GE_IS_CANONICAL_SELFTEST_XPASS: + case errorCodes.ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL: + case errorCodes.ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL_PAIR: + return selftest(code, line, detail, context, 32) default: return `Unrecognized wasm abort (code ${code}, a ${detail}, b ${context}), row ${line}` } -- 2.52.0