mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-07 20:50:39 +00:00
Add a missing wrapping_sub in NAF computation.
Found by @3for; this only affected width-7 NAF computations, which were never used in the source tree (only width 5, optimal for dynamic cases, and 8, better for static cases). Closes #272
This commit is contained in:
parent
a174911c2b
commit
01d9e904e1
1 changed files with 31 additions and 2 deletions
|
|
@ -917,7 +917,7 @@ impl Scalar {
|
||||||
naf[pos] = window as i8;
|
naf[pos] = window as i8;
|
||||||
} else {
|
} else {
|
||||||
carry = 1;
|
carry = 1;
|
||||||
naf[pos] = (window as i8) - (width as i8);
|
naf[pos] = (window as i8).wrapping_sub(width as i8);
|
||||||
}
|
}
|
||||||
|
|
||||||
pos += w;
|
pos += w;
|
||||||
|
|
@ -1264,13 +1264,42 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn non_adjacent_form() {
|
fn non_adjacent_form_test_vector() {
|
||||||
let naf = A_SCALAR.non_adjacent_form(5);
|
let naf = A_SCALAR.non_adjacent_form(5);
|
||||||
for i in 0..256 {
|
for i in 0..256 {
|
||||||
assert_eq!(naf[i], A_NAF[i]);
|
assert_eq!(naf[i], A_NAF[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn non_adjacent_form_iter(w: usize, x: &Scalar) {
|
||||||
|
let naf = x.non_adjacent_form(w);
|
||||||
|
|
||||||
|
// Reconstruct the scalar from the computed NAF
|
||||||
|
let mut y = Scalar::zero();
|
||||||
|
for i in (0..256).rev() {
|
||||||
|
y += y;
|
||||||
|
let digit = if naf[i] < 0 {
|
||||||
|
-Scalar::from((-naf[i]) as u64)
|
||||||
|
} else {
|
||||||
|
Scalar::from(naf[i] as u64)
|
||||||
|
};
|
||||||
|
y += digit;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(*x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn non_adjacent_form_random() {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
for _ in 0..1_000 {
|
||||||
|
let x = Scalar::random(&mut rng);
|
||||||
|
for w in &[5, 6, 7, 8] {
|
||||||
|
non_adjacent_form_iter(*w, &x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_u64() {
|
fn from_u64() {
|
||||||
let val: u64 = 0xdeadbeefdeadbeef;
|
let val: u64 = 0xdeadbeefdeadbeef;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue