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.

81 lines
3.0 KiB

  1. # <h1 align="center"> ark-circom </h1>
  2. Arkworks bindings to Circom's R1CS, for Groth16 Proof and Witness generation in Rust.
  3. ![Github Actions](https://github.com/gakonst/ark-circom/workflows/Tests/badge.svg)
  4. ## Documentation
  5. Clone the repository and run `cd ark-circom/ && cargo doc --open`
  6. ## Add ark-circom to your repository
  7. ```toml
  8. [dependencies]
  9. ark-circom = { git = "https://github.com/gakonst/ark-circom.git" }
  10. ```
  11. ## Example
  12. ```rust
  13. // Load the WASM and R1CS for witness and proof generation
  14. let cfg = CircomConfig::<Bn254>::new(
  15. "./test-vectors/mycircuit.wasm",
  16. "./test-vectors/mycircuit.r1cs",
  17. )?;
  18. // Insert our public inputs as key value pairs
  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. // Run a trusted setup
  25. let mut rng = thread_rng();
  26. let params = generate_random_parameters_with_reduction(circom, &mut rng)?;
  27. // Get the populated instance of the circuit with the witness
  28. let circom = builder.build()?;
  29. let inputs = circom.get_public_inputs().unwrap();
  30. // Generate the proof
  31. let proof = prove(&params, circom, &mut rng)?;
  32. // Check that the proof is valid
  33. let pvk = process_vk(&params.vk)?;
  34. let verified = verify_with_processed_vk(&pvk, &inputs, &proof)?;
  35. assert!(verified);
  36. ```
  37. ## Running the tests
  38. Tests require the following installed:
  39. 1. [`solc`](https://solidity.readthedocs.io/en/latest/installing-solidity.html). We also recommend using [solc-select](https://github.com/crytic/solc-select) for more flexibility.
  40. 2. [`ganache-cli`](https://github.com/trufflesuite/ganache-cli#installation)
  41. ## Features
  42. - [x] Witness generation using Circom's WASM witness code
  43. - [x] ZKey parsing into Arkworks Proving Key over BN254
  44. - [x] Compatibility layer for Ethereum types, so that proofs can be used in Solidity verifiers
  45. - [x] Proof generations and verification using Arkworks
  46. - [ ] CLI for common operations
  47. ## Notes
  48. The prover key generated by circom differs from the one generated by arkworks' groth16 library. While the format is the same, it represents different values.
  49. Circom 'prepares' the powers of tau by converting them to Lagrange base, i.e. from `s^i.G` -> `L_i(s).G`. This affects the witness generation process, and the caller needs to ensure the correct `R1CSToQAP` implementer is used:
  50. - use [`CircomReduction`](https://github.com/arkworks-rs/circom-compat/blob/b892c62597687c23341cda1e8e89d58bb6428f36/src/circom/qap.rs#L12) for working with circom-generated files,
  51. - use [`LibsnarkReduction`](https://github.com/arkworks-rs/groth16/blob/5272c935bda290a24cd18d0a3f994b0af70d5f27/src/r1cs_to_qap.rs#L101) for setup produced using the arkworks backend.
  52. ## Acknowledgements
  53. This library would not have been possibly without the great work done in:
  54. - [`zkutil`](https://github.com/poma/zkutil/)
  55. - [`snarkjs`](https://github.com/iden3/snarkjs/)
  56. Special shoutout to [Kobi Gurkan](https://github.com/kobigurk/) for all the help in parsing SnarkJS' ZKey file format.