Skip to content

Commit bb4cd6c

Browse files
authored
chore: checks that proof_type does not overflow (#11661)
1 parent 37f89d3 commit bb4cd6c

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

compiler/noirc_evaluator/src/acir/acir_context/generated_acir/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,15 @@ impl<F: AcirField> GeneratedAcir<F> {
324324
proof,
325325
public_inputs,
326326
key_hash,
327-
proof_type: proof_type.to_u128() as u32,
327+
proof_type: u32::try_from(proof_type.to_u128()).map_err(|_| {
328+
InternalError::General {
329+
message: format!(
330+
"proof_type value {} does not fit into a u32",
331+
proof_type.to_u128()
332+
),
333+
call_stack: self.get_call_stack(),
334+
}
335+
})?,
328336
predicate,
329337
}
330338
}

compiler/noirc_evaluator/src/acir/tests/intrinsics.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use acvm::assert_circuit_snapshot;
22

3-
use crate::acir::tests::ssa_to_acir_program;
3+
use crate::acir::tests::{ssa_to_acir_program, try_ssa_to_acir};
44

55
#[test]
66
fn vector_push_back_known_length() {
@@ -986,3 +986,27 @@ fn as_vector_for_vector_with_nested_array() {
986986
ASSERT w20 = w77
987987
");
988988
}
989+
990+
#[test]
991+
fn recursive_aggregation_proof_type_truncation_poc() {
992+
// A `proof_type` value that overflows u32 (2^32) must produce an InternalError
993+
// rather than silently truncating to 0.
994+
let src = "
995+
acir(inline) fn main f0 {
996+
b0():
997+
v0 = make_array [Field 0] : [Field; 1]
998+
v1 = make_array [Field 1] : [Field; 1]
999+
v2 = make_array [Field 2] : [Field; 1]
1000+
v3 = unchecked_add u32 4294967295, u32 1
1001+
call recursive_aggregation(v0, v1, v2, Field 3, v3)
1002+
return
1003+
}
1004+
";
1005+
1006+
let err = try_ssa_to_acir(src).expect_err("expected an error when proof_type overflows u32");
1007+
let message = err.to_string();
1008+
assert!(
1009+
message.contains("proof_type") && message.contains("does not fit into a u32"),
1010+
"unexpected error message: {message}"
1011+
);
1012+
}

compiler/noirc_evaluator/src/acir/tests/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ fn ssa_to_acir_program_with_debug_info(src: &str) -> (Program<FieldElement>, Vec
4141
}
4242

4343
/// Attempts to convert SSA to ACIR, returning the error if compilation fails.
44-
fn try_ssa_to_acir(src: &str) -> Result<(Program<FieldElement>, Vec<DebugInfo>), RuntimeError> {
44+
pub(crate) fn try_ssa_to_acir(
45+
src: &str,
46+
) -> Result<(Program<FieldElement>, Vec<DebugInfo>), RuntimeError> {
4547
let ssa = Ssa::from_str(src).unwrap();
4648
let arg_size_and_visibilities = ssa
4749
.functions

0 commit comments

Comments
 (0)