LLZK 0.1.0
Veridise's ZK Language IR
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 : BoolBinaryOpBase<"and", I1, [WitnessGen, Commutative]> {
43 let summary = "logical AND operator";
44 let description = [{
45 This operation computes the logical AND (i.e. conjunction) of two `i1` (i.e. boolean)
46 values as an `i1` value. The result is `1` if the operation is true and `0` otherwise.
47 }];
48}
49
50def LLZK_OrBoolOp : BoolBinaryOpBase<"or", I1, [WitnessGen, Commutative]> {
51 let summary = "logical OR operator";
52 let description = [{
53 This operation computes the logical OR (i.e. disjunction) of two `i1` (i.e. boolean)
54 values as an `i1` value. The result is `1` if the operation is true and `0` otherwise.
55 }];
56}
57
58def LLZK_XorBoolOp : BoolBinaryOpBase<"xor", I1, [WitnessGen, Commutative]> {
59 let summary = "logical XOR operator";
60 let description = [{
61 This operation computes the logical XOR (i.e. exclusive disjunction) of two `i1` (i.e. boolean)
62 values as an `i1` value. The result is `1` if the operation is true and `0` otherwise.
63 }];
64}
65
66def LLZK_NotBoolOp : BoolUnaryOpBase<"not", I1, [WitnessGen]> {
67 let summary = "logical NOT operator";
68 let description = [{
69 This operation computes the logical NOT (i.e. negation) of an `i1` (i.e. boolean)
70 value as an `i1` value. The result is `1` if the operation is true and `0` otherwise.
71 }];
72}
73
74//===------------------------------------------------------------------===//
75// Other operators
76//===------------------------------------------------------------------===//
77
78def LLZK_AssertOp
79 : BoolDialectOp<
80 "assert", [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
81 let summary = "assertion operation";
82 let description = [{
83 This operation asserts that a given boolean value is true. Assertions are checked
84 statically when possible. If the condition evaluates to `true`, the assertion is
85 removed. If `false`, an error is reported. Otherwise, the assertion is preserved.
86 All assertions that appear in `constrain()` functions must evaluate statically
87 (i.e. they cannot depend on inputs to the circuit) else an error is reported.
88
89 Assertion without message:
90 ```llzk
91 %1 = bool.cmp lt(%a, %b)
92 bool.assert %1
93 ```
94
95 Assertion with a message:
96 ```llzk
97 %1 = bool.cmp eq(%a, %b)
98 bool.assert %1, "expected equal values"
99 ```
100 }];
101
102 let arguments = (ins I1:$condition, OptionalAttr<StrAttr>:$msg);
103
104 let assemblyFormat = [{ $condition (`,` $msg^)? attr-dict }];
105}
106
107// Match format of Index comparisons (for now)
108def LLZK_CmpOp : BoolDialectOp<"cmp", [Pure]> {
109 let summary = "compare field element values";
110 let description = [{
111 This operation takes two field element values and compares them according to the
112 comparison predicate and returns an `i1`. The following comparisons are supported:
113
114 - `eq`: equal
115 - `ne`: not equal
116 - `lt`: less than
117 - `le`: less than or equal
118 - `gt`: greater than
119 - `ge`: greater than or equal
120
121 The result is `1` if the comparison is true and `0` otherwise.
122
123 The inequality operators (lt, gt, le, ge) for the finite field elements
124 are defined by treating the field elements as integer values:
125 `f1 op f2` iff `int(f1) op int(f2)`
126
127 Example:
128
129 ```llzk
130 // Less than comparison.
131 %0 = bool.cmp lt(%a, %b)
132
133 // Greater than or equal comparison.
134 %1 = bool.cmp ge(%a, %b)
135
136 // Not equal comparison.
137 %2 = bool.cmp ne(%a, %b)
138 ```
139 }];
140
141 let arguments = (ins LLZK_CmpPredicateAttr:$predicate, LLZK_FeltType:$lhs,
142 LLZK_FeltType:$rhs);
143 let results = (outs I1:$result);
144 let assemblyFormat = [{ `` $predicate `(` $lhs `,` $rhs `)` attr-dict }];
145}
146
147#endif // LLZK_BOOLEAN_OPS