diff --git a/src/ccs08.rs b/src/ccs08.rs index 9f8106c..5c87423 100644 --- a/src/ccs08.rs +++ b/src/ccs08.rs @@ -131,7 +131,11 @@ impl ParamsUL { let C = self.com.commit(rng, modx, Some(r)); // Fiat-Shamir heuristic - let c = hash::(proofStates.clone(), D.clone()); + let mut a = Vec::::with_capacity(self.l as usize); + for state in proofStates.clone() { + a.push(state.a); + } + let c = hash::(a, D.clone()); let mut zr = m.clone(); let mut rc = r.clone(); @@ -140,7 +144,7 @@ impl ParamsUL { for i in 0..self.l as usize { let mut dx = E::Fr::from_str(&decx[i].to_string()).unwrap(); - let proof = self.kp.prove_response(&proofStates[i].clone(), c, &mut vec!{dx}); + let proof = self.kp.prove_response(&proofStates[i].clone(), c, &mut vec! {dx}); sigProofs.push(proof); } @@ -153,9 +157,19 @@ impl ParamsUL { */ pub fn verify_ul(&self, proof: &ProofUL) -> bool { // D == C^c.h^ zr.g^zsig ? + let r = self.verify_challenge(&proof); let r1 = self.verify_part1(&proof); let r2 = self.verify_part2(&proof); - return r1 && r2; + r && r1 && r2 + } + + fn verify_challenge(&self, proof: &ProofUL) -> bool { + let mut a = Vec::::with_capacity(self.l as usize); + for sigProof in proof.sigProofs.clone() { + a.push(sigProof.a); + } + let c = hash::(a, proof.D.clone()); + proof.ch == c } fn verify_part2(&self, proof: &ProofUL) -> bool { @@ -185,27 +199,22 @@ impl ParamsUL { } D.add_assign(&aux); } - return D == proof.D; + D == proof.D } } -fn hash(a: Vec>, D: E::G2) -> E::Fr { +fn hash(a: Vec, D: E::G2) -> E::Fr { // create a Sha256 object let mut a_vec: Vec = Vec::new(); for a_el in a { - a_vec.extend(format!("{}", a_el.a).bytes()); + a_vec.extend(format!("{}", a_el).bytes()); } let mut x_vec: Vec = Vec::new(); x_vec.extend(format!("{}", D).bytes()); a_vec.extend(x_vec); - let sha2_digest = sha512::hash(a_vec.as_slice()); - let mut hash_buf: [u8; 64] = [0; 64]; - hash_buf.copy_from_slice(&sha2_digest[0..64]); - let hexresult = fmt_bytes_to_int(hash_buf); - let result = E::Fr::from_str(&hexresult); - return result.unwrap(); + util::hash_to_fr::(a_vec) } /* @@ -286,6 +295,7 @@ mod tests { use time::PreciseTime; use std::ops::Add; use core::mem; + use rand::rngs::ThreadRng; #[test] fn setup_ul_works() { @@ -454,7 +464,7 @@ mod tests { let rng = &mut rand::thread_rng(); let D = G2::rand(rng); let D2 = G2::rand(rng); - let params = setup(rng); + let params = setup::(rng); let kp = BlindKeyPair::generate(rng, ¶ms, 2); let m1 = Fr::rand(rng); let m2 = Fr::rand(rng); @@ -464,8 +474,8 @@ mod tests { let state2 = kp.prove_commitment(rng, ¶ms, &sig); let state3 = kp.prove_commitment(rng, ¶ms, &sig); let state4 = kp.prove_commitment(rng, ¶ms, &sig); - let a = vec! {state, state1, state2}; - let a2 = vec! {state3, state4}; + let a = vec! {state.a, state1.a, state2.a}; + let a2 = vec! {state3.a, state4.a}; assert_eq!(hash::(a.clone(), D.clone()).is_zero(), false); assert_ne!(hash::(a2.clone(), D.clone()), hash::(a.clone(), D.clone())); assert_ne!(hash::(a.clone(), D2.clone()), hash::(a.clone(), D.clone())); diff --git a/src/util.rs b/src/util.rs index 2b64973..c6343b5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -5,11 +5,7 @@ use ff::PrimeField; pub fn hash_g2_to_fr(x: &E::G2) -> E::Fr { let mut x_vec: Vec = Vec::new(); x_vec.extend(format!("{}", x).bytes()); - let sha2_digest = sha512::hash(x_vec.as_slice()); - - let mut hash_buf: [u8; 64] = [0; 64]; - hash_buf.copy_from_slice(&sha2_digest[0..64]); - return E::Fr::from_str(&fmt_bytes_to_int(hash_buf)).unwrap(); + hash_to_fr::(x_vec) } pub fn fmt_bytes_to_int(bytearray: [u8; 64]) -> String { @@ -23,6 +19,15 @@ pub fn fmt_bytes_to_int(bytearray: [u8; 64]) -> String { result.to_string() } +pub fn hash_to_fr(mut byteVec: Vec) -> E::Fr { + let sha2_digest = sha512::hash(byteVec.as_slice()); + let mut hash_buf: [u8; 64] = [0; 64]; + hash_buf.copy_from_slice(&sha2_digest[0..64]); + let hexresult = fmt_bytes_to_int(hash_buf); + let result = E::Fr::from_str(&hexresult); + return result.unwrap(); +} + #[cfg(test)] mod tests { @@ -35,11 +40,20 @@ mod tests { fn hash_g2_to_fr_works() { let mut two = G2::one(); two.double(); - print!("{}\n", hash_g2_to_fr::(&two)); assert_eq!(format!("{}", hash_g2_to_fr::(&two).into_repr()), "0x27cd26f702a777dbf782534ae6bf2ec4aa6cb4617c8366f10f59bef13beb8c56"); } + #[test] + fn hash_to_fr_works() { + let mut two = G2::one(); + two.double(); + let mut x_vec: Vec = Vec::new(); + x_vec.extend(format!("{}", two).bytes()); + assert_eq!(format!("{}", hash_to_fr::(x_vec).into_repr()), + "0x27cd26f702a777dbf782534ae6bf2ec4aa6cb4617c8366f10f59bef13beb8c56"); + } + #[test] fn fmt_byte_to_int_works() { assert_eq!(fmt_bytes_to_int([12, 235, 23, 123, 13, 43, 12, 235, 23, 123, 13, 43, 12, 235, 23, 123, 13, 43, 12, 235, 23, 123, 13, 43, 12, 235, 23, 123, 13, 43, 12, 235, 23, 123, 13, 43, 12, 235, 23, 123, 13, 43, 12, 235, 23, 123, 13, 43, 12, 235, 23, 123, 13, 43, 12, 235, 23, 123, 13, 43, 12, 235, 23, 123]),