Skip to content

Commit

Permalink
Merge pull request #179 from KogarashiNetwork/feature/nova-gadget
Browse files Browse the repository at this point in the history
init NIFS circuit and gadget
  • Loading branch information
ashWhiteHat authored Nov 24, 2023
2 parents 17fdce1 + 78e675a commit 06041d8
Show file tree
Hide file tree
Showing 52 changed files with 856 additions and 452 deletions.
15 changes: 1 addition & 14 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ members = [
"groth16",
"grumpkin",
"nova",
"r1cs",
"zkstd",
]

Expand Down
2 changes: 1 addition & 1 deletion groth16/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "katex-header.html"]

[dependencies]
zkstd = { path = "../zkstd", default-features = false }
r1cs = { path = "../r1cs", default-features = false }
bn-254 = { path = "../bn254", default-features = false }
grumpkin = { path = "../grumpkin", default-features = false }
rayon = { version = "1.5.1", optional = true }

[features]
Expand Down
3 changes: 2 additions & 1 deletion groth16/src/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::Error;

use r1cs::{GrumpkinDriver, R1cs};
use grumpkin::driver::GrumpkinDriver;
use zkstd::circuit::prelude::R1cs;
use zkstd::common::Debug;

/// circuit trait
Expand Down
5 changes: 3 additions & 2 deletions groth16/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ mod tests {
use crate::circuit::Circuit;
use crate::error::Error;
use crate::zksnark::ZkSnark;

use bn_254::Fr as BnScalar;
use r1cs::gadget::field::FieldAssignment;
use r1cs::{GrumpkinDriver, R1cs};
use grumpkin::driver::GrumpkinDriver;
use zkstd::circuit::prelude::{FieldAssignment, R1cs};
use zkstd::common::OsRng;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion groth16/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::poly::PointsValue;
use crate::proof::Proof;

use bn_254::Fr;
use r1cs::R1cs;
use zkstd::common::{CurveGroup, Group, RngCore};
use zkstd::r1cs::R1cs;

#[derive(Debug)]
pub struct Prover {
Expand Down
2 changes: 1 addition & 1 deletion groth16/src/zksnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::prover::Prover;
use crate::verifier::{Verifier, VerifyingKey};

use bn_254::{Fr, G1Affine, G2Affine};
use r1cs::R1cs;
use zkstd::common::{vec, Group, MulAssign, PrimeField, RngCore, Vec};
use zkstd::r1cs::R1cs;

/// Generate the arguments to prove and verify a circuit
pub struct ZkSnark {}
Expand Down
19 changes: 19 additions & 0 deletions grumpkin/src/driver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::params::PARAM_B3;
use bn_254::{Fq, Fr, G1Affine};
use zkstd::circuit::CircuitDriver;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct GrumpkinDriver;

impl CircuitDriver for GrumpkinDriver {
const NUM_BITS: u16 = 254;
type Affine = G1Affine;

type Base = Fq;

type Scalar = Fr;

fn b3() -> Self::Scalar {
PARAM_B3
}
}
3 changes: 2 additions & 1 deletion grumpkin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#![allow(clippy::suspicious_arithmetic_impl)]

mod curve;
pub mod driver;
pub mod params;

pub use curve::{Affine, Projective};
pub mod params;
1 change: 1 addition & 0 deletions grumpkin/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub const GENERATOR_Y: Fr = Fr::new_unchecked([
0xaa7b8cf435dfafbb,
0x14b34cf69dc25d68,
]);

// bn curve b param
pub(crate) const PARAM_B: Fr = Fr::new_unchecked([
0xdd7056026000005a,
Expand Down
2 changes: 1 addition & 1 deletion nova/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ categories = ["cryptography"]
keywords = ["zkp", "blockchain", "elliptic-curve"]

[dependencies]
r1cs = { path = "../r1cs", default-features = false }
zkstd = { path = "../zkstd", default-features = false }
bn-254 = { path = "../bn254", default-features = false }
grumpkin = { path = "../grumpkin", default-features = false }
serde = { version = "1.0.102", default-features = false, features = ["derive"] }
blake2b_simd = { version = "1", default-features = false }

Expand Down
2 changes: 2 additions & 0 deletions nova/src/circuit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod nifs;
mod transcript;
11 changes: 11 additions & 0 deletions nova/src/circuit/nifs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use core::marker::PhantomData;

use zkstd::circuit::prelude::{CircuitDriver, R1cs};

pub(crate) struct NifsCircuit<C: CircuitDriver> {
p: PhantomData<C>,
}

impl<C: CircuitDriver> NifsCircuit<C> {
pub(crate) fn verify(cs: &mut R1cs<C>) {}
}
46 changes: 7 additions & 39 deletions nova/src/hash/circuit.rs → nova/src/circuit/transcript.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
use crate::hash::Mimc;
use r1cs::gadget::curve::PointAssignment;
use r1cs::gadget::field::FieldAssignment;
use r1cs::{CircuitDriver, R1cs};
use zkstd::common::IntGroup;

pub(crate) struct MimcAssignment<const ROUND: usize, C: CircuitDriver> {
constants: [C::Scalar; ROUND],
}

impl<const ROUND: usize, C: CircuitDriver> Default for MimcAssignment<ROUND, C> {
fn default() -> Self {
Self {
constants: Mimc::<ROUND, C::Scalar>::default().constants,
}
}
}
use crate::gadget::MimcAssignment;

impl<const ROUND: usize, C: CircuitDriver> MimcAssignment<ROUND, C> {
pub(crate) fn hash(
&self,
cs: &mut R1cs<C>,
mut xl: FieldAssignment<C>,
mut xr: FieldAssignment<C>,
) -> FieldAssignment<C> {
for c in self.constants.iter().map(|c| FieldAssignment::constant(c)) {
let cxl = &xl + &c;
let mut ccxl = FieldAssignment::square(cs, &cxl);
ccxl = &FieldAssignment::mul(cs, &ccxl, &cxl) + &xr;
xr = xl;
xl = ccxl;
}
xl
}
}
use zkstd::circuit::prelude::{CircuitDriver, FieldAssignment, PointAssignment, R1cs};
use zkstd::common::IntGroup;

pub(crate) struct MimcROCircuit<const ROUND: usize, C: CircuitDriver> {
hasher: MimcAssignment<ROUND, C>,
Expand Down Expand Up @@ -71,14 +40,13 @@ impl<const ROUND: usize, C: CircuitDriver> MimcROCircuit<ROUND, C> {

#[cfg(test)]
mod tests {
use crate::hash::circuit::MimcROCircuit;
use super::MimcROCircuit;
use crate::hash::{MimcRO, MIMC_ROUNDS};

use bn_254::Fr;
use grumpkin::Affine;
use r1cs::gadget::curve::PointAssignment;
use r1cs::gadget::field::FieldAssignment;
use r1cs::{GrumpkinDriver, R1cs};
use grumpkin::{driver::GrumpkinDriver, Affine};
use rand_core::OsRng;
use zkstd::circuit::prelude::{FieldAssignment, PointAssignment, R1cs};
use zkstd::common::{CurveGroup, Group};

#[test]
Expand Down
3 changes: 2 additions & 1 deletion nova/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use r1cs::{CircuitDriver, DenseVectors};
use zkstd::circuit::prelude::CircuitDriver;
use zkstd::matrix::DenseVectors;

pub trait Function<C: CircuitDriver> {
fn invoke(z: &DenseVectors<C::Scalar>) -> DenseVectors<C::Scalar>;
Expand Down
4 changes: 4 additions & 0 deletions nova/src/gadget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod mimc;
mod relaxed_r1cs;

pub(crate) use mimc::MimcAssignment;
33 changes: 33 additions & 0 deletions nova/src/gadget/mimc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::hash::Mimc;

use zkstd::circuit::prelude::{CircuitDriver, FieldAssignment, R1cs};

pub(crate) struct MimcAssignment<const ROUND: usize, C: CircuitDriver> {
constants: [C::Scalar; ROUND],
}

impl<const ROUND: usize, C: CircuitDriver> Default for MimcAssignment<ROUND, C> {
fn default() -> Self {
Self {
constants: Mimc::<ROUND, C::Scalar>::default().constants,
}
}
}

impl<const ROUND: usize, C: CircuitDriver> MimcAssignment<ROUND, C> {
pub(crate) fn hash(
&self,
cs: &mut R1cs<C>,
mut xl: FieldAssignment<C>,
mut xr: FieldAssignment<C>,
) -> FieldAssignment<C> {
for c in self.constants.iter().map(|c| FieldAssignment::constant(c)) {
let cxl = &xl + &c;
let mut ccxl = FieldAssignment::square(cs, &cxl);
ccxl = &FieldAssignment::mul(cs, &ccxl, &cxl) + &xr;
xr = xl;
xl = ccxl;
}
xl
}
}
14 changes: 14 additions & 0 deletions nova/src/gadget/relaxed_r1cs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::relaxed_r1cs::RelaxedR1csInstance;

use zkstd::circuit::prelude::{CircuitDriver, FieldAssignment, PointAssignment, R1cs};

pub(crate) struct RelaxedR1csAssignment<C: CircuitDriver> {
pub(crate) commit_w: PointAssignment<C>,
pub(crate) commit_e: PointAssignment<C>,
pub(crate) u: FieldAssignment<C>,
pub(crate) x: Vec<FieldAssignment<C>>,
}

impl<C: CircuitDriver> RelaxedR1csAssignment<C> {
pub(crate) fn witness(cs: &mut R1cs<C>, relaxed_r1cs: RelaxedR1csInstance<C>) {}
}
3 changes: 1 addition & 2 deletions nova/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod circuit;
mod helper;

use helper::BlakeHelper;
Expand All @@ -9,7 +8,7 @@ use zkstd::common::{BNAffine, PrimeField};
pub(crate) const MIMC_ROUNDS: usize = 322;

pub(crate) struct Mimc<const ROUND: usize, F: PrimeField> {
constants: [F; ROUND],
pub(crate) constants: [F; ROUND],
}

impl<const ROUND: usize, F: PrimeField> Default for Mimc<ROUND, F> {
Expand Down
9 changes: 7 additions & 2 deletions nova/src/ivc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::function::Function;
use crate::proof::RecursiveProof;
use crate::{Prover, RelaxedR1cs};

use r1cs::{CircuitDriver, DenseVectors, R1cs};
use zkstd::circuit::prelude::{CircuitDriver, R1cs};
use zkstd::common::RngCore;
use zkstd::matrix::DenseVectors;

pub struct Ivc<C: CircuitDriver> {
i: usize,
Expand Down Expand Up @@ -71,8 +72,12 @@ impl<C: CircuitDriver> Ivc<C> {
mod tests {
use super::Ivc;
use crate::test::ExampleFunction;
use r1cs::{test::example_r1cs, DenseVectors, GrumpkinDriver, R1cs};

use grumpkin::driver::GrumpkinDriver;
use rand_core::OsRng;
use zkstd::circuit::prelude::R1cs;
use zkstd::matrix::DenseVectors;
use zkstd::r1cs::test::example_r1cs;

#[test]
fn ivc_test() {
Expand Down
5 changes: 3 additions & 2 deletions nova/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#![doc = include_str!("../README.md")]
#![allow(dead_code)]
#![allow(unused_variables, dead_code)]

mod circuit;
mod function;
mod gadget;
mod hash;
mod ivc;
mod pedersen;
mod proof;
mod prover;
mod relaxed_r1cs;

mod verifier;

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion nova/src/pedersen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use r1cs::DenseVectors;
use zkstd::common::{BNAffine, Group, RngCore};
use zkstd::matrix::DenseVectors;

pub struct PedersenCommitment<C: BNAffine> {
g: Vec<C>,
Expand Down
3 changes: 2 additions & 1 deletion nova/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::{
RelaxedR1cs,
};

use r1cs::{CircuitDriver, DenseVectors, R1cs};
use zkstd::circuit::prelude::{CircuitDriver, R1cs};
use zkstd::common::{Group, Ring};
use zkstd::matrix::DenseVectors;

#[allow(clippy::type_complexity)]
pub struct RecursiveProof<C: CircuitDriver> {
Expand Down
6 changes: 4 additions & 2 deletions nova/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::{
};

use crate::hash::{MimcRO, MIMC_ROUNDS};
use r1cs::{CircuitDriver, DenseVectors, R1cs};
use zkstd::circuit::prelude::{CircuitDriver, R1cs};
use zkstd::common::{Ring, RngCore};
use zkstd::matrix::DenseVectors;

pub struct Prover<C: CircuitDriver> {
// public parameters
Expand Down Expand Up @@ -88,8 +89,9 @@ impl<C: CircuitDriver> Prover<C> {
pub(crate) mod tests {
use super::{Prover, RelaxedR1cs};

use r1cs::{test::example_r1cs, GrumpkinDriver};
use grumpkin::driver::GrumpkinDriver;
use zkstd::common::OsRng;
use zkstd::r1cs::test::example_r1cs;

pub(crate) fn example_prover() -> Prover<GrumpkinDriver> {
let r1cs = example_r1cs(0);
Expand Down
Loading

0 comments on commit 06041d8

Please sign in to comment.