From a9f5dae9408f05d64b43f3ffa1d2a5b96731457b Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Fri, 25 Oct 2024 15:13:30 -0500 Subject: [PATCH 1/5] Fix display of integer tokens --- compiler/noirc_frontend/src/lexer/token.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/noirc_frontend/src/lexer/token.rs b/compiler/noirc_frontend/src/lexer/token.rs index 8f05832d26d..daf59445982 100644 --- a/compiler/noirc_frontend/src/lexer/token.rs +++ b/compiler/noirc_frontend/src/lexer/token.rs @@ -1,4 +1,4 @@ -use acvm::{acir::AcirField, FieldElement}; +use acvm::FieldElement; use noirc_errors::{Position, Span, Spanned}; use std::fmt; @@ -367,7 +367,7 @@ impl fmt::Display for Token { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Token::Ident(ref s) => write!(f, "{s}"), - Token::Int(n) => write!(f, "{}", n.to_u128()), + Token::Int(n) => write!(f, "{}", n), Token::Bool(b) => write!(f, "{b}"), Token::Str(ref b) => write!(f, "{b:?}"), Token::FmtStr(ref b) => write!(f, "f{b:?}"), From 2f8a8b67926b9ce5796b8fce0d43dcc4b0649dc4 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Fri, 25 Oct 2024 15:55:13 -0500 Subject: [PATCH 2/5] Remove 2^n notation for fields --- acvm-repo/acir_field/src/field_element.rs | 71 +---------------------- 1 file changed, 1 insertion(+), 70 deletions(-) diff --git a/acvm-repo/acir_field/src/field_element.rs b/acvm-repo/acir_field/src/field_element.rs index 2323f008dbe..47ceb903111 100644 --- a/acvm-repo/acir_field/src/field_element.rs +++ b/acvm-repo/acir_field/src/field_element.rs @@ -8,7 +8,7 @@ use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign}; use crate::AcirField; // XXX: Switch out for a trait and proper implementations -// This implementation is in-efficient, can definitely remove hex usage and Iterator instances for trivial functionality +// This implementation is inefficient, can definitely remove hex usage and Iterator instances for trivial functionality #[derive(Default, Clone, Copy, Eq, PartialOrd, Ord)] pub struct FieldElement(F); @@ -33,46 +33,6 @@ impl std::fmt::Display for FieldElement { write!(f, "-")?; } - // Number of bits needed to represent the smaller representation - let num_bits = smaller_repr.bits(); - - // Check if the number represents a power of 2 - if smaller_repr.count_ones() == 1 { - let mut bit_index = 0; - for i in 0..num_bits { - if smaller_repr.bit(i) { - bit_index = i; - break; - } - } - return match bit_index { - 0 => write!(f, "1"), - 1 => write!(f, "2"), - 2 => write!(f, "4"), - 3 => write!(f, "8"), - _ => write!(f, "2{}", superscript(bit_index)), - }; - } - - // Check if number is a multiple of a power of 2. - // This is used because when computing the quotient - // we usually have numbers in the form 2^t * q + r - // We focus on 2^64, 2^32, 2^16, 2^8, 2^4 because - // they are common. We could extend this to a more - // general factorization strategy, but we pay in terms of CPU time - let mul_sign = "×"; - for power in [64, 32, 16, 8, 4] { - let power_of_two = BigUint::from(2_u128).pow(power); - if &smaller_repr % &power_of_two == BigUint::zero() { - return write!( - f, - "2{}{}{}", - superscript(power as u64), - mul_sign, - smaller_repr / &power_of_two, - ); - } - } write!(f, "{smaller_repr}") } } @@ -409,35 +369,6 @@ impl SubAssign for FieldElement { } } -// For pretty printing powers -fn superscript(n: u64) -> String { - if n == 0 { - "⁰".to_owned() - } else if n == 1 { - "¹".to_owned() - } else if n == 2 { - "²".to_owned() - } else if n == 3 { - "³".to_owned() - } else if n == 4 { - "⁴".to_owned() - } else if n == 5 { - "⁵".to_owned() - } else if n == 6 { - "⁶".to_owned() - } else if n == 7 { - "⁷".to_owned() - } else if n == 8 { - "⁸".to_owned() - } else if n == 9 { - "⁹".to_owned() - } else if n >= 10 { - superscript(n / 10) + &superscript(n % 10) - } else { - panic!("{}", n.to_string() + " can't be converted to superscript."); - } -} - #[cfg(test)] mod tests { use super::{AcirField, FieldElement}; From 11d3e5cbb5a5c28d6e14dc39f8fb0a7846dbd5b8 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Tue, 29 Oct 2024 08:40:32 -0500 Subject: [PATCH 3/5] Fix test --- compiler/noirc_frontend/src/tests/bound_checks.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/tests/bound_checks.rs b/compiler/noirc_frontend/src/tests/bound_checks.rs index 271f9d7a1a7..7fd7d759470 100644 --- a/compiler/noirc_frontend/src/tests/bound_checks.rs +++ b/compiler/noirc_frontend/src/tests/bound_checks.rs @@ -14,7 +14,7 @@ fn overflowing_u8() { if let CompilationError::TypeError(error) = &errors[0].0 { assert_eq!( error.to_string(), - "The value `2⁸` cannot fit into `u8` which has range `0..=255`" + "The value `256` cannot fit into `u8` which has range `0..=255`" ); } else { panic!("Expected OverflowingAssignment error, got {:?}", errors[0].0); From e65257ae63e9ded24623a4690b2b27e89e8aa355 Mon Sep 17 00:00:00 2001 From: Tom French Date: Tue, 29 Oct 2024 14:17:49 +0000 Subject: [PATCH 4/5] chore: update expected error messages in tests --- compiler/noirc_frontend/src/tests/bound_checks.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/tests/bound_checks.rs b/compiler/noirc_frontend/src/tests/bound_checks.rs index 7fd7d759470..05669bda411 100644 --- a/compiler/noirc_frontend/src/tests/bound_checks.rs +++ b/compiler/noirc_frontend/src/tests/bound_checks.rs @@ -52,7 +52,7 @@ fn overflowing_i8() { if let CompilationError::TypeError(error) = &errors[0].0 { assert_eq!( error.to_string(), - "The value `2⁷` cannot fit into `i8` which has range `-128..=127`" + "The value `128` cannot fit into `i8` which has range `-128..=127`" ); } else { panic!("Expected OverflowingAssignment error, got {:?}", errors[0].0); From dc0f0530f21440ac49fe783119e601045c65e0da Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Tue, 29 Oct 2024 09:29:44 -0500 Subject: [PATCH 5/5] Fix ts tests --- tooling/noirc_abi_wasm/test/browser/errors.test.ts | 2 +- tooling/noirc_abi_wasm/test/node/errors.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tooling/noirc_abi_wasm/test/browser/errors.test.ts b/tooling/noirc_abi_wasm/test/browser/errors.test.ts index 0f75ff64a3e..cc060cff4d6 100644 --- a/tooling/noirc_abi_wasm/test/browser/errors.test.ts +++ b/tooling/noirc_abi_wasm/test/browser/errors.test.ts @@ -9,7 +9,7 @@ it('errors when an integer input overflows', async () => { const { abi, inputs } = await import('../shared/uint_overflow'); expect(() => abiEncode(abi, inputs)).to.throw( - 'The value passed for parameter `foo` does not match the specified type:\nValue Field(2³⁸) does not fall within range of allowable values for a Integer { sign: Unsigned, width: 32 }', + 'The value passed for parameter `foo` does not match the specified type:\nValue Field(274877906944) does not fall within range of allowable values for a Integer { sign: Unsigned, width: 32 }', ); }); diff --git a/tooling/noirc_abi_wasm/test/node/errors.test.ts b/tooling/noirc_abi_wasm/test/node/errors.test.ts index fba451b4a8c..491fd5a5671 100644 --- a/tooling/noirc_abi_wasm/test/node/errors.test.ts +++ b/tooling/noirc_abi_wasm/test/node/errors.test.ts @@ -5,7 +5,7 @@ it('errors when an integer input overflows', async () => { const { abi, inputs } = await import('../shared/uint_overflow'); expect(() => abiEncode(abi, inputs)).to.throw( - 'The value passed for parameter `foo` does not match the specified type:\nValue Field(2³⁸) does not fall within range of allowable values for a Integer { sign: Unsigned, width: 32 }', + 'The value passed for parameter `foo` does not match the specified type:\nValue Field(274877906944) does not fall within range of allowable values for a Integer { sign: Unsigned, width: 32 }', ); });