11import { bench , do_not_optimize , boxplot , summary , run } from "mitata" ;
22import { randomBytes } from "crypto" ;
33import {
4- aesEncryptGCM as wasmAesEncryptGCM ,
5- aesDecryptGCM as wasmAesDecryptGCM ,
6- aesEncryptCTR as wasmAesEncryptCTR ,
7- aesDecryptCTR as wasmAesDecryptCTR ,
8- aesEncrypt as wasmAesEncrypt ,
9- aesDecrypt as wasmAesDecrypt ,
10- hmacSign as wasmHmacSign ,
11- sha256 as wasmSha256 ,
124 md5 as wasmMd5 ,
135 hkdf as wasmHkdf ,
14- derivePairingCodeKey as wasmDerivePairingCodeKey ,
156} from "../dist/index.js" ;
167import {
17- aesEncryptGCM as baileysAesEncryptGCM ,
18- aesDecryptGCM as baileysAesDecryptGCM ,
19- aesEncryptCTR as baileysAesEncryptCTR ,
20- aesDecryptCTR as baileysAesDecryptCTR ,
21- aesEncrypt as baileysAesEncrypt ,
22- aesDecrypt as baileysAesDecrypt ,
23- hmacSign as baileysHmacSign ,
24- sha256 as baileysSha256 ,
258 md5 as baileysMd5 ,
269 hkdf as baileysHkdf ,
27- derivePairingCodeKey as baileysDerivePairingCodeKey ,
2810} from "baileys/lib/Utils/crypto.js" ;
2911
3012// Test data
3113const plaintext = Buffer . from ( "Benchmark test data for crypto operations " . repeat ( 10 ) ) ;
3214const key = randomBytes ( 32 ) ;
33- const iv12 = randomBytes ( 12 ) ;
34- const iv16 = randomBytes ( 16 ) ;
35- const aad = randomBytes ( 16 ) ;
3615const salt = randomBytes ( 32 ) ;
3716
38- // Pre-encrypted data for decryption benchmarks
39- const gcmEncrypted = baileysAesEncryptGCM ( plaintext , key , iv12 , aad ) ;
40- const ctrEncrypted = baileysAesEncryptCTR ( plaintext , key , iv16 ) ;
41- const cbcEncrypted = baileysAesEncrypt ( plaintext , key ) ;
42-
4317boxplot ( ( ) => {
44- summary ( ( ) => {
45- bench ( "AES-GCM Encrypt Rust/WASM" , ( ) => {
46- const result = wasmAesEncryptGCM ( plaintext , key , iv12 , aad ) ;
47- do_not_optimize ( result ) ;
48- } ) ;
49-
50- bench ( "AES-GCM Encrypt Baileys (Node)" , ( ) => {
51- const result = baileysAesEncryptGCM ( plaintext , key , iv12 , aad ) ;
52- do_not_optimize ( result ) ;
53- } ) ;
54- } ) ;
55-
56- summary ( ( ) => {
57- bench ( "AES-GCM Decrypt Rust/WASM" , ( ) => {
58- const result = wasmAesDecryptGCM ( gcmEncrypted , key , iv12 , aad ) ;
59- do_not_optimize ( result ) ;
60- } ) ;
61-
62- bench ( "AES-GCM Decrypt Baileys (Node)" , ( ) => {
63- const result = baileysAesDecryptGCM ( gcmEncrypted , key , iv12 , aad ) ;
64- do_not_optimize ( result ) ;
65- } ) ;
66- } ) ;
67-
68- summary ( ( ) => {
69- bench ( "AES-CTR Encrypt Rust/WASM" , ( ) => {
70- const result = wasmAesEncryptCTR ( plaintext , key , iv16 ) ;
71- do_not_optimize ( result ) ;
72- } ) ;
73-
74- bench ( "AES-CTR Encrypt Baileys (Node)" , ( ) => {
75- const result = baileysAesEncryptCTR ( plaintext , key , iv16 ) ;
76- do_not_optimize ( result ) ;
77- } ) ;
78- } ) ;
79-
80- summary ( ( ) => {
81- bench ( "AES-CTR Decrypt Rust/WASM" , ( ) => {
82- const result = wasmAesDecryptCTR ( ctrEncrypted , key , iv16 ) ;
83- do_not_optimize ( result ) ;
84- } ) ;
85-
86- bench ( "AES-CTR Decrypt Baileys (Node)" , ( ) => {
87- const result = baileysAesDecryptCTR ( ctrEncrypted , key , iv16 ) ;
88- do_not_optimize ( result ) ;
89- } ) ;
90- } ) ;
91-
92- summary ( ( ) => {
93- bench ( "AES-CBC Encrypt (with random IV) Rust/WASM" , ( ) => {
94- const result = wasmAesEncrypt ( plaintext , key ) ;
95- do_not_optimize ( result ) ;
96- } ) ;
97-
98- bench ( "AES-CBC Encrypt (with random IV) Baileys (Node)" , ( ) => {
99- const result = baileysAesEncrypt ( plaintext , key ) ;
100- do_not_optimize ( result ) ;
101- } ) ;
102- } ) ;
103-
104- summary ( ( ) => {
105- bench ( "AES-CBC Decrypt Rust/WASM" , ( ) => {
106- const result = wasmAesDecrypt ( cbcEncrypted , key ) ;
107- do_not_optimize ( result ) ;
108- } ) ;
109-
110- bench ( "AES-CBC Decrypt Baileys (Node)" , ( ) => {
111- const result = baileysAesDecrypt ( cbcEncrypted , key ) ;
112- do_not_optimize ( result ) ;
113- } ) ;
114- } ) ;
115-
116- summary ( ( ) => {
117- bench ( "HMAC-SHA256 Rust/WASM" , ( ) => {
118- const result = wasmHmacSign ( plaintext , key , "sha256" ) ;
119- do_not_optimize ( result ) ;
120- } ) ;
121-
122- bench ( "HMAC-SHA256 Baileys (Node)" , ( ) => {
123- const result = baileysHmacSign ( plaintext , key , "sha256" ) ;
124- do_not_optimize ( result ) ;
125- } ) ;
126- } ) ;
127-
128- summary ( ( ) => {
129- bench ( "SHA256 Rust/WASM" , ( ) => {
130- const result = wasmSha256 ( plaintext ) ;
131- do_not_optimize ( result ) ;
132- } ) ;
133-
134- bench ( "SHA256 Baileys (Node)" , ( ) => {
135- const result = baileysSha256 ( plaintext ) ;
136- do_not_optimize ( result ) ;
137- } ) ;
138- } ) ;
139-
14018 summary ( ( ) => {
14119 bench ( "MD5 Rust/WASM" , ( ) => {
14220 const result = wasmMd5 ( plaintext ) ;
@@ -160,18 +38,6 @@ boxplot(() => {
16038 do_not_optimize ( result ) ;
16139 } ) ;
16240 } ) ;
163-
164- summary ( ( ) => {
165- bench ( "derivePairingCodeKey Rust/WASM" , ( ) => {
166- const result = wasmDerivePairingCodeKey ( "12345678" , salt ) ;
167- do_not_optimize ( result ) ;
168- } ) ;
169-
170- bench ( "derivePairingCodeKey Baileys (Node)" , async ( ) => {
171- const result = await baileysDerivePairingCodeKey ( "12345678" , salt ) ;
172- do_not_optimize ( result ) ;
173- } ) ;
174- } ) ;
17541} ) ;
17642
17743await run ( ) ;
0 commit comments