Variable Length Hashing

MPS0053
StatusDraft
CategoryLibraries and Tooling
AuthorsKarmel Elshinnawi (Karmoola)
Created28-NOV-2026

Abstract

Compact’s keccak256 and secp256k1 signature functions only accept fixed-length input: they always hash the entire argument, and its size is set at compile time. Contracts therefore cant hash or verify signatures over variable-length messages. A message padded to a fixed size produces a different digest from the one the signer hashed, so the signature check fails. This blocks contracts that verify Ethereum-style signed messages of varying length, such as oracle price updates.

Vision

Compact supports variable-length input for keccak256 and secp256k1. A contract passes a fixed-size buffer and the message length, and the function hashes or verifies only the message bytes. The result matches what an off-chain signer produced.

Problem

Oracles, bridges and other services that sign variable-length messages off-chain cannot be verified on Midnight

  • keccak256 and the secp256k1 operations take fixed-length input only. There is no way to hash part of a buffer.
  • Circuits require fixed-size inputs, so variable-length messages must be padded. The padding is hashed with the message, so the digest no longer matches the signed digest.
    • Workarounds, such as verifying a signed Merkle root instead of the message, require the signer to change its protocol. External protocols built on variable-length messages will not do this.

Use Cases

  1. Oracle price updates: An oracle signs variable-length price updates (secp256k1 signature over a keccak256 digest). A contract copies a 240-byte update into a 512-byte buffer, hashes the first 240 bytes, and verifies the signature against the oracle’s public key before accepting the price. Today verification fails because the padding is hashed.
  2. Cross-chain messages: A contract verifies Ethereum-signed messages, such as attestations, intents or bridge messages, whose length depends on their content.

Goals

  1. Hash variable-length input with keccak256: hash only the first length bytes of a fixed-size buffer, where length is a runtime value and length ≤ N.
  2. Verify secp256k1 signatures and recover signers over variable-length messages.
  3. Output matches standard Ethereum keccak256 and secp256k1 over the same message.
  4. Proving cost is acceptable for use in every transaction

References

Acknowledgements

This MPS is licensed under CC-BY-4.0.