Start experimenting with full generation of all of the numeric types.

Previously, we used a little bit of generation to drive a lot of Rust
macros. This works, but it's a little confusing to read and write. In
addition, we used a lot of implementations with variable timings based
on their input, which isn't great for crypto. This is the start of an
attempt to just generate all of the relevant Rust code directly, and to
use timing-channel resistant implementations for most of the routines.
This commit is contained in:
2019-07-15 17:39:06 -07:00
parent 666378b14b
commit fa872c951a
46 changed files with 696 additions and 203 deletions

135
old/signed/modinv.rs Normal file
View File

@@ -0,0 +1,135 @@
/// Computations of the modular inverse.
pub trait ModInv: Sized {
/// Compute the modular inverse of this number under the given
/// modulus, if it exists. If self is a, the modulus / argument
/// is phi, and the result is Some(m), then (a * m) % phi = 1.
fn modinv(&self, phi: &Self) -> Option<Self>;
}
macro_rules! smodinv_impls {
($name: ident, $bigger: ident) => {
impl ModInv for $name {
fn modinv(&self, phi: &$name) -> Option<$name>
{
let (_, mut b, g) = phi.egcd(&self);
if g != $bigger::from(1u64) {
return None;
}
let bigphi = $bigger::from(phi);
while b.is_negative() {
b += &bigphi;
}
if b > bigphi {
b -= &bigphi;
}
Some($name::from(b))
}
}
}
}
macro_rules! modinv_impls {
($name: ident, $sname: ident, $uname: ident) => {
impl ModInv for $name {
fn modinv(&self, phi: &$name) -> Option<$name>
{
let (_, mut b, g) = phi.egcd(&self);
if g != $sname::from(1i64) {
return None;
}
let sphi = $sname::from($uname::from(phi));
while b.is_negative() {
b += &sphi;
}
if b > sphi {
b -= &sphi;
}
Some($name::from($uname::from(b)))
}
}
};
}
#[cfg(test)]
macro_rules! generate_modinv_tests {
($sname: ident, $tname: ident, $mname: ident) => {
#[test]
fn $mname() {
generate_modinv_tests!(body $sname, $tname, $mname);
}
};
(ignore $sname: ident, $tname: ident, $mname: ident) => {
#[test]
#[ignore]
fn $mname() {
generate_modinv_tests!(body $sname, $tname, $mname);
}
};
(body $sname: ident, $tname: ident, $mname: ident) => {
let fname = build_test_path("modinv", stringify!($sname));
run_test(fname.to_string(), 3, |case| {
let (nega, abytes) = case.get("a").unwrap();
let (negb, bbytes) = case.get("b").unwrap();
let (negc, cbytes) = case.get("c").unwrap();
assert!(!nega && !negb && !negc);
let a = $tname::from_bytes(abytes);
let b = $tname::from_bytes(bbytes);
let c = $tname::from_bytes(cbytes);
match a.modinv(&b) {
None => assert!(false),
Some(myc) => {
assert_eq!(c, myc);
}
}
});
};
}
#[cfg(test)]
macro_rules! generate_smodinv_tests {
($sname: ident, $tname: ident, $mname: ident) => {
#[test]
fn $mname() {
generate_smodinv_tests!(body $sname, $tname, $mname);
}
};
(ignore $sname: ident, $tname: ident, $mname: ident) => {
#[test]
#[ignore]
fn $mname() {
generate_smodinv_tests!(body $sname, $tname, $mname);
}
};
(body $sname: ident, $tname: ident, $mname: ident) => {
let fname = build_test_path("modinv", stringify!($sname));
run_test(fname.to_string(), 3, |case| {
let (nega, abytes) = case.get("a").unwrap();
let (negb, bbytes) = case.get("b").unwrap();
let (negc, cbytes) = case.get("c").unwrap();
assert!(!negb && !negc);
let a = $sname::new(*nega, $tname::from_bytes(abytes));
let b = $sname::new(false, $tname::from_bytes(bbytes));
let c = $sname::new(false, $tname::from_bytes(cbytes));
match a.modinv(&b) {
None => assert!(false),
Some(myc) => {
assert_eq!(c, myc);
}
}
});
};
}