|
| 1 | +import { Bench } from 'tinybench'; |
| 2 | +import rnqc from 'react-native-quick-crypto'; |
| 3 | +import { ed25519 as noble } from '@noble/curves/ed25519'; |
| 4 | +import type { BenchFn } from '../../types/benchmarks'; |
| 5 | + |
| 6 | +const TIME_MS = 1000; |
| 7 | + |
| 8 | +const ed25519_sign_verify_async: BenchFn = async () => { |
| 9 | + const message = 'hello world'; |
| 10 | + const buffer = Buffer.from(message); |
| 11 | + const ab = buffer.buffer; |
| 12 | + const arr = new Uint8Array(buffer); |
| 13 | + |
| 14 | + // rnqc setup |
| 15 | + const ed = new rnqc.Ed('ed25519', {}); |
| 16 | + await ed.generateKeyPair(); |
| 17 | + |
| 18 | + // noble setup |
| 19 | + const noblePrivateKey = noble.utils.randomPrivateKey(); |
| 20 | + const noblePublicKey = noble.getPublicKey(noblePrivateKey); |
| 21 | + |
| 22 | + const bench = new Bench({ |
| 23 | + name: 'ed25519 sign/verify (async)', |
| 24 | + time: TIME_MS, |
| 25 | + }); |
| 26 | + |
| 27 | + bench.add('rnqc', async () => { |
| 28 | + const signature = await ed.sign(ab); |
| 29 | + const verified = await ed.verify(signature, ab); |
| 30 | + if (!verified) { |
| 31 | + throw new Error('Signature verification failed'); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + bench.add('@noble/curves/ed25519', () => { |
| 36 | + const signature = noble.sign(arr, noblePrivateKey); |
| 37 | + const verified = noble.verify(signature, arr, noblePublicKey); |
| 38 | + if (!verified) { |
| 39 | + throw new Error('Signature verification failed'); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + bench.warmupTime = 100; |
| 44 | + return bench; |
| 45 | +}; |
| 46 | + |
| 47 | +const ed25519_sign_verify_sync: BenchFn = () => { |
| 48 | + const message = 'hello world'; |
| 49 | + const buffer = Buffer.from(message); |
| 50 | + const ab = buffer.buffer; |
| 51 | + const arr = new Uint8Array(buffer); |
| 52 | + |
| 53 | + // rnqc setup |
| 54 | + const ed = new rnqc.Ed('ed25519', {}); |
| 55 | + ed.generateKeyPairSync(); |
| 56 | + |
| 57 | + // noble setup |
| 58 | + const noblePrivateKey = noble.utils.randomPrivateKey(); |
| 59 | + const noblePublicKey = noble.getPublicKey(noblePrivateKey); |
| 60 | + |
| 61 | + const bench = new Bench({ |
| 62 | + name: 'ed25519 sign/verify (sync)', |
| 63 | + time: TIME_MS, |
| 64 | + }); |
| 65 | + |
| 66 | + bench.add('rnqc', () => { |
| 67 | + const signature = ed.signSync(ab); |
| 68 | + const verified = ed.verifySync(signature, ab); |
| 69 | + if (!verified) { |
| 70 | + throw new Error('Signature verification failed'); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + bench.add('@noble/curves/ed25519', () => { |
| 75 | + const signature = noble.sign(arr, noblePrivateKey); |
| 76 | + const verified = noble.verify(signature, arr, noblePublicKey); |
| 77 | + if (!verified) { |
| 78 | + throw new Error('Signature verification failed'); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + bench.warmupTime = 100; |
| 83 | + return bench; |
| 84 | +}; |
| 85 | + |
| 86 | +export default [ed25519_sign_verify_async, ed25519_sign_verify_sync]; |
0 commit comments