> For the complete documentation index, see [llms.txt](https://docs.seismic.systems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.seismic.systems/clients/python/precompiles/hkdf.md).

# hkdf

On-chain HKDF-SHA256 key derivation

Derive a 32-byte key with the HKDF precompile at `0x68`.

## Overview

`hkdf()` and `async_hkdf()` accept arbitrary input key material (`ikm`) and return a `Bytes32` derived key.

## Signature

```python
def hkdf(
    w3: Web3,
    ikm: bytes,
) -> Bytes32

async def async_hkdf(
    w3: AsyncWeb3,
    ikm: bytes,
) -> Bytes32
```

## Parameters

| Parameter | Type                 | Required | Description              |
| --------- | -------------------- | -------- | ------------------------ |
| `w3`      | `Web3` / `AsyncWeb3` | Yes      | Connected Seismic client |
| `ikm`     | `bytes`              | Yes      | Input key material       |

## Returns

| Type                                                                                    | Description         |
| --------------------------------------------------------------------------------------- | ------------------- |
| [`Bytes32`](https://docs.seismic.systems/clients/python/api-reference/types/bytes32.md) | 32-byte derived key |

## Examples

### Basic Usage

```python
from seismic_web3 import create_public_client
from seismic_web3 import precompiles as sp

w3 = create_public_client("https://testnet-1.seismictest.net/rpc")

key = sp.hkdf(w3, b"input key material")
print(key.to_0x_hex())
```

### Context Separation

```python
master = b"shared-secret"
encryption_key = sp.hkdf(w3, master + b"\x01encryption")
auth_key = sp.hkdf(w3, master + b"\x02auth")
assert encryption_key != auth_key
```

### Async Usage

```python
from seismic_web3 import create_async_public_client
from seismic_web3 import precompiles as sp

async def main():
    w3 = create_async_public_client("https://testnet-1.seismictest.net/rpc")
    key = await sp.async_hkdf(w3, b"async input")
    print(key.to_0x_hex())
```

## Gas Cost

The SDK computes gas as:

```python
from math import ceil

linear = 3000 + ceil(len(ikm) / 32) * 12
total_gas = 2 * linear + 120
```

Examples:

* `len(ikm)=0`: `6120`
* `len(ikm)=32`: `6144`

## Notes

* Deterministic: the same `ikm` always produces the same output.
* Good for deriving protocol keys from shared secrets.
* Not a password hashing function; use `argon2`/`bcrypt` for password storage.

## See Also

* [HKDF — Precompiles Reference](https://docs.seismic.systems/reference/precompiles/hkdf.md)
* [ecdh](https://docs.seismic.systems/clients/python/precompiles/ecdh.md)
* [aes-gcm-encrypt](https://docs.seismic.systems/clients/python/precompiles/aes-gcm-encrypt.md)
* [rng](https://docs.seismic.systems/clients/python/precompiles/rng.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.seismic.systems/clients/python/precompiles/hkdf.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
