|
1 | 1 | import { Bench } from 'tinybench'; |
2 | 2 | import rnqc from 'react-native-quick-crypto'; |
| 3 | +import { xsalsa20 as nobleXSalsa20 } from '@noble/ciphers/salsa.js'; |
3 | 4 | import type { BenchFn } from '../../types/benchmarks'; |
4 | 5 |
|
5 | 6 | const TIME_MS = 1000; |
@@ -33,6 +34,24 @@ const xsalsa20_encrypt_decrypt: BenchFn = () => { |
33 | 34 | } |
34 | 35 | }); |
35 | 36 |
|
| 37 | + bench.add('@noble/ciphers/salsa', () => { |
| 38 | + // Encrypt |
| 39 | + const encrypted = nobleXSalsa20(key, nonce, data); |
| 40 | + if (encrypted.length !== data.length) { |
| 41 | + throw new Error('Encryption failed: output size mismatch'); |
| 42 | + } |
| 43 | + |
| 44 | + // Decrypt |
| 45 | + const decrypted = nobleXSalsa20(key, nonce, encrypted); |
| 46 | + |
| 47 | + // Verify |
| 48 | + for (let i = 0; i < data.length; i++) { |
| 49 | + if (data[i] !== decrypted[i]) { |
| 50 | + throw new Error(`Decryption verification failed at index ${i}`); |
| 51 | + } |
| 52 | + } |
| 53 | + }); |
| 54 | + |
36 | 55 | bench.warmupTime = 100; |
37 | 56 | return bench; |
38 | 57 | }; |
@@ -73,6 +92,30 @@ const xsalsa20_encrypt_decrypt_large: BenchFn = () => { |
73 | 92 | } |
74 | 93 | }); |
75 | 94 |
|
| 95 | + bench.add('@noble/ciphers/salsa', () => { |
| 96 | + // Encrypt |
| 97 | + const encrypted = nobleXSalsa20(key, nonce, data); |
| 98 | + if (encrypted.length !== data.length) { |
| 99 | + throw new Error('Encryption failed: output size mismatch'); |
| 100 | + } |
| 101 | + |
| 102 | + // Decrypt |
| 103 | + const decrypted = nobleXSalsa20(key, nonce, encrypted); |
| 104 | + |
| 105 | + // Verify (checking first and last 100 bytes for performance) |
| 106 | + for (let i = 0; i < 100; i++) { |
| 107 | + if (data[i] !== decrypted[i]) { |
| 108 | + throw new Error(`Decryption verification failed at start index ${i}`); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + for (let i = data.length - 100; i < data.length; i++) { |
| 113 | + if (data[i] !== decrypted[i]) { |
| 114 | + throw new Error(`Decryption verification failed at end index ${i}`); |
| 115 | + } |
| 116 | + } |
| 117 | + }); |
| 118 | + |
76 | 119 | bench.warmupTime = 100; |
77 | 120 | return bench; |
78 | 121 | }; |
|
0 commit comments