Skip to content

Commit

Permalink
More Nova forward ports (lurk-lang#135)
Browse files Browse the repository at this point in the history
* Small code improvement to the minroot example (lurk-lang#264)

about 10% improvement for the non-release mode

* Reduce duplicate code across different curve cycle providers (lurk-lang#255)

* refactor: impl folding macro

* refactor: generalize curve test

* chore: rename impl_folding to impl_engine

* reorganize provider module (lurk-lang#267)

---------

Co-authored-by: field-worker <[email protected]>
Co-authored-by: ashWhiteHat <[email protected]>
Co-authored-by: Srinath Setty <[email protected]>
  • Loading branch information
4 people authored Nov 27, 2023
1 parent c49c30f commit e985fc4
Show file tree
Hide file tree
Showing 28 changed files with 591 additions and 590 deletions.
2 changes: 1 addition & 1 deletion benches/compressed-snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::marker::PhantomData;
use criterion::*;
use ff::PrimeField;
use nova_snark::{
provider::pasta::{PallasEngine, VestaEngine},
provider::{PallasEngine, VestaEngine},
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::RelaxedR1CSSNARKTrait,
Expand Down
2 changes: 1 addition & 1 deletion benches/compute-digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bellpepper_core::{num::AllocatedNum, ConstraintSystem, SynthesisError};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use ff::PrimeField;
use nova_snark::{
provider::pasta::{PallasEngine, VestaEngine},
provider::{PallasEngine, VestaEngine},
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::default_ck_hint,
Expand Down
2 changes: 1 addition & 1 deletion benches/recursive-snark-supernova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::marker::PhantomData;
use criterion::*;
use ff::PrimeField;
use nova_snark::{
provider::pasta::{PallasEngine, VestaEngine},
provider::{PallasEngine, VestaEngine},
supernova::NonUniformCircuit,
supernova::{PublicParams, RecursiveSNARK},
traits::{
Expand Down
2 changes: 1 addition & 1 deletion benches/recursive-snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::marker::PhantomData;
use criterion::*;
use ff::PrimeField;
use nova_snark::{
provider::pasta::{PallasEngine, VestaEngine},
provider::{PallasEngine, VestaEngine},
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::default_ck_hint,
Expand Down
2 changes: 1 addition & 1 deletion benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use core::time::Duration;
use criterion::*;
use ff::{PrimeField, PrimeFieldBits};
use nova_snark::{
provider::pasta::{PallasEngine, VestaEngine},
provider::{PallasEngine, VestaEngine},
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::default_ck_hint,
Expand Down
14 changes: 8 additions & 6 deletions examples/minroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bellpepper_core::{num::AllocatedNum, ConstraintSystem, SynthesisError};
use ff::PrimeField;
use flate2::{write::ZlibEncoder, Compression};
use nova_snark::{
provider::pasta::{PallasEngine, VestaEngine},
provider::{PallasEngine, VestaEngine},
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::default_ck_hint,
Expand Down Expand Up @@ -47,10 +47,12 @@ impl<F: PrimeField> MinRootIteration<F> {
let x_i_plus_1 = (x_i + y_i).pow_vartime(exp.to_u64_digits()); // computes the fifth root of x_i + y_i

// sanity check
let sq = x_i_plus_1 * x_i_plus_1;
let quad = sq * sq;
let fifth = quad * x_i_plus_1;
debug_assert_eq!(fifth, x_i + y_i);
if cfg!(debug_assertions) {
let sq = x_i_plus_1 * x_i_plus_1;
let quad = sq * sq;
let fifth = quad * x_i_plus_1;
assert_eq!(fifth, x_i + y_i);
}

let y_i_plus_1 = x_i;

Expand Down Expand Up @@ -230,7 +232,7 @@ fn main() {
)
.unwrap();

for (i, circuit_primary) in minroot_circuits.iter().take(num_steps).enumerate() {
for (i, circuit_primary) in minroot_circuits.iter().enumerate() {
let start = Instant::now();
let res = recursive_snark.prove_step(&pp, circuit_primary, &circuit_secondary);
assert!(res.is_ok());
Expand Down
2 changes: 1 addition & 1 deletion examples/minroot_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use abomonation_derive::Abomonation;
use bellpepper_core::{num::AllocatedNum, ConstraintSystem, SynthesisError};
use ff::PrimeField;
use nova_snark::{
provider::pasta::{PallasEngine, VestaEngine},
provider::{PallasEngine, VestaEngine},
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::default_ck_hint,
Expand Down
2 changes: 1 addition & 1 deletion src/bellpepper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod tests {
shape_cs::ShapeCS,
solver::SatisfyingAssignment,
},
provider::{bn256_grumpkin::Bn256Engine, pasta::PallasEngine, secp_secq::Secp256k1Engine},
provider::{Bn256Engine, PallasEngine, Secp256k1Engine},
traits::{snark::default_ck_hint, Engine},
};
use bellpepper_core::{num::AllocatedNum, ConstraintSystem};
Expand Down
5 changes: 2 additions & 3 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,9 @@ mod tests {
constants::{BN_LIMB_WIDTH, BN_N_LIMBS},
gadgets::utils::scalar_as_base,
provider::{
bn256_grumpkin::{Bn256Engine, GrumpkinEngine},
pasta::{PallasEngine, VestaEngine},
poseidon::PoseidonConstantsCircuit,
secp_secq::{Secp256k1Engine, Secq256k1Engine},
{Bn256Engine, GrumpkinEngine}, {PallasEngine, VestaEngine},
{Secp256k1Engine, Secq256k1Engine},
},
traits::{circuit::TrivialCircuit, snark::default_ck_hint},
};
Expand Down
2 changes: 1 addition & 1 deletion src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<'a, F: PrimeField, T: Digestible> DigestComputer<'a, F, T> {
#[cfg(test)]
mod tests {
use super::{DigestComputer, SimpleDigestible};
use crate::{provider::pasta::PallasEngine, traits::Engine};
use crate::{provider::PallasEngine, traits::Engine};
use ff::Field;
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
Expand Down
6 changes: 3 additions & 3 deletions src/gadgets/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,9 +788,9 @@ mod tests {
{solver::SatisfyingAssignment, test_shape_cs::TestShapeCS},
},
provider::{
bn256_grumpkin::{bn256, grumpkin, Bn256Engine, GrumpkinEngine},
pasta::{PallasEngine, VestaEngine},
secp_secq::{secp256k1, secq256k1, Secp256k1Engine, Secq256k1Engine},
bn256_grumpkin::{bn256, grumpkin},
secp_secq::{secp256k1, secq256k1},
Bn256Engine, GrumpkinEngine, Secp256k1Engine, Secq256k1Engine, {PallasEngine, VestaEngine},
},
traits::snark::default_ck_hint,
};
Expand Down
8 changes: 3 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ where
/// ```rust
/// # use nova_snark::spartan::ppsnark::RelaxedR1CSSNARK;
/// # use nova_snark::provider::ipa_pc::EvaluationEngine;
/// # use nova_snark::provider::pasta::{PallasEngine, VestaEngine};
/// # use nova_snark::provider::{PallasEngine, VestaEngine};
/// # use nova_snark::traits::{circuit::TrivialCircuit, Engine, snark::RelaxedR1CSSNARKTrait};
/// use nova_snark::PublicParams;
///
Expand Down Expand Up @@ -945,10 +945,8 @@ mod tests {
use super::*;
use crate::{
provider::{
bn256_grumpkin::{Bn256Engine, GrumpkinEngine},
pasta::{PallasEngine, VestaEngine},
secp_secq::{Secp256k1Engine, Secq256k1Engine},
DlogGroup,
traits::DlogGroup, Bn256Engine, GrumpkinEngine, PallasEngine, Secp256k1Engine,
Secq256k1Engine, VestaEngine,
},
traits::{evaluation::EvaluationEngineTrait, snark::default_ck_hint},
};
Expand Down
2 changes: 1 addition & 1 deletion src/nifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ mod tests {
solver::SatisfyingAssignment,
test_shape_cs::TestShapeCS,
},
provider::{bn256_grumpkin::Bn256Engine, pasta::PallasEngine, secp_secq::Secp256k1Engine},
provider::{Bn256Engine, PallasEngine, Secp256k1Engine},
r1cs::{commitment_key, SparseMatrix},
traits::{snark::default_ck_hint, Engine},
};
Expand Down
61 changes: 5 additions & 56 deletions src/provider/bn256_grumpkin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
use crate::{
impl_traits,
provider::{
cpu_best_multiexp,
keccak::Keccak256Transcript,
pedersen::CommitmentEngine,
poseidon::{PoseidonRO, PoseidonROCircuit},
CompressedGroup, DlogGroup,
msm::cpu_best_msm,
traits::{CompressedGroup, DlogGroup},
},
traits::{Engine, Group, PrimeFieldExt, TranscriptReprTrait},
traits::{Group, PrimeFieldExt, TranscriptReprTrait},
};
use digest::{ExtendableOutput, Update};
use ff::{FromUniformBytes, PrimeField};
Expand All @@ -30,28 +27,15 @@ use halo2curves::grumpkin::{

/// Re-exports that give access to the standard aliases used in the code base, for bn256
pub mod bn256 {
pub use halo2curves::bn256::{
Fq as Base, Fr as Scalar, G1Affine as Affine, G1Compressed as Compressed, G1 as Point,
};
pub use halo2curves::bn256::{Fq as Base, Fr as Scalar, G1Affine as Affine, G1 as Point};
}

/// Re-exports that give access to the standard aliases used in the code base, for grumpkin
pub mod grumpkin {
pub use halo2curves::grumpkin::{
Fq as Base, Fr as Scalar, G1Affine as Affine, G1Compressed as Compressed, G1 as Point,
};
pub use halo2curves::grumpkin::{Fq as Base, Fr as Scalar, G1Affine as Affine, G1 as Point};
}

/// An implementation of the Nova `Engine` trait with BN254 curve and Pedersen commitment scheme
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Bn256Engine;

/// An implementation of the Nova `Engine` trait with Grumpkin curve and Pedersen commitment scheme
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GrumpkinEngine;

impl_traits!(
Bn256Engine,
bn256,
Bn256Compressed,
Bn256Point,
Expand All @@ -61,45 +45,10 @@ impl_traits!(
);

impl_traits!(
GrumpkinEngine,
grumpkin,
GrumpkinCompressed,
GrumpkinPoint,
GrumpkinAffine,
"30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47",
"30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"
);

#[cfg(test)]
mod tests {
use super::*;
type G = bn256::Point;

fn from_label_serial(label: &'static [u8], n: usize) -> Vec<Bn256Affine> {
let mut shake = Shake256::default();
shake.update(label);
let mut reader = shake.finalize_xof();
let mut ck = Vec::new();
for _ in 0..n {
let mut uniform_bytes = [0u8; 32];
reader.read_exact(&mut uniform_bytes).unwrap();
let hash = bn256::Point::hash_to_curve("from_uniform_bytes");
ck.push(hash(&uniform_bytes).to_affine());
}
ck
}

#[test]
fn test_from_label() {
let label = b"test_from_label";
for n in [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1021,
] {
let ck_par = <G as DlogGroup>::from_label(label, n);
let ck_ser = from_label_serial(label, n);
assert_eq!(ck_par.len(), n);
assert_eq!(ck_ser.len(), n);
assert_eq!(ck_par, ck_ser);
}
}
}
2 changes: 1 addition & 1 deletion src/provider/ipa_pc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module implements `EvaluationEngine` using an IPA-based polynomial commitment scheme
use crate::{
errors::NovaError,
provider::{pedersen::CommitmentKeyExtTrait, DlogGroup},
provider::{pedersen::CommitmentKeyExtTrait, traits::DlogGroup},
spartan::polys::eq::EqPolynomial,
traits::{
commitment::{CommitmentEngineTrait, CommitmentTrait},
Expand Down
4 changes: 1 addition & 3 deletions src/provider/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ mod tests {
use crate::{
provider::keccak::Keccak256Transcript,
provider::{
bn256_grumpkin::{Bn256Engine, GrumpkinEngine},
pasta::{PallasEngine, VestaEngine},
secp_secq::{Secp256k1Engine, Secq256k1Engine},
Bn256Engine, GrumpkinEngine, PallasEngine, Secp256k1Engine, Secq256k1Engine, VestaEngine,
},
traits::{Engine, PrimeFieldExt, TranscriptEngineTrait, TranscriptReprTrait},
};
Expand Down
Loading

0 comments on commit e985fc4

Please sign in to comment.