Utils
This document is better viewed at https://docs.openzeppelin.com/confidential-contracts/api/utils |
Miscellaneous contracts and libraries containing utility functions you can use to improve security, and ease integrations when working with confidential contracts.
-
TFHESafeMath
: Implementation of safe math operations for encrypted values. -
CheckpointsConfidential
: Implementation of checkpoints for encrypted values.
Math
TFHESafeMath
import "@openzeppelin/confidential-contracts/utils/TFHESafeMath.sol";
Library providing safe arithmetic operations for encrypted values to handle potential overflows in FHE operations.
-
tryIncrease(oldValue, delta)
-
tryDecrease(oldValue, delta)
tryIncrease(euint64 oldValue, euint64 delta) → ebool success, euint64 updated
internal
Try to increase the encrypted value oldValue
by delta
. If the operation is successful,
success
will be true and updated
will be the new value. Otherwise, success
will be false
and updated
will be the original value.
tryDecrease(euint64 oldValue, euint64 delta) → ebool success, euint64 updated
internal
Try to decrease the encrypted value oldValue
by delta
. If the operation is successful,
success
will be true and updated
will be the new value. Otherwise, success
will be false
and updated
will be the original value.
Structs
CheckpointsConfidential
import "@openzeppelin/confidential-contracts/utils/structs/CheckpointsConfidential.sol";
This library defines the Trace*
struct, for checkpointing values as they change at different points in
time, and later looking up past values by block number.
To create a history of checkpoints, define a variable type CheckpointsConfidential.Trace*
in your contract, and store a new
checkpoint for the current transaction block using the push
function.
-
push(self, key, value)
-
lowerLookup(self, key)
-
upperLookup(self, key)
-
upperLookupRecent(self, key)
-
latest(self)
-
latestCheckpoint(self)
-
length(self)
-
at(self, pos)
-
push(self, key, value)
-
lowerLookup(self, key)
-
upperLookup(self, key)
-
upperLookupRecent(self, key)
-
latest(self)
-
latestCheckpoint(self)
-
length(self)
-
at(self, pos)
-
CheckpointUnorderedInsertion()
push(struct CheckpointsConfidential.TraceEuint32 self, uint256 key, euint32 value) → euint32 oldValue, euint32 newValue
internal
Pushes a (key
, value
) pair into a TraceEuint32 so that it is stored as the checkpoint.
Returns previous value and new value.
Never accept key as a user input, since an arbitrary type(uint256).max key set will disable the
library.
|
lowerLookup(struct CheckpointsConfidential.TraceEuint32 self, uint256 key) → euint32
internal
Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if there is none.
upperLookup(struct CheckpointsConfidential.TraceEuint32 self, uint256 key) → euint32
internal
Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.
upperLookupRecent(struct CheckpointsConfidential.TraceEuint32 self, uint256 key) → euint32
internal
Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.
This is a variant of upperLookup that is optimized to find "recent" checkpoint (checkpoints with high
keys).
|
latest(struct CheckpointsConfidential.TraceEuint32 self) → euint32
internal
Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
latestCheckpoint(struct CheckpointsConfidential.TraceEuint32 self) → bool exists, uint256 key, euint32 value
internal
Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value in the most recent checkpoint.
length(struct CheckpointsConfidential.TraceEuint32 self) → uint256
internal
Returns the number of checkpoints.
at(struct CheckpointsConfidential.TraceEuint32 self, uint32 pos) → uint256 key, euint32 value
internal
Returns checkpoint at given position.
push(struct CheckpointsConfidential.TraceEuint64 self, uint256 key, euint64 value) → euint64 oldValue, euint64 newValue
internal
Pushes a (key
, value
) pair into a TraceEuint64 so that it is stored as the checkpoint.
Returns previous value and new value.
Never accept key as a user input, since an arbitrary type(uint256).max key set will disable the
library.
|
lowerLookup(struct CheckpointsConfidential.TraceEuint64 self, uint256 key) → euint64
internal
Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if there is none.
upperLookup(struct CheckpointsConfidential.TraceEuint64 self, uint256 key) → euint64
internal
Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.
upperLookupRecent(struct CheckpointsConfidential.TraceEuint64 self, uint256 key) → euint64
internal
Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.
This is a variant of upperLookup that is optimized to find "recent" checkpoint (checkpoints with high
keys).
|
latest(struct CheckpointsConfidential.TraceEuint64 self) → euint64
internal
Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
latestCheckpoint(struct CheckpointsConfidential.TraceEuint64 self) → bool exists, uint256 key, euint64 value
internal
Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value in the most recent checkpoint.
length(struct CheckpointsConfidential.TraceEuint64 self) → uint256
internal
Returns the number of checkpoints.