use alloc::vec::Vec;
#[must_use]
pub(crate) fn cmp(a: &[u8], b: &[u8]) -> core::cmp::Ordering {
let a = trim(a);
let b = trim(b);
a.len().cmp(&b.len()).then_with(|| a.cmp(b))
}
#[must_use]
pub(crate) fn in_range(s: &[u8], order: &[u8]) -> bool {
!is_zero(s) && cmp(s, order) == core::cmp::Ordering::Less
}
#[must_use]
pub(crate) fn is_high(s: &[u8], order: &[u8]) -> bool {
cmp(&double(s), order) == core::cmp::Ordering::Greater
}
#[must_use]
pub(crate) fn negate(s: &[u8], order: &[u8]) -> Vec<u8> {
if cmp(s, order) != core::cmp::Ordering::Less {
return s.to_vec();
}
let width = order.len();
let mut out = alloc::vec![0u8; width];
let mut borrow = 0i16;
for i in (0..width).rev() {
let sb = i16::from(byte_at(s, width, i));
let mut d = i16::from(order[i]) - sb - borrow;
if d < 0 {
d += 256;
borrow = 1;
} else {
borrow = 0;
}
out[i] = u8::try_from(d).unwrap_or(0);
}
out
}
#[must_use]
pub(crate) fn pad_to(s: &[u8], width: usize) -> Option<Vec<u8>> {
let t = trim(s);
if t.len() > width {
return None;
}
let mut out = alloc::vec![0u8; width];
out[width - t.len()..].copy_from_slice(t);
Some(out)
}
fn trim(v: &[u8]) -> &[u8] {
let mut i = 0;
while i < v.len() && v[i] == 0 {
i += 1;
}
&v[i..]
}
fn is_zero(v: &[u8]) -> bool {
v.iter().all(|b| *b == 0)
}
fn double(v: &[u8]) -> Vec<u8> {
let v = trim(v);
let mut out = Vec::with_capacity(v.len() + 1);
out.push(0);
out.extend_from_slice(v);
let mut carry = 0u8;
for b in out.iter_mut().rev() {
let doubled = (u16::from(*b) << 1) | u16::from(carry);
carry = u8::try_from(doubled >> 8).unwrap_or(0);
*b = u8::try_from(doubled & 0xff).unwrap_or(0);
}
out
}
fn byte_at(s: &[u8], width: usize, i: usize) -> u8 {
let s = trim(s);
if s.len() > width {
return 0;
}
let offset = width - s.len();
if i < offset { 0 } else { s[i - offset] }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::signature::Curve;
#[test]
fn comparison_ignores_leading_zeros() {
use core::cmp::Ordering;
assert_eq!(cmp(&[0, 0, 1], &[1]), Ordering::Equal);
assert_eq!(cmp(&[2], &[0, 1]), Ordering::Greater);
assert_eq!(cmp(&[1, 0], &[0xff]), Ordering::Greater);
}
#[test]
fn a_scalar_must_be_in_one_to_n_minus_one() {
let n = Curve::Secp256r1.order();
assert!(in_range(&[1], n));
assert!(!in_range(&[0], n), "zero is not invertible");
assert!(!in_range(n, n), "n itself is zero mod n");
let mut above = n.to_vec();
*above.last_mut().unwrap() = 0xff;
assert!(!in_range(&above, n), "beyond n is a different scalar");
}
#[test]
fn negating_twice_is_the_identity_on_every_curve() {
for c in Curve::ALL {
let n = c.order();
for probe in [
alloc::vec![1u8],
alloc::vec![0x7f; 8],
n[..n.len() - 1].to_vec(),
] {
if !in_range(&probe, n) {
continue;
}
let twin = negate(&probe, n);
assert!(in_range(&twin, n), "{c}");
assert_eq!(
trim(&negate(&twin, n)),
trim(&probe),
"{c}: n - (n - s) = s"
);
assert!(
is_high(&probe, n) != is_high(&twin, n),
"{c}: exactly one of a malleable pair is high"
);
}
}
}
#[test]
fn the_boundary_of_the_high_half_is_n_over_two() {
for c in Curve::ALL {
let n = c.order();
let half = shift_right_one(n);
assert!(!is_high(&half, n), "{c}: n/2 is not above n/2");
let mut plus = half.clone();
increment(&mut plus);
assert!(is_high(&plus, n), "{c}: n/2 + 1 is");
assert!(!is_high(&[1], n), "{c}");
}
}
#[test]
fn padding_refuses_a_scalar_that_does_not_fit() {
assert_eq!(pad_to(&[1], 4).unwrap(), alloc::vec![0, 0, 0, 1]);
assert_eq!(pad_to(&[0, 0, 1, 2], 2).unwrap(), alloc::vec![1, 2]);
assert!(pad_to(&[1, 2, 3], 2).is_none());
}
fn shift_right_one(v: &[u8]) -> Vec<u8> {
let mut out = alloc::vec![0u8; v.len()];
let mut carry = 0u8;
for (i, b) in v.iter().enumerate() {
out[i] = (b >> 1) | (carry << 7);
carry = b & 1;
}
out
}
fn increment(v: &mut [u8]) {
for b in v.iter_mut().rev() {
let (next, carry) = b.overflowing_add(1);
*b = next;
if !carry {
return;
}
}
}
}