diff --git a/common/src/gadgets/cond_add.rs b/common/src/gadgets/cond_add.rs index fad556c..2108a10 100644 --- a/common/src/gadgets/cond_add.rs +++ b/common/src/gadgets/cond_add.rs @@ -1,4 +1,4 @@ -use ark_ec::{AffineRepr}; +use ark_ec::{AffineRepr, CurveConfig}; use ark_ff::{FftField, Field}; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use crate::{Column, FieldColumn}; @@ -38,25 +38,25 @@ impl> AffineColumn { } } +pub trait CondAdd where + F: FftField, + Curve: CurveConfig, + AffinePoint: AffineRepr, + ContAddVal: CondAddValues +{ + fn init(bitmask: BitColumn, + points: AffineColumn, + seed: AffinePoint, + domain: &Domain) -> Self; + + fn evaluate_assignment(&self, z: &F) -> ContAddVal; -// 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 struct CondAdd> { - pub(super)bitmask: BitColumn, - pub(super)points: AffineColumn, - // The polynomial `X - w^{n-1}` in the Lagrange basis - pub(super)not_last: FieldColumn, - // Accumulates the (conditional) rolling sum of the points - pub acc: AffineColumn, - pub result: P, -} - -pub struct CondAddValues { - pub bitmask: F, - pub points: (F, F), - pub not_last: F, - pub acc: (F, F), } +pub trait CondAddValues + where F: Field +{ + fn acc_coeffs_1(&self) -> (F, F); + fn acc_coeffs_2(&self) -> (F, F); +} diff --git a/common/src/gadgets/sw_cond_add.rs b/common/src/gadgets/sw_cond_add.rs index 1f9483d..2eee3e2 100644 --- a/common/src/gadgets/sw_cond_add.rs +++ b/common/src/gadgets/sw_cond_add.rs @@ -5,21 +5,42 @@ use ark_poly::{Evaluations, GeneralEvaluationDomain}; use ark_poly::univariate::DensePolynomial; use ark_std::{vec, vec::Vec}; -use crate::{Column, const_evals}; +use crate::{Column, FieldColumn, const_evals}; use crate::domain::Domain; use crate::gadgets::{ProverGadget, VerifierGadget}; use crate::gadgets::booleanity::BitColumn; use crate::gadgets::cond_add::{AffineColumn, CondAdd, CondAddValues}; -impl CondAdd> where +// 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 struct SwCondAdd> { + pub(super)bitmask: BitColumn, + pub(super)points: AffineColumn, + // The polynomial `X - w^{n-1}` in the Lagrange basis + pub(super)not_last: FieldColumn, + // Accumulates the (conditional) rolling sum of the points + pub acc: AffineColumn, + pub result: P, +} + +pub struct SwCondAddValues { + pub bitmask: F, + pub points: (F, F), + pub not_last: F, + pub acc: (F, F), +} + +impl CondAdd, SwCondAddValues> for SwCondAdd> where F: FftField, Curve: SWCurveConfig, { + // Populates the acc column starting from the supplied seed (as 0 doesn't have an affine SW representation). // As the SW addition formula used is not complete, the seed must be selected in a way that would prevent // exceptional cases (doublings or adding the opposite point). // The last point of the input column is ignored, as adding it would made the acc column overflow due the initial point. - pub fn init(bitmask: BitColumn, + fn init(bitmask: BitColumn, points: AffineColumn>, seed: Affine, domain: &Domain) -> Self { @@ -45,8 +66,8 @@ impl CondAdd> where Self { bitmask, points, acc, not_last, result } } - fn evaluate_assignment(&self, z: &F) -> CondAddValues { - CondAddValues { + fn evaluate_assignment(&self, z: &F) -> SwCondAddValues { + SwCondAddValues { bitmask: self.bitmask.evaluate(z), points: self.points.evaluate(z), not_last: self.not_last.evaluate(z), @@ -56,7 +77,7 @@ impl CondAdd> where } -impl ProverGadget for CondAdd> +impl ProverGadget for SwCondAdd> where F: FftField, Curve: SWCurveConfig, @@ -137,7 +158,7 @@ impl ProverGadget for CondAdd> } -impl VerifierGadget for CondAddValues { +impl VerifierGadget for SwCondAddValues { fn evaluate_constraints_main(&self) -> Vec { let b = self.bitmask; let (x1, y1) = self.acc; @@ -164,8 +185,9 @@ impl VerifierGadget for CondAddValues { } -impl CondAddValues { - pub fn acc_coeffs_1(&self) -> (F, F) { +impl CondAddValues for SwCondAddValues { + + fn acc_coeffs_1(&self) -> (F, F) { let b = self.bitmask; let (x1, _y1) = self.acc; let (x2, _y2) = self.points; @@ -179,7 +201,7 @@ impl CondAddValues { (c_acc_x, c_acc_y) } - pub fn acc_coeffs_2(&self) -> (F, F) { + fn acc_coeffs_2(&self) -> (F, F) { let b = self.bitmask; let (x1, y1) = self.acc; let (x2, y2) = self.points; @@ -220,7 +242,7 @@ mod tests { let bitmask_col = BitColumn::init(bitmask, &domain); let points_col = AffineColumn::private_column(points, &domain); - let gadget = CondAdd::init(bitmask_col, points_col, seed, &domain); + let gadget = SwCondAdd::init(bitmask_col, points_col, seed, &domain); let res = gadget.acc.points.last().unwrap(); assert_eq!(res, &expected_res);