Split the CryptoNum trait into pieces, in preparation for negative numbers.
This commit is contained in:
@@ -225,9 +225,7 @@ macro_rules! construct_unsigned {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CryptoNum for $type {
|
impl CryptoNumBase for $type {
|
||||||
type BarrettMu = $barrett;
|
|
||||||
|
|
||||||
fn zero() -> $type {
|
fn zero() -> $type {
|
||||||
$type { contents: [0; $count] }
|
$type { contents: [0; $count] }
|
||||||
}
|
}
|
||||||
@@ -257,12 +255,10 @@ macro_rules! construct_unsigned {
|
|||||||
from_to!($type, $count, u16, from_u16, to_u16);
|
from_to!($type, $count, u16, from_u16, to_u16);
|
||||||
from_to!($type, $count, u32, from_u32, to_u32);
|
from_to!($type, $count, u32, from_u32, to_u32);
|
||||||
from_to!($type, $count, u64, from_u64, to_u64);
|
from_to!($type, $count, u64, from_u64, to_u64);
|
||||||
|
}
|
||||||
|
|
||||||
fn divmod(&self, a: &$type, q: &mut $type, r: &mut $type) {
|
|
||||||
generic_div(&self.contents, &a.contents,
|
|
||||||
&mut q.contents, &mut r.contents);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
impl CryptoNumSerialization for $type {
|
||||||
fn to_bytes(&self) -> Vec<u8> {
|
fn to_bytes(&self) -> Vec<u8> {
|
||||||
let mut res = Vec::with_capacity($count * 8);
|
let mut res = Vec::with_capacity($count * 8);
|
||||||
for x in self.contents.iter() {
|
for x in self.contents.iter() {
|
||||||
@@ -298,6 +294,10 @@ macro_rules! construct_unsigned {
|
|||||||
assert!(i == $count);
|
assert!(i == $count);
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CryptoNumFastMod for $type {
|
||||||
|
type BarrettMu = $barrett;
|
||||||
|
|
||||||
fn barrett_mu(&self) -> Option<$barrett> {
|
fn barrett_mu(&self) -> Option<$barrett> {
|
||||||
// Step #0: Don't divide by 0.
|
// Step #0: Don't divide by 0.
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
pub trait CryptoNum {
|
pub trait CryptoNumBase {
|
||||||
/// A related type that can hold the constant required for Barrett
|
|
||||||
/// reduction.
|
|
||||||
type BarrettMu;
|
|
||||||
|
|
||||||
/// Generate the zero value for this type.
|
/// Generate the zero value for this type.
|
||||||
fn zero() -> Self;
|
fn zero() -> Self;
|
||||||
/// Generate the maximum possible value for this type.
|
/// Generate the maximum possible value for this type.
|
||||||
@@ -33,9 +29,9 @@ pub trait CryptoNum {
|
|||||||
/// Convert this back into a `u64`. This is the equivalent of masking off
|
/// Convert this back into a `u64`. This is the equivalent of masking off
|
||||||
/// the lowest 64 bits and then casting to a `u64`.
|
/// the lowest 64 bits and then casting to a `u64`.
|
||||||
fn to_u64(&self) -> u64;
|
fn to_u64(&self) -> u64;
|
||||||
/// Simultaneously compute the quotient and remainder of this number and
|
}
|
||||||
/// the given divisor.
|
|
||||||
fn divmod(&self, a: &Self, q: &mut Self, r: &mut Self);
|
pub trait CryptoNumSerialization {
|
||||||
/// Convert a number to a series of bytes, in standard order (most to
|
/// Convert a number to a series of bytes, in standard order (most to
|
||||||
/// least significant)
|
/// least significant)
|
||||||
fn to_bytes(&self) -> Vec<u8>;
|
fn to_bytes(&self) -> Vec<u8>;
|
||||||
@@ -43,6 +39,13 @@ pub trait CryptoNum {
|
|||||||
/// must be greater than or equal to the size of the number, and must be
|
/// must be greater than or equal to the size of the number, and must be
|
||||||
/// a multiple of 8 bytes long. Unused bytes should be ignored.
|
/// a multiple of 8 bytes long. Unused bytes should be ignored.
|
||||||
fn from_bytes(&[u8]) -> Self;
|
fn from_bytes(&[u8]) -> Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait CryptoNumFastMod {
|
||||||
|
/// A related type that can hold the constant required for Barrett
|
||||||
|
/// reduction.
|
||||||
|
type BarrettMu;
|
||||||
|
|
||||||
/// Compute the Barett constant mu, using this as a modulus, which we can
|
/// Compute the Barett constant mu, using this as a modulus, which we can
|
||||||
/// use later to perform faster mod operations.
|
/// use later to perform faster mod operations.
|
||||||
fn barrett_mu(&self) -> Option<Self::BarrettMu>;
|
fn barrett_mu(&self) -> Option<Self::BarrettMu>;
|
||||||
|
|||||||
Reference in New Issue
Block a user