Implement constant time equality check for scalars.

This commit is contained in:
Isis Lovecruft 2017-01-06 19:39:55 +00:00
parent 32da4c7d50
commit 7b57be69fd
Failed to extract signature

View file

@ -29,6 +29,7 @@
//! between two scalars, the `UnpackedScalar` struct is stored as
//! limbs.
use core::cmp::{Eq, PartialEq};
use core::ops::{Index, IndexMut};
use core::ops::{Neg};
@ -39,6 +40,7 @@ use rand::Rng;
use constants;
use field::{load3, load4};
use util::CTAssignable;
use util::arrays_equal_ct;
/// The `Scalar` struct represents an element in /l, where
///
@ -48,6 +50,25 @@ use util::CTAssignable;
#[derive(Copy, Clone)]
pub struct Scalar(pub [u8; 32]);
// XXX make CTEq traits
impl Eq for Scalar{}
impl PartialEq for Scalar {
/// Test equality between two `Scalar`s in constant time.
///
/// Returns
///
/// True if they are equal, and false otherwise.
fn eq(&self, other: &Self) -> bool {
let equal: u8 = arrays_equal_ct(&self.0, &other.0);
if equal == 1 {
return true;
} else {
return false;
}
}
}
impl Index<usize> for Scalar {
type Output = u8;