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_BOOLEAN_OPS
11#define LLZK_BOOLEAN_OPS
12
13include "llzk/Dialect/Bool/IR/Dialect.td"
14include "llzk/Dialect/Bool/IR/Attrs.td"
15include "llzk/Dialect/Felt/IR/Types.td"
16include "llzk/Dialect/Function/IR/OpTraits.td"
17include "llzk/Dialect/Shared/OpsBase.td"
18
19include "mlir/IR/OpAsmInterface.td"
20include "mlir/IR/OpBase.td"
21include "mlir/IR/SymbolInterfaces.td"
22include "mlir/Interfaces/SideEffectInterfaces.td"
23
24//===------------------------------------------------------------------===//
25// Op Classes
26//===------------------------------------------------------------------===//
27
28class BoolDialectOp<string mnemonic, list<Trait> traits = []>
29 : Op<BoolDialect, mnemonic, traits>;
30
31class BoolBinaryOpBase<string mnemonic, Type resultType,
32 list<Trait> traits = []>
33 : BinaryOpBase<BoolDialect, mnemonic, resultType, traits>;
34
35class BoolUnaryOpBase<string mnemonic, Type resultType, list<Trait> traits = []>
36 : UnaryOpBase<BoolDialect, mnemonic, resultType, traits>;
37
38//===------------------------------------------------------------------===//
39// Boolean operators
40//===------------------------------------------------------------------===//
41
42def LLZK_AndBoolOp
43 : BoolBinaryOpBase<"and", I1, [NotFieldNative, Commutative]> {
44 let summary = "logical AND operator";
45 let description = [{
46 This operation computes the logical AND (i.e., conjunction) of two `i1` (i.e., boolean)
47 values as an `i1` value. The result is `1` if the operation is true and `0` otherwise.
48
49 If used in a constraint expression, this operation can be converted to `a*b` on felt operands.
50 }];
51}
52
53def LLZK_OrBoolOp : BoolBinaryOpBase<"or", I1, [NotFieldNative, Commutative]> {
54 let summary = "logical OR operator";
55 let description = [{
56 This operation computes the logical OR (i.e., disjunction) of two `i1` (i.e., boolean)
57 values as an `i1` value. The result is `1` if the operation is true and `0` otherwise.
58
59 If used in a constraint expression, this operation can be converted to `a+b - a*b` on felt operands.
60 }];
61}
62
63def LLZK_XorBoolOp
64 : BoolBinaryOpBase<"xor", I1, [NotFieldNative, Commutative]> {
65 let summary = "logical XOR operator";
66 let description = [{
67 This operation computes the logical XOR (i.e., exclusive disjunction) of two `i1` (i.e., boolean)
68 values as an `i1` value. The result is `1` if the operation is true and `0` otherwise.
69
70 If used in a constraint expression, this operation can be converted to `a+b - 2*a*b` on felt operands.
71 }];
72}
73
74def LLZK_NotBoolOp : BoolUnaryOpBase<"not", I1, [NotFieldNative]> {
75 let summary = "logical NOT operator";
76 let description = [{
77 This operation computes the logical NOT (i.e., negation) of an `i1` (i.e., boolean)
78 value as an `i1` value. The result is `1` if the operation is true and `0` otherwise.
79
80 If used in a constraint expression, this operation can be converted to `1+a - 2*a` on felt operands.
81 }];
82}
83
84//===------------------------------------------------------------------===//
85// Other operators
86//===------------------------------------------------------------------===//
87
88def LLZK_AssertOp
89 : BoolDialectOp<
90 "assert", [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
91 let summary = "assertion operation";
92 let description = [{
93 This operation asserts that a given boolean value is true. Assertions are checked
94 statically when possible. If the condition evaluates to `true`, the assertion is
95 removed. If `false`, an error is reported. Otherwise, the assertion is preserved.
96 All assertions that appear in `constrain()` functions must evaluate statically
97 (i.e., they cannot depend on inputs to the circuit) else an error is reported.
98
99 Assertion without message:
100 ```llzk
101 %1 = bool.cmp lt(%a, %b)
102 bool.assert %1
103 ```
104
105 Assertion with a message:
106 ```llzk
107 %1 = bool.cmp eq(%a, %b)
108 bool.assert %1, "expected equal values"
109 ```
110 }];
111
112 let arguments = (ins I1:$condition, OptionalAttr<StrAttr>:$msg);
113
114 let assemblyFormat = [{ $condition (`,` $msg^)? attr-dict }];
115}
116
117// Match format of Index comparisons (for now)
118def LLZK_CmpOp : BoolDialectOp<"cmp", [Pure]> {
119 let summary = "compare field element values";
120 let description = [{
121 This operation takes two field element values and compares them according to the
122 comparison predicate and returns an `i1`. The following comparisons are supported:
123
124 - `eq`: equal
125 - `ne`: not equal
126 - `lt`: less than
127 - `le`: less than or equal
128 - `gt`: greater than
129 - `ge`: greater than or equal
130
131 The result is `1` if the comparison is true and `0` otherwise.
132
133 The inequality operators (lt, gt, le, ge) for the finite field elements
134 are defined by treating the field elements as integer values:
135 `f1 op f2` iff `int(f1) op int(f2)`
136
137 Example:
138
139 ```llzk
140 // Less than comparison.
141 %0 = bool.cmp lt(%a, %b)
142
143 // Greater than or equal comparison.
144 %1 = bool.cmp ge(%a, %b)
145
146 // Not equal comparison.
147 %2 = bool.cmp ne(%a, %b)
148 ```
149 }];
150
151 let arguments = (ins LLZK_CmpPredicateAttr:$predicate, LLZK_FeltType:$lhs,
152 LLZK_FeltType:$rhs);
153 let results = (outs I1:$result);
154 let assemblyFormat = [{ `` $predicate `(` $lhs `,` $rhs `)` attr-dict }];
155}
156
157#endif // LLZK_BOOLEAN_OPS