Cryptography

CosmWasm offers cryptographic primitives that are implemented as VM intrinsics. This means that they are more efficient than implementations in contract code could be, saving you gas and code size. In some cases they are also impossible to implement in contract code, since some of them require a random intermediate value and randomness is generally not exposed to contracts to ensure deterministic execution.

In the sidebar, you can find all the supported algorithms/curves along with documentation about them.

Gas costs

💡

Note that these values are in CosmWasm Gas (which is not equivalent to Cosmos SDK Gas; rather, CosmWasm Gas is eventually converted to Cosmos SDK Gas). For more info on gas, check this page.

secp256k1

  • Verify: 96_000_000 gas
  • Recover public key: 194_000_000 gas

secp256r1

  • Verify: 279_000_000 gas
  • Recover public key: 592_000_000 gas

ed25519

  • Verify: 35_000_000 gas
  • Batch verify (multiple signatures; multiple keys):
    • Base: 24_000_000 gas
    • Per signature/key pair: 21_000_000 gas
  • Batch verify (multiple signatures; one key):
    • Base: 36_000_000 gas
    • Per signature: 10_000_000 gas

BLS12-381

  • Aggregate points to G1:
    • Base: 68_000_000 gas
    • Per point: 12_000_000 gas
  • Aggregate points to G2:
    • Base: 103_500_000 gas
    • Per point: 24_500_000 gas
  • Hash to G1: 563_000_000 gas
  • Hash to G2: 871_000_000 gas
  • Pairing equality:
    • Base: 2_112_000_000 gas
    • Per item: 163_000_000 gas

You can also check these numbers in the source code (opens in a new tab).

Note on hash functions

You'll notice that CosmWasm does not implement any hash functions as native functions, and we intend to keep it that way. The reasoning for that is:

  • Hash functions are pretty much always cheap in terms of execution cost and code size
  • There are way too many hash functions out there, adding one would open the floodgates where we would have to add more
    • SHA-3 family, Keccak256, cSHAKE256, BLAKE2, BLAKE3, SHA-2 family, KangarooTwelve, etc. (and this is just a small sample set to drive the point home)
  • Benchmarking and pricing functions (and keeping those up-to-date) correctly is a lot of work
  • We want to keep the API surface as small as possible for ease of maintainance

Keep in mind that, thanks to Wasm being our execution environment, contract execution is quite fast. In most cases the perceived need to move hashing into a host function is premature optimization and not actually needed.