-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TE conditional addition gadget #44
Open
swasilyev
wants to merge
11
commits into
master
Choose a base branch
from
te-gadget
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
32270d2
mod gadgets::ec
swasilyev bc83932
representation independent stuff moved to mod ec
swasilyev 60fee2c
representation independent stuff moved to mod ec - 2
swasilyev acaf860
representation independent stuff moved to mod ec - 3
swasilyev feeaa45
CondAddValues parametrized by the jubjub
swasilyev c07490b
CondAddValues parametrized by the jubjub - 2
swasilyev caf4a59
warnings
swasilyev 00b39b2
visibility
swasilyev 838d273
fmt
swasilyev 3cf97fe
no-std
swasilyev 2156172
fmt
swasilyev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use crate::domain::Domain; | ||
use crate::gadgets::booleanity::BitColumn; | ||
use crate::{Column, FieldColumn}; | ||
use ark_ec::{AffineRepr, CurveGroup}; | ||
use ark_ff::{FftField, Field}; | ||
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; | ||
use ark_std::marker::PhantomData; | ||
use ark_std::vec::Vec; | ||
|
||
pub mod sw_cond_add; | ||
pub mod te_cond_add; | ||
|
||
// A vec of affine points from the prime-order subgroup of the curve whose base field enables FFTs, | ||
// and its convenience representation as columns of coordinates over the curve's base field. | ||
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)] | ||
pub struct AffineColumn<F: FftField, P: AffineRepr<BaseField = F>> { | ||
points: Vec<P>, | ||
pub xs: FieldColumn<F>, | ||
pub ys: FieldColumn<F>, | ||
} | ||
|
||
impl<F: FftField, P: AffineRepr<BaseField = F>> AffineColumn<F, P> { | ||
fn column(points: Vec<P>, domain: &Domain<F>, hidden: bool) -> Self { | ||
assert!(points.iter().all(|p| !p.is_zero())); | ||
let (xs, ys) = points.iter().map(|p| p.xy().unwrap()).unzip(); | ||
let xs = domain.column(xs, hidden); | ||
let ys = domain.column(ys, hidden); | ||
Self { points, xs, ys } | ||
} | ||
pub fn private_column(points: Vec<P>, domain: &Domain<F>) -> Self { | ||
Self::column(points, domain, true) | ||
} | ||
|
||
pub fn public_column(points: Vec<P>, domain: &Domain<F>) -> Self { | ||
Self::column(points, domain, false) | ||
} | ||
|
||
pub fn evaluate(&self, z: &F) -> (F, F) { | ||
(self.xs.evaluate(z), self.ys.evaluate(z)) | ||
} | ||
} | ||
|
||
// 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<F: FftField, P: AffineRepr<BaseField = F>> { | ||
bitmask: BitColumn<F>, | ||
points: AffineColumn<F, P>, | ||
// The polynomial `X - w^{n-1}` in the Lagrange basis | ||
not_last: FieldColumn<F>, | ||
// Accumulates the (conditional) rolling sum of the points | ||
pub acc: AffineColumn<F, P>, | ||
pub result: P, | ||
} | ||
|
||
impl<F, P: AffineRepr<BaseField = F>> CondAdd<F, P> | ||
where | ||
F: FftField, | ||
{ | ||
// 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<F>, | ||
points: AffineColumn<F, P>, | ||
seed: P, | ||
domain: &Domain<F>, | ||
) -> Self { | ||
assert_eq!(bitmask.bits.len(), domain.capacity - 1); | ||
assert_eq!(points.points.len(), domain.capacity - 1); | ||
let not_last = domain.not_last_row.clone(); | ||
let acc = bitmask | ||
.bits | ||
.iter() | ||
.zip(points.points.iter()) | ||
.scan(seed, |acc, (&b, point)| { | ||
if b { | ||
*acc = (*acc + point).into_affine(); | ||
} | ||
Some(*acc) | ||
}); | ||
let acc: Vec<_> = ark_std::iter::once(seed).chain(acc).collect(); | ||
let init_plus_result = acc.last().unwrap(); | ||
let result = init_plus_result.into_group() - seed.into_group(); | ||
let result = result.into_affine(); | ||
let acc = AffineColumn::private_column(acc, domain); | ||
|
||
Self { | ||
bitmask, | ||
points, | ||
acc, | ||
not_last, | ||
result, | ||
} | ||
} | ||
|
||
fn evaluate_assignment(&self, z: &F) -> CondAddValues<F, P> { | ||
CondAddValues { | ||
bitmask: self.bitmask.evaluate(z), | ||
points: self.points.evaluate(z), | ||
not_last: self.not_last.evaluate(z), | ||
acc: self.acc.evaluate(z), | ||
_phantom: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
pub struct CondAddValues<F: Field, P: AffineRepr<BaseField = F>> { | ||
pub bitmask: F, | ||
pub points: (F, F), | ||
pub not_last: F, | ||
pub acc: (F, F), | ||
pub _phantom: PhantomData<P>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: update comment