@@ -193,95 +193,74 @@ fn clamp_scalar(scalar: [u8; 32]) -> Scalar {
193
193
mod test {
194
194
use super :: * ;
195
195
196
- #[ test]
197
- fn sphf_both ( ) {
198
- // Our main function / examples to test
199
-
200
- println ! ( "Running routines, 1 for DH, else for fake (hash) CS" ) ;
201
-
202
- let mut guess = String :: new ( ) ;
203
- io:: stdin ( ) . read_line ( & mut guess)
204
- . expect ( "Failed to read line" ) ;
196
+ fn do_sphf_dh_test ( should_succeed : bool ) -> bool {
197
+ // Generates the base elements of the Diffie Hellman Language (2 points: g and h)
198
+ let base_gen=define_language ( ) ;
205
199
206
- let guess: u32 = match guess. trim ( ) . parse ( ) {
207
- Ok ( num) => num,
208
- Err ( _) => 1 ,
209
- } ;
200
+ // Generates the keys. hk : lambda, mu (two scalars), and hp : g^lambda . h^mu (a group elem)
201
+ let ( mut hk, hp) =proj_kg ( base_gen) ;
210
202
211
- if guess==1 {
212
- // Generates the base elements of the Diffie Hellman Language (2 points: g and h)
213
- let base_gen=define_language ( ) ;
203
+ // Generates a word in L, and it's witness word=(G,H) where G=g^wit, H=h^wit
204
+ let ( mut w, mut word) =generate_word ( base_gen) ;
214
205
215
- // Generates the keys. hk : lambda, mu (two scalars), and hp : g^lambda . h^mu (a group elem)
216
- let ( mut hk, hp) =proj_kg ( base_gen) ;
206
+ if ! should_succeed { // Derp the word if we need to fail
207
+ word. 1 =w* word. 1 ;
208
+ }
217
209
218
- println ! ( "Should we succeed? (0 no)" ) ;
210
+ // Prover computes his view of the hash G^lambda . H^mu
211
+ let ha=hash_hps ( hk, word) ;
212
+ hk. clear ( ) ;
213
+ // Verifier computes his view hp^wit
214
+ let hb=proj_hash ( hp, w) ;
215
+ w. clear ( ) ;
219
216
220
- let mut guess2 = String :: new ( ) ;
221
- io :: stdin ( ) . read_line ( & mut guess2 )
222
- . expect ( "Failed to read line" ) ;
217
+ // Checking if they have the same view
218
+ return verify_hps ( ha , hb ) ;
219
+ }
223
220
224
- let guess2 : u32 = match guess2 . trim ( ) . parse ( ) {
225
- Ok ( num ) => num ,
226
- Err ( _ ) => 1 ,
227
- } ;
221
+ # [ test ]
222
+ fn sphf_dh_success ( ) {
223
+ assert_eq ! ( do_sphf_dh_test ( true ) , true ) ;
224
+ }
228
225
226
+ #[ test]
227
+ fn sphf_dh_failure ( ) {
228
+ assert_eq ! ( do_sphf_dh_test( false ) , false ) ;
229
+ }
229
230
230
- // Generates a word in L, and it's witness word=(G,H) where G=g^wit, H=h^wit
231
- let ( mut w , mut word ) = generate_word ( base_gen ) ;
231
+ fn do_sphf_cs_test ( should_succeed : bool ) -> bool {
232
+ let base_gen= define_language_cs ( ) ;
232
233
233
- if guess2 == 0 { // Derp the word if we need to fail
234
- word. 1 =w* word. 1 ;
235
- }
234
+ // Generates the keys. hk : lambda, mu (two scalars), and hp : g^lambda . h^mu (a group elem)
235
+ let ( mut hk, hp) =proj_kg_cs ( base_gen) ;
236
236
237
- // Prover computes his view of the hash G^lambda . H^mu
238
- let ha=hash_hps ( hk, word) ;
239
- hk. clear ( ) ;
240
- // Verifier computes his view hp^wit
241
- let hb=proj_hash ( hp, w) ;
242
- w. clear ( ) ;
237
+ // Generates a word in L, and it's witness word=(G,H) where G=g^wit, H=h^wit
238
+ let ( mut w, mut word) =generate_word_cs ( base_gen) ;
243
239
244
- // Checking if they have the same view
245
- verify_hps ( ha , hb ) ;
240
+ if ! should_succeed { // Derp the word if we need to fail
241
+ word . 1 =w * word . 1 ;
246
242
}
247
- else {
248
- println ! ( "CS-like SPHF!" ) ;
249
- let base_gen=define_language_cs ( ) ;
250
-
251
- // Generates the keys. hk : lambda, mu (two scalars), and hp : g^lambda . h^mu (a group elem)
252
- let ( mut hk, hp) =proj_kg_cs ( base_gen) ;
253
243
254
- println ! ( "Should we succeed? (0 no)" ) ;
244
+ // Prover computes his view of the hash G^lambda . H^mu
245
+ let ha=hash_hps_cs ( hk, word) ;
246
+ hk. clear ( ) ;
255
247
256
- let mut guess2 = String :: new ( ) ;
257
- io:: stdin ( ) . read_line ( & mut guess2)
258
- . expect ( "Failed to read line" ) ;
248
+ // Verifier computes his view hp^wit
249
+ let res = hash_to_scal ( word. 1 ) ;
250
+ let hb=proj_hash_cs ( hp, w, res) ;
251
+ w. clear ( ) ;
259
252
260
- let guess2: u32 = match guess2. trim ( ) . parse ( ) {
261
- Ok ( num) => num,
262
- Err ( _) => 1 ,
263
- } ;
264
-
265
-
266
- // Generates a word in L, and it's witness word=(G,H) where G=g^wit, H=h^wit
267
- let ( mut w, mut word) =generate_word_cs ( base_gen) ;
268
-
269
- if guess2 == 0 { // Derp the word if we need to fail
270
- word. 1 =w* word. 1 ;
271
- }
272
-
273
- // Prover computes his view of the hash G^lambda . H^mu
274
- let ha=hash_hps_cs ( hk, word) ;
275
- hk. clear ( ) ;
276
-
277
- // Verifier computes his view hp^wit
278
- let res = hash_to_scal ( word. 1 ) ;
279
- let hb=proj_hash_cs ( hp, w, res) ;
280
- w. clear ( ) ;
253
+ // Checking if they have the same view
254
+ return verify_hps ( ha, hb) ;
255
+ }
281
256
282
- // Checking if they have the same view
283
- verify_hps ( ha, hb) ;
257
+ #[ test]
258
+ fn sphf_cs_success ( ) {
259
+ assert_eq ! ( do_sphf_cs_test( true ) , true ) ;
260
+ }
284
261
285
- }
262
+ #[ test]
263
+ fn sphf_cs_failure ( ) {
264
+ assert_eq ! ( do_sphf_cs_test( false ) , false ) ;
286
265
}
287
266
}
0 commit comments