Skip to content

Commit 30fda4a

Browse files
committed
feat(field): add RootOfUnityAttr
1 parent a883e37 commit 30fda4a

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

tests/Dialect/Field/prime_field_to_mod_arith.mlir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
!PF1 = !field.pf<3:i32>
33
!PFv = tensor<4x!PF1>
44
#elem = #field.pf_elem<31:i32> : !PF1
5+
#root_elem = #field.pf_elem<2:i32> : !PF1
6+
#root = #field.root_of_unity<#root_elem, 2>
57

68
!mod = !mod_arith.int<3 : i32>
79
#mont = #mod_arith.montgomery<!mod>

zkir/Dialect/Field/IR/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cc_library(
2323
":ops_inc_gen",
2424
":types_inc_gen",
2525
"//zkir/Dialect/ModArith/IR:ModArith",
26+
"//zkir/Utils:APIntUtils",
2627
"//zkir/Utils:OpUtils",
2728
"@llvm-project//llvm:Support",
2829
"@llvm-project//mlir:ArithDialect",

zkir/Dialect/Field/IR/FieldAttributes.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "mlir/IR/BuiltinAttributes.h"
44
#include "mlir/IR/BuiltinTypes.h"
5+
#include "zkir/Utils/APIntUtils.h"
56

67
namespace mlir::zkir::field {
78

@@ -17,4 +18,20 @@ LogicalResult PrimeFieldAttr::verify(
1718
return success();
1819
}
1920

21+
LogicalResult RootOfUnityAttr::verify(
22+
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
23+
PrimeFieldAttr root, IntegerAttr degree) {
24+
APInt modulus = root.getType().getModulus().getValue();
25+
APInt rootOfUnity = root.getValue().getValue();
26+
unsigned degreeValue = degree.getValue().getZExtValue();
27+
28+
if (!expMod(rootOfUnity, degreeValue, modulus).isOne()) {
29+
emitError() << rootOfUnity.getZExtValue()
30+
<< " is not a root of unity of degree " << degreeValue;
31+
return failure();
32+
}
33+
34+
return success();
35+
}
36+
2037
} // namespace mlir::zkir::field

zkir/Dialect/Field/IR/FieldAttributes.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ def Field_PrimeFieldAttr : Field_Attr<"PrimeField", "pf_elem", [TypedAttrInterfa
3737
let genVerifyDecl = 1;
3838
}
3939

40+
def Field_RootOfUnityAttr : Field_Attr<"RootOfUnity", "root_of_unity", [TypedAttrInterface]> {
41+
let summary = "An attribute representing a root of unity in a prime field";
42+
let parameters = (ins Field_PrimeFieldAttr:$root, "IntegerAttr":$degree);
43+
let assemblyFormat = "`<` $root `,` $degree `>`";
44+
let extraClassDeclaration = [{
45+
PrimeFieldType getType() const {
46+
return getRoot().getType();
47+
}
48+
}];
49+
let genVerifyDecl = 1;
50+
}
51+
4052
#endif // ZKIR_DIALECT_FIELD_IR_FIELDATTRS_TD_

0 commit comments

Comments
 (0)