LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
Field.h
Go to the documentation of this file.
1//===-- Field.h -------------------------------------------------*- C++ -*-===//
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#pragma once
11
12#include <llvm/ADT/DenseMap.h>
13#include <llvm/ADT/DynamicAPInt.h>
14#include <llvm/Support/SMTAPI.h>
15
16#include <string_view>
17
18namespace llzk {
19
25class Field {
26public:
29 static const Field &getField(const char *fieldName);
30
31 Field() = delete;
32 Field(const Field &) = default;
33 Field(Field &&) noexcept = default;
34 Field &operator=(const Field &) = default;
35
37 llvm::DynamicAPInt prime() const { return primeMod; }
38
40 llvm::DynamicAPInt half() const { return halfPrime; }
41
43 inline llvm::DynamicAPInt felt(int i) const { return reduce(i); }
44
46 inline llvm::DynamicAPInt zero() const { return felt(0); }
47
49 inline llvm::DynamicAPInt one() const { return felt(1); }
50
52 inline llvm::DynamicAPInt maxVal() const { return prime() - one(); }
53
57 llvm::DynamicAPInt reduce(const llvm::DynamicAPInt &i) const;
58 inline llvm::DynamicAPInt reduce(int i) const { return reduce(llvm::DynamicAPInt(i)); }
59 llvm::DynamicAPInt reduce(const llvm::APInt &i) const;
60
61 inline unsigned bitWidth() const { return bitwidth; }
62
64 llvm::SMTExprRef createSymbol(llvm::SMTSolverRef solver, const char *name) const {
65 return solver->mkSymbol(name, solver->getBitvectorSort(bitWidth()));
66 }
67
68 friend bool operator==(const Field &lhs, const Field &rhs) {
69 return lhs.primeMod == rhs.primeMod;
70 }
71
72private:
73 Field(std::string_view primeStr);
74
75 llvm::DynamicAPInt primeMod, halfPrime;
76 unsigned bitwidth;
77
78 static void initKnownFields(llvm::DenseMap<llvm::StringRef, Field> &knownFields);
79};
80
81} // namespace llzk
MlirStringRef name
Definition Poly.cpp:48
llvm::DynamicAPInt half() const
Returns p / 2.
Definition Field.h:40
llvm::SMTExprRef createSymbol(llvm::SMTSolverRef solver, const char *name) const
Create a SMT solver symbol with the current field's bitwidth.
Definition Field.h:64
friend bool operator==(const Field &lhs, const Field &rhs)
Definition Field.h:68
Field()=delete
Field(const Field &)=default
llvm::DynamicAPInt zero() const
Returns 0 at the bitwidth of the field.
Definition Field.h:46
llvm::DynamicAPInt prime() const
For the prime field p, returns p.
Definition Field.h:37
llvm::DynamicAPInt reduce(const llvm::DynamicAPInt &i) const
Returns i mod p and reduces the result into the appropriate bitwidth.
llvm::DynamicAPInt one() const
Returns 1 at the bitwidth of the field.
Definition Field.h:49
llvm::DynamicAPInt reduce(int i) const
Definition Field.h:58
llvm::DynamicAPInt felt(int i) const
Returns i as a signed field element.
Definition Field.h:43
llvm::DynamicAPInt reduce(const llvm::APInt &i) const
unsigned bitWidth() const
Definition Field.h:61
static const Field & getField(const char *fieldName)
Get a Field from a given field name string.
Definition Field.cpp:31
Field(Field &&) noexcept=default
llvm::DynamicAPInt maxVal() const
Returns p - 1, which is the max value possible in a prime field described by p.
Definition Field.h:52