Skip to content

Commit 8912049

Browse files
Move calldata preparation (#217)
* Remove unused trait * Move `prepare_calldata` to `solidity-verifiers` * Move `to_eth` utilities * imports * Implicit selector computation * Rename function
1 parent 091fe94 commit 8912049

File tree

12 files changed

+93
-126
lines changed

12 files changed

+93
-126
lines changed

examples/circom_full_flow.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,15 @@ use experimental_frontends::{circom::CircomFCircuit, utils::VecF};
2121
use folding_schemes::{
2222
commitment::{kzg::KZG, pedersen::Pedersen},
2323
folding::{
24-
nova::{
25-
decider_eth::{prepare_calldata, Decider as DeciderEth},
26-
Nova, PreprocessorParam,
27-
},
24+
nova::{decider_eth::Decider as DeciderEth, Nova, PreprocessorParam},
2825
traits::CommittedInstanceOps,
2926
},
3027
frontend::FCircuit,
3128
transcript::poseidon::poseidon_canonical_config,
3229
Decider, Error, FoldingScheme,
3330
};
3431
use solidity_verifiers::calldata::{
35-
get_function_selector_for_nova_cyclefold_verifier, NovaVerificationMode,
32+
prepare_calldata_for_nova_cyclefold_verifier, NovaVerificationMode,
3633
};
3734
use solidity_verifiers::{
3835
evm::{compile_solidity, Evm},
@@ -135,13 +132,8 @@ fn main() -> Result<(), Error> {
135132
println!("Decider proof verification: {}", verified);
136133

137134
// Now, let's generate the Solidity code that verifies this Decider final proof
138-
let function_selector = get_function_selector_for_nova_cyclefold_verifier(
135+
let calldata: Vec<u8> = prepare_calldata_for_nova_cyclefold_verifier(
139136
NovaVerificationMode::Explicit,
140-
nova.z_0.len(),
141-
);
142-
143-
let calldata: Vec<u8> = prepare_calldata(
144-
function_selector,
145137
nova.i,
146138
nova.z_0,
147139
nova.z_i,

examples/full_flow.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@ use std::time::Instant;
2222
use folding_schemes::{
2323
commitment::{kzg::KZG, pedersen::Pedersen},
2424
folding::{
25-
nova::{
26-
decider_eth::{prepare_calldata, Decider as DeciderEth},
27-
Nova, PreprocessorParam,
28-
},
25+
nova::{decider_eth::Decider as DeciderEth, Nova, PreprocessorParam},
2926
traits::CommittedInstanceOps,
3027
},
3128
frontend::FCircuit,
3229
transcript::poseidon::poseidon_canonical_config,
3330
Decider, Error, FoldingScheme,
3431
};
3532
use solidity_verifiers::calldata::{
36-
get_function_selector_for_nova_cyclefold_verifier, NovaVerificationMode,
33+
prepare_calldata_for_nova_cyclefold_verifier, NovaVerificationMode,
3734
};
3835
use solidity_verifiers::{
3936
evm::{compile_solidity, Evm},
@@ -120,13 +117,8 @@ fn main() -> Result<(), Error> {
120117
println!("Decider proof verification: {}", verified);
121118

122119
// Now, let's generate the Solidity code that verifies this Decider final proof
123-
let function_selector = get_function_selector_for_nova_cyclefold_verifier(
120+
let calldata: Vec<u8> = prepare_calldata_for_nova_cyclefold_verifier(
124121
NovaVerificationMode::Explicit,
125-
nova.z_0.len(),
126-
);
127-
128-
let calldata: Vec<u8> = prepare_calldata(
129-
function_selector,
130122
nova.i,
131123
nova.z_0,
132124
nova.z_i,

examples/noir_full_flow.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ use experimental_frontends::{noir::NoirFCircuit, utils::VecF};
1818
use folding_schemes::{
1919
commitment::{kzg::KZG, pedersen::Pedersen},
2020
folding::{
21-
nova::{
22-
decider_eth::{prepare_calldata, Decider as DeciderEth},
23-
Nova, PreprocessorParam,
24-
},
21+
nova::{decider_eth::Decider as DeciderEth, Nova, PreprocessorParam},
2522
traits::CommittedInstanceOps,
2623
},
2724
frontend::FCircuit,
@@ -31,7 +28,7 @@ use folding_schemes::{
3128
use std::{path::Path, time::Instant};
3229

3330
use solidity_verifiers::calldata::{
34-
get_function_selector_for_nova_cyclefold_verifier, NovaVerificationMode,
31+
prepare_calldata_for_nova_cyclefold_verifier, NovaVerificationMode,
3532
};
3633
use solidity_verifiers::{
3734
evm::{compile_solidity, Evm},
@@ -107,13 +104,8 @@ fn main() -> Result<(), Error> {
107104
println!("Decider proof verification: {}", verified);
108105

109106
// Now, let's generate the Solidity code that verifies this Decider final proof
110-
let function_selector = get_function_selector_for_nova_cyclefold_verifier(
107+
let calldata: Vec<u8> = prepare_calldata_for_nova_cyclefold_verifier(
111108
NovaVerificationMode::Explicit,
112-
nova.z_0.len(),
113-
);
114-
115-
let calldata: Vec<u8> = prepare_calldata(
116-
function_selector,
117109
nova.i,
118110
nova.z_0,
119111
nova.z_i,

examples/noname_full_flow.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ use experimental_frontends::{noname::NonameFCircuit, utils::VecF};
1919
use folding_schemes::{
2020
commitment::{kzg::KZG, pedersen::Pedersen},
2121
folding::{
22-
nova::{
23-
decider_eth::{prepare_calldata, Decider as DeciderEth},
24-
Nova, PreprocessorParam,
25-
},
22+
nova::{decider_eth::Decider as DeciderEth, Nova, PreprocessorParam},
2623
traits::CommittedInstanceOps,
2724
},
2825
frontend::FCircuit,
@@ -32,7 +29,7 @@ use folding_schemes::{
3229
use std::time::Instant;
3330

3431
use solidity_verifiers::calldata::{
35-
get_function_selector_for_nova_cyclefold_verifier, NovaVerificationMode,
32+
prepare_calldata_for_nova_cyclefold_verifier, NovaVerificationMode,
3633
};
3734
use solidity_verifiers::{
3835
evm::{compile_solidity, Evm},
@@ -128,13 +125,8 @@ fn main() -> Result<(), Error> {
128125
println!("Decider proof verification: {}", verified);
129126

130127
// Now, let's generate the Solidity code that verifies this Decider final proof
131-
let function_selector = get_function_selector_for_nova_cyclefold_verifier(
128+
let calldata: Vec<u8> = prepare_calldata_for_nova_cyclefold_verifier(
132129
NovaVerificationMode::Explicit,
133-
nova.z_0.len(),
134-
);
135-
136-
let calldata: Vec<u8> = prepare_calldata(
137-
function_selector,
138130
nova.i,
139131
nova.z_0,
140132
nova.z_i,

folding-schemes/src/folding/nova/decider_eth.rs

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
/// the Decider from decider.rs file will be more efficient.
33
/// More details can be found at the documentation page:
44
/// https://privacy-scaling-explorations.github.io/sonobe-docs/design/nova-decider-onchain.html
5-
use ark_bn254::Bn254;
6-
use ark_groth16::Groth16;
75
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
86
use ark_snark::SNARK;
97
use ark_std::{
@@ -14,17 +12,12 @@ use core::marker::PhantomData;
1412

1513
pub use super::decider_eth_circuit::DeciderEthCircuit;
1614
use super::decider_eth_circuit::DeciderNovaGadget;
17-
use super::{CommittedInstance, Nova};
15+
use super::Nova;
1816
use crate::folding::circuits::decider::DeciderEnabledNIFS;
1917
use crate::folding::traits::{InputizeNonNative, WitnessOps};
2018
use crate::frontend::FCircuit;
21-
use crate::utils::eth::ToEth;
2219
use crate::{
23-
commitment::{
24-
kzg::{Proof as KZGProof, KZG},
25-
pedersen::Params as PedersenParams,
26-
CommitmentScheme,
27-
},
20+
commitment::{kzg::Proof as KZGProof, pedersen::Params as PedersenParams, CommitmentScheme},
2821
folding::traits::Dummy,
2922
};
3023
use crate::{Curve, Error};
@@ -48,6 +41,33 @@ where
4841
kzg_challenges: [C::ScalarField; 2],
4942
}
5043

44+
impl<C, CS, S> Proof<C, CS, S>
45+
where
46+
C: Curve,
47+
CS: CommitmentScheme<C, ProverChallenge = C::ScalarField, Challenge = C::ScalarField>,
48+
S: SNARK<C::ScalarField>,
49+
{
50+
pub fn snark_proof(&self) -> &S::Proof {
51+
&self.snark_proof
52+
}
53+
54+
pub fn kzg_proofs(&self) -> &[CS::Proof; 2] {
55+
&self.kzg_proofs
56+
}
57+
58+
pub fn cmT(&self) -> &C {
59+
&self.cmT
60+
}
61+
62+
pub fn r(&self) -> C::ScalarField {
63+
self.r
64+
}
65+
66+
pub fn kzg_challenges(&self) -> [C::ScalarField; 2] {
67+
self.kzg_challenges
68+
}
69+
}
70+
5171
#[derive(Debug, Clone, Eq, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
5272
pub struct VerifierParam<C1, CS_VerifyingKey, S_VerifyingKey>
5373
where
@@ -244,49 +264,19 @@ where
244264
}
245265
}
246266

247-
/// Prepares solidity calldata for calling the NovaDecider contract
248-
#[allow(clippy::too_many_arguments)]
249-
pub fn prepare_calldata(
250-
function_signature_check: [u8; 4],
251-
i: ark_bn254::Fr,
252-
z_0: Vec<ark_bn254::Fr>,
253-
z_i: Vec<ark_bn254::Fr>,
254-
running_instance: &CommittedInstance<ark_bn254::G1Projective>,
255-
incoming_instance: &CommittedInstance<ark_bn254::G1Projective>,
256-
proof: &Proof<ark_bn254::G1Projective, KZG<Bn254>, Groth16<Bn254>>,
257-
) -> Result<Vec<u8>, Error> {
258-
Ok([
259-
function_signature_check.to_eth(),
260-
i.to_eth(), // i
261-
z_0.to_eth(), // z_0
262-
z_i.to_eth(), // z_i
263-
running_instance.cmW.to_eth(),
264-
running_instance.cmE.to_eth(),
265-
incoming_instance.cmW.to_eth(),
266-
proof.cmT.to_eth(), // cmT
267-
proof.r.to_eth(), // r
268-
proof.snark_proof.to_eth(), // pA, pB, pC
269-
proof.kzg_challenges.to_eth(), // challenge_W, challenge_E
270-
proof.kzg_proofs[0].eval.to_eth(), // eval W
271-
proof.kzg_proofs[1].eval.to_eth(), // eval E
272-
proof.kzg_proofs[0].proof.to_eth(), // W kzg_proof
273-
proof.kzg_proofs[1].proof.to_eth(), // E kzg_proof
274-
]
275-
.concat())
276-
}
277-
278267
#[cfg(test)]
279268
pub mod tests {
280-
use ark_bn254::{Fr, G1Projective as Projective};
281-
use ark_grumpkin::Projective as Projective2;
282-
use std::time::Instant;
283-
284269
use super::*;
270+
use crate::commitment::kzg::KZG;
285271
use crate::commitment::pedersen::Pedersen;
286272
use crate::folding::nova::{PreprocessorParam, ProverParams as NovaProverParams};
287273
use crate::folding::traits::CommittedInstanceOps;
288274
use crate::frontend::utils::CubicFCircuit;
289275
use crate::transcript::poseidon::poseidon_canonical_config;
276+
use ark_bn254::{Bn254, Fr, G1Projective as Projective};
277+
use ark_groth16::Groth16;
278+
use ark_grumpkin::Projective as Projective2;
279+
use std::time::Instant;
290280

291281
#[test]
292282
fn test_decider() -> Result<(), Error> {

folding-schemes/src/lib.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use ark_crypto_primitives::sponge::Absorb;
66
use ark_ec::{
7-
pairing::Pairing,
87
short_weierstrass::{Projective, SWCurveConfig},
98
CurveGroup,
109
};
@@ -287,26 +286,6 @@ pub trait Decider<
287286
) -> Result<bool, Error>;
288287
}
289288

290-
/// DeciderOnchain extends the Decider into preparing the calldata
291-
pub trait DeciderOnchain<
292-
E: Pairing,
293-
C1: Curve<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
294-
C2: Curve,
295-
>
296-
{
297-
type Proof;
298-
type CommittedInstance: Clone + Debug;
299-
300-
fn prepare_calldata(
301-
i: C1::ScalarField,
302-
z_0: Vec<C1::ScalarField>,
303-
z_i: Vec<C1::ScalarField>,
304-
running_instance: &Self::CommittedInstance,
305-
incoming_instance: &Self::CommittedInstance,
306-
proof: Self::Proof,
307-
) -> Result<Vec<u8>, Error>;
308-
}
309-
310289
/// `Field` trait is a wrapper around `PrimeField` that also includes the
311290
/// necessary bounds for the field to be used conveniently in folding schemes.
312291
pub trait Field:

folding-schemes/src/utils/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::arith::ArithSerializer;
1111
use crate::commitment::CommitmentScheme;
1212
use crate::{Curve, Error};
1313

14-
pub mod eth;
1514
pub mod gadgets;
1615
pub mod hypercube;
1716
pub mod lagrange_poly;

solidity-verifiers/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ license.workspace = true
66
repository.workspace = true
77

88
[dependencies]
9+
ark-ec = { workspace = true }
10+
ark-ff = { workspace = true }
911
ark-groth16 = { workspace = true }
1012
ark-bn254 = { workspace = true, features = ["r1cs"] }
1113
ark-poly-commit = { workspace = true }

solidity-verifiers/src/calldata.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
use crate::utils::eth::ToEth;
2+
use ark_bn254::Bn254;
3+
use ark_groth16::Groth16;
14
use crypto::digest::Digest;
25
use crypto::sha3::Sha3;
6+
use folding_schemes::commitment::kzg::KZG;
7+
use folding_schemes::folding::nova::decider_eth::Proof;
8+
use folding_schemes::folding::nova::CommittedInstance;
9+
use folding_schemes::Error;
310
use num_bigint::BigUint;
411

512
/// Specifies which API to use for a proof verification in a contract.
@@ -26,12 +33,41 @@ pub fn get_formatted_calldata(calldata: Vec<u8>) -> Vec<String> {
2633
formatted_calldata
2734
}
2835

36+
/// Prepares solidity calldata for calling the NovaDecider contract
37+
pub fn prepare_calldata_for_nova_cyclefold_verifier(
38+
verification_mode: NovaVerificationMode,
39+
i: ark_bn254::Fr,
40+
z_0: Vec<ark_bn254::Fr>,
41+
z_i: Vec<ark_bn254::Fr>,
42+
running_instance: &CommittedInstance<ark_bn254::G1Projective>,
43+
incoming_instance: &CommittedInstance<ark_bn254::G1Projective>,
44+
proof: &Proof<ark_bn254::G1Projective, KZG<Bn254>, Groth16<Bn254>>,
45+
) -> Result<Vec<u8>, Error> {
46+
let selector = get_function_selector(verification_mode, z_0.len());
47+
48+
Ok([
49+
selector.to_eth(),
50+
i.to_eth(), // i
51+
z_0.to_eth(), // z_0
52+
z_i.to_eth(), // z_i
53+
running_instance.cmW.to_eth(),
54+
running_instance.cmE.to_eth(),
55+
incoming_instance.cmW.to_eth(),
56+
proof.cmT().to_eth(), // cmT
57+
proof.r().to_eth(), // r
58+
proof.snark_proof().to_eth(), // pA, pB, pC
59+
proof.kzg_challenges().to_eth(), // challenge_W, challenge_E
60+
proof.kzg_proofs()[0].eval.to_eth(), // eval W
61+
proof.kzg_proofs()[1].eval.to_eth(), // eval E
62+
proof.kzg_proofs()[0].proof.to_eth(), // W kzg_proof
63+
proof.kzg_proofs()[1].proof.to_eth(), // E kzg_proof
64+
]
65+
.concat())
66+
}
67+
2968
/// Computes the function selector for the nova cyclefold verifier.
3069
/// It is computed on the fly since it depends on the IVC state length.
31-
pub fn get_function_selector_for_nova_cyclefold_verifier(
32-
mode: NovaVerificationMode,
33-
state_len: usize,
34-
) -> [u8; 4] {
70+
fn get_function_selector(mode: NovaVerificationMode, state_len: usize) -> [u8; 4] {
3571
let fn_sig = match mode {
3672
NovaVerificationMode::Explicit =>
3773
format!(

0 commit comments

Comments
 (0)