bls12_381: 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:56:27 +01:00
parent 7a2235ad03
commit a6f2172b20
3 changed files with 22 additions and 34 deletions

View File

@ -47,7 +47,7 @@ impl Eq for Fp {}
impl PartialEq for Fp { impl PartialEq for Fp {
#[inline] #[inline]
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).unwrap_u8() == 1 bool::from(self.ct_eq(other))
} }
} }
@ -566,7 +566,7 @@ fn test_equality() {
let eq = a == b; let eq = a == b;
let ct_eq = a.ct_eq(&b); let ct_eq = a.ct_eq(&b);
assert_eq!(eq, ct_eq.unwrap_u8() == 1); assert_eq!(eq, bool::from(ct_eq));
eq eq
} }
@ -762,18 +762,16 @@ fn test_from_bytes() {
.unwrap() .unwrap()
); );
assert!( assert!(bool::from(
Fp::from_bytes(&[ Fp::from_bytes(&[
27, 1, 17, 234, 57, 127, 230, 154, 75, 27, 167, 182, 67, 75, 172, 215, 100, 119, 75, 27, 1, 17, 234, 57, 127, 230, 154, 75, 27, 167, 182, 67, 75, 172, 215, 100, 119, 75,
132, 243, 133, 18, 191, 103, 48, 210, 160, 246, 176, 246, 36, 30, 171, 255, 254, 177, 132, 243, 133, 18, 191, 103, 48, 210, 160, 246, 176, 246, 36, 30, 171, 255, 254, 177,
83, 255, 255, 185, 254, 255, 255, 255, 255, 170, 170 83, 255, 255, 185, 254, 255, 255, 255, 255, 170, 170
]) ])
.is_none() .is_none()
.unwrap_u8() ));
== 1
);
assert!(Fp::from_bytes(&[0xff; 48]).is_none().unwrap_u8() == 1); assert!(bool::from(Fp::from_bytes(&[0xff; 48]).is_none()));
} }
#[test] #[test]
@ -823,7 +821,7 @@ fn test_inversion() {
]); ]);
assert_eq!(a.invert().unwrap(), b); assert_eq!(a.invert().unwrap(), b);
assert!(Fp::zero().invert().is_none().unwrap_u8() == 1); assert!(bool::from(Fp::zero().invert().is_none()));
} }
#[test] #[test]

View File

@ -44,7 +44,7 @@ impl Eq for Fp2 {}
impl PartialEq for Fp2 { impl PartialEq for Fp2 {
#[inline] #[inline]
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).unwrap_u8() == 1 bool::from(self.ct_eq(other))
} }
} }
@ -361,7 +361,7 @@ fn test_equality() {
let eq = a == b; let eq = a == b;
let ct_eq = a.ct_eq(&b); let ct_eq = a.ct_eq(&b);
assert_eq!(eq, ct_eq.unwrap_u8() == 1); assert_eq!(eq, bool::from(ct_eq));
eq eq
} }
@ -788,7 +788,7 @@ fn test_inversion() {
assert_eq!(a.invert().unwrap(), b); assert_eq!(a.invert().unwrap(), b);
assert!(Fp2::zero().invert().is_none().unwrap_u8() == 1); assert!(bool::from(Fp2::zero().invert().is_none()));
} }
#[test] #[test]

View File

@ -54,7 +54,7 @@ impl ConstantTimeEq for Scalar {
impl PartialEq for Scalar { impl PartialEq for Scalar {
#[inline] #[inline]
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).unwrap_u8() == 1 bool::from(self.ct_eq(other))
} }
} }
@ -834,55 +834,45 @@ fn test_from_bytes() {
); );
// -1 should work // -1 should work
assert!( assert!(bool::from(
Scalar::from_bytes(&[ Scalar::from_bytes(&[
0, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8, 0, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115 216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115
]) ])
.is_some() .is_some()
.unwrap_u8() ));
== 1
);
// modulus is invalid // modulus is invalid
assert!( assert!(bool::from(
Scalar::from_bytes(&[ Scalar::from_bytes(&[
1, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8, 1, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115 216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115
]) ])
.is_none() .is_none()
.unwrap_u8() ));
== 1
);
// Anything larger than the modulus is invalid // Anything larger than the modulus is invalid
assert!( assert!(bool::from(
Scalar::from_bytes(&[ Scalar::from_bytes(&[
2, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8, 2, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115 216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115
]) ])
.is_none() .is_none()
.unwrap_u8() ));
== 1 assert!(bool::from(
);
assert!(
Scalar::from_bytes(&[ Scalar::from_bytes(&[
1, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8, 1, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 58, 51, 72, 125, 157, 41, 83, 167, 237, 115 216, 58, 51, 72, 125, 157, 41, 83, 167, 237, 115
]) ])
.is_none() .is_none()
.unwrap_u8() ));
== 1 assert!(bool::from(
);
assert!(
Scalar::from_bytes(&[ Scalar::from_bytes(&[
1, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8, 1, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 116 216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 116
]) ])
.is_none() .is_none()
.unwrap_u8() ));
== 1
);
} }
#[test] #[test]
@ -1083,7 +1073,7 @@ fn test_squaring() {
#[test] #[test]
fn test_inversion() { fn test_inversion() {
assert_eq!(Scalar::zero().invert().is_none().unwrap_u8(), 1); assert!(bool::from(Scalar::zero().invert().is_none()));
assert_eq!(Scalar::one().invert().unwrap(), Scalar::one()); assert_eq!(Scalar::one().invert().unwrap(), Scalar::one());
assert_eq!((-&Scalar::one()).invert().unwrap(), -&Scalar::one()); assert_eq!((-&Scalar::one()).invert().unwrap(), -&Scalar::one());
@ -1143,7 +1133,7 @@ fn test_sqrt() {
for _ in 0..100 { for _ in 0..100 {
let square_root = square.sqrt(); let square_root = square.sqrt();
if square_root.is_none().unwrap_u8() == 1 { if bool::from(square_root.is_none()) {
none_count += 1; none_count += 1;
} else { } else {
assert_eq!(square_root.unwrap() * square_root.unwrap(), square); assert_eq!(square_root.unwrap() * square_root.unwrap(), square);