Crypto: parity vectors against the original client
How tools/crypto-vectors generates fixtures from the original client sources, what the fixture file holds, the Bun tests that compare the port byte for byte, and the self-test that repeats the check inside the host runtime.

The crypto port is trusted because it is compared, byte for byte, with output of the code it replaces. A generator runs the original sources with pinned time and randomness and writes one fixture file; the Bun tests replay the same inputs through src/crypto; and a hidden resource repeats a small check inside the app's real runtime.

Generator

tools/crypto-vectors/generate.ts builds an isolated workspace in the OS temp directory. It imports nothing from src/crypto.

    Copies original files from the repository and records the SHA-256 of each:

      14 files from frontend/packages/client/src (base64, blobs, cbor, signer, change, ref, signing, comment, unicode, hm-types, page-input, vault, vault-merge, encryption),

      the React Native app's key-derivation.ts and its vault crypto, seedkey, envelope and state-codec,

      notification-service.ts and notify-url.ts from the shared package, and the agents protocol version,

      the signAgentActionWith section cut out of frontend/packages/ui/src/agents/client.ts.

    Writes a package.json with the originals' real dependencies at pinned versions and runs bun install.

    Writes provenance.json (source commit, file hashes, dependency versions).

    Runs run-originals.ts there.

    Copies vectors.json to src/crypto/__tests__/fixtures/rn-client.json, only after every original call succeeded, and deletes the workspace.

run-originals.ts fixes Date.now to 1700000000000 and Math.random to 0 (for the original's quote block ids), and replaces fetch to capture notify request bodies, so "no network traffic or server writes occur".

The committed fixture records source commit 477d5d0ff766095a309a085c1a45edbb1c18e1fb. The generator reads frontend/apps/mobile/..., which no longer exists on main (see History and the React Native archive), so regenerating needs a checkout that still has it. That is an inference from the paths; the fixture itself is static.

Fixture contents

Key

Content

provenance, timestampMs

where the vectors came from, and the pinned time

mnemonicFromZeroEntropy

the phrase for 16 zero bytes

mnemonics (4)

public BIP-39 test phrases with empty, ASCII and non-ASCII passphrases: BIP-39 seed, keys, account id, signature

keys (6)

seeds with public key, account id, a signature and the signed home genesis

change, ref

a SetAttributes Change (unsigned and signed) and its home Ref

comments (6)

plain, reply with private visibility, trailing empty block, quoted range, list container, annotated text; each with record id

runtime

the three values the self-test must produce

seedkey

an exported key file

vault

two gzipped states, AEAD ciphertexts, HKDF, SHA-256 and connect id literals, base64 samples, a local and a remote envelope

clientUndefinedHex

how the client codec encodes undefined

agents (2), notify (2)

direct and delegated agents envelopes; notify state and actions bodies

All key material in it is public test data.

Bun tests

crypto.test.ts has 47 tests. Its helper expectBlob asserts four things per blob: the hex bytes, the CID, the signature, and that verifyBlob accepts it.

Group

Tests

Page

RN BIP39 / SLIP-10 / Go account parity

13

Primitives

Signed CBOR and CID vectors from the real client

12

Blobs

Distinct agents and notify signing conventions

4

Envelopes

Seedkey desktop JSON

2

Primitives

Vault crypto and Go envelope/state parity

9

Vault codec

Codec semantics, debug source, UUID

3

Vendored codecs

Cooperative mnemonic derivation

4

Primitives

review.test.ts (3 tests) and vendor.test.ts (1 test) complete the directory. They run with bun run test; see Bun unit tests.

Self-test inside the runtime

Bun is not Hermes, so src/crypto/sources.ts exports a source, cryptoSelfTest, that the app itself runs. With a fixed public test phrase ("never used for network writes") it:

    checks secure randomness and an HKDF literal,

    derives the key pair with keyPairFromMnemonicAsync,

    signs a one-paragraph comment at the pinned timestamp,

    serializes a vault state, seals it with XChaCha20-Poly1305 and opens it again (exercising gzip and the AEAD),

    signs hello,

calling yieldCryptoTurn() between steps to stay inside the turn budget. It returns {accountId, signatureHex, commentCid} and throws on any mismatch.

In app.contract the shape CryptoSelfTest and, at line 247, the resource cryptoCheck = cryptoSelfTest() are the crypto package's only declarations. The resource has no arguments or condition and nothing renders it; see The root contract.

crypto.test.contract has one test, "crypto vectors run inside the host runtime". It waits up to 20,000 ms for cryptoCheck.accountId, then expects the account id, signature and comment CID. The same three values are vectors.runtime, and the Bun test "Exact debug source matches independently generated RN vectors" ties the two together. Run on iOS, this is the only check that the port is correct on Hermes; see Contract UI tests.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime

Attributes
name
Crypto: parity vectors against the original client
summary
How tools/crypto-vectors generates fixtures from the original client sources, what the fixture file holds, the Bun tests that compare the port byte for byte, and the self-test that repeats the check inside the host runtime.