]> git.codecow.com Git - nano25519.git/commitdiff
Extract test sequence into its own function and remove useless debug option.
authorChris Duncan <chris@zoso.dev>
Tue, 14 Apr 2026 06:38:49 +0000 (23:38 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 14 Apr 2026 06:38:49 +0000 (23:38 -0700)
index.html

index bbbc26872158c6cba33ee4b463e32185591da0db..b23fe1152d442a32c8d03992a0eb35872e137a3e 100644 (file)
@@ -203,7 +203,31 @@ SPDX-License-Identifier: GPL-3.0-or-later
                        }
                }
 
-               export async function test (api, size, runs, isDebug) {
+               async function sequenceDeriveSignVerify (api, blockHash, privateKey) {
+                       let start = 0, end = 0, publicKey = null, signature = null, isValid = false
+                       if (api === 'nano25519 (async)') {
+                               start = performance.now()
+                               publicKey = await derive(privateKey, api)
+                               signature = await sign(blockHash, privateKey, publicKey, api)
+                               isValid = await verify(signature, blockHash, publicKey, api)
+                               end = performance.now()
+                       } else {
+                               start = performance.now()
+                               publicKey = derive(privateKey, api)
+                               signature = sign(blockHash, privateKey, publicKey, api)
+                               isValid = verify(signature, blockHash, publicKey, api)
+                               end = performance.now()
+                       }
+                       if (!isValid) {
+                               throw new Error(`invalid result\nblock hash: ${blockHash}\nsignature: ${signature}\npublic key: ${publicKey}`)
+                       }
+                       if (!start || !end || start > end) {
+                               throw new Error(`invalid timing\nstart: ${start}\nend: ${end}`)
+                       }
+                       return end - start
+               }
+
+               export async function test (api, size, runs) {
                        if (typeof size !== 'number' || size < 1) {
                                size = 1
                        }
@@ -359,7 +383,6 @@ SPDX-License-Identifier: GPL-3.0-or-later
                        // run tests
                        console.log(`%c${api}`, 'color:green', `Calculate truncated harmonic mean of the truncated arithmetic rate of signing random block hashes across ${runs} runs of ${size} samples.`)
                        const rates = []
-                       let start = 0, end = 0, publicKey = null, signature = null, isValid = false
                        for (let i = 0; i < runs; i++) {
                                await new Promise(r => setTimeout(r))
                                const times = []
@@ -368,32 +391,18 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                        const blockHash = random()
                                        const privateKey = blockHash
                                        try {
-                                               start = performance.now()
-                                               publicKey = await derive(privateKey, api)
-                                               signature = await sign(blockHash, privateKey, publicKey, api)
-                                               isValid = await verify(signature, blockHash, publicKey, api)
-                                               end = performance.now()
+                                               const duration = await sequenceDeriveSignVerify(api, blockHash, privateKey)
+                                               times.push(duration)
                                        } catch (err) {
                                                document.getElementById('output').innerHTML += `Error: ${err.message}<br/>`
                                                console.error(err)
                                                return
                                        }
-                                       if (!isValid) {
-                                               document.getElementById('status').innerHTML = `ERROR: invalid result\nblock hash: ${blockHash}\nsignature: ${signature}\npublic key: ${publicKey}`
-                                               return
-                                       }
-                                       if (!start || !end || start > end) {
-                                               document.getElementById('status').innerHTML = `ERROR: invalid timing\nstart: ${start}\nend: ${end}`
-                                               return
-                                       }
-                                       times.push(end - start)
                                }
                                const avg = average(times, api)
                                const result = Object.values(avg)[0]
                                const { truncatedRate } = result
                                rates.push(truncatedRate)
-                               if (isDebug) document.getElementById('output').innerHTML += `Benchmark ${i + 1} score: ${truncatedRate} ops<br/>`
-                               if (isDebug) document.getElementById('summary').innerHTML += `${JSON.stringify(average(times, api), null, '\t')}<br/>`
                        }
                        const results = Object.values(average(rates, api))[0]
                        const { truncatedHarmonic } = results
@@ -432,8 +441,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                        const api = document.getElementById('api')
                        const size = document.getElementById('size')
                        const runs = document.getElementById('runs')
-                       const isDebug = document.getElementById('isDebug')
-                       test(api.value, +size.value, +runs.value, isDebug.checked)
+                       test(api.value, +size.value, +runs.value)
                                .then(() => {
                                        event.target.disabled = false
                                })
@@ -479,10 +487,6 @@ SPDX-License-Identifier: GPL-3.0-or-later
        <input id="size" type="number" value="100" min="1" autofocus />
        <label for="runs">Test Runs</label>
        <input id="runs" type="number" value="100" min="1" autofocus />
-       <span>
-               <label for="isDebug">Debug?</label>
-               <input id="isDebug" type="checkbox" />
-       </span>
        <button id="btnStartTest" disabled>Go</button>
        <hr />
        <h3 id="status">LOADING</h3>