Return constants and sqrt impls separately for individual rendering

This commit is contained in:
Jack Grigg
2018-07-01 22:43:22 +01:00
parent 5a48059a14
commit b0b754ba04

View File

@@ -52,14 +52,17 @@ pub fn prime_field(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let mut gen = proc_macro2::TokenStream::new(); let mut gen = proc_macro2::TokenStream::new();
gen.extend(prime_field_repr_impl(&repr_ident, limbs)); let (constants_impl, sqrt_impl) = prime_field_constants_and_sqrt(
gen.extend(prime_field_constants_and_sqrt(
&ast.ident, &ast.ident,
&repr_ident, &repr_ident,
modulus, modulus,
limbs, limbs,
generator, generator,
)); );
gen.extend(prime_field_repr_impl(&repr_ident, limbs));
gen.extend(constants_impl);
gen.extend(sqrt_impl);
gen.extend(prime_field_impl(&ast.ident, &repr_ident, limbs)); gen.extend(prime_field_impl(&ast.ident, &repr_ident, limbs));
// Return the generated impl // Return the generated impl
@@ -398,7 +401,7 @@ fn prime_field_constants_and_sqrt(
modulus: BigUint, modulus: BigUint,
limbs: usize, limbs: usize,
generator: BigUint, generator: BigUint,
) -> proc_macro2::TokenStream { ) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
let modulus_num_bits = biguint_num_bits(modulus.clone()); let modulus_num_bits = biguint_num_bits(modulus.clone());
// The number of bits we should "shave" from a randomly sampled reputation, i.e., // The number of bits we should "shave" from a randomly sampled reputation, i.e.,
@@ -540,7 +543,7 @@ fn prime_field_constants_and_sqrt(
} }
inv = inv.wrapping_neg(); inv = inv.wrapping_neg();
quote! { (quote! {
/// This is the modulus m of the prime field /// This is the modulus m of the prime field
const MODULUS: #repr = #repr([#(#modulus,)*]); const MODULUS: #repr = #repr([#(#modulus,)*]);
@@ -569,9 +572,7 @@ fn prime_field_constants_and_sqrt(
/// 2^s root of unity computed by GENERATOR^t /// 2^s root of unity computed by GENERATOR^t
const ROOT_OF_UNITY: #repr = #repr(#root_of_unity); const ROOT_OF_UNITY: #repr = #repr(#root_of_unity);
}, sqrt_impl)
#sqrt_impl
}
} }
/// Implement PrimeField for the derived type. /// Implement PrimeField for the derived type.