diff --git a/src/scalar.rs b/src/scalar.rs index 565d740..c5f3734 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -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 for Scalar { type Output = u8;