Skip to content

impl ConstantTimeSelect for BoxedMontyForm and BoxedMontyparams #794

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/modular/boxed_monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod sub;
use super::{ConstMontyParams, Retrieve, div_by_2};
use mul::BoxedMontyMultiplier;

use crate::{BoxedUint, Limb, Monty, Odd, Word};
use crate::{BoxedUint, ConstantTimeSelect, Limb, Monty, Odd, Word};
use alloc::sync::Arc;
use subtle::Choice;

Expand Down Expand Up @@ -339,6 +339,29 @@ impl Zeroize for BoxedMontyForm {
}
}

impl ConstantTimeSelect for BoxedMontyForm {
fn ct_select(a: &Self, b: &Self, choice: Choice) -> Self {
Self {
montgomery_form: BoxedUint::ct_select(&a.montgomery_form, &b.montgomery_form, choice),
params: BoxedMontyParams::ct_select(&a.params, &b.params, choice).into(),
}
}
}

impl ConstantTimeSelect for BoxedMontyParams {
fn ct_select(a: &Self, b: &Self, choice: Choice) -> Self {
let modulus = BoxedUint::ct_select(a.modulus().as_ref(), b.modulus().as_ref(), choice);
Self {
modulus: Odd::new(modulus).expect("both moduli are odd by construction"),
Comment on lines +353 to +355
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this working around the absence of a ConstantTimeSelect impl on Odd or something? It would be better to add one in that case IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, I can do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it goes a bit deeper than that.

There's a blanket-ish impl like so impl<T: ConditionallySelectable> ConstantTimeSelect for T (I guess as a "bridge" between the two traits?), which conflicts with impl<T: ConstantTimeSelect> ConstantTimeSelect for Odd<T>, so it looks like what this PR does is the least-worst option unless we rethink that blanket impl.

one: BoxedUint::ct_select(&a.one, &b.one, choice),
r2: BoxedUint::ct_select(&a.r2, &b.r2, choice),
r3: BoxedUint::ct_select(&a.r3, &b.r3, choice),
mod_neg_inv: Limb::ct_select(&a.mod_neg_inv, &b.mod_neg_inv, choice),
mod_leading_zeros: u32::ct_select(&a.mod_leading_zeros, &b.mod_leading_zeros, choice),
}
}
}

/// Convert the given integer into the Montgomery domain.
#[inline]
fn convert_to_montgomery(integer: &mut BoxedUint, params: &BoxedMontyParams) {
Expand All @@ -348,6 +371,10 @@ fn convert_to_montgomery(integer: &mut BoxedUint, params: &BoxedMontyParams) {

#[cfg(test)]
mod tests {
use subtle::Choice;

use crate::ConstantTimeSelect;

use super::{BoxedMontyForm, BoxedMontyParams, BoxedUint, Limb, Odd};

#[test]
Expand All @@ -369,4 +396,21 @@ mod tests {
assert_eq!(zero.div_by_2(), zero);
assert_eq!(one.div_by_2().mul(&two), one);
}

#[test]
fn constant_time_select() {
let modulus = Odd::new(BoxedUint::from(9u8)).unwrap();
let params1 = BoxedMontyParams::new(modulus);
let modulus = Odd::new(BoxedUint::from(19u8)).unwrap();
let params2 = BoxedMontyParams::new(modulus);

assert_eq!(
params1,
BoxedMontyParams::ct_select(&params1, &params2, Choice::from(0))
);
assert_eq!(
params2,
BoxedMontyParams::ct_select(&params1, &params2, Choice::from(1))
);
}
}