Skip to content

Commit

Permalink
Merge pull request #39 from davxy/upstream-contrib
Browse files Browse the repository at this point in the history
Refactory
  • Loading branch information
davxy authored Nov 8, 2024
2 parents 5a8d95e + 708611e commit 2d6e73a
Show file tree
Hide file tree
Showing 20 changed files with 393 additions and 381 deletions.
3 changes: 1 addition & 2 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ ark-serialize.workspace = true
fflonk.workspace = true
rayon = { workspace = true, optional = true }
getrandom_or_panic = { version = "0.0.3", default-features = false }
rand_core = "0.6"

[dev-dependencies]
ark-ed-on-bls12-381-bandersnatch = { version = "0.4", default-features = false }
Expand All @@ -31,7 +30,6 @@ std = [
"ark-serialize/std",
"fflonk/std",
"getrandom_or_panic/std",
"rand_core/std"
]
parallel = [
"std",
Expand All @@ -44,3 +42,4 @@ parallel = [
]
print-trace = ["ark-std/print-trace"]
asm = ["fflonk/asm"]
test-vectors = []
6 changes: 3 additions & 3 deletions common/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<F: FftField> Domain<F> {
pub(crate) fn column(&self, mut evals: Vec<F>, hidden: bool) -> FieldColumn<F> {
let len = evals.len();
assert!(len <= self.capacity);
if self.hiding && hidden {
if self.hiding && hidden && !cfg!(feature = "test-vectors") {
evals.resize(self.capacity, F::zero());
evals.resize_with(self.domains.x1.size(), || {
F::rand(&mut getrandom_or_panic::getrandom_or_panic())
Expand Down Expand Up @@ -153,7 +153,7 @@ fn vanishes_on_row<F: FftField>(
) -> DensePolynomial<F> {
assert!(i < domain.size());
let w = domain.group_gen();
let wi = w.pow(&[i as u64]);
let wi = w.pow([i as u64]);
let wi = DensePolynomial::from_coefficients_slice(&[wi]);
let x = DensePolynomial::from_coefficients_slice(&[F::zero(), F::one()]);
&x - &wi
Expand All @@ -163,7 +163,7 @@ fn vanishes_on_row<F: FftField>(
fn vanishes_on_last_3_rows<F: FftField>(domain: GeneralEvaluationDomain<F>) -> DensePolynomial<F> {
let w = domain.group_gen();
let n3 = (domain.size() - ZK_ROWS) as u64;
let w3 = w.pow(&[n3]);
let w3 = w.pow([n3]);
let w2 = w3 * w;
let w1 = w2 * w;
assert_eq!(w1, domain.group_gen_inv());
Expand Down
2 changes: 1 addition & 1 deletion common/src/gadgets/booleanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Booleanity<F: FftField> {
bits: BitColumn<F>,
}

impl<'a, F: FftField> Booleanity<F> {
impl<F: FftField> Booleanity<F> {
pub fn init(bits: BitColumn<F>) -> Self {
Self { bits }
}
Expand Down
72 changes: 57 additions & 15 deletions common/src/gadgets/cond_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,74 @@ use ark_ff::{FftField, Field};

use crate::domain::Domain;
use crate::gadgets::booleanity::BitColumn;
use crate::AffineColumn;
use crate::{AffineColumn, FieldColumn};

pub trait CondAdd<F, AffinePoint>
use super::{ProverGadget, VerifierGadget};

/// Affine point with conditional add implementation.
///
/// Currently supported for Arkworks Short Weierstrass and Twisted Edwards affine points.
pub trait AffineCondAdd: AffineRepr
where
BaseFieldOf<Self>: FftField,
{
/// Conditional addition operation
type CondAddT: CondAdd<BaseFieldOf<Self>, Self>;
}

// Conditional affine addition.
//
// If the bit is set for a point, add the point to the acc and store,
// otherwise copy the acc value
pub trait CondAdd<F, P>: ProverGadget<F>
where
F: FftField,
AffinePoint: AffineRepr<BaseField = F>,
P: AffineRepr<BaseField = F>,
{
type CondAddValT: CondAddValues<F>;
fn init(
bitmask: BitColumn<F>,
points: AffineColumn<F, AffinePoint>,
seed: AffinePoint,
domain: &Domain<F>,
) -> Self;

fn evaluate_assignment(&self, z: &F) -> Self::CondAddValT;
fn get_acc(&self) -> AffineColumn<F, AffinePoint>;
fn get_result(&self) -> AffinePoint;
type Values: CondAddValues<F>;

fn init(bitmask: BitColumn<F>, points: AffineColumn<F, P>, seed: P, domain: &Domain<F>)
-> Self;

fn evaluate_assignment(&self, z: &F) -> Self::Values;

fn get_acc(&self) -> AffineColumn<F, P>;

fn get_result(&self) -> P;
}

pub trait CondAddValues<F>
pub trait CondAddValues<F>: VerifierGadget<F>
where
F: Field,
{
fn acc_coeffs_1(&self) -> (F, F);

fn acc_coeffs_2(&self) -> (F, F);

fn init(bitmask: F, points: (F, F), not_last: F, acc: (F, F)) -> Self;
}

pub struct CondAddGen<P>
where
P: AffineRepr,
<P as AffineRepr>::BaseField: FftField,
{
pub(super) bitmask: BitColumn<BaseFieldOf<P>>,
pub(super) points: AffineColumn<BaseFieldOf<P>, P>,
pub(super) not_last: FieldColumn<BaseFieldOf<P>>,
pub acc: AffineColumn<BaseFieldOf<P>, P>,
pub result: P,
}

pub struct CondAddValuesGen<P: AffineRepr> {
pub bitmask: BaseFieldOf<P>,
pub points: (BaseFieldOf<P>, BaseFieldOf<P>),
pub not_last: BaseFieldOf<P>,
pub acc: (BaseFieldOf<P>, BaseFieldOf<P>),
}

pub type BaseFieldOf<P> = <P as AffineRepr>::BaseField;

pub type CondAddFor<P> = <P as AffineCondAdd>::CondAddT;

pub type CondAddValuesFor<P> = <CondAddFor<P> as CondAdd<BaseFieldOf<P>, P>>::Values;
1 change: 0 additions & 1 deletion common/src/gadgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use ark_poly::{Evaluations, GeneralEvaluationDomain};
use ark_std::vec::Vec;

pub mod booleanity;
// pub mod inner_prod_pub;
pub mod cond_add;
pub mod fixed_cells;
pub mod inner_prod;
Expand Down
2 changes: 1 addition & 1 deletion common/src/gadgets/powers_of_two_multiples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ mod tests {
domain.divide_by_vanishing_poly(&c1);
domain.divide_by_vanishing_poly(&c2);

return (domain, gadget, cs);
(domain, gadget, cs)
}

#[test]
Expand Down
Loading

0 comments on commit 2d6e73a

Please sign in to comment.