From: Chris Duncan Date: Sun, 16 Aug 2026 10:08:30 +0000 (-0700) Subject: Fix offset buffer tests to correctly target offsets including 0. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=4932b4d4454bc9480e5b8e3b2b423ff10735641b;p=nano25519.git Fix offset buffer tests to correctly target offsets including 0. --- diff --git a/test/node.mjs b/test/node.mjs index 112fbd2..4858ed9 100644 --- a/test/node.mjs +++ b/test/node.mjs @@ -17,7 +17,7 @@ function check (name, test) { console.log(`${test ? '\x1b[32mPASS' : '\x1b[31mFAIL'}\x1b[0m: ${name}`) } -const bigBuf = new Uint8Array(96).fill(255) +const bigBuf = new Uint8Array(128).fill(255) let inbuf, outbuf, result, test, passes = 0, failures = 0 // Check string input validation @@ -353,7 +353,8 @@ 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() +test &&= [...new Uint8Array(outbuf.buffer, 0, 32)].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() === NANO_ORG_VECTOR.publicKey.toLowerCase() +test &&= [...new Uint8Array(outbuf.buffer, 32, 32)].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 @@ -363,24 +364,29 @@ 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() +test &&= [...new Uint8Array(outbuf.buffer, 0, 64)].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() === NANO_ORG_VECTOR.signature.toLowerCase() +test &&= [...new Uint8Array(outbuf.buffer, 64, 64)].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 // Check sync imports with byte inputs and offset output buffer -outbuf = new Uint8Array(new ArrayBuffer(96), 1, 32) +outbuf = new Uint8Array(new ArrayBuffer(128), 32, 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() +test &&= [...new Uint8Array(outbuf.buffer, 0, 32)].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() !== NANO_ORG_VECTOR.publicKey.toLowerCase() +test &&= [...new Uint8Array(outbuf.buffer, 32, 32)].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} to offset outbuf`, test) passes += +test failures += +!test -outbuf = new Uint8Array(new ArrayBuffer(96), 1, 64) +outbuf = new Uint8Array(new ArrayBuffer(128), 64, 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() +test &&= [...new Uint8Array(outbuf.buffer)].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() !== NANO_ORG_VECTOR.signature.toLowerCase() +test &&= [...new Uint8Array(outbuf.buffer, 0, 64)].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() !== NANO_ORG_VECTOR.signature.toLowerCase() +test &&= [...new Uint8Array(outbuf.buffer, 64, 64)].map(b => b.toString(16).padStart(2, '0')).join('').toLowerCase() === NANO_ORG_VECTOR.signature.toLowerCase() check(`sign message bytes ${NANO_ORG_VECTOR.blockHash} to offset outbuf`, test) passes += +test failures += +!test