Skip to content

Commit 43bd78c

Browse files
committed
feat(field): add range check to PrimeFieldAttr
1 parent 30fda4a commit 43bd78c

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

tests/Dialect/Field/prime_field_to_mod_arith.mlir

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: zkir-opt -prime-field-to-mod-arith --split-input-file %s | FileCheck %s --enable-var-scope
22
!PF1 = !field.pf<3:i32>
33
!PFv = tensor<4x!PF1>
4-
#elem = #field.pf_elem<31:i32> : !PF1
54
#root_elem = #field.pf_elem<2:i32> : !PF1
65
#root = #field.root_of_unity<#root_elem, 2>
76

zkir/Dialect/Field/IR/FieldAttributes.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,25 @@ namespace mlir::zkir::field {
88

99
LogicalResult PrimeFieldAttr::verify(
1010
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
11-
PrimeFieldType type, IntegerAttr value) {
12-
if (type.getModulus().getValue().getBitWidth() !=
13-
value.getValue().getBitWidth()) {
11+
PrimeFieldType type, IntegerAttr _value) {
12+
APInt modulus = type.getModulus().getValue();
13+
APInt value = _value.getValue();
14+
15+
// check if storage type is same
16+
if (modulus.getBitWidth() != value.getBitWidth()) {
1417
emitError()
1518
<< "prime field modulus bitwidth does not match the value bitwidth";
1619
return failure();
1720
}
21+
22+
// check if value is in the field defined by modulus
23+
if (value.uge(modulus)) {
24+
emitError() << value.getZExtValue()
25+
<< " is not in the field defined by modulus "
26+
<< modulus.getZExtValue();
27+
return failure();
28+
}
29+
1830
return success();
1931
}
2032

0 commit comments

Comments
 (0)