LLZK 0.1.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Ops.td
Go to the documentation of this file.
1//===-- Ops.td ---------------------------------------------*- tablegen -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLZK_FELT_OPS
11#define LLZK_FELT_OPS
12
13include "llzk/Dialect/Felt/IR/Dialect.td"
14include "llzk/Dialect/Felt/IR/Types.td"
15include "llzk/Dialect/Felt/IR/Attrs.td"
16include "llzk/Dialect/Function/IR/OpTraits.td"
17include "llzk/Dialect/Shared/OpsBase.td"
18include "llzk/Dialect/Felt/IR/OpInterfaces.td"
19
20include "mlir/IR/OpAsmInterface.td"
21include "mlir/IR/OpBase.td"
22include "mlir/IR/SymbolInterfaces.td"
23include "mlir/Interfaces/SideEffectInterfaces.td"
24
25//===------------------------------------------------------------------===//
26// Op Classes
27//===------------------------------------------------------------------===//
28
29class FeltDialectOp<string mnemonic, list<Trait> traits = []>
30 : Op<FeltDialect, mnemonic, traits>;
31
32class FeltDialectBinaryOp<string mnemonic, Type resultType,
33 list<Trait> traits = []>
34 : BinaryOpBase<FeltDialect, mnemonic, resultType,
35 traits#[DeclareOpInterfaceMethods<FeltBinaryOpInterface>]>;
36
37class FeltDialectUnaryOp<string mnemonic, Type resultType,
38 list<Trait> traits = []>
39 : UnaryOpBase<FeltDialect, mnemonic, resultType, traits>;
40
41//===------------------------------------------------------------------===//
42// Constants
43//===------------------------------------------------------------------===//
44
45def LLZK_FeltConstantOp
46 : FeltDialectOp<"const", [ConstantLike, Pure,
47 DeclareOpInterfaceMethods<
48 OpAsmOpInterface, ["getAsmResultNames"]>]> {
49 let summary = "field element constant";
50 let description = [{
51 This operation produces a felt-typed SSA value holding an integer constant.
52
53 Example:
54
55 ```llzk
56 %0 = llzk.const 42
57 ```
58 }];
59
60 let arguments = (ins LLZK_FeltConstAttr:$value);
61 let results = (outs LLZK_FeltType:$result);
62 let assemblyFormat = [{ $value attr-dict }];
63 let hasFolder = 1;
64}
65
66//===------------------------------------------------------------------===//
67// Operators
68//===------------------------------------------------------------------===//
69
70def LLZK_AddFeltOp : FeltDialectBinaryOp<"add", LLZK_FeltType, [Commutative]> {
71 let summary = "addition operator for field elements";
72 let description = [{}];
73}
74
75def LLZK_SubFeltOp : FeltDialectBinaryOp<"sub", LLZK_FeltType> {
76 let summary = "subtraction operator for field elements";
77 let description = [{}];
78}
79
80def LLZK_MulFeltOp : FeltDialectBinaryOp<"mul", LLZK_FeltType, [Commutative]> {
81 let summary = "multiplication operator for field elements";
82 let description = [{}];
83}
84
85def LLZK_PowFeltOp
86 : FeltDialectBinaryOp<"pow", LLZK_FeltType, [NotFieldNative]> {
87 let summary = "exponentiation operator for field elements";
88 let description = [{
89
90 Raises a field element to the power of an exponent.
91
92 ```llzk
93 %result = felt.pow %base, %exponent
94 ```
95
96 }];
97}
98
99def LLZK_DivFeltOp : FeltDialectBinaryOp<"div", LLZK_FeltType> {
100 let summary = "division operator for field elements";
101 let description = [{}];
102}
103
104def LLZK_UnsignedIntDivFeltOp
105 : FeltDialectBinaryOp<"uintdiv", LLZK_FeltType, [NotFieldNative]> {
106 let summary = "unsigned integer division operator for field elements";
107 let description = [{
108 Treats the operands as if they were unsigned integers with bitwidth
109 equal to that of the prime modulus and performs division rounding towards zero.
110 }];
111}
112
113def LLZK_SignedIntDivFeltOp
114 : FeltDialectBinaryOp<"sintdiv", LLZK_FeltType, [NotFieldNative]> {
115 let summary = "signed integer division operator for field elements";
116 let description = [{
117 Treats the operands as if they were signed integers with bitwidth
118 equal to that of the prime modulus (no additional sign bit is added)
119 and performs division rounding towards zero.
120
121 The signed integer representation of felt `f` in prime field with modulus
122 `p` follows the following formula:
123
124 signed_int(f) = f if 0 <= f < p/2 + 1
125 "p/2" here is unsigned integer division rounding towards 0
126 signed_int(f) = f-p if p/2 + 1 <= f < p
127 }];
128}
129
130def LLZK_UnsignedModFeltOp
131 : FeltDialectBinaryOp<"umod", LLZK_FeltType, [NotFieldNative]> {
132 let summary =
133 "unsigned integer modulus/remainder operator for field elements";
134 let description = [{
135 Computes the remainder that would result from the division operation performed
136 by `felt.uintdiv`.
137 }];
138}
139
140def LLZK_SignedModFeltOp
141 : FeltDialectBinaryOp<"smod", LLZK_FeltType, [NotFieldNative]> {
142 let summary = "signed integer modulus/remainder operator for field elements";
143 let description = [{
144 Computes the remainder that would result from the division operation performed
145 by `felt.sintdiv`.
146 }];
147}
148
149def LLZK_NegFeltOp : FeltDialectUnaryOp<"neg", LLZK_FeltType> {
150 let summary = "negation operator for field elements";
151 let description = [{}];
152}
153
154def LLZK_InvFeltOp
155 : FeltDialectUnaryOp<"inv", LLZK_FeltType, [NotFieldNative]> {
156 let summary = "inverse operator for field elements";
157 let description = [{}];
158}
159
160def LLZK_AndFeltOp
161 : FeltDialectBinaryOp<"bit_and",
162 LLZK_FeltType, [NotFieldNative, Commutative]> {
163 let summary = "bitwise AND operator for field elements";
164 let description = [{}];
165}
166
167def LLZK_OrFeltOp
168 : FeltDialectBinaryOp<"bit_or",
169 LLZK_FeltType, [NotFieldNative, Commutative]> {
170 let summary = "bitwise OR operator for field elements";
171 let description = [{}];
172}
173
174def LLZK_XorFeltOp
175 : FeltDialectBinaryOp<"bit_xor",
176 LLZK_FeltType, [NotFieldNative, Commutative]> {
177 let summary = "bitwise XOR operator for field elements";
178 let description = [{}];
179}
180
181def LLZK_NotFeltOp
182 : FeltDialectUnaryOp<"bit_not", LLZK_FeltType, [NotFieldNative]> {
183 let summary = "integer complement (bitwise-not) operator for field elements";
184 let description = [{
185 Treats the operand as an integer with a bitwidth equal to the bitwidth
186 of the prime field's modulus and compute the one's complement of the integer.
187 The result is converted back to a field element by applying the prime modulus.
188 }];
189}
190
191def LLZK_ShlFeltOp
192 : FeltDialectBinaryOp<"shl", LLZK_FeltType, [NotFieldNative]> {
193 let summary = "left shift operator for field elements";
194 let description = [{}];
195}
196
197def LLZK_ShrFeltOp
198 : FeltDialectBinaryOp<"shr", LLZK_FeltType, [NotFieldNative]> {
199 let summary = "right shift operator for field elements";
200 let description = [{}];
201}
202
203#endif // LLZK_FELT_OPS