Skip to content

Commit 6fa3adf

Browse files
committed
Separate all tests so we run all of them.
1 parent 34b46a8 commit 6fa3adf

File tree

1 file changed

+52
-73
lines changed

1 file changed

+52
-73
lines changed

src/sphf.rs

Lines changed: 52 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -193,95 +193,74 @@ fn clamp_scalar(scalar: [u8; 32]) -> Scalar {
193193
mod test {
194194
use super::*;
195195

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();
205199

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);
210202

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);
214205

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+
}
217209

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();
219216

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+
}
223220

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+
}
228225

226+
#[test]
227+
fn sphf_dh_failure() {
228+
assert_eq!(do_sphf_dh_test(false), false);
229+
}
229230

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();
232233

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);
236236

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);
243239

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;
246242
}
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);
253243

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();
255247

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();
259252

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+
}
281256

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+
}
284261

285-
}
262+
#[test]
263+
fn sphf_cs_failure() {
264+
assert_eq!(do_sphf_cs_test(false), false);
286265
}
287266
}

0 commit comments

Comments
 (0)