|
| 1 | +# Cryptography |
| 2 | + |
| 3 | +CosmWasm offers cryptographic primitives that are implemented as VM intrinsics. This means that they |
| 4 | +are more efficient than implementations in contract code could be, saving you gas and code size. In |
| 5 | +some cases they are also _impossible_ to implement in contract code, since some of them require a |
| 6 | +random intermediate value and randomness is generally not exposed to contracts to ensure |
| 7 | +deterministic execution. |
| 8 | + |
| 9 | +In the sidebar, you can find all the supported algorithms/curves along with documentation about |
| 10 | +them. |
| 11 | + |
| 12 | +## Gas costs |
| 13 | + |
| 14 | +:::tip |
| 15 | + |
| 16 | +Note that these values are in CosmWasm Gas (which is not equivalent to Cosmos SDK Gas; rather, |
| 17 | +CosmWasm Gas is eventually converted to Cosmos SDK Gas). For more info on gas [check this page]. |
| 18 | + |
| 19 | +::: |
| 20 | + |
| 21 | +### secp256k1 |
| 22 | + |
| 23 | +- Verify: 96_000_000 gas |
| 24 | +- Recover public key: 194_000_000 gas |
| 25 | + |
| 26 | +### secp256r1 |
| 27 | + |
| 28 | +- Verify: 279_000_000 gas |
| 29 | +- Recover public key: 592_000_000 gas |
| 30 | + |
| 31 | +### ed25519 |
| 32 | + |
| 33 | +- Verify: 35_000_000 gas |
| 34 | +- Batch verify (multiple signatures; multiple keys): |
| 35 | + - Base: 24_000_000 gas |
| 36 | + - Per signature/key pair: 21_000_000 gas |
| 37 | +- Batch verify (multiple signatures; one key): |
| 38 | + - Base: 36_000_000 gas |
| 39 | + - Per signature: 10_000_000 gas |
| 40 | + |
| 41 | +### BLS12-381 |
| 42 | + |
| 43 | +- Aggregate points to G1: |
| 44 | + - Base: 68_000_000 gas |
| 45 | + - Per point: 12_000_000 gas |
| 46 | +- Aggregate points to G2: |
| 47 | + - Base: 103_500_000 gas |
| 48 | + - Per point: 24_500_000 gas |
| 49 | +- Hash to G1: 563_000_000 gas |
| 50 | +- Hash to G2: 871_000_000 gas |
| 51 | +- Pairing equality: |
| 52 | + - Base: 2_112_000_000 gas |
| 53 | + - Per item: 163_000_000 gas |
| 54 | + |
| 55 | +You can also check these numbers in the [source code]. |
| 56 | + |
| 57 | +## Note on hash functions |
| 58 | + |
| 59 | +You'll notice that CosmWasm does not implement any hash functions as native functions, and we intend |
| 60 | +to keep it that way. The reasoning for that is: |
| 61 | + |
| 62 | +- Hash functions are pretty much always cheap in terms of execution cost _and_ code size |
| 63 | +- There are way too many hash functions out there, adding one would open the floodgates where we |
| 64 | + would have to add more |
| 65 | + - SHA-3 family, Keccak256, cSHAKE256, BLAKE2, BLAKE3, SHA-2 family, KangarooTwelve, etc. (and this |
| 66 | + is just a small sample set to drive the point home) |
| 67 | +- Benchmarking and pricing functions (and keeping those up-to-date) correctly is a lot of work |
| 68 | +- We want to keep the API surface as small as possible for ease of maintenance |
| 69 | + |
| 70 | +Keep in mind that, thanks to Wasm being our execution environment, contract execution is quite fast. |
| 71 | +In most cases the perceived need to move hashing into a host function is premature optimization and |
| 72 | +not actually needed. |
| 73 | + |
| 74 | +[check this page]: ../../architecture/gas |
| 75 | +[source code]: https://github.com/CosmWasm/cosmwasm/blob/main/packages/vm/src/environment.rs#L62-L101 |
0 commit comments