ERC-2098 · Final · ERC · 2019-03-14
ERC-2098: Compact Signature Representation
Packs an Ethereum signature into 64 bytes by storing the 1-bit yParity in the always-zero top bit of s, saving calldata and gas wherever signatures are passed around.
What problem does it solve?
A secp256k1 signature has three parts: r (32 bytes), s (32 bytes) and v/yParity, which only needs one bit but is usually sent as a whole byte (27 or 28). That makes 65 bytes, which awkwardly becomes 96 bytes when ABI-encoded into 32-byte words.
Contracts that accept signatures, such as meta-transactions, permits and multisigs, pay for every one of those bytes in calldata. ERC-2098 defines a 64-byte "compact" form that is exactly two words.
How it works
Two observations make the trick work:
yParityis always 0 or 1 (27/28 are just a historical offset).- Since Homestead (EIP-2), canonical signatures use a "low"
s, at most half the curve order, so the top bit ofsis always 0.
So the parity bit is stored in that top bit:
[ 256-bit r ][ 1-bit yParity | 255-bit s ] → (r, yParityAndS), often called (r, vs)
Decoding is a mask and a shift: s = vs & (2²⁵⁵ − 1) and yParity = vs >> 255. Because the compact form is 64 bytes with two fields, it cannot be confused with the 65-byte three-field form.
Sample code
Recovering a signer from a compact signature in Solidity:
function recoverCompact(bytes32 digest, bytes32 r, bytes32 vs) internal pure returns (address) {
bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
uint8 v = uint8((uint256(vs) >> 255) + 27);
address signer = ecrecover(digest, v, r, s);
require(signer != address(0), "bad signature");
return signer;
}
OpenZeppelin's ECDSA library offers the same as ECDSA.recover(hash, r, vs).
Producing one with ethers v6:
import { Wallet, Signature } from "ethers";
const wallet = Wallet.createRandom();
const sigHex = await wallet.signMessage("Hello World"); // 65 bytes
const sig = Signature.from(sigHex);
console.log(sig.r, sig.yParityAndS); // the two 32-byte words
console.log(sig.compactSerialized); // 64-byte hex string
// Round-trip: Signature.from also accepts the 64-byte form
const back = Signature.from(sig.compactSerialized);
console.log(back.v === sig.v && back.s === sig.s); // true
Things to know
- Status: Final. It is an encoding convention, not a protocol change; contracts must explicitly accept
(r, vs). - It only works for low-
ssignatures.ecrecoveritself also accepts high-svalues, so normalizes(and flipyParity) before compacting a signature from an unknown source. - A contract that accepts both 65-byte and 64-byte forms must make sure the same signature cannot be replayed in both encodings where uniqueness matters; track used nonces rather than used signature bytes.
- Pairs naturally with signed-message standards such as EIP-712 and ERC-2612. Smart-contract signers (ERC-1271) do not use ECDSA, so the compact form does not apply to them.