Migrate ff to rand_core 0.3 (used by rand 0.5)

This commit is contained in:
Jack Grigg 2019-07-10 18:35:05 -04:00
parent 661558e0c8
commit 8b6e6b15ae
3 changed files with 27 additions and 27 deletions

View File

@ -10,7 +10,7 @@ repository = "https://github.com/ebfull/ff"
[dependencies] [dependencies]
byteorder = "1" byteorder = "1"
rand = "0.4" rand_core = "0.3"
ff_derive = { version = "0.3.0", path = "ff_derive", optional = true } ff_derive = { version = "0.3.0", path = "ff_derive", optional = true }
[features] [features]

View File

@ -136,13 +136,6 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
} }
} }
impl ::rand::Rand for #repr {
#[inline(always)]
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
#repr(rng.gen())
}
}
impl ::std::fmt::Display for #repr { impl ::std::fmt::Display for #repr {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
try!(write!(f, "0x")); try!(write!(f, "0x"));
@ -839,22 +832,6 @@ fn prime_field_impl(
} }
} }
impl ::rand::Rand for #name {
/// Computes a uniformly random element using rejection sampling.
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
loop {
let mut tmp = #name(#repr::rand(rng));
// Mask away the unused bits at the beginning.
tmp.0.as_mut()[#top_limb_index] &= 0xffffffffffffffff >> REPR_SHAVE_BITS;
if tmp.is_valid() {
return tmp
}
}
}
}
impl From<#name> for #repr { impl From<#name> for #repr {
fn from(e: #name) -> #repr { fn from(e: #name) -> #repr {
e.into_repr() e.into_repr()
@ -904,6 +881,26 @@ fn prime_field_impl(
} }
impl ::ff::Field for #name { impl ::ff::Field for #name {
/// Computes a uniformly random element using rejection sampling.
fn random<R: ::rand_core::RngCore>(rng: &mut R) -> Self {
loop {
let mut tmp = {
let mut repr = [0u64; #limbs];
for i in 0..#limbs {
repr[i] = rng.next_u64();
}
#name(#repr(repr))
};
// Mask away the unused bits at the beginning.
tmp.0.as_mut()[#top_limb_index] &= 0xffffffffffffffff >> REPR_SHAVE_BITS;
if tmp.is_valid() {
return tmp
}
}
}
#[inline] #[inline]
fn zero() -> Self { fn zero() -> Self {
#name(#repr::from(0)) #name(#repr::from(0))

View File

@ -1,7 +1,7 @@
#![allow(unused_imports)] #![allow(unused_imports)]
extern crate byteorder; extern crate byteorder;
extern crate rand; extern crate rand_core;
#[cfg(feature = "derive")] #[cfg(feature = "derive")]
#[macro_use] #[macro_use]
@ -10,14 +10,18 @@ extern crate ff_derive;
#[cfg(feature = "derive")] #[cfg(feature = "derive")]
pub use ff_derive::*; pub use ff_derive::*;
use rand_core::RngCore;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
/// This trait represents an element of a field. /// This trait represents an element of a field.
pub trait Field: pub trait Field:
Sized + Eq + Copy + Clone + Send + Sync + fmt::Debug + fmt::Display + 'static + rand::Rand Sized + Eq + Copy + Clone + Send + Sync + fmt::Debug + fmt::Display + 'static
{ {
/// Returns an element chosen uniformly at random using a user-provided RNG.
fn random<R: RngCore>(rng: &mut R) -> Self;
/// Returns the zero element of the field, the additive identity. /// Returns the zero element of the field, the additive identity.
fn zero() -> Self; fn zero() -> Self;
@ -100,7 +104,6 @@ pub trait PrimeFieldRepr:
+ fmt::Debug + fmt::Debug
+ fmt::Display + fmt::Display
+ 'static + 'static
+ rand::Rand
+ AsRef<[u64]> + AsRef<[u64]>
+ AsMut<[u64]> + AsMut<[u64]>
+ From<u64> + From<u64>