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

chore: inline FieldElement.is_negative and document #5214

Merged
merged 2 commits into from
Jun 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
9 changes: 4 additions & 5 deletions acvm-repo/acir_field/src/field_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ impl<F: PrimeField> FieldElement<F> {
self.0
}

fn is_negative(&self) -> bool {
self.neg().num_bits() < self.num_bits()
}

fn fits_in_u128(&self) -> bool {
self.num_bits() <= 128
}
Expand Down Expand Up @@ -278,7 +274,10 @@ impl<F: PrimeField> AcirField for FieldElement<F> {
}

fn to_i128(self) -> i128 {
let is_negative = self.is_negative();
// Negative integers are represented by the range [p + i128::MIN, p) whilst
// positive integers are represented by the range [0, i128::MAX).
// We can then differentiate positive from negative values by their MSB.
let is_negative = self.neg().num_bits() < self.num_bits();
let bytes = if is_negative { self.neg() } else { self }.to_be_bytes();
i128::from_be_bytes(bytes[16..32].try_into().unwrap()) * if is_negative { -1 } else { 1 }
}
Expand Down
Loading