LLZK 2.1.1
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
ValueModel.h
Go to the documentation of this file.
1//===-- ValueModel.h - llzk-witgen runtime values ---------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2026 Project LLZK
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
10#pragma once
11
16#include "llzk/Util/Field.h"
17
18#include <llvm/ADT/DenseMap.h>
19#include <llvm/ADT/DynamicAPInt.h>
20#include <llvm/ADT/StringRef.h>
21#include <llvm/Support/Error.h>
22
23#include <cstdint>
24#include <memory>
25#include <optional>
26#include <random>
27#include <variant>
28#include <vector>
29
30namespace mlir {
31class Operation;
32class SymbolTableCollection;
33} // namespace mlir
34
35namespace llzk::witgen {
36
37struct ArrayValue;
38struct PodValue;
39struct StructValue;
40
42using ArrayValueRef = std::shared_ptr<ArrayValue>;
43
45using PodValueRef = std::shared_ptr<PodValue>;
46
48using StructValueRef = std::shared_ptr<StructValue>;
49
51using WitnessVal = std::variant<
52 std::monostate, bool, int64_t, llvm::DynamicAPInt, ArrayValueRef, PodValueRef, StructValueRef>;
53
55enum class UninitializedBehavior : std::uint8_t {
59};
60
62struct ArrayValue {
64 std::vector<WitnessVal> elements;
65};
66
68struct PodValue {
70 llvm::DenseMap<llvm::StringRef, WitnessVal> records;
71};
72
76 llvm::DenseMap<llvm::StringRef, WitnessVal> members;
77};
78
80llvm::Expected<bool> asBool(const WitnessVal &value);
81
83llvm::Expected<int64_t> asIndex(const WitnessVal &value);
84
86llvm::Expected<llvm::DynamicAPInt> asFelt(const WitnessVal &value);
87
89llvm::Expected<ArrayValueRef> asArray(const WitnessVal &value);
90
92llvm::Expected<PodValueRef> asPod(const WitnessVal &value);
93
95llvm::Expected<StructValueRef> asStruct(const WitnessVal &value);
96
98llvm::Expected<WitnessVal> defaultValue(
99 mlir::Type type, mlir::SymbolTableCollection &tables, mlir::Operation *origin,
100 const llzk::Field &field, UninitializedBehavior behavior, std::mt19937_64 *rng = nullptr
101);
102
103} // namespace llzk::witgen
Information about the prime finite field used for the interval analysis.
Definition Field.h:36
llvm::Expected< PodValueRef > asPod(const WitnessVal &value)
Require a POD value from the runtime variant.
llvm::Expected< bool > asBool(const WitnessVal &value)
Require a boolean value from the runtime variant.
UninitializedBehavior
Control how witgen materializes uninitialized/default values.
Definition ValueModel.h:55
llvm::Expected< WitnessVal > defaultValue(Type type, SymbolTableCollection &tables, Operation *origin, const Field &field, UninitializedBehavior behavior, std::mt19937_64 *rng)
Build a default value for a supported LLZK type.
std::shared_ptr< ArrayValue > ArrayValueRef
Shared runtime storage for LLZK array values.
Definition ValueModel.h:42
std::shared_ptr< PodValue > PodValueRef
Shared runtime storage for LLZK POD values.
Definition ValueModel.h:45
llvm::Expected< int64_t > asIndex(const WitnessVal &value)
Require an index value from the runtime variant.
std::variant< std::monostate, bool, int64_t, llvm::DynamicAPInt, ArrayValueRef, PodValueRef, StructValueRef > WitnessVal
Runtime value representation used by the tool-local interpreter.
Definition ValueModel.h:51
llvm::Expected< llvm::DynamicAPInt > asFelt(const WitnessVal &value)
Require a felt value from the runtime variant.
std::shared_ptr< StructValue > StructValueRef
Shared runtime storage for LLZK struct values.
Definition ValueModel.h:48
llvm::Expected< StructValueRef > asStruct(const WitnessVal &value)
Require a struct value from the runtime variant.
llvm::Expected< ArrayValueRef > asArray(const WitnessVal &value)
Require an array value from the runtime variant.
Materialized array value with flattened element storage.
Definition ValueModel.h:62
array::ArrayType type
Definition ValueModel.h:63
std::vector< WitnessVal > elements
Definition ValueModel.h:64
Materialized POD value keyed by record name.
Definition ValueModel.h:68
llvm::DenseMap< llvm::StringRef, WitnessVal > records
Definition ValueModel.h:70
Materialized struct value keyed by member name.
Definition ValueModel.h:74
llvm::DenseMap< llvm::StringRef, WitnessVal > members
Definition ValueModel.h:76
component::StructType type
Definition ValueModel.h:75