Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zeromorph EvaluationEngine trait #71

Merged
merged 17 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ cfg-if = "1.0.0"
once_cell = "1.18.0"
anyhow = "1.0.72"
rand = "0.8.4"
rand_xorshift = "0.3.0"

[target.'cfg(any(target_arch = "x86_64", target_arch = "aarch64"))'.dependencies]
pasta-msm = { git="https://github.com/lurk-lab/pasta-msm", branch="dev", version = "0.1.4" }
Expand Down
19 changes: 19 additions & 0 deletions ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,22 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

------------------------------------------------------------
https://github.com/AztecProtocol/aztec-packages/

Licensed under Apache 2.0

Copyright 2022 Aztec

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2 changes: 1 addition & 1 deletion examples/minroot_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ fn main() {
MinRootCircuit<<G1 as Group>::Scalar>,
TrivialCircuit<<G2 as Group>::Scalar>,
>::new(&circuit_primary, &circuit_secondary, None, None);
assert!(result.clone() == pp, "not equal!");
assert!(*result == pp, "not equal!");
assert!(remaining.is_empty());
} else {
println!("Something terrible happened");
Expand Down
40 changes: 17 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,13 +948,10 @@ mod tests {
use core::fmt::Write;

use super::*;
#[allow(dead_code)]
type ZM<E> = provider::non_hiding_zeromorph::ZMEvaluation<E>;
type ZM<E> = provider::non_hiding_zeromorph::ZMPCS<E>;
type EE<G> = provider::ipa_pc::EvaluationEngine<G>;
type S<G, EE> = spartan::snark::RelaxedR1CSSNARK<G, EE>;
type SPrime<G, EE> = spartan::ppsnark::RelaxedR1CSSNARK<G, EE>;
#[allow(dead_code)]
type SZM<G1, E> = spartan::snark::RelaxedR1CSSNARK<G1, ZM<E>>;

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

test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _, EE<_>, EE<_>>(
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _, ZM<halo2curves::bn256::Bn256>, EE<_>>(
&trivial_circuit1_grumpkin,
&trivial_circuit2_grumpkin,
"184d05f08dca260f010cb48c6cf8c5eb61dedfc270e5a18226eb622cf7da0203",
);
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _, EE<_>, EE<_>>(
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _, ZM<halo2curves::bn256::Bn256>, EE<_>>(
&cubic_circuit1_grumpkin,
&trivial_circuit2_grumpkin,
"2fb992932b2a642b4ce8f52646a7ef6a5a486682716cf969df50021107afff03",
Expand Down Expand Up @@ -1306,8 +1303,7 @@ mod tests {
let (pk, vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&pp).unwrap();

// produce a compressed SNARK
let res =
CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &pk, &recursive_snark);
let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &pk, &recursive_snark);
assert!(res.is_ok());
let compressed_snark = res.unwrap();

Expand All @@ -1330,7 +1326,7 @@ mod tests {
test_ivc_nontrivial_with_compression_with::<
bn256::Point,
grumpkin::Point,
S<bn256::Point, EE<_>>, // SZM<bn256::Point, halo2curves::bn256::Bn256>,
S<bn256::Point, ZM<halo2curves::bn256::Bn256>>,
Copy link
Contributor Author

@huitseeker huitseeker Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This translates the former test_ivc_nontrivial_with_zm_compression

S<grumpkin::Point, EE<_>>,
>();
test_ivc_nontrivial_with_compression_with::<
Expand All @@ -1341,17 +1337,6 @@ mod tests {
>();
}

#[test]
#[ignore]
fn test_ivc_nontrivial_with_zm_compression() {
test_ivc_nontrivial_with_compression_with::<
bn256::Point,
grumpkin::Point,
S<bn256::Point, EE<_>>, // SZM<bn256::Point, halo2curves::bn256::Bn256>,
S<grumpkin::Point, EE<_>>,
>();
}

fn test_ivc_nontrivial_with_spark_compression_with<G1, G2, E1, E2>()
where
G1: Group<Base = <G2 as Group>::Scalar>,
Expand Down Expand Up @@ -1456,8 +1441,12 @@ mod tests {
type G2 = pasta_curves::vesta::Point;

test_ivc_nontrivial_with_spark_compression_with::<G1, G2, EE<_>, EE<_>>();
test_ivc_nontrivial_with_spark_compression_with::<bn256::Point, grumpkin::Point, EE<_>, EE<_>>(
);
test_ivc_nontrivial_with_spark_compression_with::<
bn256::Point,
grumpkin::Point,
ZM<halo2curves::bn256::Bn256>,
EE<_>,
>();
test_ivc_nontrivial_with_spark_compression_with::<
secp256k1::Point,
secq256k1::Point,
Expand Down Expand Up @@ -1610,7 +1599,12 @@ mod tests {
type G2 = pasta_curves::vesta::Point;

test_ivc_nondet_with_compression_with::<G1, G2, EE<_>, EE<_>>();
test_ivc_nondet_with_compression_with::<bn256::Point, grumpkin::Point, EE<_>, EE<_>>();
test_ivc_nondet_with_compression_with::<
bn256::Point,
grumpkin::Point,
ZM<halo2curves::bn256::Bn256>,
EE<_>,
>();
test_ivc_nondet_with_compression_with::<secp256k1::Point, secq256k1::Point, EE<_>, EE<_>>();
}

Expand Down
5 changes: 4 additions & 1 deletion src/provider/bn256_grumpkin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use halo2curves::grumpkin::{
G1Affine as GrumpkinAffine, G1Compressed as GrumpkinCompressed, G1 as GrumpkinPoint,
};

use super::kzg_commitment::KZGCommitmentEngine;

/// Re-exports that give access to the standard aliases used in the code base, for bn256
pub mod bn256 {
pub use halo2curves::bn256::{
Expand Down Expand Up @@ -58,7 +60,8 @@ impl_traits!(
Bn256Compressed,
Bn256Point,
Bn256Affine,
"30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"
"30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
KZGCommitmentEngine<halo2curves::bn256::Bn256>
);

impl_traits!(
Expand Down
72 changes: 72 additions & 0 deletions src/provider/kzg_commitment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! Commitment engine for KZG commitments
//!

use std::marker::PhantomData;

use group::{prime::PrimeCurveAffine, Curve};
use pairing::Engine;
use rand::rngs::StdRng;
use rand_core::SeedableRng;
use serde::{Deserialize, Serialize};

use crate::traits::{
commitment::{CommitmentEngineTrait, Len},
Group,
};

use super::{
non_hiding_kzg::{UVKZGCommitment, UVUniversalKZGParam},
pedersen::Commitment,
};

/// Provides a commitment engine
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct KZGCommitmentEngine<E: Engine> {
_p: PhantomData<E>,
}

impl<E: Engine> CommitmentEngineTrait<E::G1> for KZGCommitmentEngine<E>
where
E::G1: Group<PreprocessedGroupElement = E::G1Affine>,
E::G1Affine: Serialize + for<'de> Deserialize<'de>,
E::G2Affine: Serialize + for<'de> Deserialize<'de>,
{
type CommitmentKey = UVUniversalKZGParam<E>;
type Commitment = Commitment<E::G1>;

fn setup(label: &'static [u8], n: usize) -> Self::CommitmentKey {
// TODO: this is just for testing, replace by grabbing from a real setup for production
let mut bytes = [0u8; 32];
let len = label.len().min(32);
bytes[..len].copy_from_slice(&label[..len]);
let rng = &mut StdRng::from_seed(bytes);
UVUniversalKZGParam::gen_srs_for_testing(rng, n.next_power_of_two())
}

fn commit(ck: &Self::CommitmentKey, v: &[<E::G1 as Group>::Scalar]) -> Self::Commitment {
assert!(ck.length() >= v.len());
Commitment {
comm: E::G1::vartime_multiscalar_mul(v, &ck.powers_of_g[..v.len()]),
}
}
}

impl<E: Engine> From<Commitment<E::G1>> for UVKZGCommitment<E>
where
E::G1: Group,
{
fn from(c: Commitment<E::G1>) -> Self {
UVKZGCommitment(c.comm.to_affine())
}
}

impl<E: Engine> From<UVKZGCommitment<E>> for Commitment<E::G1>
where
E::G1: Group,
{
fn from(c: UVKZGCommitment<E>) -> Self {
Commitment {
comm: c.0.to_curve(),
}
}
}
22 changes: 20 additions & 2 deletions src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod poseidon;
pub mod secp_secq;

// a non-hiding variant of {kzg, zeromorph}
pub mod kzg_commitment;
pub mod non_hiding_kzg;
pub mod non_hiding_zeromorph;

Expand Down Expand Up @@ -156,7 +157,24 @@ macro_rules! impl_traits {
$name_compressed:ident,
$name_curve:ident,
$name_curve_affine:ident,
$order_str:literal
$order_str:expr
) => {
impl_traits!(
$name,
$name_compressed,
$name_curve,
$name_curve_affine,
$order_str,
CommitmentEngine<Self>
);
};
(
$name:ident,
$name_compressed:ident,
$name_curve:ident,
$name_curve_affine:ident,
$order_str:literal,
$commitment_engine:ty
) => {
impl Group for $name::Point {
type Base = $name::Base;
Expand All @@ -166,7 +184,7 @@ macro_rules! impl_traits {
type RO = PoseidonRO<Self::Base, Self::Scalar>;
type ROCircuit = PoseidonROCircuit<Self::Base>;
type TE = Keccak256Transcript<Self>;
type CE = CommitmentEngine<Self>;
type CE = $commitment_engine;

fn vartime_multiscalar_mul(
scalars: &[Self::Scalar],
Expand Down
Loading
Loading