Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove byte decomposition in compute_decomposition #6159

Merged
merged 4 commits into from
Oct 10, 2024
Merged
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
22 changes: 10 additions & 12 deletions noir_stdlib/src/field/bn254.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ global PLO: Field = 53438638232309528389504892708671455233;
global PHI: Field = 64323764613183177041862057485226039389;

pub(crate) global TWO_POW_128: Field = 0x100000000000000000000000000000000;
global TWO_POW_64: Field = 0x10000000000000000;

// Decomposes a single field into two 16 byte fields.
fn compute_decomposition(x: Field) -> (Field, Field) {
let x_bytes: [u8; 32] = x.to_le_bytes();

let mut low: Field = 0;
let mut high: Field = 0;

let mut offset = 1;
for i in 0..16 {
low += (x_bytes[i] as Field) * offset;
high += (x_bytes[i + 16] as Field) * offset;
offset *= 256;
}
fn compute_decomposition(mut x: Field) -> (Field, Field) {
// Here's we're taking advantage of truncating 64 bit limbs from the input field
// and then subtracting them from the input such the field division is equivalent to integer division.
let low_lower_64 = (x as u64) as Field;
x = (x - low_lower_64) / TWO_POW_64;
let low_upper_64 = (x as u64) as Field;

let high = (x - low_upper_64) / TWO_POW_64;
let low = low_upper_64 * TWO_POW_64 + low_lower_64;

(low, high)
}
Expand Down
Loading