Integrate changes to trait impls from pairing into ff_derive

This commit is contained in:
Jack Grigg
2018-07-01 00:05:56 -04:00
parent 29a9161981
commit 5a48059a14

View File

@@ -254,7 +254,7 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
#[inline(always)] #[inline(always)]
fn mul2(&mut self) { fn mul2(&mut self) {
let mut last = 0; let mut last = 0;
for i in self.0.iter_mut() { for i in &mut self.0 {
let tmp = *i >> 63; let tmp = *i >> 63;
*i <<= 1; *i <<= 1;
*i |= last; *i |= last;
@@ -484,42 +484,40 @@ fn prime_field_constants_and_sqrt(
// Tonelli-Shank's algorithm for q mod 16 = 1 // Tonelli-Shank's algorithm for q mod 16 = 1
// https://eprint.iacr.org/2012/685.pdf (page 12, algorithm 5) // https://eprint.iacr.org/2012/685.pdf (page 12, algorithm 5)
if self.is_zero() { match self.legendre() {
return Some(*self); ::ff::LegendreSymbol::Zero => Some(*self),
} ::ff::LegendreSymbol::QuadraticNonResidue => None,
::ff::LegendreSymbol::QuadraticResidue => {
let mut c = #name(ROOT_OF_UNITY);
let mut r = self.pow(#t_plus_1_over_2);
let mut t = self.pow(#t);
let mut m = S;
if self.pow(#mod_minus_1_over_2) != Self::one() { while t != Self::one() {
None let mut i = 1;
} else { {
let mut c = #name(#repr(#root_of_unity)); let mut t2i = t;
let mut r = self.pow(#t_plus_1_over_2);
let mut t = self.pow(#t);
let mut m = #s;
while t != Self::one() {
let mut i = 1;
{
let mut t2i = t;
t2i.square();
loop {
if t2i == Self::one() {
break;
}
t2i.square(); t2i.square();
i += 1; loop {
if t2i == Self::one() {
break;
}
t2i.square();
i += 1;
}
} }
}
for _ in 0..(m - i - 1) { for _ in 0..(m - i - 1) {
c.square();
}
r.mul_assign(&c);
c.square(); c.square();
t.mul_assign(&c);
m = i;
} }
r.mul_assign(&c);
c.square();
t.mul_assign(&c);
m = i;
}
Some(r) Some(r)
}
} }
} }
} }
@@ -681,23 +679,20 @@ fn prime_field_impl(
} }
for i in 1..(limbs * 2) { for i in 1..(limbs * 2) {
let k = get_temp(i); let temp0 = get_temp(limbs * 2 - i);
let temp1 = get_temp(limbs * 2 - i - 1);
if i == 1 { if i == 1 {
gen.extend(quote!{ gen.extend(quote!{
let tmp0 = #k >> 63; let #temp0 = #temp1 >> 63;
let #k = #k << 1;
}); });
} else if i == (limbs * 2 - 1) { } else if i == (limbs * 2 - 1) {
gen.extend(quote!{ gen.extend(quote!{
let #k = tmp0; let #temp0 = #temp0 << 1;
}); });
} else { } else {
gen.extend(quote!{ gen.extend(quote!{
let tmp1 = #k >> 63; let #temp0 = (#temp0 << 1) | (#temp1 >> 63);
let #k = #k << 1;
let #k = #k | tmp0;
let tmp0 = tmp1;
}); });
} }
} }
@@ -796,6 +791,8 @@ fn prime_field_impl(
proc_macro2::Punct::new(',', proc_macro2::Spacing::Alone), proc_macro2::Punct::new(',', proc_macro2::Spacing::Alone),
); );
let top_limb_index = limbs - 1;
quote!{ quote!{
impl Copy for #name { } impl Copy for #name { }
@@ -846,9 +843,10 @@ fn prime_field_impl(
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self { fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
loop { loop {
let mut tmp = #name(#repr::rand(rng)); let mut tmp = #name(#repr::rand(rng));
for _ in 0..REPR_SHAVE_BITS {
tmp.0.div2(); // Mask away the unused bits at the beginning.
} tmp.0.as_mut()[#top_limb_index] &= 0xffffffffffffffff >> REPR_SHAVE_BITS;
if tmp.is_valid() { if tmp.is_valid() {
return tmp return tmp
} }