Utils
Smart contract utils utilities and implementations
Miscellaneous contracts and libraries containing utility functions you can use to improve security, and ease integrations when working with confidential contracts.
FHESafeMath
: Implementation of safe math operations for encrypted values.CheckpointsConfidential
: Implementation of checkpoints for encrypted values.HandleAccessManager
: Minimal contract that adds a function to give allowance to callers for a given ciphertext handle.
Math
Structs
Other
import "@openzeppelin/contracts/utils/FHESafeMath.sol";
Library providing safe arithmetic operations for encrypted values to handle potential overflows in FHE operations.
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.
tryAdd(euint64 a, euint64 b) → ebool success, euint64 res
internal
#Try to add a
and b
. If the operation is successful, success
will be true and res
will be the sum of a
and b
. Otherwise, success
will be false, and res
will be 0.
trySub(euint64 a, euint64 b) → ebool success, euint64 res
internal
#Try to subtract b
from a
. If the operation is successful, success
will be true and res
will be a - b
. Otherwise, success
will be false, and res
will be 0.
import "@openzeppelin/contracts/utils/HandleAccessManager.sol";
getHandleAllowance(bytes32 handle, address account, bool persistent)
public
#Get handle access for the given handle handle
. Access will be given to the
account account
with the given persistence flag.
NOTE: This function call is gated by msg.sender
and validated by the
HandleAccessManager._validateHandleAllowance
function.
_validateHandleAllowance(bytes32 handle)
internal
#Unimplemented function that must revert if the message sender is not allowed to call
HandleAccessManager.getHandleAllowance
for the given handle.
import "@openzeppelin/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 CheckpointsConfidential.push
function.
Functions
- 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)
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.
NOTE: This is a variant of CheckpointsConfidential.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.
NOTE: This is a variant of CheckpointsConfidential.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.
at(struct CheckpointsConfidential.TraceEuint64 self, uint32 pos) → uint256 key, euint64 value
internal
#Returns checkpoint at given position.
import "@openzeppelin/contracts/utils/structs/temporary-Checkpoints.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. See VotesConfidential
as an example.
To create a history of checkpoints define a variable type Checkpoints.Trace*
in your contract, and store a new
checkpoint for the current transaction block using the CheckpointsConfidential.push
function.
Functions
- 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)
- 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)
push(struct Checkpoints.Trace256 self, uint256 key, uint256 value) → uint256 oldValue, uint256 newValue
internal
#Pushes a (key
, value
) pair into a Trace256 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 Checkpoints.Trace256 self, uint256 key) → uint256
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 Checkpoints.Trace256 self, uint256 key) → uint256
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 Checkpoints.Trace256 self, uint256 key) → uint256
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.
NOTE: This is a variant of CheckpointsConfidential.upperLookup
that is optimized to find "recent" checkpoint (checkpoints with high
keys).
latest(struct Checkpoints.Trace256 self) → uint256
internal
#Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
latestCheckpoint(struct Checkpoints.Trace256 self) → bool exists, uint256 _key, uint256 _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 Checkpoints.Trace256 self) → uint256
internal
#Returns the number of checkpoints.
at(struct Checkpoints.Trace256 self, uint32 pos) → struct Checkpoints.Checkpoint256
internal
#Returns checkpoint at given position.
push(struct Checkpoints.Trace224 self, uint32 key, uint224 value) → uint224 oldValue, uint224 newValue
internal
#Pushes a (key
, value
) pair into a Trace224 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(uint32).max
key set will disable the
library.
lowerLookup(struct Checkpoints.Trace224 self, uint32 key) → uint224
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 Checkpoints.Trace224 self, uint32 key) → uint224
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 Checkpoints.Trace224 self, uint32 key) → uint224
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.
NOTE: This is a variant of CheckpointsConfidential.upperLookup
that is optimized to find "recent" checkpoint (checkpoints with high
keys).
latest(struct Checkpoints.Trace224 self) → uint224
internal
#Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
latestCheckpoint(struct Checkpoints.Trace224 self) → bool exists, uint32 _key, uint224 _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 Checkpoints.Trace224 self) → uint256
internal
#Returns the number of checkpoints.
at(struct Checkpoints.Trace224 self, uint32 pos) → struct Checkpoints.Checkpoint224
internal
#Returns checkpoint at given position.
push(struct Checkpoints.Trace208 self, uint48 key, uint208 value) → uint208 oldValue, uint208 newValue
internal
#Pushes a (key
, value
) pair into a Trace208 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(uint48).max
key set will disable the
library.
lowerLookup(struct Checkpoints.Trace208 self, uint48 key) → uint208
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 Checkpoints.Trace208 self, uint48 key) → uint208
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 Checkpoints.Trace208 self, uint48 key) → uint208
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.
NOTE: This is a variant of CheckpointsConfidential.upperLookup
that is optimized to find "recent" checkpoint (checkpoints with high
keys).
latest(struct Checkpoints.Trace208 self) → uint208
internal
#Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
latestCheckpoint(struct Checkpoints.Trace208 self) → bool exists, uint48 _key, uint208 _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 Checkpoints.Trace208 self) → uint256
internal
#Returns the number of checkpoints.
at(struct Checkpoints.Trace208 self, uint32 pos) → struct Checkpoints.Checkpoint208
internal
#Returns checkpoint at given position.
push(struct Checkpoints.Trace160 self, uint96 key, uint160 value) → uint160 oldValue, uint160 newValue
internal
#Pushes a (key
, value
) pair into a Trace160 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(uint96).max
key set will disable the
library.
lowerLookup(struct Checkpoints.Trace160 self, uint96 key) → uint160
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 Checkpoints.Trace160 self, uint96 key) → uint160
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 Checkpoints.Trace160 self, uint96 key) → uint160
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.
NOTE: This is a variant of CheckpointsConfidential.upperLookup
that is optimized to find "recent" checkpoint (checkpoints with high
keys).
latest(struct Checkpoints.Trace160 self) → uint160
internal
#Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
latestCheckpoint(struct Checkpoints.Trace160 self) → bool exists, uint96 _key, uint160 _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 Checkpoints.Trace160 self) → uint256
internal
#Returns the number of checkpoints.
at(struct Checkpoints.Trace160 self, uint32 pos) → struct Checkpoints.Checkpoint160
internal
#Returns checkpoint at given position.
CheckpointUnorderedInsertion()
error
#A value was attempted to be inserted on a past checkpoint.