diff --git a/Cargo.toml b/Cargo.toml index e9656ba..d422494 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ff_ce" -version = "0.14.1" +version = "0.14.2" authors = ["Sean Bowe ", "Alex Gluchowski ", "Alex Vlasov "] @@ -27,4 +27,4 @@ serde = "1" [features] default = [] derive = ["ff_derive_ce"] -asm_derive = ["derive", "ff_derive_ce/asm"] \ No newline at end of file +asm_derive = ["derive", "ff_derive_ce/asm"] diff --git a/ff_derive/Cargo.toml b/ff_derive/Cargo.toml index 268004e..a4c9f18 100644 --- a/ff_derive/Cargo.toml +++ b/ff_derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ff_derive_ce" -version = "0.11.0" +version = "0.11.1" authors = ["Sean Bowe ", "Alex Gluchowski ", "Alex Vlasov "] diff --git a/ff_derive/src/asm/asm_derive.rs b/ff_derive/src/asm/asm_derive.rs index 3cd945a..7b22218 100644 --- a/ff_derive/src/asm/asm_derive.rs +++ b/ff_derive/src/asm/asm_derive.rs @@ -84,6 +84,11 @@ pub fn prime_field_asm_impl(input: proc_macro::TokenStream) -> proc_macro::Token assert!(can_use_optimistic_cios_mul, "Can only derive for moduluses that fit in 255 bits - epsilon"); assert!(can_use_optimistic_cios_sqr, "Can only derive for moduluses that fit in 254 bits - epsilon"); + let random_id = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .subsec_nanos(); + let mut gen = proc_macro2::TokenStream::new(); let (constants_impl, mont_inv, sqrt_impl) = prime_field_constants_with_inv_and_sqrt( @@ -92,11 +97,12 @@ pub fn prime_field_asm_impl(input: proc_macro::TokenStream) -> proc_macro::Token modulus, limbs, generator, + random_id ); gen.extend(constants_impl); gen.extend(prime_field_repr_impl(&repr_ident, limbs)); - gen.extend(prime_field_impl(&ast.ident, &repr_ident, mont_inv, limbs)); + gen.extend(prime_field_impl(&ast.ident, &repr_ident, mont_inv, limbs, random_id)); gen.extend(sqrt_impl); // Return the generated impl @@ -328,6 +334,7 @@ fn prime_field_constants_with_inv_and_sqrt( modulus: BigUint, limbs: usize, generator: BigUint, + random_id: u32 ) -> (proc_macro2::TokenStream, u64, proc_macro2::TokenStream) { let modulus_num_bits = biguint_num_bits(modulus.clone()); @@ -507,14 +514,16 @@ fn prime_field_constants_with_inv_and_sqrt( }; for i in 0..4 { - let m = get_temp_with_literal(MODULUS_PREFIX, i); - let n = get_temp_with_literal(MODULUS_NEGATED_PREFIX, i); + let m = get_temp_with_literal(&format!("{}{}_", MODULUS_PREFIX, random_id), i); + let n = get_temp_with_literal(&format!("{}{}_", MODULUS_NEGATED_PREFIX, random_id), i); let value = modulus[i]; let limb_neg = modulus_negated[i]; constants_gen.extend( quote!{ + #[no_mangle] static #m: u64 = #value; + #[no_mangle] static #n: u64 = #limb_neg; } ); @@ -529,6 +538,7 @@ fn prime_field_impl( repr: &syn::Ident, mont_inv: u64, limbs: usize, + random_id: u32, ) -> proc_macro2::TokenStream { // The parameter list for the mont_reduce() internal method. // r0: u64, mut r1: u64, mut r2: u64, ... @@ -608,14 +618,17 @@ fn prime_field_impl( proc_macro2::Punct::new(',', proc_macro2::Spacing::Alone), ); - let mul_asm_impl = mul_impl(mont_inv, MODULUS_PREFIX); - let sqr_asm_impl = sqr_impl(mont_inv, MODULUS_PREFIX); + let modulus_random_prefix = format!("{}{}_", MODULUS_PREFIX, random_id) + let modulus_neg_random_prefix = format!("{}{}_", MODULUS_NEGATED_PREFIX, random_id) + + let mul_asm_impl = mul_impl(mont_inv, &modulus_random_prefix); + let sqr_asm_impl = sqr_impl(mont_inv, &modulus_random_prefix); // let add_asm_impl = add_impl(MODULUS_PREFIX); - let add_asm_impl = add_impl(MODULUS_NEGATED_PREFIX); - let sub_asm_impl = sub_impl(MODULUS_PREFIX); + let add_asm_impl = add_impl(&modulus_neg_random_prefix); + let sub_asm_impl = sub_impl(&modulus_random_prefix); // let sub_asm_impl = sub_impl(MODULUS_NEGATED_PREFIX); // let double_asm_impl = double_impl(MODULUS_PREFIX); - let double_asm_impl = double_impl(MODULUS_NEGATED_PREFIX); + let double_asm_impl = double_impl(&modulus_neg_random_prefix); quote!{ impl ::std::marker::Copy for #name { } diff --git a/ff_derive/src/asm/impls_4.rs b/ff_derive/src/asm/impls_4.rs index aa1e86c..9a0da6a 100644 --- a/ff_derive/src/asm/impls_4.rs +++ b/ff_derive/src/asm/impls_4.rs @@ -22,7 +22,7 @@ pub(crate) fn mul_impl(mont_inv: u64, modulus_static_prefix: &str) -> proc_macro let mut r3: u64; unsafe { - asm!( + core::arch::asm!( // round 0 "mov rdx, qword ptr [{a_ptr} + 0]", "xor r8d, r8d", @@ -228,7 +228,7 @@ pub(crate) fn sqr_impl(mont_inv: u64, modulus_static_prefix: &str) -> proc_macro let mut r3: u64; unsafe { - asm!( + core::arch::asm!( // round 0 "mov rdx, qword ptr [{a_ptr} + 0]", "xor r8d, r8d", @@ -426,7 +426,7 @@ pub(crate) fn add_impl(modulus_static_prefix: &str) -> proc_macro2::TokenStream let mut r3: u64; unsafe { - asm!( + core::arch::asm!( // we sum (a+b) using addition chain with OF // and sum (a+b) - p using addition chain with CF // if (a+b) does not overflow the modulus @@ -477,7 +477,7 @@ pub(crate) fn add_impl(modulus_static_prefix: &str) -> proc_macro2::TokenStream } // unsafe { - // asm!( + // core::arch::asm!( // "xor r12d, r12d", // "mov r12, qword ptr [{a_ptr} + 0]", // "mov r13, qword ptr [{a_ptr} + 8]", @@ -553,7 +553,7 @@ pub(crate) fn double_impl(modulus_static_prefix: &str) -> proc_macro2::TokenStre let mut r3: u64; unsafe { - asm!( + core::arch::asm!( // we sum (a+b) using addition chain with OF // and sum (a+b) - p using addition chain with CF // if (a+b) does not overflow the modulus @@ -604,7 +604,7 @@ pub(crate) fn double_impl(modulus_static_prefix: &str) -> proc_macro2::TokenStre } // unsafe { - // asm!( + // core::arch::asm!( // "xor r12d, r12d", // "mov r12, qword ptr [{a_ptr} + 0]", // "mov r13, qword ptr [{a_ptr} + 8]", @@ -679,7 +679,7 @@ pub(crate) fn sub_impl(modulus_static_prefix: &str) -> proc_macro2::TokenStream let mut r3: u64; unsafe { - asm!( + core::arch::asm!( "xor r12d, r12d", "mov r12, qword ptr [{a_ptr} + 0]", "sub r12, qword ptr [{b_ptr} + 0]", @@ -738,4 +738,4 @@ pub(crate) fn sub_impl(modulus_static_prefix: &str) -> proc_macro2::TokenStream }); gen -} \ No newline at end of file +}