Skip to content

Commit 6f677be

Browse files
authored
Merge pull request #71 from lurk-lab/zeromorph_wip
Zeromorph EvaluationEngine trait
2 parents 582d324 + fd172b8 commit 6f677be

13 files changed

Lines changed: 435 additions & 210 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ cfg-if = "1.0.0"
4545
once_cell = "1.18.0"
4646
anyhow = "1.0.72"
4747
rand = "0.8.4"
48+
rand_xorshift = "0.3.0"
4849

4950
[target.'cfg(any(target_arch = "x86_64", target_arch = "aarch64"))'.dependencies]
5051
pasta-msm = { git="https://github.com/lurk-lab/pasta-msm", branch="dev", version = "0.1.4" }

ThirdPartyNotices.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,22 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
5252
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
5353
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
5454
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55+
56+
------------------------------------------------------------
57+
https://github.com/AztecProtocol/aztec-packages/
58+
59+
Licensed under Apache 2.0
60+
61+
Copyright 2022 Aztec
62+
63+
Licensed under the Apache License, Version 2.0 (the "License");
64+
you may not use this file except in compliance with the License.
65+
You may obtain a copy of the License at
66+
67+
http://www.apache.org/licenses/LICENSE-2.0
68+
69+
Unless required by applicable law or agreed to in writing, software
70+
distributed under the License is distributed on an "AS IS" BASIS,
71+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
72+
See the License for the specific language governing permissions and
73+
limitations under the License.

examples/minroot_serde.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn main() {
194194
MinRootCircuit<<G1 as Group>::Scalar>,
195195
TrivialCircuit<<G2 as Group>::Scalar>,
196196
>::new(&circuit_primary, &circuit_secondary, None, None);
197-
assert!(result.clone() == pp, "not equal!");
197+
assert!(*result == pp, "not equal!");
198198
assert!(remaining.is_empty());
199199
} else {
200200
println!("Something terrible happened");

src/lib.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -948,13 +948,10 @@ mod tests {
948948
use core::fmt::Write;
949949

950950
use super::*;
951-
#[allow(dead_code)]
952-
type ZM<E> = provider::non_hiding_zeromorph::ZMEvaluation<E>;
951+
type ZM<E> = provider::non_hiding_zeromorph::ZMPCS<E>;
953952
type EE<G> = provider::ipa_pc::EvaluationEngine<G>;
954953
type S<G, EE> = spartan::snark::RelaxedR1CSSNARK<G, EE>;
955954
type SPrime<G, EE> = spartan::ppsnark::RelaxedR1CSSNARK<G, EE>;
956-
#[allow(dead_code)]
957-
type SZM<G1, E> = spartan::snark::RelaxedR1CSSNARK<G1, ZM<E>>;
958955

959956
use ::bellpepper_core::{num::AllocatedNum, ConstraintSystem, SynthesisError};
960957
use core::marker::PhantomData;
@@ -1068,12 +1065,12 @@ mod tests {
10681065
let trivial_circuit2_grumpkin = TrivialCircuit::<<grumpkin::Point as Group>::Scalar>::default();
10691066
let cubic_circuit1_grumpkin = CubicCircuit::<<bn256::Point as Group>::Scalar>::default();
10701067

1071-
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _, EE<_>, EE<_>>(
1068+
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _, ZM<halo2curves::bn256::Bn256>, EE<_>>(
10721069
&trivial_circuit1_grumpkin,
10731070
&trivial_circuit2_grumpkin,
10741071
"184d05f08dca260f010cb48c6cf8c5eb61dedfc270e5a18226eb622cf7da0203",
10751072
);
1076-
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _, EE<_>, EE<_>>(
1073+
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _, ZM<halo2curves::bn256::Bn256>, EE<_>>(
10771074
&cubic_circuit1_grumpkin,
10781075
&trivial_circuit2_grumpkin,
10791076
"2fb992932b2a642b4ce8f52646a7ef6a5a486682716cf969df50021107afff03",
@@ -1306,8 +1303,7 @@ mod tests {
13061303
let (pk, vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&pp).unwrap();
13071304

13081305
// produce a compressed SNARK
1309-
let res =
1310-
CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &pk, &recursive_snark);
1306+
let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &pk, &recursive_snark);
13111307
assert!(res.is_ok());
13121308
let compressed_snark = res.unwrap();
13131309

@@ -1330,7 +1326,7 @@ mod tests {
13301326
test_ivc_nontrivial_with_compression_with::<
13311327
bn256::Point,
13321328
grumpkin::Point,
1333-
S<bn256::Point, EE<_>>, // SZM<bn256::Point, halo2curves::bn256::Bn256>,
1329+
S<bn256::Point, ZM<halo2curves::bn256::Bn256>>,
13341330
S<grumpkin::Point, EE<_>>,
13351331
>();
13361332
test_ivc_nontrivial_with_compression_with::<
@@ -1341,17 +1337,6 @@ mod tests {
13411337
>();
13421338
}
13431339

1344-
#[test]
1345-
#[ignore]
1346-
fn test_ivc_nontrivial_with_zm_compression() {
1347-
test_ivc_nontrivial_with_compression_with::<
1348-
bn256::Point,
1349-
grumpkin::Point,
1350-
S<bn256::Point, EE<_>>, // SZM<bn256::Point, halo2curves::bn256::Bn256>,
1351-
S<grumpkin::Point, EE<_>>,
1352-
>();
1353-
}
1354-
13551340
fn test_ivc_nontrivial_with_spark_compression_with<G1, G2, E1, E2>()
13561341
where
13571342
G1: Group<Base = <G2 as Group>::Scalar>,
@@ -1456,8 +1441,12 @@ mod tests {
14561441
type G2 = pasta_curves::vesta::Point;
14571442

14581443
test_ivc_nontrivial_with_spark_compression_with::<G1, G2, EE<_>, EE<_>>();
1459-
test_ivc_nontrivial_with_spark_compression_with::<bn256::Point, grumpkin::Point, EE<_>, EE<_>>(
1460-
);
1444+
test_ivc_nontrivial_with_spark_compression_with::<
1445+
bn256::Point,
1446+
grumpkin::Point,
1447+
ZM<halo2curves::bn256::Bn256>,
1448+
EE<_>,
1449+
>();
14611450
test_ivc_nontrivial_with_spark_compression_with::<
14621451
secp256k1::Point,
14631452
secq256k1::Point,
@@ -1610,7 +1599,12 @@ mod tests {
16101599
type G2 = pasta_curves::vesta::Point;
16111600

16121601
test_ivc_nondet_with_compression_with::<G1, G2, EE<_>, EE<_>>();
1613-
test_ivc_nondet_with_compression_with::<bn256::Point, grumpkin::Point, EE<_>, EE<_>>();
1602+
test_ivc_nondet_with_compression_with::<
1603+
bn256::Point,
1604+
grumpkin::Point,
1605+
ZM<halo2curves::bn256::Bn256>,
1606+
EE<_>,
1607+
>();
16141608
test_ivc_nondet_with_compression_with::<secp256k1::Point, secq256k1::Point, EE<_>, EE<_>>();
16151609
}
16161610

src/provider/bn256_grumpkin.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use halo2curves::grumpkin::{
2727
G1Affine as GrumpkinAffine, G1Compressed as GrumpkinCompressed, G1 as GrumpkinPoint,
2828
};
2929

30+
use super::kzg_commitment::KZGCommitmentEngine;
31+
3032
/// Re-exports that give access to the standard aliases used in the code base, for bn256
3133
pub mod bn256 {
3234
pub use halo2curves::bn256::{
@@ -58,7 +60,8 @@ impl_traits!(
5860
Bn256Compressed,
5961
Bn256Point,
6062
Bn256Affine,
61-
"30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"
63+
"30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
64+
KZGCommitmentEngine<halo2curves::bn256::Bn256>
6265
);
6366

6467
impl_traits!(

src/provider/kzg_commitment.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! Commitment engine for KZG commitments
2+
//!
3+
4+
use std::marker::PhantomData;
5+
6+
use group::{prime::PrimeCurveAffine, Curve};
7+
use pairing::Engine;
8+
use rand::rngs::StdRng;
9+
use rand_core::SeedableRng;
10+
use serde::{Deserialize, Serialize};
11+
12+
use crate::traits::{
13+
commitment::{CommitmentEngineTrait, Len},
14+
Group,
15+
};
16+
17+
use super::{
18+
non_hiding_kzg::{UVKZGCommitment, UVUniversalKZGParam},
19+
pedersen::Commitment,
20+
};
21+
22+
/// Provides a commitment engine
23+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
24+
pub struct KZGCommitmentEngine<E: Engine> {
25+
_p: PhantomData<E>,
26+
}
27+
28+
impl<E: Engine> CommitmentEngineTrait<E::G1> for KZGCommitmentEngine<E>
29+
where
30+
E::G1: Group<PreprocessedGroupElement = E::G1Affine>,
31+
E::G1Affine: Serialize + for<'de> Deserialize<'de>,
32+
E::G2Affine: Serialize + for<'de> Deserialize<'de>,
33+
{
34+
type CommitmentKey = UVUniversalKZGParam<E>;
35+
type Commitment = Commitment<E::G1>;
36+
37+
fn setup(label: &'static [u8], n: usize) -> Self::CommitmentKey {
38+
// TODO: this is just for testing, replace by grabbing from a real setup for production
39+
let mut bytes = [0u8; 32];
40+
let len = label.len().min(32);
41+
bytes[..len].copy_from_slice(&label[..len]);
42+
let rng = &mut StdRng::from_seed(bytes);
43+
UVUniversalKZGParam::gen_srs_for_testing(rng, n.next_power_of_two())
44+
}
45+
46+
fn commit(ck: &Self::CommitmentKey, v: &[<E::G1 as Group>::Scalar]) -> Self::Commitment {
47+
assert!(ck.length() >= v.len());
48+
Commitment {
49+
comm: E::G1::vartime_multiscalar_mul(v, &ck.powers_of_g[..v.len()]),
50+
}
51+
}
52+
}
53+
54+
impl<E: Engine> From<Commitment<E::G1>> for UVKZGCommitment<E>
55+
where
56+
E::G1: Group,
57+
{
58+
fn from(c: Commitment<E::G1>) -> Self {
59+
UVKZGCommitment(c.comm.to_affine())
60+
}
61+
}
62+
63+
impl<E: Engine> From<UVKZGCommitment<E>> for Commitment<E::G1>
64+
where
65+
E::G1: Group,
66+
{
67+
fn from(c: UVKZGCommitment<E>) -> Self {
68+
Commitment {
69+
comm: c.0.to_curve(),
70+
}
71+
}
72+
}

src/provider/mod.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod poseidon;
1313
pub mod secp_secq;
1414

1515
// a non-hiding variant of {kzg, zeromorph}
16+
pub mod kzg_commitment;
1617
pub mod non_hiding_kzg;
1718
pub mod non_hiding_zeromorph;
1819

@@ -156,7 +157,24 @@ macro_rules! impl_traits {
156157
$name_compressed:ident,
157158
$name_curve:ident,
158159
$name_curve_affine:ident,
159-
$order_str:literal
160+
$order_str:expr
161+
) => {
162+
impl_traits!(
163+
$name,
164+
$name_compressed,
165+
$name_curve,
166+
$name_curve_affine,
167+
$order_str,
168+
CommitmentEngine<Self>
169+
);
170+
};
171+
(
172+
$name:ident,
173+
$name_compressed:ident,
174+
$name_curve:ident,
175+
$name_curve_affine:ident,
176+
$order_str:literal,
177+
$commitment_engine:ty
160178
) => {
161179
impl Group for $name::Point {
162180
type Base = $name::Base;
@@ -166,7 +184,7 @@ macro_rules! impl_traits {
166184
type RO = PoseidonRO<Self::Base, Self::Scalar>;
167185
type ROCircuit = PoseidonROCircuit<Self::Base>;
168186
type TE = Keccak256Transcript<Self>;
169-
type CE = CommitmentEngine<Self>;
187+
type CE = $commitment_engine;
170188

171189
fn vartime_multiscalar_mul(
172190
scalars: &[Self::Scalar],

0 commit comments

Comments
 (0)