Skip to content
Lattice

ECDSA

Git Source

Title: ECDSA

Author: Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/ECDSA.sol)

Elliptic Curve Digital Signature Algorithm (ECDSA) operations.

Utility library for verifying and recovering signers from compact signatures. High-S signatures are rejected to prevent signature malleability. Based on OpenZeppelin v5 and Solady patterns.

Functions

recover

Returns the address that signed a hashed message with signature.

Reverts with the appropriate error for invalid signatures.

function recover(bytes32 hash, bytes memory signature) internal pure returns (address signer);

Parameters

NameTypeDescription
hashbytes32The hash of the signed message.
signaturebytesThe signature bytes (65 bytes).

Returns

NameTypeDescription
signeraddressThe recovered signer address.

recover

Returns the address that signed a hashed message with compact (r, vs) signature.

EIP-2098 compact signature format.

function recover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address signer);

Parameters

NameTypeDescription
hashbytes32The hash of the signed message.
rbytes32The r component of the signature.
vsbytes32The combined v and s component (yParity bit in high bit of vs).

Returns

NameTypeDescription
signeraddressThe recovered signer address.

recover

Returns the address that signed a hashed message with (v, r, s) components.

function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address signer);

Parameters

NameTypeDescription
hashbytes32The hash of the signed message.
vuint8The recovery byte (27 or 28).
rbytes32The r component of the signature.
sbytes32The s component of the signature.

Returns

NameTypeDescription
signeraddressThe recovered signer address.

tryRecover

Attempts to recover the signer from a full signature, returning an error enum instead of reverting.

function tryRecover(bytes32 hash, bytes memory signature)
        internal
        pure
        returns (address recovered, RecoverError err, bytes32 errArg);

Parameters

NameTypeDescription
hashbytes32The hash of the signed message.
signaturebytesThe signature bytes.

Returns

NameTypeDescription
recoveredaddressThe recovered signer (address(0) on error).
errRecoverErrorThe error enum value.
errArgbytes32Additional error argument (s value for InvalidSignatureS, length for InvalidSignatureLength).

tryRecover

Attempts to recover the signer from an EIP-2098 compact (r, vs) signature.

function tryRecover(bytes32 hash, bytes32 r, bytes32 vs)
        internal
        pure
        returns (address recovered, RecoverError err, bytes32 errArg);

Parameters

NameTypeDescription
hashbytes32The hash of the signed message.
rbytes32The r component.
vsbytes32The combined vs component (high bit is yParity, lower 255 bits are s).

Returns

NameTypeDescription
recoveredaddressThe recovered signer address.
errRecoverErrorThe error enum value.
errArgbytes32Additional error argument.

tryRecover

Attempts to recover the signer from (v, r, s) components.

function tryRecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s)
        internal
        pure
        returns (address recovered, RecoverError err, bytes32 errArg);

Parameters

NameTypeDescription
hashbytes32The hash of the signed message.
vuint8The recovery byte.
rbytes32The r component.
sbytes32The s component.

Returns

NameTypeDescription
recoveredaddressThe recovered signer address.
errRecoverErrorThe error enum value.
errArgbytes32Additional error argument.

toEthSignedMessageHash

Returns the Ethereum signed message hash of a 32-byte hash.

Produces the hash that would have been signed by eth_sign / personal_sign.

function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32);

Parameters

NameTypeDescription
hashbytes32The original message hash.

Returns

NameTypeDescription
<none>bytes32The prefixed hash: keccak256("\x19Ethereum Signed Message:\n32" ++ hash).

toEthSignedMessageHash

Returns the Ethereum signed message hash of arbitrary bytes.

Produces keccak256("\x19Ethereum Signed Message:\nlength" ++ s).

function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32);

Parameters

NameTypeDescription
sbytesThe message bytes.

Returns

NameTypeDescription
<none>bytes32The prefixed hash.

toTypedDataHash

Returns the EIP-712 typed data hash.

Produces keccak256("\x19\x01" ++ domainSeparator ++ structHash).

function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32);

Parameters

NameTypeDescription
domainSeparatorbytes32The EIP-712 domain separator.
structHashbytes32The hash of the typed data struct.

Returns

NameTypeDescription
<none>bytes32The final digest.

_throwError

Throws the appropriate error for a given RecoverError.

function _throwError(RecoverError err, bytes32 errArg) private pure;

Parameters

NameTypeDescription
errRecoverError
errArgbytes32

_toString

Converts a uint256 to its decimal ASCII bytes representation.

function _toString(uint256 value) private pure returns (bytes memory);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>bytes

Errors

ECDSAInvalidSignature

error ECDSAInvalidSignature();

ECDSAInvalidSignatureLength

error ECDSAInvalidSignatureLength(uint256 length);

Parameters

NameTypeDescription
lengthuint256

ECDSAInvalidSignatureS

error ECDSAInvalidSignatureS(bytes32 s);

Parameters

NameTypeDescription
sbytes32

Enums

RecoverError

enum RecoverError {
        NoError,
        InvalidSignature,
        InvalidSignatureLength,
        InvalidSignatureS
    }

Variants

NameDescription
NoError
InvalidSignature
InvalidSignatureLength
InvalidSignatureS