Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ff_ce"
version = "0.14.1"
version = "0.14.2"
authors = ["Sean Bowe <ewillbefull@gmail.com>",
"Alex Gluchowski <alex@gluchowski.net>",
"Alex Vlasov <alex.m.vlasov@gmail.com>"]
Expand All @@ -27,4 +27,4 @@ serde = "1"
[features]
default = []
derive = ["ff_derive_ce"]
asm_derive = ["derive", "ff_derive_ce/asm"]
asm_derive = ["derive", "ff_derive_ce/asm"]
2 changes: 1 addition & 1 deletion ff_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ff_derive_ce"
version = "0.11.0"
version = "0.11.1"
authors = ["Sean Bowe <ewillbefull@gmail.com>",
"Alex Gluchowski <alex@gluchowski.net>",
"Alex Vlasov <alex.m.vlasov@gmail.com>"]
Expand Down
29 changes: 21 additions & 8 deletions ff_derive/src/asm/asm_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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;
}
);
Expand All @@ -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, ...
Expand Down Expand Up @@ -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 { }
Expand Down
16 changes: 8 additions & 8 deletions ff_derive/src/asm/impls_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]",
Expand Down Expand Up @@ -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]",
Expand Down Expand Up @@ -738,4 +738,4 @@ pub(crate) fn sub_impl(modulus_static_prefix: &str) -> proc_macro2::TokenStream
});

gen
}
}