# crypto · Chromia Docs (concept)

> The crypto namespace provides cryptographic functions for hashing, key management, signing, and Ethereum operations.

Source: https://docs.chromia.com/rell/language-features/systemlib/namespaces/crypto/

# crypto

On this page

* [Overview](#overview)
* [Key functions](#key-functions)
* [Example: Verify a signature](#example-verify-a-signature)
* [Example: Ethereum signing and recovery](#example-ethereum-signing-and-recovery)

The `crypto` namespace provides cryptographic functions for hashing, key management, signing, and Ethereum operations.
For complete API documentation, see the
[Standard Library `crypto` reference](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/).

## Overview

The `crypto` namespace includes:

* **Hashing** – [`sha256`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/root/sha256/),
  [`keccak256`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/root/keccak256/) for computing
  cryptographic hashes
* **Key derivation** – [`privkey_to_pubkey`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/privkey_to_pubkey/) for deriving public keys from private keys
* **Signature verification** –
  [`verify_signature`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/root/verify_signature/),
  [`get_signature`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/get_signature/)
* **Ethereum operations** – [`eth_sign`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/eth_sign/),
  [`eth_ecrecover`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/root/eth_ecrecover/),
  [`eth_privkey_to_address`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/eth_privkey_to_address/), [`eth_pubkey_to_address`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/eth_pubkey_to_address/)
* **Key encoding** – [`pubkey_encode`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/pubkey_encode/), [`pubkey_to_xy`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/pubkey_to_xy/), [`xy_to_pubkey`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/xy_to_pubkey/) for format conversions

## Key functions

* [`crypto.sha256`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/sha256/),
  [`crypto.keccak256`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/keccak256/) – Compute 32-byte
  hashes
* [`crypto.privkey_to_pubkey`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/privkey_to_pubkey/) –
  Derive public key from private key
* [`crypto.verify_signature`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/verify_signature/) –
  Verify a signature against message and public key
* [`crypto.eth_sign`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/eth_sign/) – Create an
  Ethereum signature
* [`crypto.eth_ecrecover`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/eth_ecrecover/) – Recover
  public key from Ethereum signature
* [`crypto.eth_pubkey_to_address`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/crypto/eth_pubkey_to_address/)
  – Derive Ethereum address from public key

## Example: Verify a signature

Rell

```
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

Rell

```
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`).

[View this page on docs.chromia.com](https://docs.chromia.com/rell/language-features/systemlib/namespaces/crypto/)Content mirrored from the live docs
