mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-04 20:24:10 +00:00
Implement constant time equality check for scalars.
This commit is contained in:
parent
32da4c7d50
commit
7b57be69fd
1 changed files with 21 additions and 0 deletions
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue