Skip to main content

crypto

The crypto namespace provides cryptographic functions for hashing, key management, signing, and Ethereum operations. For complete API documentation, see the Standard Library crypto reference.

Overview​

The crypto namespace includes:

  • Hashing – sha256, keccak256 for computing cryptographic hashes
  • Key derivation – privkey_to_pubkey for deriving public keys from private keys
  • Signature verification – verify_signature, get_signature
  • Ethereum operations – eth_sign, eth_ecrecover, eth_privkey_to_address, eth_pubkey_to_address
  • Key encoding – pubkey_encode, pubkey_to_xy, xy_to_pubkey for format conversions

Key functions​

Example: Verify a signature​

val pubkey = x'036d6caac248af96f6afa7f904f550253a0f3ef3f5aa2fe6838a95b216691468e2';
val message = x'DEADBEEF';
val signature = x'8ac02f17b508815fa9495177395925e41fd7db595ad35e54a56be6284e5b8e08...';

val ok = crypto.verify_signature(message, pubkey, signature);
print(ok); // prints "true"

Example: Ethereum signing and recovery​

val privkey = x'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f';
val pubkey = crypto.privkey_to_pubkey(privkey);

val hash = 'Hello'.to_bytes();
val (r, s, rec_id) = crypto.eth_sign(hash, privkey);

val recovered_pubkey = x'04' + crypto.eth_ecrecover(r, s, rec_id, hash);
require(recovered_pubkey == pubkey);
note

All functions that accept a public key support compressed (33-byte), uncompressed (65-byte), and 64-byte formats (as returned by eth_ecrecover).