17#include <mlir/IR/BuiltinTypes.h>
19#include <llvm/ADT/SmallVector.h>
20#include <llvm/ADT/Twine.h>
30 if (ShapedType::isDynamic(dim)) {
31 return makeError(llvm::Twine(context) +
" requires a static shape");
34 return makeError(llvm::Twine(context) +
" has a negative dimension");
36 llvm::DynamicAPInt value(dim);
37 return checkedToSize(value, llvm::Twine(context) +
" dimension");
42 llvm::DynamicAPInt count(1);
43 for (int64_t dim : shape) {
46 return dimSize.takeError();
54 if (!type.hasStaticShape()) {
55 return makeError(llvm::Twine(context) +
" requires a static shape");
57 int64_t count = type.getNumElements();
59 return makeError(llvm::Twine(context) +
" has an invalid negative element count");
68 std::random_device rd;
69 std::seed_seq seed {rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()};
70 return std::mt19937_64(seed);
74 const llvm::DynamicAPInt prime = field.
prime();
83 const unsigned bitWidth = field.
bitWidth();
84 const unsigned numWords = (bitWidth + 63U) / 64U;
85 std::uniform_int_distribution<uint64_t> wordDist;
86 llvm::SmallVector<uint64_t, 4> words(numWords);
88 for (uint64_t &word : words) {
93 llvm::DynamicAPInt value =
toDynamicAPInt(llvm::APInt(bitWidth, words));
95 return field.
reduce(value);
101 return std::uniform_int_distribution<
102 int64_t>(std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max())(rng);
106 return std::uniform_int_distribution<int>(0, 1)(rng) != 0;
This file implements helpers for DynamicAPInt operations and conversions that LLVM does not provide.
Information about the prime finite field used for the interval analysis.
llvm::DynamicAPInt prime() const
For the prime field p, returns p.
llvm::DynamicAPInt reduce(const llvm::DynamicAPInt &i) const
Returns the canonical representative of i mod p in [0, p).
unsigned bitWidth() const
Width sufficient for the modulus and its representatives, independent of literal storage.
std::mt19937_64 makeDefaultValueRng(const WitgenOptions &options)
Seed an RNG for random/default witness value materialization.
llvm::Expected< size_t > getStaticShapeElementCount(llvm::ArrayRef< int64_t > shape, llvm::StringRef context)
Return the static element count for one shape, rejecting dynamic sizes.
llvm::DynamicAPInt randomFieldElement(std::mt19937_64 &rng, const Field &field)
Draw a uniformly distributed field element in [0, prime).
bool randomBoolValue(std::mt19937_64 &rng)
Draw a uniformly distributed boolean value.
llvm::Expected< size_t > getStaticElementCount(ShapedType type, llvm::StringRef context)
llvm::Expected< size_t > checkedShapeDimToSize(int64_t dim, llvm::StringRef context)
Convert one static dimension to size_t, rejecting dynamic or invalid sizes.
int64_t randomIndexValue(std::mt19937_64 &rng)
Draw a uniformly distributed signed index value.
llvm::Error makeError(const llvm::Twine &msg)
Build a string-backed error for user-facing witgen failures.
Expected< size_t > checkedToSize(const DynamicAPInt &value, const Twine &context)
DynamicAPInt toDynamicAPInt(StringRef str)
Configure one llzk-witgen execution.
std::optional< uint64_t > randomSeed