From: Chris Duncan Date: Tue, 14 Apr 2026 06:38:49 +0000 (-0700) Subject: Extract test sequence into its own function and remove useless debug option. X-Git-Tag: v1.0.2~6 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=481928fc763c17777cb3b8a92e3782d94de99ea9;p=nano25519.git Extract test sequence into its own function and remove useless debug option. --- diff --git a/index.html b/index.html index bbbc268..b23fe11 100644 --- a/index.html +++ b/index.html @@ -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}
` 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
` - if (isDebug) document.getElementById('summary').innerHTML += `${JSON.stringify(average(times, api), null, '\t')}
` } 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 - - - -

LOADING