-
Notifications
You must be signed in to change notification settings - Fork 15
Select secret witness bits via subtle #98
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,63 +1,117 @@ | ||
| //! 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. | ||
| //! Constant-time handling of secret witness bits (the prover's ring | ||
| //! position and the blinding scalar bits). The secret bit is wrapped | ||
| //! into a `subtle::Choice` (an optimization barrier) on entry and | ||
| //! selection is limb-wise on the raw Montgomery representation, so no | ||
| //! field arithmetic -- and none of arkworks' data-dependent conditional | ||
| //! reductions -- ever touches the bits. Defense in depth, not a complete | ||
| //! countermeasure: the unconditional point additions run variable-time | ||
| //! arkworks field arithmetic on secret-derived accumulator values, and | ||
| //! the column FFTs and polynomial commitment MSMs downstream 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; | ||
| use ark_ff::{BigInt, Field, Fp, FpConfig}; | ||
| use ark_std::marker::PhantomData; | ||
| use subtle::ConditionallySelectable; | ||
|
|
||
| pub use subtle::Choice; | ||
|
|
||
| /// Conversion into [`Choice`]. The `bool` impl applies subtle's | ||
| /// optimization barrier, preventing the compiler from branching on the | ||
| /// bit later. (`From<bool> for Choice` does not exist upstream, hence | ||
| /// this local trait.) | ||
| pub trait IntoChoice { | ||
| fn into_choice(self) -> Choice; | ||
| } | ||
|
|
||
| impl IntoChoice for Choice { | ||
| fn into_choice(self) -> Choice { | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl IntoChoice for bool { | ||
| fn into_choice(self) -> Choice { | ||
| Choice::from(self as u8) | ||
| } | ||
| } | ||
|
|
||
| /// 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() | ||
| pub fn bit_to_field<F: Field + CondSelect>(bit: bool) -> F { | ||
| F::select(bit, &F::one(), &F::zero()) | ||
| } | ||
|
|
||
| /// 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) | ||
| /// Constant-time two-way select. | ||
| pub trait CondSelect: Sized { | ||
| fn select(bit: impl IntoChoice, if_true: &Self, if_false: &Self) -> Self; | ||
| } | ||
|
|
||
| /// 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<P: FpConfig<N>, const N: usize> CondSelect for Fp<P, N> { | ||
| fn select(bit: impl IntoChoice, if_true: &Self, if_false: &Self) -> Self { | ||
| let limbs = | ||
| <[u64; N]>::conditional_select(&if_false.0 .0, &if_true.0 .0, bit.into_choice()); | ||
| Fp(BigInt(limbs), PhantomData) | ||
| } | ||
| } | ||
|
|
||
| impl<C: TECurveConfig> PointSelect<C::BaseField> for TeProjective<C> { | ||
| fn select(mask: C::BaseField, if_true: &Self, if_false: &Self) -> Self { | ||
| impl<C: TECurveConfig> CondSelect for TeProjective<C> | ||
| where | ||
| C::BaseField: CondSelect, | ||
| { | ||
| fn select(bit: impl IntoChoice, if_true: &Self, if_false: &Self) -> Self { | ||
| let choice = bit.into_choice(); | ||
| 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), | ||
| CondSelect::select(choice, &if_true.x, &if_false.x), | ||
| CondSelect::select(choice, &if_true.y, &if_false.y), | ||
| CondSelect::select(choice, &if_true.t, &if_false.t), | ||
| CondSelect::select(choice, &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 { | ||
| impl<C: SWCurveConfig> CondSelect for SwProjective<C> | ||
| where | ||
| C::BaseField: CondSelect, | ||
| { | ||
| fn select(bit: impl IntoChoice, if_true: &Self, if_false: &Self) -> Self { | ||
| let choice = bit.into_choice(); | ||
| 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), | ||
| CondSelect::select(choice, &if_true.x, &if_false.x), | ||
| CondSelect::select(choice, &if_true.y, &if_false.y), | ||
| CondSelect::select(choice, &if_true.z, &if_false.z), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use ark_ed_on_bls12_381_bandersnatch::Fq; | ||
| use ark_ed_on_bls12_381_bandersnatch::{EdwardsProjective, Fq}; | ||
| use ark_std::{test_rng, UniformRand}; | ||
|
|
||
| #[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)); | ||
| } | ||
|
|
||
| // The selected values feed committed columns, so they are | ||
| // consensus-critical: the select must return the operand bit for bit, | ||
| // not merely an equivalent representation. | ||
| #[test] | ||
| fn select_returns_exact_operand() { | ||
| let rng = &mut test_rng(); | ||
| let a = Fq::rand(rng); | ||
| let b = Fq::rand(rng); | ||
| assert_eq!(Fq::select(true, &a, &b), a); | ||
| assert_eq!(Fq::select(false, &a, &b), b); | ||
|
|
||
| let p = EdwardsProjective::rand(rng); | ||
| let q = EdwardsProjective::rand(rng); | ||
| let s = EdwardsProjective::select(true, &p, &q); | ||
| assert_eq!((s.x, s.y, s.t, s.z), (p.x, p.y, p.t, p.z)); | ||
| let s = EdwardsProjective::select(false, &p, &q); | ||
| assert_eq!((s.x, s.y, s.t, s.z), (q.x, q.y, q.t, q.z)); | ||
| } | ||
| } |
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
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.
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.
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.
ahh okay so here there is extra work, but this seems tiny compared to the full proof cost.