LLZK
3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Toggle main menu visibility
Loading...
Searching...
No Matches
OverflowSemantics.cpp
Go to the documentation of this file.
1
//===-- OverflowSemantics.cpp - Cast overflow helpers -----------*- 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
#include "
llzk/Dialect/Cast/Util/OverflowSemantics.h
"
11
12
#include <llvm/Support/ErrorHandling.h>
13
14
using namespace
llvm
;
15
16
namespace
llzk::cast
{
17
namespace
{
18
19
static
DynamicAPInt positiveModulo(
const
DynamicAPInt &value,
const
DynamicAPInt &modulus) {
20
DynamicAPInt result = value % modulus;
21
return
result < 0 ? result + modulus : result;
22
}
23
24
}
// namespace
25
26
std::optional<DynamicAPInt>
27
applyIntToFeltOverflow
(
const
DynamicAPInt &value,
const
Field
&field,
OverflowSemantics
overflow) {
28
switch
(overflow) {
29
case
OverflowSemantics::ASSERT
:
30
return
value < 0 || value >= field.
prime
() ? std::nullopt : std::optional(value);
31
case
OverflowSemantics::SATURATE
:
32
return
value < 0 ? field.
zero
() : (value < field.
maxVal
() ? value : field.
maxVal
());
33
case
OverflowSemantics::WRAP
:
34
return
field.
reduce
(value);
35
case
OverflowSemantics::TRUNCATE
:
36
return
field.
reduce
(positiveModulo(value, DynamicAPInt(1) << field.
bitWidth
()));
37
}
38
llvm_unreachable(
"unknown overflow semantics"
);
39
}
40
41
std::optional<DynamicAPInt>
applyFeltToIndexOverflow
(
42
const
DynamicAPInt &value,
unsigned
indexBitWidth,
OverflowSemantics
overflow
43
) {
44
const
DynamicAPInt safeUpperBound = (DynamicAPInt(1) << (indexBitWidth - 1)) - 1;
45
switch
(overflow) {
46
case
OverflowSemantics::ASSERT
:
47
return
value < 0 || value > safeUpperBound ? std::nullopt : std::optional(value);
48
case
OverflowSemantics::SATURATE
:
49
return
value < 0 ? DynamicAPInt(0) : (value < safeUpperBound ? value : safeUpperBound);
50
case
OverflowSemantics::WRAP
:
51
case
OverflowSemantics::TRUNCATE
:
52
return
positiveModulo(value, safeUpperBound + 1);
53
}
54
llvm_unreachable(
"unknown overflow semantics"
);
55
}
56
57
}
// namespace llzk::cast
OverflowSemantics.h
llzk::Field
Information about the prime finite field used for the interval analysis.
Definition
Field.h:36
llzk::Field::zero
llvm::DynamicAPInt zero() const
Returns the zero field element.
Definition
Field.h:79
llzk::Field::prime
llvm::DynamicAPInt prime() const
For the prime field p, returns p.
Definition
Field.h:70
llzk::Field::reduce
llvm::DynamicAPInt reduce(const llvm::DynamicAPInt &i) const
Returns the canonical representative of i mod p in [0, p).
llzk::Field::bitWidth
unsigned bitWidth() const
Width sufficient for the modulus and its representatives, independent of literal storage.
Definition
Field.h:103
llzk::Field::maxVal
llvm::DynamicAPInt maxVal() const
Returns p - 1, which is the max value possible in a prime field described by p.
Definition
Field.h:85
llvm
Definition
SourceRefLattice.cpp:411
llzk::cast
Definition
Ops.cpp:71
llzk::cast::applyIntToFeltOverflow
std::optional< DynamicAPInt > applyIntToFeltOverflow(const DynamicAPInt &value, const Field &field, OverflowSemantics overflow)
Definition
OverflowSemantics.cpp:27
llzk::cast::applyFeltToIndexOverflow
std::optional< DynamicAPInt > applyFeltToIndexOverflow(const DynamicAPInt &value, unsigned indexBitWidth, OverflowSemantics overflow)
Definition
OverflowSemantics.cpp:41
llzk::cast::OverflowSemantics
OverflowSemantics
Definition
Enums.h.inc:13
llzk::cast::OverflowSemantics::TRUNCATE
@ TRUNCATE
Definition
Enums.h.inc:17
llzk::cast::OverflowSemantics::ASSERT
@ ASSERT
Definition
Enums.h.inc:14
llzk::cast::OverflowSemantics::SATURATE
@ SATURATE
Definition
Enums.h.inc:15
llzk::cast::OverflowSemantics::WRAP
@ WRAP
Definition
Enums.h.inc:16
lib
Dialect
Cast
Util
OverflowSemantics.cpp
Generated by
1.17.0
Copyright 2025 Veridise Inc. under the Apache License v2.0. Copyright 2026 Project LLZK under the Apache License v2.0.