diff --git a/tinysnark/src/lib.rs b/tinysnark/src/lib.rs index a8c6bbc..39d9fb9 100644 --- a/tinysnark/src/lib.rs +++ b/tinysnark/src/lib.rs @@ -50,8 +50,8 @@ mod tests { assert!(!cs.test(&[10.into()], &[6.into(), 2.into()])); let kp = Keypair::new(&cs); - let proof = Proof::new(&kp, &cs, &[10.into()], &[5.into(), 2.into()]); - assert!(proof.verify(&kp, &cs, &[10.into()])); + let proof = Proof::new(&kp, &[10.into()], &[5.into(), 2.into()]); + assert!(proof.verify(&kp, &[10.into()])); } { let mut cs = ConstraintSystem::new(0, 1); @@ -66,8 +66,8 @@ mod tests { assert!(!cs.test(&[], &[2.into()])); let kp = Keypair::new(&cs); - let proof = Proof::new(&kp, &cs, &[], &[1.into()]); - assert!(proof.verify(&kp, &cs, &[])); + let proof = Proof::new(&kp, &[], &[1.into()]); + assert!(proof.verify(&kp, &[])); } } diff --git a/tinysnark/src/r1cs.rs b/tinysnark/src/r1cs.rs index e589924..f7db5b4 100644 --- a/tinysnark/src/r1cs.rs +++ b/tinysnark/src/r1cs.rs @@ -75,13 +75,17 @@ impl ConstraintSystem { struct R1CSKeypair; pub struct Keypair { - kp: *mut R1CSKeypair + kp: *mut R1CSKeypair, + primary_size: usize, + aux_size: usize } impl Keypair { pub fn new(constraint_system: &ConstraintSystem) -> Keypair { Keypair { - kp: unsafe { tinysnark_gen_keypair(constraint_system.cs) } + kp: unsafe { tinysnark_gen_keypair(constraint_system.cs) }, + primary_size: constraint_system.primary_size, + aux_size: constraint_system.aux_size } } } @@ -105,22 +109,24 @@ pub struct Proof { } impl Proof { - pub fn new(keypair: &Keypair, constraint_system: &ConstraintSystem, primary: &[FieldT], aux: &[FieldT]) + pub fn new(keypair: &Keypair, primary: &[FieldT], aux: &[FieldT]) -> Proof { - assert_eq!(primary.len(), constraint_system.primary_size); - assert_eq!(aux.len(), constraint_system.aux_size); + assert_eq!(primary.len(), keypair.primary_size); + assert_eq!(aux.len(), keypair.aux_size); unsafe { Proof { - proof: tinysnark_gen_proof(keypair.kp, constraint_system.cs, primary.get_unchecked(0), aux.get_unchecked(0)) + proof: tinysnark_gen_proof(keypair.kp, primary.get_unchecked(0), aux.get_unchecked(0)) } } } - pub fn verify(&self, keypair: &Keypair, constraint_system: &ConstraintSystem, primary: &[FieldT]) -> bool { + pub fn verify(&self, keypair: &Keypair, primary: &[FieldT]) -> bool { + assert_eq!(primary.len(), keypair.primary_size); + unsafe { - tinysnark_verify_proof(self.proof, keypair.kp, constraint_system.cs, primary.get_unchecked(0)) + tinysnark_verify_proof(self.proof, keypair.kp, primary.get_unchecked(0)) } } } @@ -133,12 +139,10 @@ impl Drop for Proof { extern "C" { fn tinysnark_gen_proof(keypair: *mut R1CSKeypair, - cs: *mut R1ConstraintSystem, primary: *const FieldT, aux: *const FieldT) -> *mut R1CSProof; fn tinysnark_verify_proof(proof: *mut R1CSProof, keypair: *mut R1CSKeypair, - cs: *mut R1ConstraintSystem, primary: *const FieldT) -> bool; fn tinysnark_drop_proof(proof: *mut R1CSProof); } \ No newline at end of file diff --git a/tinysnark/tinysnark.cpp b/tinysnark/tinysnark.cpp index a25ba52..1313c68 100644 --- a/tinysnark/tinysnark.cpp +++ b/tinysnark/tinysnark.cpp @@ -18,12 +18,11 @@ struct tinysnark_linear_term { size_t index; }; -extern "C" void * tinysnark_gen_proof(void * kp, void * ics, FieldT* primary, FieldT* aux) { - r1cs_constraint_system* cs = static_cast*>(ics); +extern "C" void * tinysnark_gen_proof(void * kp, FieldT* primary, FieldT* aux) { r1cs_ppzksnark_keypair* keypair = static_cast*>(kp); - r1cs_primary_input primary_input(primary, primary+(cs->primary_input_size)); - r1cs_auxiliary_input aux_input(aux, aux+(cs->auxiliary_input_size)); + r1cs_primary_input primary_input(primary, primary+(keypair->pk.constraint_system.primary_input_size)); + r1cs_auxiliary_input aux_input(aux, aux+(keypair->pk.constraint_system.auxiliary_input_size)); auto proof = new r1cs_ppzksnark_proof( r1cs_ppzksnark_prover(keypair->pk, primary_input, aux_input) @@ -32,12 +31,11 @@ extern "C" void * tinysnark_gen_proof(void * kp, void * ics, FieldT* primary, Fi return static_cast(std::move(proof)); } -extern "C" bool tinysnark_verify_proof(void * iproof, void * kp, void * ics, FieldT* primary) { +extern "C" bool tinysnark_verify_proof(void * iproof, void * kp, FieldT* primary) { r1cs_ppzksnark_proof* proof = static_cast*>(iproof); - r1cs_constraint_system* cs = static_cast*>(ics); r1cs_ppzksnark_keypair* keypair = static_cast*>(kp); - r1cs_primary_input primary_input(primary, primary+(cs->primary_input_size)); + r1cs_primary_input primary_input(primary, primary+(keypair->pk.constraint_system.primary_input_size)); return r1cs_ppzksnark_verifier_strong_IC(keypair->vk, primary_input, *proof); }