From 9df7911a37893fae1e8b8664134f5ef1e2a7b858 Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 10 Jun 2024 18:31:04 +0100 Subject: [PATCH 1/2] chore: inline `FieldElement.is_negative` and document --- acvm-repo/acir_field/src/field_element.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/acvm-repo/acir_field/src/field_element.rs b/acvm-repo/acir_field/src/field_element.rs index 81582b9fbac..fc05dd9294e 100644 --- a/acvm-repo/acir_field/src/field_element.rs +++ b/acvm-repo/acir_field/src/field_element.rs @@ -174,10 +174,6 @@ impl FieldElement { self.0 } - fn is_negative(&self) -> bool { - self.neg().num_bits() < self.num_bits() - } - fn fits_in_u128(&self) -> bool { self.num_bits() <= 128 } @@ -278,8 +274,12 @@ impl AcirField for FieldElement { } 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 } } From 7c28409c1ad58ab66c1dac5a88e0dd634aa23c5c Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 10 Jun 2024 18:32:32 +0100 Subject: [PATCH 2/2] Update acvm-repo/acir_field/src/field_element.rs --- acvm-repo/acir_field/src/field_element.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/acvm-repo/acir_field/src/field_element.rs b/acvm-repo/acir_field/src/field_element.rs index fc05dd9294e..2c14aefee2a 100644 --- a/acvm-repo/acir_field/src/field_element.rs +++ b/acvm-repo/acir_field/src/field_element.rs @@ -279,7 +279,6 @@ impl AcirField for FieldElement { // 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 } }