failures += +!test
// test both string inputs and byte inputs
-for (const { privateKey, publicKey, message, signature } of PYTHON_ED25519_BLAKE2B_VECTORS) {
- result = derive(privateKey)
- test = result.toLowerCase() === publicKey
- check(`derive from private key string ${privateKey}`, test)
- passes += +test
- failures += +!test
+// Six checks per vector is thousands of lines, so print each failure in full
+// and the set as one summary line. Every check still counts toward the totals.
+const vectorChecks = { passes: 0, failures: 0 }
- result = sign(message, privateKey, publicKey)
- test = result.toLowerCase() === signature.slice(0, 128)
- check(`sign message string length ${message.length >> 1} bytes`, test)
- passes += +test
- failures += +!test
+/**
+ * Like `check`, but prints only failures, with the vector, the expected value
+ * and the actual one. A throw counts as a failure rather than ending the run, so
+ * one bad vector cannot hide the rest.
+ * @param {number} v Vector index
+ * @param {string} name
+ * @param {() => unknown} run
+ * @param {unknown} expected
+ */
+function checkVector (v, name, run, expected) {
+ let actual
+ try {
+ actual = run()
+ } catch (err) {
+ actual = `threw ${typeof err === 'object' && err != null && 'message' in err ? err.message : err}`
+ }
+ const test = typeof actual === 'string' ? actual.toLowerCase() === expected : actual === expected
+ if (!test) {
+ check(`vector ${v}: ${name}\n expected ${expected}\n actual ${actual}`, false)
+ }
+ vectorChecks.passes += +test
+ vectorChecks.failures += +!test
+}
- result = verify(signature.slice(0, 128), message, publicKey)
- test = result === true
- check(`verify signature string ${signature.slice(0, 128)}`, test)
- passes += +test
- failures += +!test
+for (const [v, { privateKey, publicKey, message, signature }] of PYTHON_ED25519_BLAKE2B_VECTORS.entries()) {
+ const signatureHex = signature.slice(0, 128)
+ checkVector(v, `derive from private key string ${privateKey}`, () => derive(privateKey), publicKey)
+ checkVector(v, `sign message string length ${message.length >> 1} bytes`, () => sign(message, privateKey, publicKey), signatureHex)
+ checkVector(v, `verify signature string ${signatureHex}`, () => verify(signatureHex, message, publicKey), true)
const privateKeyBytes = new Uint8Array(privateKey.match(/.{2}/g)?.map(b => parseInt(b, 16)) ?? [])
const publicKeyBytes = new Uint8Array(publicKey.match(/.{2}/g)?.map(b => parseInt(b, 16)) ?? [])
const messageBytes = new Uint8Array(message.match(/.{2}/g)?.map(b => parseInt(b, 16)) ?? [])
const signatureBytes = new Uint8Array(signature.match(/.{2}/g)?.slice(0, 64).map(b => parseInt(b, 16)) ?? [])
- result = derive(privateKeyBytes)
- test = result.toLowerCase() === publicKey
- check(`derive from private key bytes ${privateKey}`, test)
- passes += +test
- failures += +!test
-
- result = sign(messageBytes, privateKeyBytes, publicKeyBytes)
- test = result.toLowerCase() === signature.slice(0, 128)
- check(`sign message bytes length ${messageBytes.byteLength}`, test)
- passes += +test
- failures += +!test
-
- result = verify(signatureBytes, messageBytes, publicKeyBytes)
- test = result === true
- check(`verify signature bytes ${signature.slice(0, 128)}`, test)
- passes += +test
- failures += +!test
+ checkVector(v, `derive from private key bytes ${privateKey}`, () => derive(privateKeyBytes), publicKey)
+ checkVector(v, `sign message bytes length ${messageBytes.byteLength}`, () => sign(messageBytes, privateKeyBytes, publicKeyBytes), signatureHex)
+ checkVector(v, `verify signature bytes ${signatureHex}`, () => verify(signatureBytes, messageBytes, publicKeyBytes), true)
}
+test = vectorChecks.failures === 0
+check(`${PYTHON_ED25519_BLAKE2B_VECTORS.length} python-ed25519-blake2b vectors, derive, sign and verify from strings and bytes: ${vectorChecks.passes} passed, ${vectorChecks.failures} failed`, test)
+passes += vectorChecks.passes
+failures += vectorChecks.failures
+
// Every message byte must reach the module. The vectors above stop at 1023
// bytes, so past that point nothing asserts that bytes are hashed at all, and a
// marshalling bug that drops them is invisible to a round trip: sign and verify