mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-06 20:41:14 +00:00
Make FieldElement's conditional_assign a trait impl
This commit is contained in:
parent
c92ebdd0ac
commit
74049e039e
1 changed files with 39 additions and 34 deletions
73
src/field.rs
73
src/field.rs
|
|
@ -25,6 +25,7 @@ use std::cmp::{Eq, PartialEq};
|
||||||
use std::ops::Neg;
|
use std::ops::Neg;
|
||||||
|
|
||||||
use util::byte_is_nonzero;
|
use util::byte_is_nonzero;
|
||||||
|
use util::CTAssignable;
|
||||||
|
|
||||||
/// FieldElements are represented as an array of ten "Limbs", which are radix
|
/// FieldElements are represented as an array of ten "Limbs", which are radix
|
||||||
/// 25.5, that is, each Limb of a FieldElement alternates between being
|
/// 25.5, that is, each Limb of a FieldElement alternates between being
|
||||||
|
|
@ -142,6 +143,44 @@ impl<'a> Neg for &'a FieldElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CTAssignable for FieldElement {
|
||||||
|
/// Conditionally assign another FieldElement to this one.
|
||||||
|
///
|
||||||
|
/// If `choice == 0`, replace `self` with `self`:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use curve25519_dalek::field::FieldElement;
|
||||||
|
/// # use curve25519_dalek::util::CTAssignable;
|
||||||
|
/// let f = FieldElement([1,1,1,1,1,1,1,1,1,1]);
|
||||||
|
/// let g = FieldElement([2,2,2,2,2,2,2,2,2,2]);
|
||||||
|
/// let mut h = FieldElement([1,1,1,1,1,1,1,1,1,1]);
|
||||||
|
/// h.conditional_assign(&g, 0);
|
||||||
|
/// assert!(h == f);
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// If `choice == 1`, replace `self` with `f`:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use curve25519_dalek::field::FieldElement;
|
||||||
|
/// # use curve25519_dalek::util::CTAssignable;
|
||||||
|
/// # let f = FieldElement([1,1,1,1,1,1,1,1,1,1]);
|
||||||
|
/// # let g = FieldElement([2,2,2,2,2,2,2,2,2,2]);
|
||||||
|
/// # let mut h = FieldElement([1,1,1,1,1,1,1,1,1,1]);
|
||||||
|
/// h.conditional_assign(&g, 1);
|
||||||
|
/// assert!(h == g);
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// # Preconditions
|
||||||
|
///
|
||||||
|
/// * `choice` in {0,1}
|
||||||
|
fn conditional_assign(&mut self, f: &FieldElement, choice: u8) {
|
||||||
|
let mask = -(choice as Limb);
|
||||||
|
for i in 0..10 {
|
||||||
|
self[i] ^= mask & (self[i] ^ f[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Convert an array of (at least) three bytes into an i64.
|
/// Convert an array of (at least) three bytes into an i64.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
@ -179,40 +218,6 @@ impl FieldElement {
|
||||||
FieldElement([ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
|
FieldElement([ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Conditionally assign another FieldElement to this one.
|
|
||||||
///
|
|
||||||
/// If `choice == 0`, replace `self` with `self`:
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # use curve25519_dalek::field::FieldElement;
|
|
||||||
/// let f = FieldElement([1,1,1,1,1,1,1,1,1,1]);
|
|
||||||
/// let g = FieldElement([2,2,2,2,2,2,2,2,2,2]);
|
|
||||||
/// let mut h = FieldElement([1,1,1,1,1,1,1,1,1,1]);
|
|
||||||
/// h.conditional_assign(&g, 0);
|
|
||||||
/// assert!(h == f);
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// If `choice == 1`, replace `self` with `f`:
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # use curve25519_dalek::field::FieldElement;
|
|
||||||
/// # let f = FieldElement([1,1,1,1,1,1,1,1,1,1]);
|
|
||||||
/// # let g = FieldElement([2,2,2,2,2,2,2,2,2,2]);
|
|
||||||
/// # let mut h = FieldElement([1,1,1,1,1,1,1,1,1,1]);
|
|
||||||
/// h.conditional_assign(&g, 1);
|
|
||||||
/// assert!(h == g);
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// # Preconditions
|
|
||||||
///
|
|
||||||
/// * `choice` in {0,1}
|
|
||||||
pub fn conditional_assign(&mut self, f: &FieldElement, choice: u8) {
|
|
||||||
let mask = -(choice as Limb);
|
|
||||||
for i in 0..10 {
|
|
||||||
self[i] ^= mask & (self[i] ^ f[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn combine_coeffs(input: &[i64;10]) -> FieldElement { //FeCombine
|
fn combine_coeffs(input: &[i64;10]) -> FieldElement { //FeCombine
|
||||||
let mut c = [0i64;10];
|
let mut c = [0i64;10];
|
||||||
let mut h = input.clone();
|
let mut h = input.clone();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue