]> git.codecow.com Git - nano25519.git/commitdiff
Adjust buffer offset tests and remove unused function.
authorChris Duncan <chris@zoso.dev>
Sun, 16 Aug 2026 09:44:07 +0000 (02:44 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 16 Aug 2026 09:44:07 +0000 (02:44 -0700)
test/node.mjs

index e5e9213ceaf4d2ffeaf6c40f922b515f7dd09521..112fbd23ff41aa0e14e05ebff15bc3a4a3766af4 100644 (file)
@@ -17,19 +17,8 @@ function check (name, test) {
        console.log(`${test ? '\x1b[32mPASS' : '\x1b[31mFAIL'}\x1b[0m: ${name}`)
 }
 
-/**
- *
- * @param {Uint8Array<ArrayBuffer>} a
- * @param {Uint8Array<ArrayBuffer>} 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()