LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Attrs.td
Go to the documentation of this file.
1//===-- Attrs.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_ATTRS
11#define LLZK_FELT_ATTRS
12
13include "llzk/Dialect/Felt/IR/Dialect.td"
14include "llzk/Dialect/LLZK/IR/AttributeHelper.td"
15
16include "mlir/IR/AttrTypeBase.td"
17include "mlir/IR/BuiltinAttributeInterfaces.td"
18
19def LLZK_FeltConstAttr
20 : AttrDef<FeltDialect, "FeltConst", [TypedAttrInterface]> {
21 let mnemonic = "const";
22 let summary = "finite field element";
23 let description = [{
24 A felt attribute represents a finite field element.
25 }];
26
27 let parameters = (ins APIntParameter<"The felt constant value">:$value,
28 DefaultValuedParameter<"FeltType", "FeltType::get($_ctxt)">:$type);
29
30 let hasCustomAssemblyFormat = 1;
31
32 let builders = [
33 // Like the default builder but with convenience overloads for
34 // constructing the APInt.
35 AttrBuilder<
36 (ins "unsigned":$numBits, "::llvm::StringRef":$str, "FeltType":$ty),
37 [{ return $_get(context, ::llvm::APInt(numBits, str, 10), ty); }]>,
38 AttrBuilder<
39 (ins "unsigned":$numBits, "::llvm::ArrayRef<uint64_t>":$parts,
40 "FeltType":$ty),
41 [{ return $_get(context, ::llvm::APInt(numBits, parts), ty); }]>,
42 // All APInt combinations but using felt type with unspecified field.
43 AttrBuilder<(ins "::llvm::APInt":$value),
44 [{ return get(context, value, FeltType::get(context)); }]>,
45 AttrBuilder<
46 (ins "unsigned":$numBits, "::llvm::StringRef":$str),
47 [{ return get(context, numBits, str, FeltType::get(context)); }]>,
48 AttrBuilder<
49 (ins "unsigned":$numBits, "::llvm::ArrayRef<uint64_t>":$parts),
50 [{ return get(context, numBits, parts, FeltType::get(context)); }]>,
51 // All APInt combinations but using the field name to construct felt type.
52 AttrBuilder<
53 (ins "::llvm::APInt":$value, "::llvm::StringRef":$fieldName),
54 [{ return get(context, value, FeltType::get(context, fieldName)); }]>,
55 AttrBuilder<
56 (ins "unsigned":$numBits, "::llvm::StringRef":$str,
57 "::llvm::StringRef":$fieldName),
58 [{ return get(context, numBits, str, FeltType::get(context, fieldName)); }]>,
59 AttrBuilder<
60 (ins "unsigned":$numBits, "::llvm::ArrayRef<uint64_t>":$parts,
61 "::llvm::StringRef":$fieldName),
62 [{ return get(context, numBits, parts, FeltType::get(context, fieldName)); }]>];
63
64 let extraClassDeclaration = [{
65 operator ::llvm::APInt() const { return getValue(); }
66
67 /// Returns the field name from the stored FeltType, or a null StringAttr
68 /// if no type is stored.
69 ::mlir::StringAttr getFieldName() const;
70 }];
71}
72
73def LLZK_FieldSpecAttr : AttrDef<FeltDialect, "FieldSpec"> {
74 let mnemonic = "field";
75 let summary = "prime field specification";
76 let description = [{
77 A specification of a prime field for use by felt types.
78
79 These specifications are provided in the `llzk.fields` attribute on the root
80 module as either a single element or a flat array, for example:
81
82 module attributes {llzk.lang, llzk.fields = field<foo, 7> { ... }
83 module attributes {llzk.lang, llzk.fields = [field<>]} { ... }
84
85 Specifications should not be provided for built-in fields, which include:
86 - babybear
87 - bn128/bn254
88 - goldilocks
89 - koalabear
90 - mersenne31
91 }];
92
93 let parameters = (ins "::mlir::StringAttr":$fieldName,
94 APIntParameter<"The prime modulus">:$prime);
95
96 // Format is [{ `<` $fieldName `,` $prime `>` }], but with custom parsing
97 // to enable caching of the Field object definition.
98 let hasCustomAssemblyFormat = 1;
99
100 let builders =
101 [AttrBuilder<(ins "::llvm::StringRef":$fieldName, "unsigned":$numBits,
102 "::llvm::StringRef":$primeStr),
103 [{
104 return $_get(context, ::mlir::StringAttr::get(context, fieldName), ::llvm::APInt(numBits, primeStr, 10));
105 }]>,
106 AttrBuilder<(ins "::llvm::StringRef":$fieldName, "unsigned":$numBits,
107 "::llvm::ArrayRef<uint64_t>":$parts),
108 [{
109 return $_get(context, ::mlir::StringAttr::get(context, fieldName), ::llvm::APInt(numBits, parts));
110 }]>];
111}
112
113#endif // LLZK_FELT_ATTRS