On-chain cryptography examples
The cryptography examples show how Solana programs use runtime support for pairing-friendly curves. BLS12-381 demonstrates group arithmetic in two point groups. BN254 adds pairing-based aggregate signature checks. Both examples keep the program stateless so an engineer can focus on the boundary between transaction data, a curve operation, and the returned result.
Cryptography example suite
These examples aim to make expensive curve work available to an on-chain program without building a complete signature or proof system around it. The program receives encoded points and scalars in an instruction, asks the Solana runtime to perform the supported operation, and exposes the result as transaction return data.
| Curve example | Main capability | On-chain role |
|---|---|---|
| BLS12-381 | G1 and G2 addition, subtraction, and scalar multiplication | Exercises the group operations used by pairing-based signatures and proofs |
| BN254 | G2 addition, scalar multiplication, and a pairing check | Aggregates public keys and checks one BLS signature relation |
The detailed operation layouts and examples are in BLS12-381 and BN254 curve operations.
Curve operation families
BLS12-381 group arithmetic
BLS12-381 provides arithmetic over its G1 and G2 point groups. The example uses the runtime curve operation support for point addition, point subtraction, and scalar multiplication, which gives higher-level programs building blocks for signature and proof systems.
The wire format uses fixed-size, big-endian values. G1 points occupy 96 bytes, G2 points occupy 192 bytes, and scalars occupy 32 bytes. These sizes are part of the request contract rather than choices made by the example at run time.
References: cryptography/…/README.md• L3-18 cryptography/…/lib.rs• L12-28
BN254 pairing operations
BN254 provides the group work needed for an aggregate BLS check. The example adds G2 public keys on chain, then uses a pairing relation to decide whether one aggregate signature matches the supplied message hash and public keys.
The pairing check reduces several signer checks to one relation. A valid request returns the aggregate public key. A request with an incomplete or incorrect signature fails instead of returning a cryptographic result.
References: cryptography/…/README.md• L1-15 cryptography/…/lib.rs• L23-33 cryptography/…/lib.rs• L104-127
On-chain request flow
The two examples follow the same high-level path. The client builds a transaction instruction, the program selects a curve operation, and the runtime supplies the curve result.
flowchart LR
A[Client encodes points and scalars] --> B[Solana instruction]
B --> C[Stateless cryptography program]
C --> D[Runtime curve operation]
D --> E[Return data or transaction error]
Instruction boundary
Each request starts with an operation discriminator. The remaining bytes contain the operands for that operation. The program does not infer missing operands or accept variable encodings for a fixed operation, so the client and program must agree on the curve, byte order, operand order, and total length.
The result is returned as transaction return data. This keeps the examples useful as small building blocks, while a larger application can consume the result in its own transaction or client flow.
References: cryptography/…/lib.rs• L43-79 cryptography/…/lib.rs• L60-101 cryptography/…/bls12-381.test.ts• L62-87
Stateless program model
The programs do not read or update accounts for the cryptographic work. All input needed for an operation travels in the instruction, and the program returns only the computed point or the aggregate public key. This model makes the examples clear, but it also means that callers must supply and validate the surrounding application context.
For aggregate verification, the request carries the aggregate signature, the negated message hash, and one or more G2 public keys. The program performs the public-key aggregation during that request rather than storing an intermediate aggregate.
References: cryptography/…/lib.rs• L43-47 cryptography/…/lib.rs• L104-126 cryptography/…/test.rs• L52-60
Runtime constraints
Encoding and size constraints
The runtime APIs use fixed binary layouts. BLS12-381 uses big-endian G1, G2, and scalar values. BN254 uses 64-byte G1 points, 128-byte G2 points, and 32-byte scalars. A request must contain complete operands in the expected order before the runtime can process it.
These constraints shape client code and protocol design. A caller that changes a point encoding or adds a field must also change the instruction contract on both sides. The examples do not provide a text format or a state layer that can hide this boundary.
References: cryptography/…/README.md• L7-16 cryptography/…/README.md• L7-13 cryptography/…/lib.rs• L75-100
Cluster and test-runtime availability
The cryptographic operations depend on Solana runtime syscalls. LiteSVM supports the operations used by these examples, which allows local transaction tests. Public-cluster use depends on the matching Solana feature gates, so local success does not by itself prove that a deployed cluster will accept the same instruction.
When the program is built outside the Solana target, the syscall path is unavailable. This separates local unit behavior from the runtime behavior that the examples are intended to exercise.
Each example now has its own package manifest, workspace policy, and lockfile. The manifests provide matching build, test, and deploy commands. The workspace files keep dependency override rules with each example, while the lockfiles pin the Solana client and local test tool versions for that example. These package files keep local runs repeatable, but they do not change the runtime feature requirements of a deployed cluster.
References: cryptography/…/README.md• L18-25 cryptography/…/README.md• L15-22 cryptography/…/lib.rs• L88-116 cryptography/…/lib.rs• L165-185 cryptography/…/package.json• L5-22 cryptography/…/pnpm-workspace.yaml• L1-3 cryptography/…/pnpm-lock.yaml• L1-38 cryptography/…/package.json• L5-22 cryptography/…/pnpm-workspace.yaml• L1-3 cryptography/…/pnpm-lock.yaml• L1-38
Validation boundaries
Request validation
The programs reject unknown operation discriminators and input that does not contain the exact operands for the selected operation. BN254 aggregate verification also requires at least one complete G2 public key after its signature and message fields.
This validation protects the syscall boundary from ambiguous input. It does not validate the business meaning of a message, signer set, or application request. Those checks remain the responsibility of the caller that assembles the cryptographic input.
References: cryptography/…/lib.rs• L48-58 cryptography/…/lib.rs• L81-86 cryptography/…/lib.rs• L65-72 cryptography/…/lib.rs• L113-125
Verification guarantees
The test suites exercise the full client-to-program path in LiteSVM. They confirm that known point arithmetic produces the expected encoded points, that a complete BN254 aggregate signature passes, and that malformed or incomplete requests fail. They therefore demonstrate the request contract and result handling rather than proving the security of a wider protocol.
References: cryptography/…/bls12-381.test.ts• L89-118 cryptography/…/test.rs• L61-99 cryptography/…/bn254.test.ts• L103-138 cryptography/…/test.rs• L62-109
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the operation discriminator order aligned between each program and its client callers.
- Keep point sizes, scalar order, and big-endian encoding aligned between transaction builders and syscall inputs.
- Recheck public-cluster feature-gate support when adding a new runtime cryptography operation.
- Preserve the BN254 aggregate verification relation when changing the public-key aggregation or pairing input.
- Keep each package manifest, workspace policy, and lockfile aligned when changing client or local-test dependencies.
- Update the child page link and operation summary when the supported curve surface changes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Operand bytes have the wrong length or order | The program rejects the instruction before curve work completes | Use the fixed curve layouts and keep client codecs aligned with the program contract |
| A caller uses an operation not supported by the active runtime | The syscall fails or is unavailable on the target | Check the target cluster's feature-gate status before deployment |
| Aggregate signature data does not match all supplied public keys | BN254 aggregate verification fails and no aggregate key is returned | Build the aggregate signature, message hash, and public-key list from the same signer set |
| Local LiteSVM support is mistaken for public-cluster support | A transaction that passes local tests can fail after deployment | Treat runtime feature activation as a deployment prerequisite |
| A package manifest, workspace policy, or lockfile is changed without the others | Local installs can use a different client or test tool set than intended | Update the package metadata and dependency records together |
Key files
| File | Why to read it |
|---|---|
| cryptography/…/README.md | States the purpose, supported BLS12-381 operations, encoding sizes, and runtime availability |
| cryptography/…/lib.rs | Shows the BLS12-381 request boundary and return-data model |
| cryptography/…/README.md | States the BN254 operation set and the aggregate verification goal |
| cryptography/…/lib.rs | Shows how G2 aggregation and the pairing check form one on-chain verification flow |
| cryptography/…/bls12-381.test.ts | Demonstrates client construction and end-to-end BLS12-381 result checks |
| cryptography/…/bn254.test.ts | Demonstrates BN254 point operations, aggregate verification, and rejection behavior |
| cryptography/…/package.json, cryptography/…/pnpm-workspace.yaml, cryptography/…/pnpm-lock.yaml | Define the BLS12-381 package commands, dependency override policy, and pinned client toolchain |
| cryptography/…/package.json, cryptography/…/pnpm-workspace.yaml, cryptography/…/pnpm-lock.yaml | Define the BN254 package commands, dependency override policy, and pinned client toolchain |