1
1
const shutter = require ( "../dist/shutter-crypto" ) ;
2
2
const ethers = require ( "ethers" ) ;
3
+ const fs = require ( "node:fs" ) ;
4
+ const util = require ( "node:util" ) ;
5
+ const process = require ( "node:process" ) ;
6
+ const Buffer = require ( "buffer" ) . Buffer ;
3
7
4
- var encrypted ;
5
8
describe ( "Test shutter crypto" , ( ) => {
6
9
test ( "init the wasm library" , async ( ) => {
7
10
try {
@@ -10,8 +13,10 @@ describe("Test shutter crypto", () => {
10
13
expect ( error ) . toBeNone ( ) ;
11
14
}
12
15
} ) ;
16
+ var encrypted ;
17
+ var msg ;
13
18
test ( "encrypt a message" , async ( ) => {
14
- const msg = ethers . getBytes ( Buffer . from ( "a message" ) ) ;
19
+ msg = ethers . getBytes ( Buffer . from ( "a message" ) ) ;
15
20
const hexString =
16
21
"0b4e86e0ed51ef774210d1c0fe0be6f1b4f0695d5d396b3b003547f752ac82e316375aa37b1739c9c8472b1b5ae09477565bf9d2c0d7db0c39576f4615d32703262d5854bfbac8a60eb6d227f397289e6e51f979b56476b7f7f32a45ede7a61f21d893a54ab6e65b283342adc41d53df5432569c6a8c2304921bce3ea148efb4" ;
17
22
const eonkey = Uint8Array . from ( Buffer . from ( hexString , "hex" ) ) ;
@@ -41,6 +46,7 @@ describe("Test shutter crypto", () => {
41
46
} catch ( error ) {
42
47
expect ( error ) . toBeNull ( ) ;
43
48
}
49
+ expect ( ethers . hexlify ( decrypted ) ) . toEqual ( ethers . hexlify ( msg ) ) ;
44
50
} ) ;
45
51
} ) ;
46
52
@@ -95,3 +101,120 @@ describe("Test known values (values obtained from 'rolling-shutter crypto encryp
95
101
expect ( decrypted ) . toEqual ( expected ) ;
96
102
} ) ;
97
103
} ) ;
104
+
105
+ function get_testdata ( ) {
106
+ const env_name = "SHUTTER_WASM_JSON_TESTDATA" ;
107
+ try {
108
+ var testdata = process . env [ env_name ] ;
109
+ if ( ! testdata ) {
110
+ testdata = "./shutter_testdata.json" ;
111
+ }
112
+ var json_tests = fs . readFileSync ( testdata , "utf-8" , ( err , data ) => {
113
+ if ( err ) {
114
+ console . log ( err ) ;
115
+ } else {
116
+ return data ;
117
+ }
118
+ } ) ;
119
+ } catch ( err ) {
120
+ console . error (
121
+ `Could not read the json test file at '${ testdata } '.
122
+ Run 'rolling-shutter crypto testdata /path/to/crypto_test.json'
123
+ and set/export ${ env_name } =/path/to/crypto_test.json`
124
+ ) ;
125
+ }
126
+ const test_cases = JSON . parse ( json_tests ) ;
127
+
128
+ const _encryption_tests = test_cases . filter ( ( tc ) => {
129
+ return tc [ "type" ] === "encryption" ;
130
+ } ) ;
131
+ const encryption_tests = _encryption_tests . map ( ( tc ) => {
132
+ return [
133
+ util . format ( "[%s] %s: %s" , tc . id , tc . name , tc . description ) ,
134
+ tc . test_data ,
135
+ ] ;
136
+ } ) ;
137
+ const _decryption_tests = test_cases . filter ( ( tc ) => {
138
+ return tc [ "type" ] === "decryption" ;
139
+ } ) ;
140
+ const decryption_tests = _decryption_tests . map ( ( tc ) => {
141
+ return [
142
+ util . format ( "[%s] %s: %s" , tc . id , tc . name , tc . description ) ,
143
+ tc . test_data ,
144
+ ] ;
145
+ } ) ;
146
+ const _verification_tests = test_cases . filter ( ( tc ) => {
147
+ return tc [ "type" ] === "verification" ;
148
+ } ) ;
149
+ const verification_tests = _verification_tests . map ( ( tc ) => {
150
+ return [
151
+ util . format ( "[%s] %s: %s" , tc . id , tc . name , tc . description ) ,
152
+ tc . test_data ,
153
+ ] ;
154
+ } ) ;
155
+ return [ encryption_tests , decryption_tests , verification_tests ] ;
156
+ }
157
+
158
+ describe ( "Run the json tests" , ( ) => {
159
+ var [ encryption_tests , decryption_tests , verification_tests ] = get_testdata ( ) ;
160
+ test . each ( encryption_tests ) ( "%s" , async ( name , test_data ) => {
161
+ const msg = ethers . getBytes ( Buffer . from ( test_data . message . slice ( 2 ) , "hex" ) ) ;
162
+ const eonkey = ethers . getBytes (
163
+ Buffer . from ( test_data . eon_public_key . slice ( 2 ) , "hex" )
164
+ ) ;
165
+ const epoch_id = ethers . getBytes (
166
+ Buffer . from ( test_data . epoch_id . slice ( 2 ) , "hex" )
167
+ ) ;
168
+ const sigma = ethers . getBytes ( Buffer . from ( test_data . sigma . slice ( 2 ) , "hex" ) ) ;
169
+ var enc_result ;
170
+ try {
171
+ enc_result = await shutter . encrypt ( msg , eonkey , epoch_id , sigma ) ;
172
+ } catch ( error ) {
173
+ expect ( error ) . toBeNull ( ) ;
174
+ }
175
+ enc_result = ethers . hexlify ( enc_result ) ;
176
+ expect ( enc_result ) . toEqual ( test_data . expected ) ;
177
+ } ) ;
178
+ test . each ( decryption_tests ) ( "%s" , async ( name , test_data ) => {
179
+ const cipher = ethers . getBytes (
180
+ Buffer . from ( test_data . cipher . slice ( 2 ) , "hex" )
181
+ ) ;
182
+ const decryption_key = ethers . getBytes (
183
+ Buffer . from ( test_data . epoch_secret_key . slice ( 2 ) , "hex" )
184
+ ) ;
185
+ var dec_result ;
186
+ try {
187
+ dec_result = await shutter . decrypt ( cipher , decryption_key ) ;
188
+ } catch ( error ) {
189
+ if ( test_data . expected != "0x" ) {
190
+ expect ( error ) . toBeNull ( ) ;
191
+ } else {
192
+ return ;
193
+ }
194
+ }
195
+ dec_result = ethers . hexlify ( dec_result ) ;
196
+ expect ( dec_result ) . toEqual ( test_data . expected ) ;
197
+ } ) ;
198
+ test . each ( verification_tests ) ( "%s" , async ( name , test_data ) => {
199
+ const epoch_secret = ethers . getBytes (
200
+ Buffer . from ( test_data . epoch_secret_key . slice ( 2 ) , "hex" )
201
+ ) ;
202
+ const eonkey = ethers . getBytes (
203
+ Buffer . from ( test_data . eon_public_key . slice ( 2 ) , "hex" )
204
+ ) ;
205
+ const epoch_id = ethers . getBytes (
206
+ Buffer . from ( test_data . epoch_id . slice ( 2 ) , "hex" )
207
+ ) ;
208
+ var verification ;
209
+ try {
210
+ verification = await shutter . verifyDecryptionKey (
211
+ epoch_secret ,
212
+ eonkey ,
213
+ epoch_id
214
+ ) ;
215
+ } catch ( error ) {
216
+ expect ( error ) . toBeNull ( ) ;
217
+ }
218
+ expect ( verification == test_data . expected ) ;
219
+ } ) ;
220
+ } ) ;
0 commit comments