Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
7b49928
placeholder for SmallFp
z-tech Sep 4, 2025
daa99f7
migrate smallfp implementation from blendy
benbencik Sep 24, 2025
0fadbb9
add tests
benbencik Sep 24, 2025
3d6636e
add benchmarks
benbencik Sep 25, 2025
f7de469
add square root precomputation
benbencik Oct 2, 2025
4d835f4
split sampling method into cases
benbencik Oct 3, 2025
4ba1448
fix mul overflow issue
benbencik Oct 3, 2025
d1937cc
extend testing suite (some tests failing)
benbencik Oct 3, 2025
a1a3343
rewrite sampling function
benbencik Oct 3, 2025
9cc57f5
fix overflowing bug in multiplication
benbencik Oct 3, 2025
be2664d
fix the computation for bit size
benbencik Oct 3, 2025
e39da81
consider (de)serialization of small elements
benbencik Oct 4, 2025
3c7daf9
rewrite computation for two adic root of unity
benbencik Oct 4, 2025
8a907d8
use safe mul to avoid overflows in compile-time
benbencik Oct 4, 2025
ea4cce2
update the smallfp tests
benbencik Oct 4, 2025
db896d3
Merge branch 'smallfp-test' into small_fp
benbencik Oct 4, 2025
2945217
rewrite mul_assing to handle overflows correctly
benbencik Oct 5, 2025
582ac3d
move tests and benches to test-curves
benbencik Oct 8, 2025
b91c704
update doccomments
benbencik Oct 8, 2025
f2f3fb1
use the provided bench templates for fields
benbencik Oct 9, 2025
f8c81e8
Merge branch 'master' into small_fp
benbencik Oct 9, 2025
176fc97
add info about small fields to readme
benbencik Oct 9, 2025
0cad96a
add pending PR 1044
benbencik Oct 9, 2025
742bb39
fix markdown linter error
benbencik Oct 9, 2025
4ccddce
add mont multiplication fastpath
benbencik Oct 10, 2025
42d99e9
clean unused type
benbencik Oct 12, 2025
6976823
specify mont mul impl at compile time
benbencik Oct 12, 2025
7a7f18b
add inlinging for arithmetic ops
benbencik Oct 12, 2025
f59fa7d
replace modulo operation in addition
benbencik Oct 12, 2025
676a7c3
Merge branch 'master' into small_fp
z-tech Oct 13, 2025
d663208
remove branching from mont multiplicaiton
benbencik Oct 13, 2025
2a6e5e3
Merge branch 'master' into small_fp
z-tech Oct 14, 2025
8acdef2
update utils helper functions
benbencik Oct 14, 2025
391f0ba
specify supported moduli
benbencik Oct 14, 2025
234bd8f
reduce duplicity in tests
benbencik Oct 15, 2025
9172802
revert ff Cargo.toml changes
benbencik Oct 15, 2025
94f79a0
Update ff/src/fields/models/small_fp/small_fp_backend.rs
z-tech Nov 10, 2025
a91ada2
chkpt
z-tech Nov 10, 2025
e825b86
fmt
z-tech Nov 10, 2025
ad5883e
Merge pull request #7 from benbencik/rn_modulus_u128_and_clippy_errors
z-tech Nov 10, 2025
677650f
use shave bits in sample func
z-tech Nov 10, 2025
230f862
Merge pull request #8 from benbencik/fix_sample_performance
z-tech Nov 10, 2025
32beb82
fix loop
z-tech Nov 10, 2025
740537d
Merge pull request #9 from benbencik/fix_sample_performance
z-tech Nov 10, 2025
0dd67b3
chkpt
z-tech Nov 11, 2025
63589c5
depend on from(u128), from(u128) should reduce
z-tech Nov 11, 2025
70f96cc
Merge pull request #10 from benbencik/update_from_u8_from_u16
z-tech Nov 11, 2025
bc23b85
Merge small_fp_bench: optimize SmallFp Montgomery backend
benbencik Dec 10, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- (`ark-poly`) Add fast polynomial division
- (`ark-ec`) Improve GLV scalar multiplication performance by skipping leading zeroes.
- (`ark-poly`) Make `SparsePolynomial.coeffs` field public
- [\#1044](https://github.com/arkworks-rs/algebra/pull/1044) Add implementation for small field with native integer types

### Breaking changes

Expand Down
31 changes: 30 additions & 1 deletion ff-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use proc_macro::TokenStream;
use syn::{Expr, ExprLit, Item, ItemFn, Lit, Meta};

mod montgomery;
mod small_fp;
mod unroll;

pub(crate) mod utils;
Expand Down Expand Up @@ -74,6 +75,34 @@ pub fn mont_config(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
.into()
}

/// Derive the `SmallFpConfig` trait for small prime fields.
///
/// The attributes available to this macro are:
/// * `modulus`: Specify the prime modulus underlying this prime field.
/// * `generator`: Specify the generator of the multiplicative subgroup.
/// * `backend`: Specify either "standard" or "montgomery" backend.
#[proc_macro_derive(SmallFpConfig, attributes(modulus, generator, backend))]
pub fn small_fp_config(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();

let modulus: u128 = fetch_attr("modulus", &ast.attrs)
.expect("Please supply a modulus attribute")
.parse()
.expect("Modulus should be a number");

let generator: u128 = fetch_attr("generator", &ast.attrs)
.expect("Please supply a generator attribute")
.parse()
.expect("Generator should be a number");

let backend: String = fetch_attr("backend", &ast.attrs)
.expect("Please supply a backend attribute")
.parse()
.expect("Backend should be a string");

small_fp::small_fp_config_helper(modulus, generator, backend, ast.ident).into()
}

const ARG_MSG: &str = "Failed to parse unroll threshold; must be a positive integer";

/// Attribute used to unroll for loops found inside a function block.
Expand Down Expand Up @@ -152,7 +181,7 @@ fn test_str_to_limbs() {
if sign == Minus {
string.insert(0, '-');
}
let (is_positive, limbs) = utils::str_to_limbs(&string.to_string());
let (is_positive, limbs) = utils::str_to_limbs(&string.clone());
assert_eq!(
limbs[0],
format!("{}u64", signed_number.unsigned_abs() as u64),
Expand Down
54 changes: 54 additions & 0 deletions ff-macros/src/small_fp/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
mod montgomery_backend;
mod standard_backend;
mod utils;

use quote::quote;

/// This function is called by the `#[derive(SmallFp)]` macro and generates
/// the implementation of the `SmallFpConfig`
pub(crate) fn small_fp_config_helper(
modulus: u128,
generator: u128,
backend: String,
config_name: proc_macro2::Ident,
) -> proc_macro2::TokenStream {
let ty = match modulus {
m if m < 1u128 << 8 => quote! { u8 },
m if m < 1u128 << 16 => quote! { u16 },
m if m < 1u128 << 32 => quote! { u32 },
m if m < 1u128 << 64 => quote! { u64 },
_ => quote! { u128 },
};

let backend_impl = match backend.as_str() {
"standard" => standard_backend::backend_impl(&ty, modulus, generator),
"montgomery" => {
assert!(modulus < 1u128 << 127,
"SmallFpConfig montgomery backend supports only moduli < 2^127. Use MontConfig with BigInt instead of SmallFp."
);
montgomery_backend::backend_impl(&ty, modulus, generator)
},

_ => panic!("Unknown backend type: {}", backend),
};

let new_impl = match backend.as_str() {
"standard" => standard_backend::new(),
"montgomery" => montgomery_backend::new(modulus, ty.clone()),
_ => panic!("Unknown backend type: {}", backend),
};

quote! {
const _: () = {
use ark_ff::{SmallFp, SmallFpConfig};

impl SmallFpConfig for #config_name {
#backend_impl
}

impl #config_name {
#new_impl
}
};
}
}
Loading
Loading