Skip to content

Commit

Permalink
chore: update nova-snark to official 0.35 (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanKung authored Mar 29, 2024
1 parent 7500ff5 commit ce2973a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 48 deletions.
42 changes: 26 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/node/src/backend/snark/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub fn bigint_to_field(v: js_sys::BigInt, field: SupportedPrimeField) -> Result<
}
}
SupportedPrimeField::Bn256KZG => {
type F = <provider::hyperkzg::Bn256EngineKZG as Engine>::Scalar;
type F = <provider::Bn256EngineKZG as Engine>::Scalar;
Field {
value: FieldEnum::Bn256KZG(bigint2ff::<F>(v)?),
}
Expand Down
36 changes: 15 additions & 21 deletions crates/node/src/backend/snark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub enum CircuitGenerator {
/// Circuit based on pallas curve
Pallas(circuit::WasmCircuitGenerator<<provider::PallasEngine as Engine>::Scalar>),
/// Circuit based on KZG bn256
Bn256KZG(circuit::WasmCircuitGenerator<<provider::hyperkzg::Bn256EngineKZG as Engine>::Scalar>),
Bn256KZG(circuit::WasmCircuitGenerator<<provider::Bn256EngineKZG as Engine>::Scalar>),
}

/// Supported prime field
Expand Down Expand Up @@ -220,7 +220,7 @@ pub enum CircuitEnum {
/// Based on pallas curve
Pallas(circuit::Circuit<<provider::PallasEngine as Engine>::Scalar>),
/// based on bn256 and KZG
Bn256KZG(circuit::Circuit<<provider::hyperkzg::Bn256EngineKZG as Engine>::Scalar>),
Bn256KZG(circuit::Circuit<<provider::Bn256EngineKZG as Engine>::Scalar>),
}

#[wasm_export]
Expand Down Expand Up @@ -251,7 +251,7 @@ pub enum FieldEnum {
/// field of pallas curve
Pallas(<provider::PallasEngine as Engine>::Scalar),
/// bn256 with kzg
Bn256KZG(<provider::hyperkzg::Bn256EngineKZG as Engine>::Scalar),
Bn256KZG(<provider::Bn256EngineKZG as Engine>::Scalar),
}

#[wasm_export]
Expand All @@ -266,9 +266,7 @@ impl Field {
value: FieldEnum::Pallas(<provider::PallasEngine as Engine>::Scalar::from(v)),
},
SupportedPrimeField::Bn256KZG => Self {
value: FieldEnum::Bn256KZG(
<provider::hyperkzg::Bn256EngineKZG as Engine>::Scalar::from(v),
),
value: FieldEnum::Bn256KZG(<provider::Bn256EngineKZG as Engine>::Scalar::from(v)),
},
}
}
Expand Down Expand Up @@ -316,7 +314,7 @@ impl SNARKTaskBuilder {
})
}
SupportedPrimeField::Bn256KZG => {
type F = <provider::hyperkzg::Bn256EngineKZG as Engine>::Scalar;
type F = <provider::Bn256EngineKZG as Engine>::Scalar;
let r1cs =
r1cs::load_r1cs::<F>(r1cs::Path::Local(r1cs_path), r1cs::Format::Bin).await?;
let witness_calculator =
Expand Down Expand Up @@ -365,7 +363,7 @@ impl SNARKTaskBuilder {
})
}
SupportedPrimeField::Bn256KZG => {
type F = <provider::hyperkzg::Bn256EngineKZG as Engine>::Scalar;
type F = <provider::Bn256EngineKZG as Engine>::Scalar;
let r1cs =
r1cs::load_r1cs::<F>(r1cs::Path::Remote(r1cs_path), r1cs::Format::Bin).await?;
let witness_calculator =
Expand Down Expand Up @@ -495,7 +493,7 @@ impl SNARKTaskBuilder {
Ok(circuits)
}
CircuitGenerator::Bn256KZG(g) => {
type F = <provider::hyperkzg::Bn256EngineKZG as Engine>::Scalar;
type F = <provider::Bn256EngineKZG as Engine>::Scalar;

let input: circuit::Input<F> = public_input
.into_iter()
Expand Down Expand Up @@ -573,7 +571,7 @@ impl SNARKTaskBuilder {
})
.collect();
let inputs = circuits[0].get_public_inputs();
let pp = SNARK::<E1, E2>::gen_pp::<S1, S2>(circuits[0].clone());
let pp = SNARK::<E1, E2>::gen_pp::<S1, S2>(circuits[0].clone())?;
let snark = SNARK::<E1, E2>::new(&circuits[0], &pp, &inputs, &vec![
<E2 as Engine>::Scalar::from(0),
])?;
Expand Down Expand Up @@ -602,7 +600,7 @@ impl SNARKTaskBuilder {
})
.collect();
let inputs = circuits[0].get_public_inputs();
let pp = SNARK::<E1, E2>::gen_pp::<S1, S2>(circuits[0].clone());
let pp = SNARK::<E1, E2>::gen_pp::<S1, S2>(circuits[0].clone())?;
let snark = SNARK::<E1, E2>::new(&circuits[0], &pp, &inputs, &vec![
<E2 as Engine>::Scalar::from(0),
])?;
Expand All @@ -613,7 +611,7 @@ impl SNARKTaskBuilder {
})
}
CircuitEnum::Bn256KZG(_) => {
type E1 = provider::hyperkzg::Bn256EngineKZG;
type E1 = provider::Bn256EngineKZG;
type E2 = provider::GrumpkinEngine;
type EE1 = hyperkzg::EvaluationEngine<E1>;
type EE2 = ipa_pc::EvaluationEngine<E2>;
Expand All @@ -630,7 +628,7 @@ impl SNARKTaskBuilder {
})
.collect();
let inputs = circuits[0].get_public_inputs();
let pp = SNARK::<E1, E2>::gen_pp::<S1, S2>(circuits[0].clone());
let pp = SNARK::<E1, E2>::gen_pp::<S1, S2>(circuits[0].clone())?;
let snark = SNARK::<E1, E2>::new(&circuits[0], &pp, &inputs, &vec![
<E2 as Engine>::Scalar::from(0),
])?;
Expand Down Expand Up @@ -816,7 +814,7 @@ impl SNARKBehaviour {
Ok(SNARKVerifyTask::PallasVasta(serde_json::to_string(&proof)?))
}
SNARKProofTask::Bn256KZGGrumpkin(s) => {
type E1 = provider::hyperkzg::Bn256EngineKZG;
type E1 = provider::Bn256EngineKZG;
type E2 = provider::GrumpkinEngine;
type EE1 = hyperkzg::EvaluationEngine<E1>;
type EE2 = ipa_pc::EvaluationEngine<E2>;
Expand Down Expand Up @@ -878,7 +876,7 @@ impl SNARKBehaviour {
}
}
SNARKVerifyTask::Bn256KZGGrumpkin(p) => {
type E1 = provider::hyperkzg::Bn256EngineKZG;
type E1 = provider::Bn256EngineKZG;
type E2 = provider::GrumpkinEngine;
type EE1 = hyperkzg::EvaluationEngine<E1>;
type EE2 = ipa_pc::EvaluationEngine<E2>;
Expand Down Expand Up @@ -910,12 +908,8 @@ impl From<SNARKGenerator<provider::VestaEngine, provider::PallasEngine>> for SNA
}
}

impl From<SNARKGenerator<provider::hyperkzg::Bn256EngineKZG, provider::GrumpkinEngine>>
for SNARKProofTask
{
fn from(
snark: SNARKGenerator<provider::hyperkzg::Bn256EngineKZG, provider::GrumpkinEngine>,
) -> Self {
impl From<SNARKGenerator<provider::Bn256EngineKZG, provider::GrumpkinEngine>> for SNARKProofTask {
fn from(snark: SNARKGenerator<provider::Bn256EngineKZG, provider::GrumpkinEngine>) -> Self {
Self::Bn256KZGGrumpkin(snark)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/node/src/backend/types/snark.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Backend message types for SNARK
//! ==============================
use rings_snark::prelude::nova::provider::hyperkzg::Bn256EngineKZG;
use rings_snark::prelude::nova::provider::Bn256EngineKZG;
use rings_snark::prelude::nova::provider::GrumpkinEngine;
use rings_snark::prelude::nova::provider::PallasEngine;
use rings_snark::prelude::nova::provider::VestaEngine;
Expand Down
4 changes: 3 additions & 1 deletion crates/snark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ llvm = ["wasmer-compiler-llvm"]

[dependencies]
# https://github.com/microsoft/Nova
nova-snark = { git = "https://github.com/RingsNetwork/Rings-Nova", version = "0.34", rev = "95a65981b009dceb2f6b0bf2fe706110ac4d2a0e" }
# nova-snark = { git = "https://github.com/RingsNetwork/Rings-Nova", version = "0.34", rev = "95a65981b009dceb2f6b0bf2fe706110ac4d2a0e" }

ff = { version = "0.13", features = ["derive"] }

Expand All @@ -40,10 +40,12 @@ thiserror = "1"
eyre = "0.6.11"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
nova-snark = { version = "0.35" }
wasmer = "4.2.5"
wasmer-compiler-llvm = { version = "4.1.1", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
nova-snark = { version = "0.35", features = ["portable"], default-features = false }
wasmer = { version = "4.2.5", features = ["js-default"], default-features = false }

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
Expand Down
8 changes: 4 additions & 4 deletions crates/snark/src/snark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,20 @@ where
{
/// Create public params
#[inline]
pub fn gen_pp<S1, S2>(circom: Circuit<E1::Scalar>) -> PublicParams<E1, E2>
pub fn gen_pp<S1, S2>(circom: Circuit<E1::Scalar>) -> Result<PublicParams<E1, E2>>
where
S1: RelaxedR1CSSNARKTrait<E1>,
S2: RelaxedR1CSSNARKTrait<E2>,
{
let circuit_primary = circom.clone();
let circuit_secondary = TrivialCircuit::<E2::Scalar>::default();
nova::PublicParams::setup(
let pp = nova::PublicParams::setup(
&circuit_primary,
&circuit_secondary,
S1::ck_floor().deref(),
S2::ck_floor().deref(),
)
.into()
)?;
Ok(pp.into())
}

/// Create public params with circom, and public input
Expand Down
4 changes: 2 additions & 2 deletions crates/snark/src/tests/native/test_snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn test_calcu_sha256_recursive_snark() -> Result<()> {

assert_eq!(recursive_circuits.len(), round);

let pp = snark::SNARK::<E1, E2>::gen_pp::<S1, S2>(public_circuit.clone());
let pp = snark::SNARK::<E1, E2>::gen_pp::<S1, S2>(public_circuit.clone()).unwrap();
print_mem_status(Some("after gen pp"));
let mut rec_snark = snark::SNARK::<E1, E2>::new(
&recursive_circuits[0],
Expand Down Expand Up @@ -160,7 +160,7 @@ pub async fn test_calcu_bn256_recursive_snark_with_private_input() -> Result<()>

assert_eq!(recursive_circuits.len(), 3);
// init pp with ouptn inputs
let pp = snark::SNARK::<E1, E2>::gen_pp::<S1, S2>(circuit_0.clone());
let pp = snark::SNARK::<E1, E2>::gen_pp::<S1, S2>(circuit_0.clone()).unwrap();
let mut rec_snark_iter = snark::SNARK::<E1, E2>::new(
&recursive_circuits[0].clone(),
&pp,
Expand Down
2 changes: 1 addition & 1 deletion examples/snark/bn256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async fn main() {

assert_eq!(recursive_circuits.len(), 3);
// init pp with ouptn inputs
let pp = snark::SNARK::<E1, E2>::gen_pp::<S1, S2>(circuit_0.clone());
let pp = snark::SNARK::<E1, E2>::gen_pp::<S1, S2>(circuit_0.clone()).unwrap();
let mut rec_snark_iter = snark::SNARK::<E1, E2>::new(
&recursive_circuits[0].clone(),
&pp,
Expand Down
2 changes: 1 addition & 1 deletion examples/snark/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub async fn merkle_tree_path_proof() {

let start = Instant::now();
// init pp with ouptn inputs
let pp = snark::SNARK::<E1, E2>::gen_pp::<S1, S2>(circuit_0.clone());
let pp = snark::SNARK::<E1, E2>::gen_pp::<S1, S2>(circuit_0.clone()).unwrap();
println!("gen public parasm, took {:?} ", start.elapsed());

let start = Instant::now();
Expand Down

0 comments on commit ce2973a

Please sign in to comment.