Skip to content
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

Add skip_while condition for scalar mul #124

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions src/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,29 @@ impl G1Projective {
G1Projective::conditional_select(&tmp, self, rhs.is_identity())
}

/// Multiply this point by a scalar, using variable-time multiplication.
pub fn multiply_vartime(&self, by: &Scalar) -> G1Projective {
let mut acc = G1Projective::identity();

// This is a simple double-and-add implementation of point
// multiplication, moving from most significant to least
// significant bit of the scalar.
//
// We skip the leading bit as part of the vartime implementation.
for bit in by
.to_bytes()
.iter()
.rev()
.flat_map(|byte| (0..8).rev().map(move |i| Choice::from((byte >> i) & 1u8)))
.skip_while(|c| !bool::from(*c))
{
acc = acc.double();
acc = G1Projective::conditional_select(&acc, &(acc + self), bit);

Choose a reason for hiding this comment

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

It seems like you could just use a branch here, rather than conditional_select, since this is variable-time code?

Likewise you could use a bool instead of Choice, which will avoid subtle's memory barriers and should make the code easier for rustc to optimize.

}

acc
}

fn multiply(&self, by: &[u8; 32]) -> G1Projective {
let mut acc = G1Projective::identity();

Expand Down Expand Up @@ -1572,6 +1595,10 @@ fn test_projective_scalar_multiplication() {
let c = a * b;

assert_eq!((g * a) * b, g * c);
assert_eq!(
g.multiply_vartime(&a).multiply_vartime(&b),
g.multiply_vartime(&c)
);
}

#[test]
Expand Down
27 changes: 27 additions & 0 deletions src/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,29 @@ impl G2Projective {
G2Projective::conditional_select(&tmp, self, rhs.is_identity())
}

/// Multiply this point by a scalar, using variable-time multiplication.
pub fn multiply_vartime(&self, by: &Scalar) -> G2Projective {
let mut acc = G2Projective::identity();

// This is a simple double-and-add implementation of point
// multiplication, moving from most significant to least
// significant bit of the scalar.
//
// We skip the leading bit as part of the vartime implementation.
for bit in by
.to_bytes()
.iter()
.rev()
.flat_map(|byte| (0..8).rev().map(move |i| Choice::from((byte >> i) & 1u8)))
.skip_while(|c| !bool::from(*c))
{
acc = acc.double();
acc = G2Projective::conditional_select(&acc, &(acc + self), bit);
}

acc
}

fn multiply(&self, by: &[u8]) -> G2Projective {
let mut acc = G2Projective::identity();

Expand Down Expand Up @@ -1836,6 +1859,10 @@ fn test_projective_scalar_multiplication() {
let c = a * b;

assert_eq!((g * a) * b, g * c);
assert_eq!(
g.multiply_vartime(&a).multiply_vartime(&b),
g.multiply_vartime(&c)
);
}

#[test]
Expand Down
23 changes: 23 additions & 0 deletions src/pairings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,29 @@ impl Gt {
pub fn double(&self) -> Gt {
Gt(self.0.square())
}

/// Multiply this point by a scalar, using variable-time multiplication.
pub fn multiply_vartime(self, other: &Scalar) -> Gt {
let mut acc = Gt::identity();

// This is a simple double-and-add implementation of group element
// multiplication, moving from most significant to least
// significant bit of the scalar.
//
// We skip the leading bit as part of the vartime implementation.
for bit in other
.to_bytes()
.iter()
.rev()
.flat_map(|byte| (0..8).rev().map(move |i| Choice::from((byte >> i) & 1u8)))
.skip_while(|c| !bool::from(*c))
{
acc = acc.double();
acc = Gt::conditional_select(&acc, &(acc + self), bit);
}

acc
}
}

impl<'a> Neg for &'a Gt {
Expand Down