Skip to main content

Cryptography Builtins

Coco provides built-in cryptographic functions for hashing and signature verification.

Hash Functions​

FunctionDescription
Builtins.Sha256(data)SHA-256 hash → hash U256
Builtins.Keccak(data)Keccak-256 hash → hash U256
Builtins.Blake2b(data)Blake2b hash → hash U256

Signature Verification​

FunctionDescription
Builtins.Sigverify(data, signature, pubkey)Verify signature → ok Bool

Verifies that data was signed by the private key corresponding to pubkey.

Example​

coco HashExample

endpoint pure ComputeHash(data Bytes) -> (hash U256):
hash = Builtins.Sha256(data)

endpoint pure VerifySignature(
data Bytes,
signature Bytes,
pubkey Bytes
) -> (valid Bool):
valid = Builtins.Sigverify(data, signature: signature, pubkey: pubkey)
Argument names

A bare variable passes its own name, which must match the parameter name (data, signature, pubkey). The example above compiles because its variables have those names. With other names, label the arguments: Builtins.Sha256(blob) fails with "expected argument name data at position 0, called with blob", while Builtins.Sha256(data: blob) works. See Argument names.

Named outputs

The output names in the tables (hash, ok) are only checked when you use the optional capture form, as in h = (hash) <- Builtins.Sha256(data) or valid = (ok) <- Builtins.Sigverify(data, signature, pubkey). A different name is a compile error. See Named outputs.