Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

bump rand to v0.8 #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ exclude = [

[dependencies]
byteorder = "1"
rand = "0.4"
ff_derive_ce = { version = "0.10.*", optional = true }
# ff_derive_ce = { path = "ff_derive", optional = true }
rand = "0.8"
# ff_derive_ce = { version = "0.10.*", optional = true }
ff_derive_ce = { path = "ff_derive", optional = true }
hex = {version = "0.4"}

[features]
default = []
with-serde = ["ff_derive_ce/serde"]
derive = ["ff_derive_ce"]
asm_derive = ["derive", "ff_derive_ce/asm"]
1 change: 1 addition & 0 deletions ff_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ syn = "1"
[features]
default = []
asm = []
serde = []
17 changes: 11 additions & 6 deletions ff_derive/src/asm/asm_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,14 @@ pub fn prime_field_asm_impl(input: proc_macro::TokenStream) -> proc_macro::Token

// Implement PrimeFieldRepr for the wrapped ident `repr` with `limbs` limbs.
fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenStream {
let derive = if cfg!(feature = "serde") {
quote! { #[derive(Copy, Clone, PartialEq, Eq, Default, ::serde::Serialize, ::serde::Deserialize)] }
} else {
quote! { #[derive(Copy, Clone, PartialEq, Eq, Default)] }
};
quote! {

#[derive(Copy, Clone, PartialEq, Eq, Default)]
#derive
pub struct #repr(
pub [u64; #limbs]
);
Expand All @@ -124,9 +129,9 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
}
}

impl ::rand::Rand for #repr {
impl ::rand::distributions::Distribution<#repr> for ::rand::distributions::Standard {
#[inline(always)]
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
fn rand<R: ::rand::Rng>(rng: &mut R) -> #repr {
#repr(rng.gen())
}
}
Expand Down Expand Up @@ -662,11 +667,11 @@ fn prime_field_impl(
}
}

impl ::rand::Rand for #name {
impl ::rand::distributions::Distribution<#name> for ::rand::distributions::Standard {
/// Computes a uniformly random element using rejection sampling.
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
fn rand<R: ::rand::Rng>(rng: &mut R) -> #name {
loop {
let mut tmp = #name(#repr::rand(rng));
let mut tmp = #name(::rand::distributions::Standard.sample(rng));

// Mask away the unused bits at the beginning.
tmp.0.as_mut()[#top_limb_index] &= 0xffffffffffffffff >> REPR_SHAVE_BITS;
Expand Down
24 changes: 13 additions & 11 deletions ff_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ fn fetch_attr(name: &str, attrs: &[syn::Attribute]) -> Option<String> {
}
}
}
_ => {
panic!("attribute {} should be a string", name);
}
_ => continue
}
}
}
Expand All @@ -175,9 +173,14 @@ fn fetch_attr(name: &str, attrs: &[syn::Attribute]) -> Option<String> {

// Implement PrimeFieldRepr for the wrapped ident `repr` with `limbs` limbs.
fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenStream {
let derive = if cfg!(feature = "serde") {
quote! { #[derive(Copy, Clone, PartialEq, Eq, Default, ::serde::Serialize, ::serde::Deserialize)] }
} else {
quote! { #[derive(Copy, Clone, PartialEq, Eq, Default)] }
};
quote! {

#[derive(Copy, Clone, PartialEq, Eq, Default)]
#derive
pub struct #repr(
pub [u64; #limbs]
);
Expand All @@ -194,9 +197,8 @@ 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 {
impl ::rand::distributions::Distribution<#repr> for ::rand::distributions::Standard {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> #repr {
#repr(rng.gen())
}
}
Expand Down Expand Up @@ -1129,11 +1131,11 @@ fn prime_field_impl(
}
}

impl ::rand::Rand for #name {
impl ::rand::distributions::Distribution<#name> for ::rand::distributions::Standard {
/// Computes a uniformly random element using rejection sampling.
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> #name {
loop {
let mut tmp = #name(#repr::rand(rng));
let mut tmp = #name(::rand::distributions::Standard.sample(rng));

// Mask away the unused bits at the beginning.
tmp.0.as_mut()[#top_limb_index] &= TOP_LIMB_SHAVE_MASK;
Expand Down
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern crate byteorder;
extern crate rand;
extern crate hex as hex_ext;
use rand::distributions::{Distribution, Standard};
pub mod hex {
pub use hex_ext::*;
}
Expand All @@ -20,9 +21,13 @@ use std::fmt;
use std::hash;
use std::io::{self, Read, Write};

/// Backwards compatiablity Marker Rand trait
pub trait Rand: Sized {}
impl<T> Rand for T where Standard: Distribution<T> { }

/// This trait represents an element of a field.
pub trait Field:
Sized + Eq + Copy + Clone + Send + Sync + fmt::Debug + fmt::Display + 'static + rand::Rand + hash::Hash + Default
Sized + Eq + Copy + Clone + Send + Sync + fmt::Debug + fmt::Display + 'static + Rand + hash::Hash + Default
{
/// Returns the zero element of the field, the additive identity.
fn zero() -> Self;
Expand Down Expand Up @@ -106,7 +111,7 @@ pub trait PrimeFieldRepr:
+ fmt::Debug
+ fmt::Display
+ 'static
+ rand::Rand
+ Rand
+ AsRef<[u64]>
+ AsMut<[u64]>
+ From<u64>
Expand Down
2 changes: 1 addition & 1 deletion tester/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ edition = "2018"

[dependencies]
ff = {package = "ff_ce", path = "../", features = ["derive"]}
rand = "0.4"
rand = "0.8"

[dev-dependencies]
criterion = "0.3"
Expand Down
8 changes: 4 additions & 4 deletions tester/src/mul_variant0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ const NEGATIVE_ONE: Fs = Fs(FsRepr([0xaa9f02ab1d6124de, 0xb3524a6466112932, 0x73
#[derive(Copy, Clone, PartialEq, Eq, Default, Debug, Hash)]
pub struct FsRepr(pub [u64; 4]);

impl ::rand::Rand for FsRepr {
impl ::rand::distributions::Distribution<FsRepr> for ::rand::distributions::Standard {
#[inline(always)]
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
fn rand<R: ::rand::Rng>(rng: &mut R) -> FsRepr {
FsRepr(rng.gen())
}
}
Expand Down Expand Up @@ -235,10 +235,10 @@ impl ::std::fmt::Display for Fs
}
}

impl ::rand::Rand for Fs {
impl ::rand::distributions::Distribution<Fs> for ::rand::distributions::Standard {
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
loop {
let mut tmp = Fs(FsRepr::rand(rng));
let mut tmp = Fs(::rand::distributions::Standard.sample(rng));

// Mask away the unused bits at the beginning.
tmp.0.as_mut()[3] &= 0xffffffffffffffff >> REPR_SHAVE_BITS;
Expand Down
6 changes: 3 additions & 3 deletions tester/tmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ mod test_large_cios_field {
Ok(())
}
}
impl ::rand::Rand for FrRepr {
impl ::rand::distributions::Distribution<FrRepr> for ::rand::distributions::Standard {
#[inline(always)]
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
FrRepr(rng.gen())
Expand Down Expand Up @@ -380,11 +380,11 @@ mod test_large_cios_field {
))
}
}
impl ::rand::Rand for Fr {
impl ::rand::distributions::Distribution<Fr> for ::rand::distributions::Standard {
/// Computes a uniformly random element using rejection sampling.
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
loop {
let mut tmp = Fr(FrRepr::rand(rng));
let mut tmp = Fr(::rand::distributions::Standard.sample(rng));
tmp.0.as_mut()[3usize] &= 0xffffffffffffffff >> REPR_SHAVE_BITS;
if tmp.is_valid() {
return tmp;
Expand Down