1//===-- Ops.td ---------------------------------------------*- tablegen -*-===//
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
8//===----------------------------------------------------------------------===//
10#ifndef LLZK_BOOLEAN_OPS
11#define LLZK_BOOLEAN_OPS
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"
19include "mlir/IR/OpAsmInterface.td"
20include "mlir/IR/OpBase.td"
21include "mlir/IR/SymbolInterfaces.td"
22include "mlir/Interfaces/SideEffectInterfaces.td"
24//===------------------------------------------------------------------===//
26//===------------------------------------------------------------------===//
28class BoolDialectOp<string mnemonic, list<Trait> traits = []>
29 : Op<BoolDialect, mnemonic, traits>;
31class BoolBinaryOpBase<string mnemonic, Type resultType,
32 list<Trait> traits = []>
33 : BinaryOpBase<BoolDialect, mnemonic, resultType, traits>;
35class BoolUnaryOpBase<string mnemonic, Type resultType, list<Trait> traits = []>
36 : UnaryOpBase<BoolDialect, mnemonic, resultType, traits>;
38//===------------------------------------------------------------------===//
40//===------------------------------------------------------------------===//
43 : BoolBinaryOpBase<"and", I1, [NotFieldNative, Commutative]> {
44 let summary = "logical AND operator";
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.
49 If used in a constraint expression, this operation can be converted to `a*b` on felt operands.
53def LLZK_OrBoolOp : BoolBinaryOpBase<"or", I1, [NotFieldNative, Commutative]> {
54 let summary = "logical OR operator";
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.
59 If used in a constraint expression, this operation can be converted to `a+b - a*b` on felt operands.
64 : BoolBinaryOpBase<"xor", I1, [NotFieldNative, Commutative]> {
65 let summary = "logical XOR operator";
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.
70 If used in a constraint expression, this operation can be converted to `a+b - 2*a*b` on felt operands.
74def LLZK_NotBoolOp : BoolUnaryOpBase<"not", I1, [NotFieldNative]> {
75 let summary = "logical NOT operator";
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.
80 If used in a constraint expression, this operation can be converted to `1+a - 2*a` on felt operands.
84//===------------------------------------------------------------------===//
86//===------------------------------------------------------------------===//
90 "assert", [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
91 let summary = "assertion operation";
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.
99 Assertion without message:
101 %1 = bool.cmp lt(%a, %b)
105 Assertion with a message:
107 %1 = bool.cmp eq(%a, %b)
108 bool.assert %1, "expected equal values"
112 let arguments = (ins I1:$condition, OptionalAttr<StrAttr>:$msg);
114 let assemblyFormat = [{ $condition (`,` $msg^)? attr-dict }];
117// Match format of Index comparisons (for now)
118def LLZK_CmpOp : BoolDialectOp<"cmp", [Pure]> {
119 let summary = "compare field element values";
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:
127 - `le`: less than or equal
129 - `ge`: greater than or equal
131 The result is `1` if the comparison is true and `0` otherwise.
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)`
140 // Less than comparison.
141 %0 = bool.cmp lt(%a, %b)
143 // Greater than or equal comparison.
144 %1 = bool.cmp ge(%a, %b)
146 // Not equal comparison.
147 %2 = bool.cmp ne(%a, %b)
151 let arguments = (ins LLZK_CmpPredicateAttr:$predicate, LLZK_FeltType:$lhs,
153 let results = (outs I1:$result);
154 let assemblyFormat = [{ `` $predicate `(` $lhs `,` $rhs `)` attr-dict }];
157#endif // LLZK_BOOLEAN_OPS