From 72c533ac628f21c52b1dc4ad1b6315b8584d1abb Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Mon, 26 Jul 2021 17:53:45 +0300 Subject: [PATCH] refactor: rename circuit -> circom --- Cargo.toml | 1 - src/{circuit => circom}/builder.rs | 10 +++++----- src/{circuit/circom.rs => circom/circuit.rs} | 6 +++--- src/{circuit => circom}/mod.rs | 9 ++++++--- src/{circom_qap.rs => circom/qap.rs} | 0 src/{circuit => circom}/r1cs_reader.rs | 2 +- src/lib.rs | 12 +++++------- src/zkey.rs | 4 ++-- tests/groth16.rs | 4 ++-- tests/solidity.rs | 9 +++------ 10 files changed, 27 insertions(+), 30 deletions(-) rename src/{circuit => circom}/builder.rs (92%) rename src/{circuit/circom.rs => circom/circuit.rs} (96%) rename src/{circuit => circom}/mod.rs (65%) rename src/{circom_qap.rs => circom/qap.rs} (100%) rename src/{circuit => circom}/r1cs_reader.rs (99%) diff --git a/Cargo.toml b/Cargo.toml index 277500f..0eec769 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,6 @@ ethers = { git = "https://github.com/gakonst/ethers-rs", features = ["abigen"] } serde_json = "1.0.64" serde = "1.0.126" thiserror = "1.0.26" -memmap = "0.7.0" ark-serialize = "0.3.0" [dev-dependencies] diff --git a/src/circuit/builder.rs b/src/circom/builder.rs similarity index 92% rename from src/circuit/builder.rs rename to src/circom/builder.rs index deca738..995650a 100644 --- a/src/circuit/builder.rs +++ b/src/circom/builder.rs @@ -6,22 +6,22 @@ use super::{CircomCircuit, R1CS}; use num_bigint::BigInt; use std::collections::HashMap; -use crate::{circuit::R1CSFile, WitnessCalculator}; +use crate::{circom::R1CSFile, witness::WitnessCalculator}; use color_eyre::Result; pub struct CircomBuilder { - pub cfg: CircuitConfig, + pub cfg: CircomConfig, pub inputs: HashMap>, } // Add utils for creating this from files / directly from bytes -pub struct CircuitConfig { +pub struct CircomConfig { pub r1cs: R1CS, pub wtns: WitnessCalculator, pub sanity_check: bool, } -impl CircuitConfig { +impl CircomConfig { pub fn new(wtns: impl AsRef, r1cs: impl AsRef) -> Result { let wtns = WitnessCalculator::new(wtns).unwrap(); let reader = File::open(r1cs)?; @@ -37,7 +37,7 @@ impl CircuitConfig { impl CircomBuilder { /// Instantiates a new builder using the provided WitnessGenerator and R1CS files /// for your circuit - pub fn new(cfg: CircuitConfig) -> Self { + pub fn new(cfg: CircomConfig) -> Self { Self { cfg, inputs: HashMap::new(), diff --git a/src/circuit/circom.rs b/src/circom/circuit.rs similarity index 96% rename from src/circuit/circom.rs rename to src/circom/circuit.rs index d79dbde..9eee67a 100644 --- a/src/circuit/circom.rs +++ b/src/circom/circuit.rs @@ -3,7 +3,7 @@ use ark_relations::r1cs::{ ConstraintSynthesizer, ConstraintSystemRef, LinearCombination, SynthesisError, Variable, }; -use crate::circuit::R1CS; +use super::R1CS; use color_eyre::Result; @@ -84,13 +84,13 @@ impl ConstraintSynthesizer for CircomCircuit { #[cfg(test)] mod tests { use super::*; - use crate::{CircomBuilder, CircuitConfig}; + use crate::{CircomBuilder, CircomConfig}; use ark_bn254::{Bn254, Fr}; use ark_relations::r1cs::ConstraintSystem; #[test] fn satisfied() { - let cfg = CircuitConfig::::new( + let cfg = CircomConfig::::new( "./test-vectors/mycircuit.wasm", "./test-vectors/mycircuit.r1cs", ) diff --git a/src/circuit/mod.rs b/src/circom/mod.rs similarity index 65% rename from src/circuit/mod.rs rename to src/circom/mod.rs index ddd47ee..30fd840 100644 --- a/src/circuit/mod.rs +++ b/src/circom/mod.rs @@ -3,11 +3,14 @@ use ark_ec::PairingEngine; pub mod r1cs_reader; pub use r1cs_reader::{R1CSFile, R1CS}; -mod circom; -pub use circom::CircomCircuit; +mod circuit; +pub use circuit::CircomCircuit; mod builder; -pub use builder::{CircomBuilder, CircuitConfig}; +pub use builder::{CircomBuilder, CircomConfig}; + +mod qap; +pub use qap::R1CStoQAPCircom; pub type Constraints = (ConstraintVec, ConstraintVec, ConstraintVec); pub type ConstraintVec = Vec<(usize, ::Fr)>; diff --git a/src/circom_qap.rs b/src/circom/qap.rs similarity index 100% rename from src/circom_qap.rs rename to src/circom/qap.rs diff --git a/src/circuit/r1cs_reader.rs b/src/circom/r1cs_reader.rs similarity index 99% rename from src/circuit/r1cs_reader.rs rename to src/circom/r1cs_reader.rs index 2c1b73d..9185687 100644 --- a/src/circuit/r1cs_reader.rs +++ b/src/circom/r1cs_reader.rs @@ -7,7 +7,7 @@ use ark_ec::PairingEngine; use ark_ff::FromBytes; use ark_std::io::Read; -use crate::circuit::{ConstraintVec, Constraints}; +use super::{ConstraintVec, Constraints}; #[derive(Clone, Debug)] pub struct R1CS { diff --git a/src/lib.rs b/src/lib.rs index c433988..9dfb836 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,14 +66,12 @@ //! Groth16 Proof verification: //! `snarkjs groth16 verify verification_key.json public.json proof.json` -mod circom_wasm; -pub use circom_wasm::WitnessCalculator; +mod witness; -pub mod circuit; -pub use circuit::{CircomBuilder, CircomCircuit, CircuitConfig}; +pub mod circom; +pub use circom::{CircomBuilder, CircomCircuit, CircomConfig, R1CStoQAPCircom}; pub mod ethereum; -pub mod zkey; - -pub mod circom_qap; +mod zkey; +pub use zkey::read_zkey; diff --git a/src/zkey.rs b/src/zkey.rs index 1c8531e..c62e6c6 100644 --- a/src/zkey.rs +++ b/src/zkey.rs @@ -299,7 +299,7 @@ mod tests { use serde_json::Value; use std::fs::File; - use crate::{circom_qap::R1CStoQAPCircom, CircomBuilder, CircuitConfig}; + use crate::{circom::R1CStoQAPCircom, CircomBuilder, CircomConfig}; use ark_groth16::{create_random_proof_with_qap as prove, prepare_verifying_key, verify_proof}; use ark_std::rand::thread_rng; use num_traits::{One, Zero}; @@ -769,7 +769,7 @@ mod tests { let mut file = File::open(path).unwrap(); let params = read_zkey(&mut file).unwrap(); // binfile.proving_key().unwrap(); - let cfg = CircuitConfig::::new( + let cfg = CircomConfig::::new( "./test-vectors/mycircuit.wasm", "./test-vectors/mycircuit.r1cs", ) diff --git a/tests/groth16.rs b/tests/groth16.rs index dbec85d..754bb95 100644 --- a/tests/groth16.rs +++ b/tests/groth16.rs @@ -1,4 +1,4 @@ -use ark_circom::{CircomBuilder, CircuitConfig}; +use ark_circom::{CircomBuilder, CircomConfig}; use ark_std::rand::thread_rng; use color_eyre::Result; @@ -9,7 +9,7 @@ use ark_groth16::{ #[test] fn groth16_proof() -> Result<()> { - let cfg = CircuitConfig::::new( + let cfg = CircomConfig::::new( "./test-vectors/mycircuit.wasm", "./test-vectors/mycircuit.r1cs", )?; diff --git a/tests/solidity.rs b/tests/solidity.rs index 2742ee2..c2a7190 100644 --- a/tests/solidity.rs +++ b/tests/solidity.rs @@ -1,6 +1,6 @@ use ark_circom::{ ethereum::{Inputs, Proof, VerifyingKey}, - CircomBuilder, CircuitConfig, + CircomBuilder, CircomConfig, }; use ark_std::rand::thread_rng; use color_eyre::Result; @@ -18,7 +18,7 @@ use std::{convert::TryFrom, sync::Arc}; #[tokio::test] async fn solidity_verifier() -> Result<()> { - let cfg = CircuitConfig::::new( + let cfg = CircomConfig::::new( "./test-vectors/mycircuit.wasm", "./test-vectors/mycircuit.r1cs", )?; @@ -70,10 +70,7 @@ async fn solidity_verifier() -> Result<()> { Ok(()) } -abigen!( - Groth16Verifier, - "./tests/verifier_abi.json" -); +abigen!(Groth16Verifier, "./tests/verifier_abi.json"); impl Groth16Verifier { async fn check_proof, P: Into, VK: Into>(