Skip to content

Commit

Permalink
primary nifs
Browse files Browse the repository at this point in the history
  • Loading branch information
PayneJoe committed Dec 14, 2023
1 parent 08fefc5 commit 4544fdf
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 36 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod error;
pub mod nifs;
pub mod poseidon;
pub mod primary;
pub mod secondary;
32 changes: 0 additions & 32 deletions src/nifs.rs

This file was deleted.

16 changes: 15 additions & 1 deletion src/primary/bn254_field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//// configurations for bn254 curve/field
use crate::poseidon::poseidon_constant::{PoseidonDefaultConfig, PoseidonDefaultConfigEntry};
use ark_ff::{fields::models::*, MontBackend, MontConfig};
use ark_crypto_primitives::sponge::Absorb;
use ark_ff::{fields::models::*, MontBackend, MontConfig, PrimeField};

/// for bn254 base field Fq
#[derive(MontConfig)]
Expand All @@ -11,6 +12,19 @@ pub struct FqBackend;
type FqConfig = MontBackend<FqBackend, 4>;
pub type Fq = Fp256<FqConfig>;

// pub struct BaseField(Fq);

// impl Absorb for BaseField {
// // convert BaseField into bytes
// fn to_sponge_bytes(&self, dest: &mut Vec<u8>) {
// todo!()
// }
// // convert BaseField into ScalarField
// fn to_sponge_field_elements<F: PrimeField>(&self, dest: &mut Vec<F>) {
// todo!()
// }
// }

impl PoseidonDefaultConfig<4> for FqConfig {
const PARAMS_OPT_FOR_CONSTRAINTS: [PoseidonDefaultConfigEntry; 7] = [
PoseidonDefaultConfigEntry::new(2, 17, 8, 31, 0),
Expand Down
1 change: 1 addition & 0 deletions src/primary/circuit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// primary circuit implementation based BN254 curve
2 changes: 2 additions & 0 deletions src/primary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod bn254_curve;
pub mod bn254_field;
pub mod circuit;
pub mod kzg;
pub mod nifs;
pub mod plonk;
81 changes: 81 additions & 0 deletions src/primary/nifs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/// Non-interactive Folding Scheme based Plonkish Nova over BN254 curve
///
///
use ark_crypto_primitives::sponge::poseidon::PoseidonSponge;
use ark_crypto_primitives::sponge::CryptographicSponge;
use ark_ec::pairing::Pairing;
use ark_ff::{BigInt, BigInteger};
use ark_ff::{Field, PrimeField};
use jf_primitives::pcs::prelude::Commitment;

use std::marker::PhantomData;

use super::bn254_field::Fq;
use super::plonk::{
CommitmentKey, PLONKInstance, PLONKShape, PLONKWitness, RelaxedPLONKInstance,
RelaxedPLONKWitness,
};
use crate::error::MyError;
use crate::poseidon::poseidon_constant::PoseidonDefaultConfigField;

pub struct NIFS<E: Pairing> {
pub(crate) comm_T: Commitment<E>,
_p: PhantomData<E>,
}

impl<E: Pairing> NIFS<E> {
pub fn prove(
ck: &CommitmentKey<E>,
pp_digest: &E::ScalarField,
S: &PLONKShape<E>,
U1: &RelaxedPLONKInstance<E>,
W1: &RelaxedPLONKWitness<E>,
U2: &PLONKInstance<E>,
W2: &PLONKWitness<E>,
) -> Result<(NIFS<E>, (RelaxedPLONKInstance<E>, RelaxedPLONKWitness<E>)), MyError> {
let sponge_constant = Fq::get_default_poseidon_parameters(3, false).unwrap();
let mut sponge = PoseidonSponge::<Fq>::new(&sponge_constant);
todo!()
}
pub fn verifiy(
&self,
pp_digest: &E::ScalarField,
U1: &RelaxedPLONKInstance<E>,
U2: &PLONKInstance<E>,
) -> Result<RelaxedPLONKInstance<E>, MyError> {
todo!()
}
}

#[cfg(test)]
mod tests {
use ark_ec::pairing::Pairing;
use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial};
use jf_primitives::pcs::{
prelude::{UnivariateKzgPCS, UnivariateUniversalParams},
PolynomialCommitmentScheme, StructuredReferenceString,
};

use crate::primary::kzg::gen_srs_for_testing;
use ark_bn254::Bn254;
use jf_utils::test_rng;

fn test_pcs_end_to_end_template<E>()
where
E: Pairing,
{
let degree = 4;
let rng = &mut test_rng();
let pp: UnivariateUniversalParams<E> = gen_srs_for_testing(rng, degree, 1).unwrap();
let (ck, _) = pp.trim(degree).unwrap();
let p = <DensePolynomial<E::ScalarField> as DenseUVPolynomial<E::ScalarField>>::rand(
degree, rng,
);
let comm = UnivariateKzgPCS::<E>::commit(&ck, &p).unwrap();
assert!(comm == comm, "");
}
#[test]
fn test_pcs() {
test_pcs_end_to_end_template::<Bn254>();
}
}
4 changes: 2 additions & 2 deletions src/primary/plonk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// plonk instances for primary circuit over BN254 curve
///
/// computation of cross terms followed from chapter 3.5 of protostar: https://eprint.iacr.org/2023/620.pdf
/// computation of cross terms followed from chapter 3.4 of protostar: https://eprint.iacr.org/2023/620.pdf
///
use ark_ec::pairing::Pairing;
use ark_ec::CurveGroup;
Expand All @@ -19,7 +19,7 @@ use crate::primary::kzg::gen_srs_for_testing;

use std::marker::PhantomData;

type CommitmentKey<E> = UnivariateProverParam<E>;
pub(crate) type CommitmentKey<E> = UnivariateProverParam<E>;

/// Public parameters for a given PLONK
#[derive(Clone)]
Expand Down

0 comments on commit 4544fdf

Please sign in to comment.