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.4 KiB

3 years ago
3 years ago
3 years ago
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::{ethereum, CircomBuilder, CircomConfig};
  2. use ark_std::rand::thread_rng;
  3. use color_eyre::Result;
  4. use ark_bn254::Bn254;
  5. use ark_groth16::{create_random_proof as prove, generate_random_parameters};
  6. use ethers::{
  7. contract::ContractError,
  8. prelude::abigen,
  9. providers::{Http, Middleware, Provider},
  10. utils::Anvil,
  11. };
  12. use std::{convert::TryFrom, sync::Arc};
  13. #[tokio::test]
  14. async fn solidity_verifier() -> Result<()> {
  15. let cfg = CircomConfig::<Bn254>::new(
  16. "./test-vectors/mycircuit.wasm",
  17. "./test-vectors/mycircuit.r1cs",
  18. )?;
  19. let mut builder = CircomBuilder::new(cfg);
  20. builder.push_input("a", 3);
  21. builder.push_input("b", 11);
  22. // create an empty instance for setting it up
  23. let circom = builder.setup();
  24. let mut rng = thread_rng();
  25. let params = generate_random_parameters::<Bn254, _, _>(circom, &mut rng)?;
  26. let circom = builder.build()?;
  27. let inputs = circom.get_public_inputs().unwrap();
  28. let proof = prove(circom, &params, &mut rng)?;
  29. // launch the network & compile the verifier
  30. let anvil = Anvil::new().spawn();
  31. let acc = anvil.addresses()[0];
  32. let provider = Provider::<Http>::try_from(anvil.endpoint())?;
  33. let provider = provider.with_sender(acc);
  34. let provider = Arc::new(provider);
  35. // deploy the verifier
  36. let contract = Groth16Verifier::deploy(provider.clone(), ())?
  37. .send()
  38. .await?;
  39. // check the proof
  40. let verified = contract
  41. .check_proof(proof, params.vk, inputs.as_slice())
  42. .await?;
  43. assert!(verified);
  44. Ok(())
  45. }
  46. // We need to implement the conversion from the Ark-Circom's internal Ethereum types to
  47. // the ones expected by the abigen'd types. Could we maybe provide a convenience
  48. // macro for these, given that there's room for implementation error?
  49. abigen!(Groth16Verifier, "./tests/verifier_artifact.json");
  50. use groth_16_verifier::{G1Point, G2Point, Proof, VerifyingKey};
  51. impl From<ethereum::G1> for G1Point {
  52. fn from(src: ethereum::G1) -> Self {
  53. Self { x: src.x, y: src.y }
  54. }
  55. }
  56. impl From<ethereum::G2> for G2Point {
  57. fn from(src: ethereum::G2) -> Self {
  58. // We should use the `.as_tuple()` method which handles converting
  59. // the G2 elements to have the second limb first
  60. let src = src.as_tuple();
  61. Self { x: src.0, y: src.1 }
  62. }
  63. }
  64. impl From<ethereum::Proof> for Proof {
  65. fn from(src: ethereum::Proof) -> Self {
  66. Self {
  67. a: src.a.into(),
  68. b: src.b.into(),
  69. c: src.c.into(),
  70. }
  71. }
  72. }
  73. impl From<ethereum::VerifyingKey> for VerifyingKey {
  74. fn from(src: ethereum::VerifyingKey) -> Self {
  75. Self {
  76. alfa_1: src.alpha1.into(),
  77. beta_2: src.beta2.into(),
  78. gamma_2: src.gamma2.into(),
  79. delta_2: src.delta2.into(),
  80. ic: src.ic.into_iter().map(|i| i.into()).collect(),
  81. }
  82. }
  83. }
  84. impl<M: Middleware> Groth16Verifier<M> {
  85. async fn check_proof<
  86. I: Into<ethereum::Inputs>,
  87. P: Into<ethereum::Proof>,
  88. VK: Into<ethereum::VerifyingKey>,
  89. >(
  90. &self,
  91. proof: P,
  92. vk: VK,
  93. inputs: I,
  94. ) -> Result<bool, ContractError<M>> {
  95. // convert into the expected format by the contract
  96. let proof = proof.into().into();
  97. let vk = vk.into().into();
  98. let inputs = inputs.into().0;
  99. // query the contract
  100. let res = self.verify(inputs, proof, vk).call().await?;
  101. Ok(res)
  102. }
  103. }