Skip to content

Commit

Permalink
Document to_xy timing expectations
Browse files Browse the repository at this point in the history
Removes conditional branch in the Ed25519 to_xy.
  • Loading branch information
kayabaNerve committed Dec 8, 2024
1 parent a02cf55 commit cbb5ffa
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions crypto/divisors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub trait DivisorCurve: Group + ConstantTimeEq + ConditionallySelectable + Zeroi
/// Convert a point to its x and y coordinates.
///
/// Returns None if passed the point at infinity.
///
/// This function may run in time variable to if the point is the identity.
fn to_xy(point: Self) -> Option<(Self::FieldElement, Self::FieldElement)>;
}

Expand Down Expand Up @@ -505,6 +507,7 @@ mod pasta {

#[cfg(any(test, feature = "ed25519"))]
mod ed25519 {
use subtle::{Choice, ConditionallySelectable};
use group::{
ff::{Field, PrimeField},
Group, GroupEncoding,
Expand Down Expand Up @@ -552,9 +555,13 @@ mod ed25519 {
((D * edwards_y_sq) + Self::FieldElement::ONE).invert().unwrap())
.sqrt()
.unwrap();
if u8::from(bool::from(edwards_x.is_odd())) != x_is_odd {
edwards_x = -edwards_x;
}

// Negate the x coordinate if the sign doesn't match
edwards_x = <_>::conditional_select(
&edwards_x,
&-edwards_x,
edwards_x.is_odd() ^ Choice::from(x_is_odd),
);

// Calculate the x and y coordinates for Wei25519
let edwards_y_plus_one = Self::FieldElement::ONE + edwards_y;
Expand Down

0 comments on commit cbb5ffa

Please sign in to comment.