-
Notifications
You must be signed in to change notification settings - Fork 15
Branch free witness generation #97
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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,63 @@ | ||
| //! Branch-free handling of secret witness bits (the prover's ring position | ||
| //! and the blinding scalar bits). These helpers keep secret-dependent | ||
| //! branches and memory indexing out of witness generation. They are defense | ||
| //! in depth, not a complete side-channel countermeasure: later pipeline | ||
| //! stages (in particular the polynomial commitment MSMs) still process the | ||
| //! witness in variable time. | ||
|
|
||
| use ark_ec::short_weierstrass::{Projective as SwProjective, SWCurveConfig}; | ||
| use ark_ec::twisted_edwards::{Projective as TeProjective, TECurveConfig}; | ||
| use ark_ff::Field; | ||
|
|
||
| /// Lifts a bit to a field element without branching on its value. | ||
| /// | ||
| /// `F::from(bool)` bottoms out in arkworks' `from_bigint`, which returns | ||
| /// early for zero; mapping the bit to 1 or 2 first routes both values | ||
| /// through the same Montgomery conversion. | ||
| pub fn bit_to_field<F: Field>(bit: bool) -> F { | ||
| F::from(bit as u64 + 1) - F::one() | ||
| } | ||
|
|
||
| /// Arithmetic two-way select. `mask` must be 0 or 1. | ||
| pub fn field_select<F: Field>(mask: F, if_true: F, if_false: F) -> F { | ||
| if_false + mask * (if_true - if_false) | ||
| } | ||
|
|
||
| /// Coordinate-wise arithmetic select between two curve points. | ||
| pub trait PointSelect<F: Field>: Sized { | ||
| /// `mask` must be 0 or 1. | ||
| fn select(mask: F, if_true: &Self, if_false: &Self) -> Self; | ||
| } | ||
|
|
||
| impl<C: TECurveConfig> PointSelect<C::BaseField> for TeProjective<C> { | ||
| fn select(mask: C::BaseField, if_true: &Self, if_false: &Self) -> Self { | ||
| Self::new_unchecked( | ||
| field_select(mask, if_true.x, if_false.x), | ||
| field_select(mask, if_true.y, if_false.y), | ||
| field_select(mask, if_true.t, if_false.t), | ||
| field_select(mask, if_true.z, if_false.z), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl<C: SWCurveConfig> PointSelect<C::BaseField> for SwProjective<C> { | ||
| fn select(mask: C::BaseField, if_true: &Self, if_false: &Self) -> Self { | ||
| Self::new_unchecked( | ||
| field_select(mask, if_true.x, if_false.x), | ||
| field_select(mask, if_true.y, if_false.y), | ||
| field_select(mask, if_true.z, if_false.z), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use ark_ed_on_bls12_381_bandersnatch::Fq; | ||
|
|
||
| #[test] | ||
| fn bit_lift_is_exact() { | ||
| assert_eq!(bit_to_field::<Fq>(false), Fq::from(0)); | ||
| assert_eq!(bit_to_field::<Fq>(true), Fq::from(1)); | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ use ark_poly::{Evaluations, GeneralEvaluationDomain, Polynomial}; | |
|
|
||
| use ark_std::{vec, vec::Vec}; | ||
|
|
||
| use crate::cond_select::bit_to_field; | ||
| use crate::domain::Domain; | ||
| use crate::gadgets::VerifierGadget; | ||
| use crate::{const_evals, Column, FieldColumn}; | ||
|
|
@@ -16,10 +17,7 @@ pub struct BitColumn<F: FftField> { | |
|
|
||
| impl<F: FftField> BitColumn<F> { | ||
| pub fn init(bits: Vec<bool>, domain: &Domain<F>) -> Self { | ||
| let bits_as_field_elements = bits | ||
| .iter() | ||
| .map(|&b| if b { F::one() } else { F::zero() }) | ||
| .collect(); | ||
| let bits_as_field_elements = bits.iter().map(|&bit| bit_to_field(bit)).collect(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd probably have passed an iterator here, but whatever. |
||
| let col = domain.column(bits_as_field_elements); | ||
| Self { bits, col } | ||
| } | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
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.
Why not use https://github.com/dalek-cryptography/subtle here?
Arkworks doesn't have constant time below us, but nothing wrong in having our stuff be constant time, and hoping their improves. Also, one could write arkworks curve wrappers over curves that claim constant time, although whether this claims hold is another matter.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah
subtleintroduction is a nice follow-up. Perhaps I'll open a PR to introduce it.But let's first ship these crates as downstream we need them for production asap
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.
@burdges #98