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:
72
old/unsigned/rand.rs
Normal file
72
old/unsigned/rand.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
macro_rules! random_impls {
|
||||
($name: ident, $uniform: ident) => {
|
||||
impl Distribution<$name> for Standard {
|
||||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $name
|
||||
{
|
||||
let mut res = $name::zero();
|
||||
|
||||
for x in res.value.iter_mut() {
|
||||
*x = rng.next_u64();
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
pub struct $uniform {
|
||||
low: $name,
|
||||
high: $name,
|
||||
inclusive: bool
|
||||
}
|
||||
|
||||
impl UniformSampler for $uniform {
|
||||
type X = $name;
|
||||
|
||||
fn new<B1,B2>(low: B1, high: B2) -> Self
|
||||
where B1: SampleBorrow<Self::X> + Sized,
|
||||
B2: SampleBorrow<Self::X> + Sized
|
||||
{
|
||||
$uniform {
|
||||
low: low.borrow().clone(),
|
||||
high: high.borrow().clone(),
|
||||
inclusive: false
|
||||
}
|
||||
}
|
||||
|
||||
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
|
||||
where B1: SampleBorrow<Self::X> + Sized,
|
||||
B2: SampleBorrow<Self::X> + Sized
|
||||
{
|
||||
$uniform {
|
||||
low: low.borrow().clone(),
|
||||
high: high.borrow().clone(),
|
||||
inclusive: true
|
||||
}
|
||||
}
|
||||
|
||||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
|
||||
loop {
|
||||
let candidate = rng.gen();
|
||||
|
||||
if candidate < self.low {
|
||||
continue;
|
||||
}
|
||||
|
||||
if candidate > self.high {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !self.inclusive && (candidate == self.high) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SampleUniform for $name {
|
||||
type Sampler = $uniform;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user