Skip to content

Commit

Permalink
[fix] modular setup macro padding (#1252)
Browse files Browse the repository at this point in the history
* fix modular setup macro padding

* cleanup

* fix
  • Loading branch information
luffykai authored Jan 23, 2025
1 parent 586b026 commit 864d0a7
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions extensions/algebra/moduli-setup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ pub fn moduli_declare(input: TokenStream) -> TokenStream {
let modulus = modulus.expect("modulus parameter is required");
let modulus_bytes = string_to_bytes(&modulus);
let mut limbs = modulus_bytes.len();
let mut block_size = 32;

if limbs < 32 {
if limbs <= 32 {
limbs = 32;
proc_macro::Diagnostic::new(proc_macro::Level::Warning, "`limbs` has been set to 32 because it was too small; this is going to be changed once we support more flexible reads").emit();
} else if limbs <= 48 {
limbs = 48;
block_size = 16;
} else {
panic!("limbs must be at most 48");
}

// The largest power of two so that at most 10% of all space is wasted
let block_size = 1usize << ((limbs - 1) ^ (limbs + limbs / 9)).ilog2();
let limbs = limbs.next_multiple_of(block_size);
let modulus_bytes = modulus_bytes
.into_iter()
.chain(vec![0u8; limbs])
Expand Down Expand Up @@ -717,14 +719,14 @@ pub fn moduli_init(input: TokenStream) -> TokenStream {
let modulus_bytes = string_to_bytes(&modulus);
let mut limbs = modulus_bytes.len();

if limbs < 32 {
if limbs <= 32 {
limbs = 32;
proc_macro::Diagnostic::new(proc_macro::Level::Warning, "`limbs` has been set to 32 because it was too small; this is going to be changed once we support more flexible reads").emit();
} else if limbs <= 48 {
limbs = 48;
} else {
panic!("limbs must be at most 48");
}

// The largest power of two so that at most 10% of all space is wasted
let block_size = 1usize << ((limbs - 1) ^ (limbs + limbs / 9)).ilog2();
let limbs = limbs.next_multiple_of(block_size);
let modulus_bytes = modulus_bytes
.into_iter()
.chain(vec![0u8; limbs])
Expand Down

0 comments on commit 864d0a7

Please sign in to comment.