jubjub: Replace Choice::unwrap_u8 with bool::from

The latter is clearer and the intended route for un-CT-ing Choices.
This commit is contained in:
Jack Grigg 2020-08-12 15:51:03 +01:00
parent 6b1281e8e0
commit 7a2235ad03
2 changed files with 32 additions and 42 deletions

View File

@ -54,7 +54,7 @@ impl ConstantTimeEq for Fr {
impl PartialEq for Fr {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).unwrap_u8() == 1
bool::from(self.ct_eq(other))
}
}
@ -805,57 +805,47 @@ fn test_from_bytes() {
);
// -1 should work
assert!(
assert!(bool::from(
Fr::from_bytes(&[
182, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14
])
.is_some()
.unwrap_u8()
== 1
);
));
// modulus is invalid
assert!(
assert!(bool::from(
Fr::from_bytes(&[
183, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14
])
.is_none()
.unwrap_u8()
== 1
);
));
// Anything larger than the modulus is invalid
assert!(
assert!(bool::from(
Fr::from_bytes(&[
184, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14
])
.is_none()
.unwrap_u8()
== 1
);
));
assert!(
assert!(bool::from(
Fr::from_bytes(&[
183, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 104, 6, 169, 175, 51, 101, 234, 180, 125, 14
])
.is_none()
.unwrap_u8()
== 1
);
));
assert!(
assert!(bool::from(
Fr::from_bytes(&[
183, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 15
])
.is_none()
.unwrap_u8()
== 1
);
));
}
#[test]
@ -1056,7 +1046,7 @@ fn test_squaring() {
#[test]
fn test_inversion() {
assert_eq!(Fr::zero().invert().is_none().unwrap_u8(), 1);
assert!(bool::from(Fr::zero().invert().is_none()));
assert_eq!(Fr::one().invert().unwrap(), Fr::one());
assert_eq!((-&Fr::one()).invert().unwrap(), -&Fr::one());
@ -1113,7 +1103,7 @@ fn test_sqrt() {
for _ in 0..100 {
let square_root = square.sqrt();
if square_root.is_none().unwrap_u8() == 1 {
if bool::from(square_root.is_none()) {
none_count += 1;
} else {
assert_eq!(square_root.unwrap() * square_root.unwrap(), square);

View File

@ -77,7 +77,7 @@ impl ConstantTimeEq for AffinePoint {
impl PartialEq for AffinePoint {
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).unwrap_u8() == 1
bool::from(self.ct_eq(other))
}
}
@ -136,7 +136,7 @@ impl ConditionallySelectable for ExtendedPoint {
impl PartialEq for ExtendedPoint {
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).unwrap_u8() == 1
bool::from(self.ct_eq(other))
}
}
@ -907,9 +907,9 @@ fn test_is_on_curve_var() {
#[test]
fn test_d_is_non_quadratic_residue() {
assert!(EDWARDS_D.sqrt().is_none().unwrap_u8() == 1);
assert!((-EDWARDS_D).sqrt().is_none().unwrap_u8() == 1);
assert!((-EDWARDS_D).invert().unwrap().sqrt().is_none().unwrap_u8() == 1);
assert!(bool::from(EDWARDS_D.sqrt().is_none()));
assert!(bool::from((-EDWARDS_D).sqrt().is_none()));
assert!(bool::from((-EDWARDS_D).invert().unwrap().sqrt().is_none()));
}
#[test]
@ -1121,9 +1121,9 @@ const EIGHT_TORSION: [AffinePoint; 8] = [
#[test]
fn find_eight_torsion() {
let g = ExtendedPoint::from(FULL_GENERATOR);
assert!(g.is_small_order().unwrap_u8() == 0);
assert!(!bool::from(g.is_small_order()));
let g = g.multiply(&FR_MODULUS_BYTES);
assert!(g.is_small_order().unwrap_u8() == 1);
assert!(bool::from(g.is_small_order()));
let mut cur = g;
@ -1142,22 +1142,22 @@ fn find_curve_generator() {
let mut trial_bytes = [0; 32];
for _ in 0..255 {
let a = AffinePoint::from_bytes(trial_bytes);
if a.is_some().unwrap_u8() == 1 {
if bool::from(a.is_some()) {
let a = a.unwrap();
assert!(a.is_on_curve_vartime());
let b = ExtendedPoint::from(a);
let b = b.multiply(&FR_MODULUS_BYTES);
assert!(b.is_small_order().unwrap_u8() == 1);
assert!(bool::from(b.is_small_order()));
let b = b.double();
assert!(b.is_small_order().unwrap_u8() == 1);
assert!(bool::from(b.is_small_order()));
let b = b.double();
assert!(b.is_small_order().unwrap_u8() == 1);
if b.is_identity().unwrap_u8() == 0 {
assert!(bool::from(b.is_small_order()));
if !bool::from(b.is_identity()) {
let b = b.double();
assert!(b.is_small_order().unwrap_u8() == 1);
assert!(b.is_identity().unwrap_u8() == 1);
assert!(bool::from(b.is_small_order()));
assert!(bool::from(b.is_identity()));
assert_eq!(FULL_GENERATOR, a);
assert!(a.mul_by_cofactor().is_torsion_free().unwrap_u8() == 1);
assert!(bool::from(a.mul_by_cofactor().is_torsion_free()));
return;
}
}
@ -1171,7 +1171,7 @@ fn find_curve_generator() {
#[test]
fn test_small_order() {
for point in EIGHT_TORSION.iter() {
assert!(point.is_small_order().unwrap_u8() == 1);
assert!(bool::from(point.is_small_order()));
}
}
@ -1186,11 +1186,11 @@ fn test_is_identity() {
assert!(a.v != b.v);
assert!(a.z != b.z);
assert!(a.is_identity().unwrap_u8() == 1);
assert!(b.is_identity().unwrap_u8() == 1);
assert!(bool::from(a.is_identity()));
assert!(bool::from(b.is_identity()));
for point in EIGHT_TORSION.iter() {
assert!(point.mul_by_cofactor().is_identity().unwrap_u8() == 1);
assert!(bool::from(point.mul_by_cofactor().is_identity()));
}
}