Skip to content

Commit

Permalink
fix example demo
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhuachuang committed Oct 16, 2020
1 parent 91f95ea commit df0e559
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ blake2s_simd = "0.5"
num-traits = { version = "0.2", default-features = false }
hex-literal = "0.2"
sha2 = "0.9"
postcard = { version = "0.5", default-features = false, features = ["alloc"] }

[profile.test]
opt-level = 3
32 changes: 16 additions & 16 deletions examples/mini.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use ckb_zkp::{
bn_256::{Bn_256, Fr},
groth16::{
create_random_proof, generate_random_parameters, prove_to_bytes,
verifier::prepare_verifying_key, verify_from_bytes, verify_proof,
create_random_proof, generate_random_parameters, verifier::prepare_verifying_key,
verify_proof, Parameters, Proof, VerifyingKey,
},
math::{PrimeField, ToBytes},
math::PrimeField,
r1cs::{ConstraintSynthesizer, ConstraintSystem, SynthesisError},
};
use rand::prelude::*;
Expand Down Expand Up @@ -58,15 +58,13 @@ fn main() {
};
let params = generate_random_parameters::<Bn_256, _, _>(c, &mut rng).unwrap();

// you need save this prove key,
// when prove, use it as a params.
let mut pk_bytes = vec![];
params.write(&mut pk_bytes).unwrap();

// you need save this verify key,
// when verify, use it as a params.
let mut vk_bytes = vec![];
params.vk.write(&mut vk_bytes).unwrap();
let vk_bytes = postcard::to_allocvec(&params.vk).unwrap();

// you need save this prove key,
// when prove, use it as a params.
let params_bytes = postcard::to_allocvec(&params).unwrap();

// Prepare the verification key (for proof verification)
let pvk = prepare_verifying_key(&params.vk);
Expand Down Expand Up @@ -94,18 +92,20 @@ fn main() {
let v_time = v_start.elapsed();
println!("GROTH16 VERIFY TIME: {:?}", v_time);

println!("GROTH16 START PROVE & VERIFY WITH BYTES...");
println!("Test serialize & verify...");
let circuit = Mini {
x: Some(x),
y: Some(y),
z: Some(z),
num: 10,
};

let (proof_bytes, publics_bytes) =
prove_to_bytes(&params, circuit, &mut rng, &[Fr::from(10u32)]).unwrap();

assert!(verify_from_bytes::<Bn_256>(&vk_bytes, &proof_bytes, &publics_bytes).unwrap());
let params2: Parameters<Bn_256> = postcard::from_bytes(&params_bytes).unwrap();
let vk2: VerifyingKey<Bn_256> = postcard::from_bytes(&vk_bytes).unwrap();
let pvk2 = prepare_verifying_key(&vk2);
let proof = create_random_proof(&params2, circuit, &mut rng).unwrap();
let proof_bytes = postcard::to_allocvec(&proof).unwrap();
let proof2: Proof<Bn_256> = postcard::from_bytes(&proof_bytes).unwrap();
assert!(verify_proof(&pvk2, &proof2, &[Fr::from(10u32)]).unwrap());

println!("all is ok");
}
2 changes: 1 addition & 1 deletion math/src/curves/models/bls12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub use self::{
g2::{G2Affine, G2Prepared, G2Projective},
};

#[derive(Derivative)]
#[derive(Derivative, Serialize, Deserialize)]
#[derivative(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct Bls12<P: Bls12Parameters>(PhantomData<fn() -> P>);

Expand Down
2 changes: 1 addition & 1 deletion math/src/curves/models/bn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub use self::{
g2::{G2Affine, G2Prepared, G2Projective},
};

#[derive(Derivative)]
#[derive(Derivative, Serialize, Deserialize)]
#[derivative(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct Bn<P: BnParameters>(PhantomData<P>);

Expand Down
2 changes: 1 addition & 1 deletion math/src/curves/models/mnt4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub trait MNT4Parameters: 'static {
>;
}

#[derive(Derivative)]
#[derive(Derivative, Serialize, Deserialize)]
#[derivative(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct MNT4<P: MNT4Parameters>(PhantomData<fn() -> P>);

Expand Down
2 changes: 1 addition & 1 deletion math/src/curves/models/mnt6/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub trait MNT6Parameters: 'static {
>;
}

#[derive(Derivative)]
#[derive(Derivative, Serialize, Deserialize)]
#[derivative(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct MNT6<P: MNT6Parameters>(PhantomData<fn() -> P>);

Expand Down

0 comments on commit df0e559

Please sign in to comment.