LLZK 0.1.0
An open-source IR for Zero Knowledge (ZK) circuits
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
13
14#include <llvm/ADT/DenseMap.h>
15#include <llvm/ADT/DynamicAPInt.h>
16#include <llvm/Support/SMTAPI.h>
17
18#include <string_view>
19
20namespace llzk {
21
27class Field {
28public:
31 static const Field &getField(const char *fieldName);
32
33 Field() = delete;
34 Field(const Field &) = default;
35 Field(Field &&) noexcept = default;
36 Field &operator=(const Field &) = default;
37
39 llvm::DynamicAPInt prime() const { return primeMod; }
40
42 llvm::DynamicAPInt half() const { return halfPrime; }
43
45 inline llvm::DynamicAPInt felt(int i) const { return reduce(i); }
46
48 inline llvm::DynamicAPInt zero() const { return felt(0); }
49
51 inline llvm::DynamicAPInt one() const { return felt(1); }
52
54 inline llvm::DynamicAPInt maxVal() const { return prime() - one(); }
55
57 llvm::DynamicAPInt inv(const llvm::DynamicAPInt &i) const;
58
59 llvm::DynamicAPInt inv(const llvm::APInt &i) const;
60
64 llvm::DynamicAPInt reduce(const llvm::DynamicAPInt &i) const;
65 inline llvm::DynamicAPInt reduce(int i) const { return reduce(llvm::DynamicAPInt(i)); }
66 llvm::DynamicAPInt reduce(const llvm::APInt &i) const;
67
68 inline unsigned bitWidth() const { return bitwidth; }
69
71 llvm::SMTExprRef createSymbol(llvm::SMTSolverRef solver, const char *name) const {
72 return solver->mkSymbol(name, solver->getBitvectorSort(bitWidth()));
73 }
74
75 friend bool operator==(const Field &lhs, const Field &rhs) {
76 return lhs.primeMod == rhs.primeMod;
77 }
78
79private:
80 Field(std::string_view primeStr);
81
82 llvm::DynamicAPInt primeMod, halfPrime;
83 unsigned bitwidth;
84
85 static void initKnownFields(llvm::DenseMap<llvm::StringRef, Field> &knownFields);
86};
87
88} // namespace llzk
This file implements helper methods for constructing DynamicAPInts.
llvm::DynamicAPInt half() const
Returns p / 2.
Definition Field.h:42
llvm::SMTExprRef createSymbol(llvm::SMTSolverRef solver, const char *name) const
Create a SMT solver symbol with the current field's bitwidth.
Definition Field.h:71
friend bool operator==(const Field &lhs, const Field &rhs)
Definition Field.h:75
Field()=delete
Field(const Field &)=default
llvm::DynamicAPInt zero() const
Returns 0 at the bitwidth of the field.
Definition Field.h:48
llvm::DynamicAPInt prime() const
For the prime field p, returns p.
Definition Field.h:39
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:51
llvm::DynamicAPInt reduce(int i) const
Definition Field.h:65
llvm::DynamicAPInt felt(int i) const
Returns i as a signed field element.
Definition Field.h:45
llvm::DynamicAPInt reduce(const llvm::APInt &i) const
llvm::DynamicAPInt inv(const llvm::DynamicAPInt &i) const
Returns the multiplicative inverse of i in prime field p.
unsigned bitWidth() const
Definition Field.h:68
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:54