Skip to content

Commit f21957b

Browse files
committed
Circom external inputs (#91)
* circom: add external_inputs * adapt new external_inputs interface to the FoldingScheme trait and Nova impl * adapt examples to new FCircuit external_inputs interface * add state_len & external_inputs_len params to CircomFCircuit * add examples/circom_full_flow.rs * merge the params initializer functions, clippy * circom: move r1cs reading to FCircuit::new instead of each step * CI/examples: add circom so it can run the circom_full_flow example
1 parent 9bbdfc5 commit f21957b

File tree

21 files changed

+631
-260
lines changed

21 files changed

+631
-260
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,18 @@ jobs:
7979
steps:
8080
- uses: actions/checkout@v2
8181
- uses: actions-rs/toolchain@v1
82+
- name: Download Circom
83+
run: |
84+
mkdir -p $HOME/bin
85+
curl -sSfL https://github.com/iden3/circom/releases/download/v2.1.6/circom-linux-amd64 -o $HOME/bin/circom
86+
chmod +x $HOME/bin/circom
87+
echo "$HOME/bin" >> $GITHUB_PATH
8288
- name: Download solc
8389
run: |
8490
curl -sSfL https://github.com/ethereum/solidity/releases/download/v0.8.4/solc-static-linux -o /usr/local/bin/solc
8591
chmod +x /usr/local/bin/solc
92+
- name: Execute compile.sh to generate .r1cs and .wasm from .circom
93+
run: bash ./folding-schemes/src/frontend/circom/test_folder/compile.sh
8694
- name: Run examples tests
8795
run: cargo test --examples
8896
- name: Run examples

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
Cargo.lock
33

44
# Circom generated files
5-
folding-schemes/src/frontend/circom/test_folder/cubic_circuit.r1cs
65
folding-schemes/src/frontend/circom/test_folder/cubic_circuit_js/
6+
folding-schemes/src/frontend/circom/test_folder/external_inputs_js/
7+
*.r1cs
8+
*.sym
79

810
# generated contracts at test time
911
solidity-verifiers/generated

examples/circom_full_flow.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#![allow(non_snake_case)]
2+
#![allow(non_camel_case_types)]
3+
#![allow(clippy::upper_case_acronyms)]
4+
///
5+
/// This example performs the full flow:
6+
/// - define the circuit to be folded
7+
/// - fold the circuit with Nova+CycleFold's IVC
8+
/// - generate a DeciderEthCircuit final proof
9+
/// - generate the Solidity contract that verifies the proof
10+
/// - verify the proof in the EVM
11+
///
12+
use ark_bn254::{constraints::GVar, Bn254, Fr, G1Projective as G1};
13+
14+
use ark_groth16::Groth16;
15+
use ark_grumpkin::{constraints::GVar as GVar2, Projective as G2};
16+
17+
use std::path::PathBuf;
18+
use std::time::Instant;
19+
20+
use folding_schemes::{
21+
commitment::{kzg::KZG, pedersen::Pedersen},
22+
folding::nova::{
23+
decider_eth::{prepare_calldata, Decider as DeciderEth},
24+
Nova,
25+
},
26+
frontend::{circom::CircomFCircuit, FCircuit},
27+
Decider, FoldingScheme,
28+
};
29+
use solidity_verifiers::{
30+
evm::{compile_solidity, Evm},
31+
utils::get_function_selector_for_nova_cyclefold_verifier,
32+
verifiers::nova_cyclefold::get_decider_template_for_cyclefold_decider,
33+
NovaCycleFoldVerifierKey,
34+
};
35+
36+
mod utils;
37+
use utils::init_ivc_and_decider_params;
38+
39+
fn main() {
40+
// set the initial state
41+
let z_0 = vec![Fr::from(3_u32)];
42+
43+
// set the external inputs to be used at each step of the IVC, it has length of 10 since this
44+
// is the number of steps that we will do
45+
let external_inputs = vec![
46+
vec![Fr::from(6u32), Fr::from(7u32)],
47+
vec![Fr::from(8u32), Fr::from(9u32)],
48+
vec![Fr::from(10u32), Fr::from(11u32)],
49+
vec![Fr::from(12u32), Fr::from(13u32)],
50+
vec![Fr::from(14u32), Fr::from(15u32)],
51+
vec![Fr::from(6u32), Fr::from(7u32)],
52+
vec![Fr::from(8u32), Fr::from(9u32)],
53+
vec![Fr::from(10u32), Fr::from(11u32)],
54+
vec![Fr::from(12u32), Fr::from(13u32)],
55+
vec![Fr::from(14u32), Fr::from(15u32)],
56+
];
57+
58+
// initialize the Circom circuit
59+
let r1cs_path =
60+
PathBuf::from("./folding-schemes/src/frontend/circom/test_folder/external_inputs.r1cs");
61+
let wasm_path = PathBuf::from(
62+
"./folding-schemes/src/frontend/circom/test_folder/external_inputs_js/external_inputs.wasm",
63+
);
64+
65+
let f_circuit_params = (r1cs_path, wasm_path, 1, 2);
66+
let f_circuit = CircomFCircuit::<Fr>::new(f_circuit_params).unwrap();
67+
68+
let (fs_prover_params, kzg_vk, g16_pk, g16_vk) =
69+
init_ivc_and_decider_params::<CircomFCircuit<Fr>>(f_circuit.clone());
70+
71+
pub type NOVA =
72+
Nova<G1, GVar, G2, GVar2, CircomFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>>;
73+
pub type DECIDERETH_FCircuit = DeciderEth<
74+
G1,
75+
GVar,
76+
G2,
77+
GVar2,
78+
CircomFCircuit<Fr>,
79+
KZG<'static, Bn254>,
80+
Pedersen<G2>,
81+
Groth16<Bn254>,
82+
NOVA,
83+
>;
84+
85+
// initialize the folding scheme engine, in our case we use Nova
86+
let mut nova = NOVA::init(&fs_prover_params, f_circuit.clone(), z_0).unwrap();
87+
// run n steps of the folding iteration
88+
for (i, external_inputs_at_step) in external_inputs.iter().enumerate() {
89+
let start = Instant::now();
90+
nova.prove_step(external_inputs_at_step.clone()).unwrap();
91+
println!("Nova::prove_step {}: {:?}", i, start.elapsed());
92+
}
93+
94+
let rng = rand::rngs::OsRng;
95+
let start = Instant::now();
96+
let proof = DECIDERETH_FCircuit::prove(
97+
(g16_pk, fs_prover_params.cs_params.clone()),
98+
rng,
99+
nova.clone(),
100+
)
101+
.unwrap();
102+
println!("generated Decider proof: {:?}", start.elapsed());
103+
104+
let verified = DECIDERETH_FCircuit::verify(
105+
(g16_vk.clone(), kzg_vk.clone()),
106+
nova.i,
107+
nova.z_0.clone(),
108+
nova.z_i.clone(),
109+
&nova.U_i,
110+
&nova.u_i,
111+
&proof,
112+
)
113+
.unwrap();
114+
assert!(verified);
115+
println!("Decider proof verification: {}", verified);
116+
117+
// Now, let's generate the Solidity code that verifies this Decider final proof
118+
let function_selector =
119+
get_function_selector_for_nova_cyclefold_verifier(nova.z_0.len() * 2 + 1);
120+
121+
let calldata: Vec<u8> = prepare_calldata(
122+
function_selector,
123+
nova.i,
124+
nova.z_0,
125+
nova.z_i,
126+
&nova.U_i,
127+
&nova.u_i,
128+
proof,
129+
)
130+
.unwrap();
131+
132+
// prepare the setup params for the solidity verifier
133+
let nova_cyclefold_vk = NovaCycleFoldVerifierKey::from((g16_vk, kzg_vk, f_circuit.state_len()));
134+
135+
// generate the solidity code
136+
let decider_solidity_code = get_decider_template_for_cyclefold_decider(nova_cyclefold_vk);
137+
138+
// verify the proof against the solidity code in the EVM
139+
let nova_cyclefold_verifier_bytecode = compile_solidity(&decider_solidity_code, "NovaDecider");
140+
let mut evm = Evm::default();
141+
let verifier_address = evm.create(nova_cyclefold_verifier_bytecode);
142+
let (_, output) = evm.call(verifier_address, calldata.clone());
143+
assert_eq!(*output.last().unwrap(), 1);
144+
145+
// save smart contract and the calldata
146+
println!("storing nova-verifier.sol and the calldata into files");
147+
use std::fs;
148+
fs::write(
149+
"./examples/nova-verifier.sol",
150+
decider_solidity_code.clone(),
151+
)
152+
.unwrap();
153+
fs::write("./examples/solidity-calldata.calldata", calldata.clone()).unwrap();
154+
let s = solidity_verifiers::utils::get_formatted_calldata(calldata.clone());
155+
fs::write("./examples/solidity-calldata.inputs", s.join(",\n")).expect("");
156+
}

0 commit comments

Comments
 (0)