From: Chris Duncan Date: Sun, 16 Aug 2026 09:44:07 +0000 (-0700) Subject: Adjust buffer offset tests and remove unused function. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=a57a0089c827d51d9151cea664ac67a3248bceed;p=nano25519.git Adjust buffer offset tests and remove unused function. --- diff --git a/test/node.mjs b/test/node.mjs index e5e9213..112fbd2 100644 --- a/test/node.mjs +++ b/test/node.mjs @@ -17,19 +17,8 @@ function check (name, test) { console.log(`${test ? '\x1b[32mPASS' : '\x1b[31mFAIL'}\x1b[0m: ${name}`) } -/** - * - * @param {Uint8Array} a - * @param {Uint8Array} b - */ -function overlaps (a, b) { - return a.buffer === b.buffer - && a.byteOffset === b.byteOffset - && a.byteLength === b.byteLength - && a.byteLength !== 0 -} - -let outbuf, length, result, test, passes = 0, failures = 0 +const bigBuf = new Uint8Array(96).fill(255) +let inbuf, outbuf, result, test, passes = 0, failures = 0 // Check string input validation try { @@ -341,30 +330,38 @@ passes += +test failures += +!test // Check sync imports with byte inputs and preallocated output buffer +inbuf = new Uint8Array(bigBuf.buffer.slice(), 0, 32) +inbuf.set(NANO_ORG_VECTOR.privateKeyBytes) outbuf = new Uint8Array(32) -derive(NANO_ORG_VECTOR.privateKeyBytes, outbuf) +derive(inbuf, outbuf) test &&= [...outbuf].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() === NANO_ORG_VECTOR.publicKey.toLowerCase() check(`derive from private key bytes ${NANO_ORG_VECTOR.privateKey} and allocate result to output buffer`, test) passes += +test failures += +!test +inbuf = new Uint8Array(bigBuf.buffer.slice(), 0, 64) +inbuf.set(NANO_ORG_VECTOR.secretKeyBytes) outbuf = new Uint8Array(64) -sign(NANO_ORG_VECTOR.blockHashBytes, NANO_ORG_VECTOR.secretKeyBytes, outbuf) +sign(NANO_ORG_VECTOR.blockHashBytes, inbuf, outbuf) test &&= [...outbuf].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() === NANO_ORG_VECTOR.signature.toLowerCase() check(`sign message bytes ${NANO_ORG_VECTOR.blockHash} and allocate result to output buffer`, test) passes += +test failures += +!test -outbuf = new Uint8Array(new ArrayBuffer(96), 0, 32) -derive(NANO_ORG_VECTOR.privateKeyBytes, outbuf) +inbuf = new Uint8Array(bigBuf.buffer.slice(), 1, 32) +inbuf.set(NANO_ORG_VECTOR.privateKeyBytes) +outbuf = new Uint8Array(bigBuf.buffer.slice(), 0, 32) +derive(inbuf, outbuf) test &&= [...outbuf].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() === NANO_ORG_VECTOR.publicKey.toLowerCase() test &&= [...new Uint8Array(outbuf.buffer)].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() !== NANO_ORG_VECTOR.publicKey.toLowerCase() check(`derive from private key bytes ${NANO_ORG_VECTOR.privateKey} and allocate result to output buffer`, test) passes += +test failures += +!test -outbuf = new Uint8Array(new ArrayBuffer(96), 0, 64) -sign(NANO_ORG_VECTOR.blockHashBytes, NANO_ORG_VECTOR.secretKeyBytes, outbuf) +inbuf = new Uint8Array(bigBuf.buffer.slice(), 1, 64) +inbuf.set(NANO_ORG_VECTOR.secretKeyBytes) +outbuf = new Uint8Array(bigBuf.buffer.slice(), 0, 64) +sign(NANO_ORG_VECTOR.blockHashBytes, inbuf, outbuf) test &&= [...outbuf].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() === NANO_ORG_VECTOR.signature.toLowerCase() test &&= [...new Uint8Array(outbuf.buffer)].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() !== NANO_ORG_VECTOR.publicKey.toLowerCase() check(`sign message bytes ${NANO_ORG_VECTOR.blockHash} and allocate result to output buffer`, test) @@ -372,8 +369,7 @@ passes += +test failures += +!test // Check sync imports with byte inputs and offset output buffer -length = 33 + crypto.getRandomValues(new Uint8Array(1))[0] -outbuf = new Uint8Array(new ArrayBuffer(length), length - 32, 32) +outbuf = new Uint8Array(new ArrayBuffer(96), 1, 32) derive(NANO_ORG_VECTOR.privateKeyBytes, outbuf) test &&= [...outbuf].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() === NANO_ORG_VECTOR.publicKey.toLowerCase() test &&= [...new Uint8Array(outbuf.buffer)].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() !== NANO_ORG_VECTOR.publicKey.toLowerCase() @@ -381,8 +377,7 @@ check(`derive from private key bytes ${NANO_ORG_VECTOR.privateKey} to offset out passes += +test failures += +!test -length = 65 + crypto.getRandomValues(new Uint8Array(1))[0] -outbuf = new Uint8Array(new ArrayBuffer(length), length - 64, 64) +outbuf = new Uint8Array(new ArrayBuffer(96), 1, 64) sign(NANO_ORG_VECTOR.blockHashBytes, NANO_ORG_VECTOR.secretKeyBytes, outbuf) test &&= [...outbuf].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() === NANO_ORG_VECTOR.signature.toLowerCase() test &&= [...new Uint8Array(outbuf.buffer)].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() !== NANO_ORG_VECTOR.publicKey.toLowerCase()