Create a modular multiplication trait, and build a slow implementation using mod.
This commit is contained in:
@@ -1,8 +1,13 @@
|
|||||||
use cryptonum::{U192, U256, U384, U512, U576,
|
use cryptonum::{U192, U256, U384, U512, U576,
|
||||||
U1024, U2048, U3072, U4096, U8192,
|
U1024, U2048, U3072, U4096, U8192,
|
||||||
U15360};
|
U15360};
|
||||||
|
use cryptonum::division::divmod;
|
||||||
use std::ops::{Mul,MulAssign};
|
use std::ops::{Mul,MulAssign};
|
||||||
|
|
||||||
|
pub trait ModMul<T=Self> {
|
||||||
|
fn modmul(&mut self, x: &Self, m: &T);
|
||||||
|
}
|
||||||
|
|
||||||
// This is algorithm 14.12 from "Handbook of Applied Cryptography"
|
// This is algorithm 14.12 from "Handbook of Applied Cryptography"
|
||||||
pub fn raw_multiplication(x: &[u64], y: &[u64], w: &mut [u64])
|
pub fn raw_multiplication(x: &[u64], y: &[u64], w: &mut [u64])
|
||||||
{
|
{
|
||||||
@@ -77,6 +82,29 @@ macro_rules! generate_multipliers
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ModMul for $name {
|
||||||
|
fn modmul(&mut self, x: &$name, m: &$name) {
|
||||||
|
let mut mulres = Vec::with_capacity(2 * self.values.len());
|
||||||
|
mulres.resize(2 * self.values.len(), 0);
|
||||||
|
raw_multiplication(&self.values, &x.values, &mut mulres);
|
||||||
|
let mut widerm = Vec::with_capacity(mulres.len());
|
||||||
|
widerm.extend_from_slice(&m.values);
|
||||||
|
widerm.resize(mulres.len(), 0);
|
||||||
|
let mut dead = Vec::with_capacity(widerm.len());
|
||||||
|
dead.resize(widerm.len(), 0);
|
||||||
|
let mut answer = Vec::with_capacity(widerm.len());
|
||||||
|
answer.resize(widerm.len(), 0);
|
||||||
|
divmod(&mulres, &widerm, &mut dead, &mut answer);
|
||||||
|
for i in 0..answer.len() {
|
||||||
|
if i < self.values.len() {
|
||||||
|
self.values[i] = answer[i];
|
||||||
|
} else {
|
||||||
|
assert_eq!(answer[i], 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,66 +120,99 @@ generate_multipliers!(U4096, 4096);
|
|||||||
generate_multipliers!(U8192, 8192);
|
generate_multipliers!(U8192, 8192);
|
||||||
generate_multipliers!(U15360, 15360);
|
generate_multipliers!(U15360, 15360);
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
use cryptonum::Decoder;
|
|
||||||
#[cfg(test)]
|
|
||||||
use testing::run_test;
|
|
||||||
|
|
||||||
macro_rules! generate_tests {
|
macro_rules! generate_tests {
|
||||||
($name: ident, $testname: ident) => (
|
( $( $name:ident ),* ) => {
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
#[allow(non_snake_case)]
|
mod normal {
|
||||||
fn $testname() {
|
use cryptonum::Decoder;
|
||||||
let fname = format!("tests/math/multiplication{}.test",
|
use super::*;
|
||||||
stringify!($name));
|
use testing::run_test;
|
||||||
run_test(fname.to_string(), 3, |case| {
|
|
||||||
let (neg0, abytes) = case.get("a").unwrap();
|
|
||||||
let (neg1, bbytes) = case.get("b").unwrap();
|
|
||||||
let (neg2, cbytes) = case.get("c").unwrap();
|
|
||||||
|
|
||||||
assert!(!neg0 && !neg1 && !neg2);
|
$(
|
||||||
let mut a = $name::from_bytes(abytes);
|
#[test]
|
||||||
let b = $name::from_bytes(bbytes);
|
#[allow(non_snake_case)]
|
||||||
let c = $name::from_bytes(cbytes);
|
fn $name() {
|
||||||
assert_eq!(&a * &b, c);
|
let fname = format!("tests/math/multiplication{}.test",
|
||||||
a *= b;
|
stringify!($name));
|
||||||
assert_eq!(a, c);
|
run_test(fname.to_string(), 3, |case| {
|
||||||
});
|
let (neg0, abytes) = case.get("a").unwrap();
|
||||||
|
let (neg1, bbytes) = case.get("b").unwrap();
|
||||||
|
let (neg2, cbytes) = case.get("c").unwrap();
|
||||||
|
|
||||||
|
assert!(!neg0 && !neg1 && !neg2);
|
||||||
|
let mut a = $name::from_bytes(abytes);
|
||||||
|
let b = $name::from_bytes(bbytes);
|
||||||
|
let c = $name::from_bytes(cbytes);
|
||||||
|
assert_eq!(&a * &b, c);
|
||||||
|
a *= b;
|
||||||
|
assert_eq!(a, c);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)*
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
($name: ident, $testname: ident, $dblname: ident, $doubletest: ident) => (
|
#[cfg(test)]
|
||||||
generate_tests!($name, $testname);
|
mod expanding {
|
||||||
#[test]
|
use cryptonum::encoding::{Decoder,raw_decoder};
|
||||||
#[allow(non_snake_case)]
|
use super::*;
|
||||||
fn $doubletest() {
|
use testing::run_test;
|
||||||
let fname = format!("tests/math/expandingmul{}.test",
|
|
||||||
stringify!($name));
|
|
||||||
run_test(fname.to_string(), 3, |case| {
|
|
||||||
let (neg0, abytes) = case.get("a").unwrap();
|
|
||||||
let (neg1, bbytes) = case.get("b").unwrap();
|
|
||||||
let (neg2, cbytes) = case.get("c").unwrap();
|
|
||||||
|
|
||||||
assert!(!neg0 && !neg1 && !neg2);
|
$(
|
||||||
let a = $name::from_bytes(abytes);
|
#[test]
|
||||||
let b = $name::from_bytes(bbytes);
|
#[allow(non_snake_case)]
|
||||||
let c = $dblname::from_bytes(cbytes);
|
fn $name() {
|
||||||
let mut r = $dblname::new();
|
let fname = format!("tests/math/expandingmul{}.test",
|
||||||
raw_multiplication(&a.values, &b.values, &mut r.values);
|
stringify!($name));
|
||||||
assert_eq!(c, r);
|
run_test(fname.to_string(), 3, |case| {
|
||||||
});
|
let (neg0, abytes) = case.get("a").unwrap();
|
||||||
|
let (neg1, bbytes) = case.get("b").unwrap();
|
||||||
|
let (neg2, cbytes) = case.get("c").unwrap();
|
||||||
|
|
||||||
|
assert!(!neg0 && !neg1 && !neg2);
|
||||||
|
let a = $name::from_bytes(abytes);
|
||||||
|
let b = $name::from_bytes(bbytes);
|
||||||
|
let mut c = Vec::with_capacity(a.values.len() * 2);
|
||||||
|
c.resize(a.values.len() * 2, 0);
|
||||||
|
raw_decoder(&cbytes, &mut c);
|
||||||
|
let mut r = Vec::with_capacity(c.len());
|
||||||
|
r.resize(c.len(), 0);
|
||||||
|
raw_multiplication(&a.values, &b.values, &mut r);
|
||||||
|
assert_eq!(c, r);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)*
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod slow_modular {
|
||||||
|
use cryptonum::Decoder;
|
||||||
|
use super::*;
|
||||||
|
use testing::run_test;
|
||||||
|
|
||||||
|
$(
|
||||||
|
#[test]
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
fn $name() {
|
||||||
|
let fname = format!("tests/math/modmul{}.test",
|
||||||
|
stringify!($name));
|
||||||
|
run_test(fname.to_string(), 4, |case| {
|
||||||
|
let (neg0, abytes) = case.get("a").unwrap();
|
||||||
|
let (neg1, bbytes) = case.get("b").unwrap();
|
||||||
|
let (neg2, mbytes) = case.get("m").unwrap();
|
||||||
|
let (neg3, cbytes) = case.get("c").unwrap();
|
||||||
|
|
||||||
|
assert!(!neg0 && !neg1 && !neg2 && !neg3);
|
||||||
|
let mut a = $name::from_bytes(abytes);
|
||||||
|
let b = $name::from_bytes(bbytes);
|
||||||
|
let m = $name::from_bytes(mbytes);
|
||||||
|
let c = $name::from_bytes(cbytes);
|
||||||
|
a.modmul(&b, &m);
|
||||||
|
assert_eq!(a, c);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_tests!(U192,u192,U384,expandingU384);
|
generate_tests!(U192, U256, U384, U512, U576, U1024, U2048, U3072, U4096, U8192, U15360);
|
||||||
generate_tests!(U256,u256,U512,expandingU512);
|
|
||||||
generate_tests!(U384,u384);
|
|
||||||
generate_tests!(U512,u512,U1024,expandingU1024);
|
|
||||||
generate_tests!(U576,u576);
|
|
||||||
generate_tests!(U1024,u1024,U2048,expandingU2048);
|
|
||||||
generate_tests!(U2048,u2048,U4096,expandingU4096);
|
|
||||||
generate_tests!(U3072,u3072);
|
|
||||||
generate_tests!(U4096,u4096,U8192,expandingU8192);
|
|
||||||
generate_tests!(U8192,u8192);
|
|
||||||
generate_tests!(U15360,u15360);
|
|
||||||
@@ -13,6 +13,7 @@ testTypes = [("addition", addTest),
|
|||||||
("subtraction", subTest),
|
("subtraction", subTest),
|
||||||
("multiplication", mulTest),
|
("multiplication", mulTest),
|
||||||
("expandingmul", expmulTest),
|
("expandingmul", expmulTest),
|
||||||
|
("modmul", modmulTest),
|
||||||
("squaring", squareTest),
|
("squaring", squareTest),
|
||||||
("division", divTest)
|
("division", divTest)
|
||||||
]
|
]
|
||||||
@@ -96,6 +97,19 @@ expmulTest bitsize gen0 = (res, gen2)
|
|||||||
("b", showHex b' ""),
|
("b", showHex b' ""),
|
||||||
("c", showHex c "")]
|
("c", showHex c "")]
|
||||||
|
|
||||||
|
modmulTest :: Int -> StdGen -> (Map String String, StdGen)
|
||||||
|
modmulTest bitsize gen0 = (res, gen2)
|
||||||
|
where
|
||||||
|
(a, gen1) = random gen0
|
||||||
|
(b, gen2) = random gen1
|
||||||
|
(m, gen3) = random gen2
|
||||||
|
[a',b',m'] = splitMod bitsize [a,b,m]
|
||||||
|
c = (a' * b') `mod` m'
|
||||||
|
res = Map.fromList [("a", showHex a' ""),
|
||||||
|
("b", showHex b' ""),
|
||||||
|
("m", showHex m' ""),
|
||||||
|
("c", showHex c "")]
|
||||||
|
|
||||||
squareTest :: Int -> StdGen -> (Map String String, StdGen)
|
squareTest :: Int -> StdGen -> (Map String String, StdGen)
|
||||||
squareTest bitsize gen0 = (res, gen1)
|
squareTest bitsize gen0 = (res, gen1)
|
||||||
where
|
where
|
||||||
|
|||||||
4000
tests/math/modmulU1024.test
Normal file
4000
tests/math/modmulU1024.test
Normal file
File diff suppressed because it is too large
Load Diff
4000
tests/math/modmulU15360.test
Normal file
4000
tests/math/modmulU15360.test
Normal file
File diff suppressed because it is too large
Load Diff
4000
tests/math/modmulU192.test
Normal file
4000
tests/math/modmulU192.test
Normal file
File diff suppressed because it is too large
Load Diff
4000
tests/math/modmulU2048.test
Normal file
4000
tests/math/modmulU2048.test
Normal file
File diff suppressed because it is too large
Load Diff
4000
tests/math/modmulU256.test
Normal file
4000
tests/math/modmulU256.test
Normal file
File diff suppressed because it is too large
Load Diff
4000
tests/math/modmulU3072.test
Normal file
4000
tests/math/modmulU3072.test
Normal file
File diff suppressed because it is too large
Load Diff
4000
tests/math/modmulU384.test
Normal file
4000
tests/math/modmulU384.test
Normal file
File diff suppressed because it is too large
Load Diff
4000
tests/math/modmulU4096.test
Normal file
4000
tests/math/modmulU4096.test
Normal file
File diff suppressed because it is too large
Load Diff
4000
tests/math/modmulU512.test
Normal file
4000
tests/math/modmulU512.test
Normal file
File diff suppressed because it is too large
Load Diff
4000
tests/math/modmulU576.test
Normal file
4000
tests/math/modmulU576.test
Normal file
File diff suppressed because it is too large
Load Diff
4000
tests/math/modmulU8192.test
Normal file
4000
tests/math/modmulU8192.test
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user