Browse Source

[MOVEONLY] Move generated code around slightly in ff_derive

This reduces the differences in the generated code between the current
implementations of Fq and Fr in pairing, and their derived versions.
master
Jack Grigg 6 years ago
parent
commit
623dbd0d74
No known key found for this signature in database
GPG Key ID: 1B8D649257DB0829
  1. 40
      ff_derive/src/lib.rs

40
ff_derive/src/lib.rs

@ -60,10 +60,10 @@ pub fn prime_field(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
generator, generator,
); );
gen.extend(prime_field_repr_impl(&repr_ident, limbs));
gen.extend(constants_impl); gen.extend(constants_impl);
gen.extend(sqrt_impl); gen.extend(prime_field_repr_impl(&repr_ident, limbs));
gen.extend(prime_field_impl(&ast.ident, &repr_ident, limbs)); gen.extend(prime_field_impl(&ast.ident, &repr_ident, limbs));
gen.extend(sqrt_impl);
// Return the generated impl // Return the generated impl
gen.into() gen.into()
@ -124,13 +124,6 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
#[derive(Copy, Clone, PartialEq, Eq, Default)] #[derive(Copy, Clone, PartialEq, Eq, Default)]
pub struct #repr(pub [u64; #limbs]); pub struct #repr(pub [u64; #limbs]);
impl ::rand::Rand for #repr {
#[inline(always)]
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
#repr(rng.gen())
}
}
impl ::std::fmt::Debug for #repr impl ::std::fmt::Debug for #repr
{ {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
@ -143,6 +136,13 @@ 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"));
@ -217,17 +217,6 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
self.0.iter().all(|&e| e == 0) self.0.iter().all(|&e| e == 0)
} }
#[inline(always)]
fn div2(&mut self) {
let mut t = 0;
for i in self.0.iter_mut().rev() {
let t2 = *i << 63;
*i >>= 1;
*i |= t;
t = t2;
}
}
#[inline(always)] #[inline(always)]
fn shr(&mut self, mut n: u32) { fn shr(&mut self, mut n: u32) {
if n as usize >= 64 * #limbs { if n as usize >= 64 * #limbs {
@ -254,6 +243,17 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
} }
} }
#[inline(always)]
fn div2(&mut self) {
let mut t = 0;
for i in self.0.iter_mut().rev() {
let t2 = *i << 63;
*i >>= 1;
*i |= t;
t = t2;
}
}
#[inline(always)] #[inline(always)]
fn mul2(&mut self) { fn mul2(&mut self) {
let mut last = 0; let mut last = 0;

Loading…
Cancel
Save