-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
114 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,6 +1,6 @@ | ||
use std::str::FromStr; | ||
|
||
use curve25519_dalek::edwards::CompressedEdwardsY; | ||
use crate::curve25519_dalek::CompressedEdwardsY; | ||
use generic_array::GenericArray; | ||
use num::{BigUint, Num, One}; | ||
use serde::{Deserialize, Serialize}; | ||
|
@@ -72,7 +72,7 @@ impl EdwardsParameters for Ed25519Parameters { | |
/// | ||
/// This function always returns the nonnegative square root, in the sense that the least | ||
/// significant bit of the result is always 0. | ||
pub fn ed25519_sqrt(a: &BigUint) -> BigUint { | ||
pub fn ed25519_sqrt(a: &BigUint) -> Option<BigUint> { | ||
// Here is a description of how to calculate sqrt in the Curve25519 base field: | ||
// ssh://[email protected]/succinctlabs/curve25519-dalek/blob/ | ||
// e2d1bd10d6d772af07cac5c8161cd7655016af6d/curve25519-dalek/src/field.rs#L256 | ||
|
@@ -108,18 +108,18 @@ pub fn ed25519_sqrt(a: &BigUint) -> BigUint { | |
let flipped_sign_sqrt = beta_squared == neg_a; | ||
|
||
if !correct_sign_sqrt && !flipped_sign_sqrt { | ||
panic!("a is not a square"); | ||
return None; | ||
} | ||
|
||
let beta_bytes = beta.to_bytes_le(); | ||
if (beta_bytes[0] & 1) == 1 { | ||
beta = (&modulus - &beta) % &modulus; | ||
} | ||
|
||
beta | ||
Some(beta) | ||
} | ||
|
||
pub fn decompress(compressed_point: &CompressedEdwardsY) -> AffinePoint<Ed25519> { | ||
pub fn decompress(compressed_point: &CompressedEdwardsY) -> Option<AffinePoint<Ed25519>> { | ||
let mut point_bytes = *compressed_point.as_bytes(); | ||
let sign = point_bytes[31] >> 7 == 1; | ||
// mask out the sign bit | ||
|
@@ -134,15 +134,15 @@ pub fn decompress(compressed_point: &CompressedEdwardsY) -> AffinePoint<Ed25519> | |
let v_inv = v.modpow(&(modulus - BigUint::from(2u64)), modulus); | ||
let u_div_v = (u * &v_inv) % modulus; | ||
|
||
let mut x = ed25519_sqrt(&u_div_v); | ||
let mut x = ed25519_sqrt(&u_div_v)?; | ||
|
||
// sqrt always returns the nonnegative square root, | ||
// so we negate according to the supplied sign bit. | ||
if sign { | ||
x = modulus - &x; | ||
} | ||
|
||
AffinePoint::new(x, y.clone()) | ||
Some(AffinePoint::new(x, y.clone())) | ||
} | ||
|
||
#[cfg(test)] | ||
|
@@ -178,7 +178,7 @@ mod tests { | |
|
||
CompressedEdwardsY(compressed) | ||
}; | ||
assert_eq!(point, decompress(&compressed_point)); | ||
assert_eq!(point, decompress(&compressed_point).unwrap()); | ||
|
||
// Double the point to create a "random" point for the next iteration. | ||
point = point.clone() + point.clone(); | ||
|
This file contains 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 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 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 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