-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathristretto255.benchmarks.min.js
1 lines (1 loc) · 4.44 KB
/
ristretto255.benchmarks.min.js
1
!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";Uint8Array.prototype.fill||(Uint8Array.prototype.fill=Array.prototype.fill);const e=performance.now(),t=100,n=[],r=[],a=[],o=[],s=ristretto255.unsafe.point.alloc();for(let e=0;e<t;e++){const t=ristretto255.unsafe.point.getRandom();n.push(t),r.push(ristretto255.unsafe.point.toBytes(t));const s=new Uint8Array(32);for(let t=0;t<32;t++)s[t]=e;a.push(s),o.push(ristretto255.scalar.getRandom())}function i(e){return e.reduce((function(e,t){return e+t}),0)/e.length}[{name:"High-level ristretto255 group operations",functions:[{name:"getRandom",description:"Generate a random group element",execute:()=>ristretto255.getRandom()},{name:"isValid",description:"Check if a value represents a valid element",execute:e=>ristretto255.isValid(r[e])},{name:"fromHash",description:"Hash to group: generate a group element from 64-element byte array, e.g. an output of SHA-512",execute:e=>ristretto255.fromHash(a[e])},{name:"add",description:"Add two group elements",execute:e=>ristretto255.add(r[e],r[(e+1)%t])},{name:"sub",description:"Subtract two group elements",execute:e=>ristretto255.sub(r[e],r[(e+1)%t])},{name:"scalarMultBase",description:"Multiply a generator of the group by a scalar",execute:e=>ristretto255.scalarMultBase(o[e])},{name:"scalarMult",description:"Multiply a group element by a scalar",execute:e=>ristretto255.scalarMult(o[e],r[e])}]},{name:"Scalar operations",functions:[{name:"scalar.getRandom",description:"Generate a random scalar",execute:()=>ristretto255.scalar.getRandom()},{name:"scalar.invert",description:"Invert a scalar",execute:e=>ristretto255.scalar.invert(o[e])},{name:"scalar.negate",description:"Negate a scalar",execute:e=>ristretto255.scalar.negate(o[e],o[e%t])},{name:"scalar.add",description:"Add two scalars",execute:e=>ristretto255.scalar.add(o[e],o[e%t])},{name:"scalar.sub",description:"Subtract two scalars",execute:e=>ristretto255.scalar.sub(o[e],o[e%t])},{name:"scalar.mul",description:"Multiply two scalars",execute:e=>ristretto255.scalar.mul(o[e],o[e%t])}]},{name:"Low-level unsafe functions (unless if used by a cryptographer)",functions:[{name:"unsafe.point.toBytes",description:"Serialize a curve25519 point to ristretto255 group element",execute:e=>ristretto255.unsafe.point.toBytes(n[e])},{name:"unsafe.point.fromBytes",description:"Deserialize a curve25519 point from ristretto255 group element",execute:e=>ristretto255.unsafe.point.fromBytes(s,r[e])},{name:"unsafe.point.getRandom",description:"Generate a random ristretto255 group element represented as curve25519 point",execute:()=>ristretto255.unsafe.point.getRandom()},{name:"unsafe.point.fromHash",description:"Generate a ristretto255 group element represented as curve25519 point from a 64 elements byte array such as an output of SHA512",execute:e=>ristretto255.unsafe.point.fromHash(a[e])},{name:"unsafe.point.add",description:"Add two curve25519 points",execute:e=>ristretto255.unsafe.point.add(n[e],n[e%t])},{name:"unsafe.point.sub",description:"Subtract two curve25519 points",execute:e=>ristretto255.unsafe.point.sub(n[e],n[e%t])},{name:"unsafe.point.scalarMultBase",description:"Multiply a curve25519's base point by a scalar",execute:e=>ristretto255.unsafe.point.scalarMultBase(n[e],o[e])},{name:"unsafe.point.scalarMult",description:"Multiply a curve25519's point by a scalar",execute:e=>ristretto255.unsafe.point.scalarMult(n[e],n[e%t],o[e])}]}].forEach((e=>{const n=e.functions.map((e=>{const n=[];for(let r=0;r<t;r++){const t=performance.now();e.execute(r);const a=performance.now();n.push(a-t)}const r=i(n),a=function(e){const t=i(e),n=e.map((function(e){const n=e-t;return n*n}));return Math.sqrt(i(n))}(n);return{functionName:e.name,description:e.description,timing:`${r.toFixed(2)}<small font-size="smaller"> ± ${a.toFixed(2)}</small>`}}));document.getElementById("container").innerHTML+=((e,t)=>`\n<h2>${e}</h2>\n<div class="benchmarks">\n <table class="benchmarks">\n <tr>\n <th>Function name</th>\n <th>Time in ms</th>\n <th>Comments</th>\n </tr>\n ${t.map((e=>`\n <tr>\n <td>${e.functionName}</td>\n <td>${e.timing}</td>\n <td>${e.description}</td>\n </tr>\n `)).join("")}\n </table>\n</div>`)(e.name,n)}));const c=performance.now();document.getElementById("total_time").innerHTML=`Benchmark runtime: ${((c-e)/1e3).toFixed(2)} sec with 100 reps on each operation.`}));