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.

109 lines
2.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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, Fr};
  5. use ark_crypto_primitives::snark::SNARK;
  6. use ark_groth16::Groth16;
  7. type GrothBn = Groth16<Bn254>;
  8. #[tokio::test]
  9. async fn groth16_proof() -> Result<()> {
  10. let cfg = CircomConfig::<Fr>::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 = GrothBn::generate_random_parameters_with_reduction(circom, &mut rng)?;
  21. let circom = builder.build()?;
  22. let inputs = circom.get_public_inputs().unwrap();
  23. let proof = GrothBn::prove(&params, circom, &mut rng)?;
  24. let pvk = GrothBn::process_vk(&params.vk).unwrap();
  25. let verified = GrothBn::verify_with_processed_vk(&pvk, &inputs, &proof)?;
  26. assert!(verified);
  27. Ok(())
  28. }
  29. #[tokio::test]
  30. async fn groth16_proof_wrong_input() {
  31. let cfg = CircomConfig::<Fr>::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 fail
  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 = GrothBn::generate_random_parameters_with_reduction(circom, &mut rng).unwrap();
  44. let _ = builder.build().unwrap_err();
  45. }
  46. #[tokio::test]
  47. #[cfg(feature = "circom-2")]
  48. async fn groth16_proof_circom2() -> Result<()> {
  49. let cfg = CircomConfig::<Fr>::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 = GrothBn::generate_random_parameters_with_reduction(circom, &mut rng)?;
  60. let circom = builder.build()?;
  61. let inputs = circom.get_public_inputs().unwrap();
  62. let proof = GrothBn::prove(&params, circom, &mut rng)?;
  63. let pvk = GrothBn::process_vk(&params.vk).unwrap();
  64. let verified = GrothBn::verify_with_processed_vk(&pvk, &inputs, &proof)?;
  65. assert!(verified);
  66. Ok(())
  67. }
  68. #[tokio::test]
  69. #[cfg(feature = "circom-2")]
  70. async fn witness_generation_circom2() -> Result<()> {
  71. let cfg = CircomConfig::<Fr>::new(
  72. "./test-vectors/circom2_multiplier2.wasm",
  73. "./test-vectors/circom2_multiplier2.r1cs",
  74. )?;
  75. let mut builder = CircomBuilder::new(cfg);
  76. builder.push_input("a", 3);
  77. builder.push_input("b", 0x100000000u64 - 1);
  78. assert!(builder.build().is_ok());
  79. Ok(())
  80. }