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.

119 lines
3.3 KiB

3 years ago
4 months ago
3 years ago
4 months ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months 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::<Bn254>::new(
  11. "./test-vectors/mycircuit_js/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() -> Result<()> {
  31. let cfg = CircomConfig::<Bn254>::new(
  32. "./test-vectors/mycircuit_js/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 verification
  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 circom = builder.build().unwrap();
  45. // we need to manually specify the public input, else the circuit builder will take the default for b = 0, and set public input to 0 (=11*0).
  46. let inputs = vec![Fr::from(33u64)];
  47. let proof = GrothBn::prove(&params, circom, &mut rng).unwrap();
  48. let pvk = GrothBn::process_vk(&params.vk).unwrap();
  49. let verified = GrothBn::verify_with_processed_vk(&pvk, &inputs, &proof).unwrap();
  50. assert!(!verified);
  51. Ok(())
  52. }
  53. #[tokio::test]
  54. async fn groth16_proof_circom() -> Result<()> {
  55. let cfg = CircomConfig::<Bn254>::new(
  56. "test-vectors/circuit2_js/circuit2.wasm",
  57. "test-vectors/circuit2.r1cs",
  58. )?;
  59. let mut builder = CircomBuilder::new(cfg);
  60. builder.push_input("a", 3);
  61. builder.push_input("b", 11);
  62. // create an empty instance for setting it up
  63. let circom = builder.setup();
  64. let mut rng = thread_rng();
  65. let params = GrothBn::generate_random_parameters_with_reduction(circom, &mut rng)?;
  66. let circom = builder.build()?;
  67. let inputs = circom.get_public_inputs().unwrap();
  68. let proof = GrothBn::prove(&params, circom, &mut rng)?;
  69. let pvk = GrothBn::process_vk(&params.vk).unwrap();
  70. let verified = GrothBn::verify_with_processed_vk(&pvk, &inputs, &proof)?;
  71. assert!(verified);
  72. Ok(())
  73. }
  74. #[tokio::test]
  75. async fn witness_generation_circom() -> Result<()> {
  76. let cfg = CircomConfig::<Bn254>::new(
  77. "test-vectors/circuit2_js/circuit2.wasm",
  78. "test-vectors/circuit2.r1cs",
  79. )?;
  80. let mut builder = CircomBuilder::new(cfg);
  81. builder.push_input("a", 3);
  82. builder.push_input("b", 0x100000000u64 - 1);
  83. assert!(builder.build().is_ok());
  84. Ok(())
  85. }