From 623dbd0d74504d6b5d598ef83c654a1091670222 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 1 Jul 2018 22:49:10 +0100 Subject: [PATCH] [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. --- ff_derive/src/lib.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/ff_derive/src/lib.rs b/ff_derive/src/lib.rs index 86ecf4a..e792cd8 100644 --- a/ff_derive/src/lib.rs +++ b/ff_derive/src/lib.rs @@ -60,10 +60,10 @@ pub fn prime_field(input: proc_macro::TokenStream) -> proc_macro::TokenStream { generator, ); - gen.extend(prime_field_repr_impl(&repr_ident, limbs)); 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(sqrt_impl); // Return the generated impl 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)] pub struct #repr(pub [u64; #limbs]); - impl ::rand::Rand for #repr { - #[inline(always)] - fn rand(rng: &mut R) -> Self { - #repr(rng.gen()) - } - } - impl ::std::fmt::Debug for #repr { 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(rng: &mut R) -> Self { + #repr(rng.gen()) + } + } + impl ::std::fmt::Display for #repr { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { 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) } - #[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)] fn shr(&mut self, mut n: u32) { 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)] fn mul2(&mut self) { let mut last = 0;