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/APSInt.h>
13#include <llvm/ADT/DenseMap.h>
14#include <llvm/Support/SMTAPI.h>
15
16#include <string_view>
17
18namespace llzk {
19
22class Field {
23public:
26 static const Field &getField(const char *fieldName);
27
28 Field() = delete;
29 Field(const Field &) = default;
30 Field(Field &&) = default;
31 Field &operator=(const Field &) = default;
32
34 llvm::APSInt prime() const { return primeMod; }
35
37 llvm::APSInt half() const { return halfPrime; }
38
40 inline llvm::APSInt felt(int i) const { return reduce(i); }
41
43 inline llvm::APSInt zero() const { return felt(0); }
44
46 inline llvm::APSInt one() const { return felt(1); }
47
49 inline llvm::APSInt maxVal() const { return prime() - one(); }
50
54 llvm::APSInt reduce(llvm::APSInt i) const;
55 llvm::APSInt reduce(int i) const;
56
57 inline unsigned bitWidth() const { return primeMod.getBitWidth(); }
58
60 llvm::SMTExprRef createSymbol(llvm::SMTSolverRef solver, const char *name) const {
61 return solver->mkSymbol(name, solver->getBitvectorSort(bitWidth()));
62 }
63
64 friend bool operator==(const Field &lhs, const Field &rhs) {
65 return lhs.primeMod == rhs.primeMod;
66 }
67
68private:
69 Field(std::string_view primeStr);
70
71 llvm::APSInt primeMod, halfPrime;
72
73 static void initKnownFields(llvm::DenseMap<llvm::StringRef, Field> &knownFields);
74};
75
76} // namespace llzk
MlirStringRef name
llvm::APSInt one() const
Returns 1 at the bitwidth of the field.
Definition Field.h:46
llvm::SMTExprRef createSymbol(llvm::SMTSolverRef solver, const char *name) const
Create a SMT solver symbol with the current field's bitwidth.
Definition Field.h:60
llvm::APSInt zero() const
Returns 0 at the bitwidth of the field.
Definition Field.h:43
friend bool operator==(const Field &lhs, const Field &rhs)
Definition Field.h:64
Field()=delete
llvm::APSInt felt(int i) const
Returns i as a signed field element.
Definition Field.h:40
Field(const Field &)=default
Field(Field &&)=default
llvm::APSInt half() const
Returns p / 2.
Definition Field.h:37
unsigned bitWidth() const
Definition Field.h:57
Field & operator=(const Field &)=default
llvm::APSInt maxVal() const
Returns p - 1, which is the max value possible in a prime field described by p.
Definition Field.h:49
static const Field & getField(const char *fieldName)
Get a Field from a given field name string.
Definition Field.cpp:26
llvm::APSInt prime() const
For the prime field p, returns p.
Definition Field.h:34
llvm::APSInt reduce(llvm::APSInt i) const
Returns i mod p and reduces the result into the appropriate bitwidth.
Definition Field.cpp:52