You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.4 KiB

3 years ago
3 years ago
  1. use ark_circom::{CircomBuilder, CircomConfig};
  2. use ark_std::rand::thread_rng;
  3. use color_eyre::Result;
  4. use ark_bn254::Bn254;
  5. use ark_groth16::{
  6. create_random_proof as prove, generate_random_parameters, prepare_verifying_key, verify_proof,
  7. };
  8. #[test]
  9. fn groth16_proof() -> Result<()> {
  10. let cfg = CircomConfig::<Bn254>::new(
  11. "./test-vectors/mycircuit.wasm",
  12. "./test-vectors/mycircuit.r1cs",
  13. )?;
  14. let mut builder = CircomBuilder::new(cfg);
  15. builder.push_input("a", 3);
  16. builder.push_input("b", 11);
  17. // create an empty instance for setting it up
  18. let circom = builder.setup();
  19. let mut rng = thread_rng();
  20. let params = generate_random_parameters::<Bn254, _, _>(circom, &mut rng)?;
  21. let circom = builder.build()?;
  22. let inputs = circom.get_public_inputs().unwrap();
  23. let proof = prove(circom, &params, &mut rng)?;
  24. let pvk = prepare_verifying_key(&params.vk);
  25. let verified = verify_proof(&pvk, &proof, &inputs)?;
  26. assert!(verified);
  27. Ok(())
  28. }
  29. #[test]
  30. fn groth16_proof_wrong_input() {
  31. let cfg = CircomConfig::<Bn254>::new(
  32. "./test-vectors/mycircuit.wasm",
  33. "./test-vectors/mycircuit.r1cs",
  34. )
  35. .unwrap();
  36. let mut builder = CircomBuilder::new(cfg);
  37. builder.push_input("a", 3);
  38. // This isn't a public input to the circuit, should faild
  39. builder.push_input("foo", 11);
  40. // create an empty instance for setting it up
  41. let circom = builder.setup();
  42. let mut rng = thread_rng();
  43. let _params = generate_random_parameters::<Bn254, _, _>(circom, &mut rng).unwrap();
  44. builder.build().unwrap_err();
  45. }
  46. #[test]
  47. #[cfg(feature = "circom-2")]
  48. fn groth16_proof_circom2() -> Result<()> {
  49. let cfg = CircomConfig::<Bn254>::new(
  50. "./test-vectors/circom2_multiplier2.wasm",
  51. "./test-vectors/circom2_multiplier2.r1cs",
  52. )?;
  53. let mut builder = CircomBuilder::new(cfg);
  54. builder.push_input("a", 3);
  55. builder.push_input("b", 11);
  56. // create an empty instance for setting it up
  57. let circom = builder.setup();
  58. let mut rng = thread_rng();
  59. let params = generate_random_parameters::<Bn254, _, _>(circom, &mut rng)?;
  60. let circom = builder.build()?;
  61. let inputs = circom.get_public_inputs().unwrap();
  62. let proof = prove(circom, &params, &mut rng)?;
  63. let pvk = prepare_verifying_key(&params.vk);
  64. let verified = verify_proof(&pvk, &proof, &inputs)?;
  65. assert!(verified);
  66. Ok(())
  67. }