* @returns 0 if `s < p`, else -1
*/
export function ge_is_canonical (s: StaticArray<u8>): 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)
}
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)
}
*/
//@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()
}
* - `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
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<u8>(MAX_MESSAGE_BYTELENGTH)
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.
* 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<u8>, actual: StaticArray<u8>): void {
+function xpass_equalbytes (vector: i32, expected: StaticArray<u8>, actual: StaticArray<u8>, 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<u8>, actual: StaticArray<u8>): void {
+function xfail_equalbytes_one (vector: i32, position: i32, mask: i32, expected: StaticArray<u8>, actual: StaticArray<u8>, 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<u8>, actual: StaticArray<u8>): void {
+function xfail_equalbytes_pair (vector: i32, i: i32, j: i32, expected: StaticArray<u8>, actual: StaticArray<u8>, 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)
}
}
}
}
-function xpass_ge_is_canonical (vector: i32, data: StaticArray<u8>): void {
- if (!ge_is_canonical(data)) {
- raise(ERROR_SELFTEST_XPASS, vector, 0)
+function xpass_ge_is_canonical (vector: i32, data: StaticArray<u8>, 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<u8>): 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<u8>, 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<u8>): 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<u8>, 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<usize>(actual), changetype<usize>(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<usize>(actual), changetype<usize>(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<usize>(actual), changetype<usize>(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<usize>(actual), changetype<usize>(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]
}
* 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<string, number> = {
ERROR_UNKNOWN: 0,
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']
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`
}
/**
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}`
}