Skip to content

Commit

Permalink
chore: improve proptesting of 128bit values in noirc_abi (#7485)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Feb 21, 2025
1 parent f442c31 commit 1dfe472
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc b3f9ae88d54944ca274764f4d99a2023d4b0ac09beb89bc599cbba1e45dd3620 # shrinks to (typ, value) = (Integer { sign: Signed, width: 1 }, -1)
cc afc4c7b26dcb04c8bb68af61dd8422745802d5a722b2200a9cb69a8425154b0a # shrinks to (abi, input_map) = (Abi { parameters: [AbiParameter { name: "A", typ: Struct { path: "\u{8}\u{6d174}\0:.Ѩ**¥\u{feff}`\u{4}o\u{55a3b}2=\u{6}:<", fields: [("\u{10bba4}Y&\u{83c23}?${\u{dc4b5}Z𬰫'º\u{1b}\u{37592}\u{feff}Ѩ.\u{4b753}c\u{736c6}", Integer { sign: Signed, width: 128 })] }, visibility: Public }], return_type: Some(AbiReturnType { abi_type: Tuple { fields: [Array { length: 1, typ: Field }, Tuple { fields: [Boolean] }, Array { length: 5, typ: Struct { path: "|\u{99332}?\u{7}\u{5131a}{q", fields: [("*🕴\u{d41d7}?\r*&O\\\u{c01a6}5{\t\u{9a8f1}\"\u{464e9}hM\\", Integer { sign: Signed, width: 83 }), ("ä:$\u{d4b96}\u{911ce}\u{1cc1c}EL{.¥🕴\u{36eb8}\u{feff}\u{a0}\u{40267}.🕴🕴{\u{97de2}\u{df645}\u{b}B}\u{ba0d1}<P`z\u{10a4b9}", Boolean), ("\u{c51c4}'\u{e661f}=\u{107aea}\t", Field)] } }, Struct { path: "2=\0`Ñzr`*?\u{acb6c}{\tÜ\u{5})\u{ce3a2}P:`\\\u{202e}wR\u{202e}¥\u{c7209}*\0\u{1}", fields: [("\u{d9ac0}\u{7f}\t\0X{¥\0&\u{5de37}=%", String { length: 543 }), ("\\G*]\u{4a486}?0�\u{9126f}\u{202e}\u{545b7}¥\u{feff}\r\u{7b50d}Y'\0", Boolean), ("D🕴\u{a0b6c}\u{720f3}\u{72bb4}/ÍY\u{1a862}Ѩ=\u{6}&Ѩ$\u{3d4ba}", Integer { sign: Signed, width: 69 }), ("Ⱥ\0x\u{7f}\u{b9336}\u{c248f}Ѩ\"bv", Integer { sign: Unsigned, width: 44 }), ("\u{ac1ab}豜`)O\",Ⱥ\r<(%0&A\u{1}\r\u{8cb4e}Ѩ\u{67650}%]\u{7e76e}Ⱥ\u{104ae}e\\", Field), ("\u{fad13}\u{d7b53}🕴Ѩ*\u{55041}=\\\0\\<\\}\u{fbfd8}\"W2e&\u{5c161}\\nѨ⺆", Field), ("]\"\u{8d4d3}", Struct { path: "?m\u{b}\u{1b}�\t\u{7f}\r\u{feff}{\t🕴", fields: [("\u{832b9}@`\u{4e33a}{\u{7f}&$.\0Ꙗ'Ⱥ\u{fbda7}R(o¿\u{feff}m\0\\\u{b}+\u{3f976}\u{a4ee0}�:\rb\u{92e8e}", Integer { sign: Unsigned, width: 104 }), ("_𢐏.\u{202e}", String { length: 218 }), ("~'\t", String { length: 221 }), ("/\"\u{57ff4}\u{385f2}?\u{7}𭗬\0î/%Ⱥv?\u{6f4ac}&", Integer { sign: Signed, width: 101 })] })] }, Array { length: 5, typ: Boolean }, Tuple { fields: [Integer { sign: Unsigned, width: 9 }, Boolean, Array { length: 3, typ: Field }, Tuple { fields: [Boolean, Field, String { length: 873 }, String { length: 259 }, Boolean, String { length: 494 }, String { length: 828 }, String { length: 997 }, Boolean] }, Field, String { length: 421 }] }, Integer { sign: Unsigned, width: 20 }] }, visibility: Public }), error_types: {} }, {"A": Struct({"\u{10bba4}Y&\u{83c23}?${\u{dc4b5}Z𬰫'º\u{1b}\u{37592}\u{feff}Ѩ.\u{4b753}c\u{736c6}": Field(2548261944240406328114325585528328)})})
6 changes: 3 additions & 3 deletions tooling/noirc_abi/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fn ensure_unique_strings<'a>(iter: impl Iterator<Item = &'a mut String>) {

proptest::prop_compose! {
pub(super) fn arb_field_from_integer(bit_size: u32)(value: u128)-> FieldElement {
let width = (bit_size % 128).clamp(1, 127);
let max_value = 2u128.pow(width) - 1;
let width = (bit_size % 129).clamp(1, 128);
let max_value = if bit_size == 128 { u128::MAX } else { (1u128 << width) - 1 };
FieldElement::from(value.clamp(0, max_value))
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ fn arb_primitive_abi_type() -> SBoxedStrategy<AbiType> {
Just(AbiType::Field),
Just(AbiType::Boolean),
any::<(Sign, u32)>().prop_map(|(sign, width)| {
let width = (width % 128).clamp(1, 127);
let width = (width % 129).clamp(1, 128);
AbiType::Integer { sign, width }
}),
// restrict length of strings to avoid running out of memory
Expand Down
23 changes: 15 additions & 8 deletions tooling/noirc_abi/src/input_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,11 @@ fn parse_str_to_signed(
error: err_msg.to_string(),
})
.and_then(|bigint| {
let max = BigInt::from(2_u128.pow(width - 1) - 1);
let min = BigInt::from(-(2_i128.pow(width - 1)));
let min = if width == 128 { i128::MIN } else { -(1 << (width - 1)) };
let max = if width == 128 { i128::MAX } else { (1 << (width - 1)) - 1 };

let max = BigInt::from(max);
let min = BigInt::from(min);

if bigint < min {
return Err(InputParserError::InputUnderflowsMinimum {
Expand Down Expand Up @@ -398,8 +401,8 @@ fn parse_integer_to_signed(
width: u32,
arg_name: &str,
) -> Result<FieldElement, InputParserError> {
let min = -(1 << (width - 1));
let max = (1 << (width - 1)) - 1;
let min = if width == 128 { i128::MIN } else { -(1 << (width - 1)) };
let max = if width == 128 { i128::MAX } else { (1 << (width - 1)) - 1 };

if integer < min {
return Err(InputParserError::InputUnderflowsMinimum {
Expand All @@ -417,8 +420,12 @@ fn parse_integer_to_signed(
});
}

let integer = if integer < 0 { (1 << width) + integer } else { integer };
Ok(FieldElement::from(integer as u128))
let integer = if integer < 0 {
FieldElement::from(2u32).pow(&width.into()) + FieldElement::from(integer)
} else {
FieldElement::from(integer)
};
Ok(integer)
}

fn field_from_big_uint(bigint: BigUint) -> FieldElement {
Expand All @@ -439,9 +446,9 @@ fn field_from_big_int(bigint: BigInt) -> FieldElement {

fn field_to_signed_hex(f: FieldElement, bit_size: u32) -> String {
let f_u128 = f.to_u128();
let max = 2_u128.pow(bit_size - 1) - 1;
let max = if bit_size == 128 { i128::MAX as u128 } else { (1 << (bit_size - 1)) - 1 };
if f_u128 > max {
let f = FieldElement::from(2_u128.pow(bit_size) - f_u128);
let f = FieldElement::from(2u32).pow(&bit_size.into()) - f;
format!("-0x{}", f.to_hex())
} else {
format!("0x{}", f.to_hex())
Expand Down

0 comments on commit 1dfe472

Please sign in to comment.