simple.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. use bellman::gadgets::multipack;
  2. use bellman::groth16;
  3. use blake2s_simd::Params as Blake2sParams;
  4. use bls12_381::Bls12;
  5. use ff::Field;
  6. use group::{Curve, Group, GroupEncoding};
  7. mod simple_circuit;
  8. use simple_circuit::InputSpend;
  9. fn main() {
  10. use rand::rngs::OsRng;
  11. let ak = jubjub::SubgroupPoint::random(&mut OsRng);
  12. let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
  13. let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret + ak;
  14. let params = {
  15. let c = InputSpend {
  16. secret: None,
  17. ak: None,
  18. value: None,
  19. is_cool: None,
  20. path: None,
  21. };
  22. groth16::generate_random_parameters::<Bls12, _, _>(c, &mut OsRng).unwrap()
  23. };
  24. let pvk = groth16::prepare_verifying_key(&params.vk);
  25. let c = InputSpend {
  26. secret: Some(secret),
  27. ak: Some(ak),
  28. value: Some(110),
  29. is_cool: Some(true),
  30. path: Some(bls12_381::Scalar::one()),
  31. };
  32. let proof = groth16::create_random_proof(c, &params, &mut OsRng).unwrap();
  33. let mut public_input = [bls12_381::Scalar::zero(); 4];
  34. {
  35. let result = jubjub::ExtendedPoint::from(public);
  36. let affine = result.to_affine();
  37. //let (u, v) = (affine.get_u(), affine.get_v());
  38. let u = affine.get_u();
  39. let v = affine.get_v();
  40. public_input[0] = u;
  41. public_input[1] = v;
  42. }
  43. {
  44. const CRH_IVK_PERSONALIZATION: &[u8; 8] = b"Zcashivk";
  45. let preimage = [42; 80];
  46. let hash_result = {
  47. let mut hash = [0; 32];
  48. hash.copy_from_slice(
  49. Blake2sParams::new()
  50. .hash_length(32)
  51. .personal(CRH_IVK_PERSONALIZATION)
  52. .to_state()
  53. .update(&ak.to_bytes())
  54. .finalize()
  55. .as_bytes(),
  56. );
  57. hash
  58. };
  59. // Pack the hash as inputs for proof verification.
  60. let hash = multipack::bytes_to_bits_le(&hash_result);
  61. let hash = multipack::compute_multipacking(&hash);
  62. // There are 2 chunks for a blake hash
  63. assert_eq!(hash.len(), 2);
  64. public_input[2] = hash[0];
  65. public_input[3] = hash[1];
  66. }
  67. assert!(groth16::verify_proof(&pvk, &proof, &public_input).is_ok());
  68. }