Give credit where credit is due.
This commit is contained in:
@@ -3,28 +3,29 @@ use cryptonum::{U192, U256, U384, U512, U576,
|
|||||||
U15360};
|
U15360};
|
||||||
use std::ops::{Mul,MulAssign};
|
use std::ops::{Mul,MulAssign};
|
||||||
|
|
||||||
fn raw_multiplication(x: &[u64], y: &[u64], z: &mut [u64])
|
// This is algorithm 14.12 from "Handbook of Applied Cryptography"
|
||||||
|
fn raw_multiplication(x: &[u64], y: &[u64], w: &mut [u64])
|
||||||
{
|
{
|
||||||
assert_eq!(x.len(), y.len());
|
assert_eq!(x.len(), y.len());
|
||||||
assert_eq!(x.len() * 2, z.len());
|
assert_eq!(x.len() * 2, w.len());
|
||||||
|
|
||||||
// clear out the destination array, because we're going to use it as a
|
// clear out the destination array, because we're going to use it as a
|
||||||
// temporary
|
// temporary
|
||||||
for i in 0..z.len() {
|
for i in 0..w.len() {
|
||||||
z[i] = 0;
|
w[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for i in 0..y.len() { // this may legitimately be off by one
|
for i in 0..y.len() { // this may legitimately be off by one
|
||||||
let mut carry = 0;
|
let mut carry = 0;
|
||||||
for j in 0..x.len() { // ditto
|
for j in 0..x.len() { // ditto
|
||||||
let old = z[i+j] as u128;
|
let old = w[i+j] as u128;
|
||||||
let x128 = x[j] as u128;
|
let x128 = x[j] as u128;
|
||||||
let y128 = y[i] as u128;
|
let y128 = y[i] as u128;
|
||||||
let uv = old + (x128 * y128) + carry;
|
let uv = old + (x128 * y128) + carry;
|
||||||
z[i+j] = uv as u64;
|
w[i+j] = uv as u64;
|
||||||
carry = uv >> 64;
|
carry = uv >> 64;
|
||||||
}
|
}
|
||||||
z[i+x.len()] = carry as u64;
|
w[i+x.len()] = carry as u64;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user