Skip to content

Commit

Permalink
chore(fflonk): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
saitima committed Oct 30, 2024
1 parent f9d912d commit 427e203
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 1,412 deletions.
11 changes: 11 additions & 0 deletions crates/bellman/src/kate_commitment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ impl<E: Engine, T: CrsType> PartialEq for Crs<E, T> {
impl<E: Engine, T: CrsType> Eq for Crs<E, T> {}

impl<E: Engine, T: CrsType> Crs<E, T> {
pub fn new(g1_bases: Vec<E::G1Affine>, g2_monomial_bases: Vec<E::G2Affine>) -> Self {
Self {
g1_bases: Arc::new(g1_bases),
g2_monomial_bases: Arc::new(g2_monomial_bases),
_marker: std::marker::PhantomData,
}
}

pub fn write<W: Write>(&self, mut writer: W) -> std::io::Result<()> {
writer.write_u64::<BigEndian>(self.g1_bases.len() as u64)?;
for g in &self.g1_bases[..] {
Expand Down Expand Up @@ -106,7 +114,10 @@ impl<E: Engine> Crs<E, CrsForMonomialForm> {
pub fn crs_42(size: usize, worker: &Worker) -> Self {
// kind of how ceremony would work
assert!(size.is_power_of_two());
Self::non_power_of_two_crs_42(size, worker)
}

pub fn non_power_of_two_crs_42(size: usize, worker: &Worker) -> Self {
let mut g2 = vec![E::G2Affine::one(); 2];

use crate::domain::EvaluationDomain;
Expand Down
5 changes: 1 addition & 4 deletions crates/fflonk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ keywords.workspace = true
categories.workspace = true

[dependencies]
circuit_definitions = {git = "https://github.com/matter-labs/zksync-protocol", package = "circuit_definitions", branch = "si/fflonk"}
# circuit_definitions = { path = "../../../zksync-protocol/crates/circuit_definitions" }
# circuit_definitions = "=0.150.5"
# franklin_crypto = {path = "../franklin-crypto", package = "franklin-crypto"}
franklin-crypto.workspace = true
num-bigint = { version = "0.4", features = ["serde"] }
num-traits = "0.2"
rand = "0.4"
Expand Down
1,114 changes: 0 additions & 1,114 deletions crates/fflonk/src/convenience.rs

This file was deleted.

6 changes: 2 additions & 4 deletions crates/fflonk/src/definitions/proof.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::io::{Read, Write};

use bellman::{bn256::FrRepr, PrimeFieldRepr};
use bellman::PrimeFieldRepr;

use super::*;

Expand All @@ -27,7 +25,7 @@ pub fn fe_slice_into_be_byte_array<F: PrimeField>(values: &[F]) -> Vec<[u8; 32]>
}

impl<E: Engine, C: Circuit<E>> FflonkProof<E, C> {
pub(crate) fn empty() -> Self {
pub fn empty() -> Self {
Self {
n: 0,
inputs: vec![],
Expand Down
1 change: 0 additions & 1 deletion crates/fflonk/src/definitions/setup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::io::{Read, Write};

use super::*;
use bellman::plonk::better_better_cs::cs::SetupAssembly;
use bellman::plonk::better_cs::keys::read_curve_affine;
use bellman::plonk::better_cs::keys::write_curve_affine;

Expand Down
15 changes: 6 additions & 9 deletions crates/fflonk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![feature(generic_const_exprs)]
#![feature(allocator_api)]
pub use circuit_definitions;
pub use circuit_definitions::snark_wrapper::franklin_crypto;
use circuit_definitions::snark_wrapper::franklin_crypto::bellman::plonk::better_better_cs::{cs::PlonkCsWidth3Params, gates::naive_main_gate::NaiveMainGate};
pub use franklin_crypto;
pub use franklin_crypto::bellman;
use franklin_crypto::bellman::plonk::better_better_cs::{cs::PlonkCsWidth3Params, gates::naive_main_gate::NaiveMainGate};

use bellman::{
bn256::{Bn256, Fr},
Expand Down Expand Up @@ -34,17 +33,15 @@ mod definitions;
pub use definitions::*;
pub mod prover;
use prover::*;
pub mod verifier;
pub mod utils;
pub use utils::*;
pub mod verifier;
pub use verifier::*;
pub mod convenience;
pub use convenience::*;

#[cfg(test)]
mod test;
mod utils;
use utils::*;
pub use utils::{compute_generators, compute_power_of_two_root_of_generator, num_system_polys_from_vk};

pub const L1_VERIFIER_DOMAIN_SIZE_LOG: usize = 23;
pub const MAX_COMBINED_DEGREE_FACTOR: usize = 9;
pub(crate) const SANITY_CHECK: bool = true;
pub type FflonkAssembly<E, S, A = std::alloc::Global> = Assembly<E, PlonkCsWidth3Params, NaiveMainGate, S, A>;
290 changes: 22 additions & 268 deletions crates/fflonk/src/test.rs

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions crates/fflonk/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use bellman::plonk::better_better_cs::utils::FieldBinop;
use franklin_crypto::plonk::circuit::bigint::{biguint_to_fe, fe_to_biguint, repr_to_biguint};
use franklin_crypto::plonk::circuit::{
bigint::{biguint_to_fe, fe_to_biguint, repr_to_biguint},
linear_combination::LinearCombination,
};
use num_bigint::BigUint;
use num_traits::{One, Zero};

Expand All @@ -20,7 +23,7 @@ pub fn lcm(numbers: &[usize]) -> usize {
lcm
}

pub(crate) fn compute_max_combined_degree_from_assembly<E: Engine, P: PlonkConstraintSystemParams<E>, MG: MainGate<E>, S: SynthesisMode, C: Circuit<E>>(assembly: &Assembly<E, P, MG, S>) -> usize {
pub fn compute_max_combined_degree_from_assembly<E: Engine, P: PlonkConstraintSystemParams<E>, MG: MainGate<E>, S: SynthesisMode, C: Circuit<E>>(assembly: &Assembly<E, P, MG, S>) -> usize {
let has_custom_gate = assembly.sorted_gates.len() > 1;
let has_lookup = assembly.num_table_lookups > 0 && assembly.tables.len() > 0;
let main_gate_quotient_degree = main_gate_quotient_degree(&assembly.sorted_gates);
Expand Down Expand Up @@ -182,7 +185,7 @@ pub(crate) fn multiply_monomials<F: PrimeField>(poly1: &[F], poly2: &[F]) -> Vec
result
}

pub(crate) fn compute_lagrange_basis_inverses<F: PrimeField>(num_polys: usize, h: F, y: F) -> Vec<F> {
pub fn compute_lagrange_basis_inverses<F: PrimeField>(num_polys: usize, h: F, y: F) -> Vec<F> {
assert!(num_polys.is_power_of_two());
let degree = num_polys as u64;
// L_i(x) = (w_i/(N*h^{N-1})) * (X^N-h^N)/(X-w_i*h)
Expand Down Expand Up @@ -213,7 +216,7 @@ pub(crate) fn compute_lagrange_basis_inverses<F: PrimeField>(num_polys: usize, h
inverses
}

pub(crate) fn compute_lagrange_basis_inverses_for_union_set<F: PrimeField>(num_polys: usize, h: F, h_shifted: F, y: F, omega: F) -> (Vec<F>, Vec<F>) {
pub fn compute_lagrange_basis_inverses_for_union_set<F: PrimeField>(num_polys: usize, h: F, h_shifted: F, y: F, omega: F) -> (Vec<F>, Vec<F>) {
let degree = num_polys as usize;
let degree_as_fe = F::from_str(&degree.to_string()).unwrap();

Expand Down Expand Up @@ -639,7 +642,7 @@ pub(crate) fn batch_inversion<F: PrimeField>(values: &mut [F]) {
values.copy_from_slice(&products);
}

pub(crate) fn horner_evaluation<F: PrimeField>(coeffs: &[F], x: F) -> F {
pub fn horner_evaluation<F: PrimeField>(coeffs: &[F], x: F) -> F {
// c0 + c1*x + c2*x^2 + c3*x^3
// c0 + x*(c1 + x*(c2 + x*c3))
let mut sum = coeffs.last().unwrap().clone();
Expand Down Expand Up @@ -1639,7 +1642,7 @@ pub fn custom_gate_quotient_degree<E: Engine>(sorted_gates: &[Box<dyn GateIntern
sorted_gates[0].degree()
}

pub(crate) fn construct_set_difference_monomials<F: PrimeField>(
pub fn construct_set_difference_monomials<F: PrimeField>(
z: F,
z_omega: F,
interpolation_size_of_setup: usize,
Expand Down Expand Up @@ -1862,12 +1865,9 @@ pub struct FflonkTestCircuit;
impl Circuit<Bn256> for FflonkTestCircuit {
type MainGate = NaiveMainGate;

fn synthesize<CS: circuit_definitions::snark_wrapper::franklin_crypto::bellman::plonk::better_better_cs::cs::ConstraintSystem<Bn256> + 'static>(
&self,
cs: &mut CS,
) -> Result<(), circuit_definitions::snark_wrapper::franklin_crypto::bellman::SynthesisError> {
use circuit_definitions::snark_wrapper::franklin_crypto::bellman::Field;
use circuit_definitions::snark_wrapper::franklin_crypto::plonk::circuit::allocated_num::Num;
fn synthesize<CS: franklin_crypto::bellman::plonk::better_better_cs::cs::ConstraintSystem<Bn256> + 'static>(&self, cs: &mut CS) -> Result<(), franklin_crypto::bellman::SynthesisError> {
use franklin_crypto::bellman::Field;
use franklin_crypto::plonk::circuit::allocated_num::Num;
let a = Fr::from_str(&65.to_string()).unwrap();
let b = Fr::from_str(&66.to_string()).unwrap();
let mut c = a;
Expand Down

0 comments on commit 427e203

Please sign in to comment.