]> git.codecow.com Git - nano25519.git/commitdiff
Add custom message byte length to speed test page.
authorChris Duncan <chris@codecow.com>
Fri, 25 Sep 2026 16:48:13 +0000 (09:48 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 25 Sep 2026 16:48:13 +0000 (09:48 -0700)
test/index.html

index 9dafca9309c617f5c96a33d86eb3174e5e5418d1..3ab659ea70289df5361f9486db88be5d135c3e2c 100644 (file)
@@ -49,6 +49,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                if (nano25519 && NANO_ORG_VECTOR && PYTHON_ED25519_BLAKE2B_VECTORS) {
                        console.info('Test vectors loaded')
                        document.getElementById('btnStartTest').disabled = false
+                       document.getElementById('btnStartTest').focus()
                        document.getElementById('status').innerHTML = `READY`
                }
 
@@ -114,9 +115,11 @@ SPDX-License-Identifier: GPL-3.0-or-later
                }
 
                /** @type { { bytes: Uint8Array, hex: string } } */
-               const rand32 = {
-                       get bytes() { return crypto.getRandomValues(new Uint8Array(32)) },
-                       get hex() { return b2a(this.bytes) }
+               const rand = (length = 32) => {
+                       return {
+                               get bytes() { return crypto.getRandomValues(new Uint8Array(length)) },
+                               get hex() { return b2a(this.bytes) }
+                       }
                }
 
                /** @type { (hex: string) => Uint8Array } */
@@ -223,7 +226,13 @@ SPDX-License-Identifier: GPL-3.0-or-later
                 * @param {number} runs
                 * @returns {void}
                 */
-               export async function test(api, size, runs) {
+               export async function test(api, bytes, size, runs) {
+                       if (typeof bytes !== 'number' || bytes < 1) {
+                               bytes = 1
+                       }
+                       if (bytes > 65536) {
+                               bytes = 65536
+                       }
                        if (typeof size !== 'number' || size < 1) {
                                size = 1
                        }
@@ -351,7 +360,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                        passes += +test
                        failures += +!test
 
-                       result = nano25519.verify(NANO_ORG_VECTOR.signature, rand32.hex, NANO_ORG_VECTOR.publicKey)
+                       result = nano25519.verify(NANO_ORG_VECTOR.signature, rand(32).hex, NANO_ORG_VECTOR.publicKey)
                        console.log(result)
                        test = result === false
                        check(`verify() output for random block hash`, test)
@@ -408,11 +417,11 @@ SPDX-License-Identifier: GPL-3.0-or-later
 
                                        try {
                                                if (api === 'nano25519 with bulk block verification') {
-                                                       privateKey = rand32.bytes
+                                                       privateKey = rand(32).bytes
                                                        publicKey = derive(privateKey, 'nano25519')
                                                        const blocks = []
                                                        for (let b = 0; b < nano25519.constants.MAX_VERIFY_BLOCKS; b++) {
-                                                               message = rand32.bytes
+                                                               message = rand(32).bytes
                                                                signature = sign(message, privateKey, publicKey, 'nano25519')
                                                                blocks[b] = { hash: message, signature }
                                                        }
@@ -429,8 +438,8 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                                        signTimes[i].push(0)
                                                        verifyTimes[i].push(duration)
                                                } else {
-                                                       privateKey = rand32.bytes
-                                                       message = crypto.getRandomValues(new Uint8Array(32768))
+                                                       privateKey = rand(32).bytes
+                                                       message = rand(bytes).bytes
 
                                                        start = performance.now()
                                                        publicKey = derive(privateKey, api)
@@ -484,13 +493,17 @@ SPDX-License-Identifier: GPL-3.0-or-later
                }
 
                function startValidation(event) {
+                       const bytes = document.getElementById('bytes')
                        const size = document.getElementById('size')
                        const api = document.getElementById('api')?.value
                        if (api === 'nano25519 with bulk block verification') {
+                               bytes.value = nano25519.constants.BLOCKHASH_BYTELENGTH
+                               bytes.disabled = true
                                size.value = nano25519.constants.MAX_VERIFY_BLOCKS
                                size.dataset.value = 1
                                size.disabled = true
                        } else {
+                               bytes.disabled = false
                                size.value = 100
                                size.dataset.value = 0
                                size.disabled = false
@@ -517,9 +530,10 @@ SPDX-License-Identifier: GPL-3.0-or-later
                function startTest(event) {
                        event.target.disabled = true
                        const api = document.getElementById('api')
+                       const bytes = document.getElementById('bytes')
                        const size = document.getElementById('size')
                        const runs = document.getElementById('runs')
-                       test(api.value, +size.dataset.value ? 1 : +size.value, +runs.value)
+                       test(api.value, +bytes.value, +size.dataset.value ? 1 : +size.value, +runs.value)
                                .then(() => {
                                        event.target.disabled = false
                                })
@@ -579,10 +593,12 @@ SPDX-License-Identifier: GPL-3.0-or-later
                        <option>TweetNaCl.js</option>
                </select>
        </span>
+       <label for="bytes">Message Bytes</label>
+       <input id="bytes" type="number" value="32" min="1" max="65536" />
        <label for="runs">Test Runs</label>
-       <input id="runs" type="number" value="100" min="1" autofocus />
+       <input id="runs" type="number" value="100" min="1" />
        <label for="size">Test Size</label>
-       <input id="size" type="number" value="100" min="1" autofocus />
+       <input id="size" type="number" value="100" min="1" />
        <button id="btnStartTest" disabled>Go</button>
        <hr />
        <h3 id="status">LOADING</h3>