BLS12-381 and BN254 curve operations
The BLS12-381 and BN254 examples show how a Solana program can use runtime cryptographic operations for pairing-friendly curves. They accept byte-level instructions, send curve work to native runtime operations, and return the computed point or verification result through transaction return data. This page is a focused companion to On-chain cryptography.
System purpose
These examples isolate curve operations from a larger application. They help an engineer see which inputs a program instruction must prepare and how the runtime result proves that the requested point relation or signature relation is valid.
The examples use no account state. Each transaction carries the operation and its operands. The program checks the input shape, calls the matching curve service, and exposes the result as return data.
Curve operation models
BLS12-381 operations
BLS12-381 supports point addition, point subtraction, and scalar multiplication in both its G1 and G2 groups. G1 and G2 are two related point groups used by pairing-based systems; the example treats them as separate operation targets and does not perform pairings itself. The program maps six instruction discriminators to these operations through cryptography/…/lib.rs• L12-78.
The instruction payload uses big-endian, uncompressed values. A G1 point is 96 bytes, a G2 point is 192 bytes, and a scalar is 32 bytes. Addition and subtraction receive two points. Multiplication receives the scalar first and the point second. The runtime returns a point with the same size as its group.
References: cryptography/…/lib.rs• L12-78 cryptography/…/README.md• L7-18
BN254 operations
BN254 supports G2 addition and G2 scalar multiplication. It also supports an aggregate BLS signature check that uses a pairing. The example therefore shows both point preparation and a complete verification path through the BN254 runtime service. The operation mapping is defined in cryptography/…/lib.rs• L12-28 and #L60-L72.
The point encoding is big-endian and uncompressed. A G1 point is 64 bytes, a G2 point is 128 bytes, and a scalar is 32 bytes. G2 addition receives two points. G2 multiplication receives a point followed by a scalar. Both point operations return one G2 point as transaction return data.
References: cryptography/…/lib.rs• L12-28 cryptography/…/lib.rs• L60-101 cryptography/…/README.md• L7-15
Instruction interfaces
The first byte of every instruction is a discriminator. It selects the curve operation, while the remaining bytes form a fixed layout for that operation. The program rejects an unknown discriminator or an input that does not contain complete operands.
BLS12-381 instruction layouts
The BLS12-381 interface uses the following layouts after the discriminator:
| Discriminator | Operation | Operand layout | Return value |
|---|---|---|---|
0 | G1 addition | G1 point ‖ G1 point | G1 point |
1 | G1 subtraction | G1 point ‖ G1 point | G1 point |
2 | G1 multiplication | 32-byte scalar ‖ G1 point | G1 point |
3 | G2 addition | G2 point ‖ G2 point | G2 point |
4 | G2 subtraction | G2 point ‖ G2 point | G2 point |
5 | G2 multiplication | 32-byte scalar ‖ G2 point | G2 point |
The client creates the instruction by placing the discriminator before the encoded operands. The tests use generator multiples so that addition, subtraction, and multiplication have known point results. This verifies both the wire layout and the returned bytes through LiteSVM in cryptography/…/bls12-381.test.ts• L32-46 and #L62-L118.
References: cryptography/…/lib.rs• L43-58 cryptography/…/bls12-381.test.ts• L32-46 cryptography/…/bls12-381.test.ts• L62-118
BN254 instruction layouts
The BN254 interface uses the following layouts after the discriminator:
| Discriminator | Operation | Operand layout | Return value |
|---|---|---|---|
0 | G2 addition | G2 point ‖ G2 point | G2 point |
1 | G2 multiplication | G2 point ‖ 32-byte scalar | G2 point |
2 | Aggregate verification | G1 aggregate signature ‖ G1 negated message hash ‖ one or more G2 public keys | Aggregate G2 public key when valid |
The aggregate verification payload must contain at least one public key. Its length must remain a whole number of 128-byte G2 points after the two 64-byte G1 values. The client-side codec in cryptography/…/bn254.test.ts• L48-61 makes this fixed layout explicit.
References: cryptography/…/lib.rs• L60-127 cryptography/…/bn254.test.ts• L39-61 cryptography/…/bn254.test.ts• L76-101
Aggregate signature verification
The BN254 example verifies several BLS signatures over one message without checking each signature as a separate pairing. It first combines the supplied G2 public keys with G2 additions. It then checks the combined key against the aggregate signature and the supplied negated message hash.
The verification relation is:
e(aggregate signature, G2 generator) ×
e(negated message hash, aggregate public key) = 1
The program returns the aggregate public key only after the pairing result is one. A mismatched signature or an incomplete signer set causes the instruction to fail instead of returning a misleading key. The end-to-end path is covered by cryptography/…/lib.rs• L104-163 and cryptography/…/bn254.test.ts• L119-130.
The diagram shows how the instruction payload becomes an aggregate key and then a pairing decision.
flowchart LR
A[Aggregate signature] --> V[Pairing check]
B[Negated message hash] --> V
C[G2 public keys] --> D[Repeated G2 additions]
D --> V
V -->|relation equals one| E[Return aggregate public key]
V -->|relation fails| F[Reject instruction]
Public-key aggregation
The public keys are supplied as one contiguous sequence. The first key becomes the working aggregate. Each later key is added to it through the BN254 G2 operation. This makes the aggregate signature check depend on the full signer set represented by the payload.
References: cryptography/…/lib.rs• L113-143 cryptography/…/bn254.test.ts• L94-101
Pairing decision
The pairing input contains two point pairs: the aggregate signature with the canonical G2 generator, and the negated message hash with the aggregate public key. The runtime produces a 32-byte result. The example accepts it only when it represents the value one, then returns the aggregate public key.
References: cryptography/…/lib.rs• L146-163 cryptography/…/test.rs• L20-25
Runtime availability
The examples depend on native curve services rather than ordinary program-side curve math. LiteSVM provides the services used by the examples today, while public cluster use depends on the matching Solana feature gate.
BLS12-381 availability
The BLS12-381 group service is available in the local LiteSVM test environment. Public clusters need the enable_bls12_381_syscall feature gate before these instructions can run there. A service failure is reported as a program error rather than as curve return data.
References: cryptography/…/README.md• L18-25 cryptography/…/lib.rs• L88-116
BN254 availability
The BN254 G2 and pairing services are available in LiteSVM for these examples. Public cluster use of the G2 services needs the enable_alt_bn128_g2_syscalls feature gate. Invalid operand lengths and failed aggregate checks remain distinct program errors, so callers can tell malformed input from a cryptographic mismatch.
References: cryptography/…/README.md• L15-22 cryptography/…/lib.rs• L46-52 cryptography/…/lib.rs• L165-185
Change checklist
When these files or sections are changed, remember to consider these:
- Keep each discriminator, operand order, and point size aligned between the on-chain program and its TypeScript and Rust instruction builders.
- Keep aggregate verification vectors consistent across the encoded G1 values, the ordered G2 public-key list, and the expected pairing relation.
- Recheck cluster feature-gate assumptions when changing a syscall or adding a new curve operation.
- Preserve return-data sizes because clients use them to distinguish a point result from a failed transaction.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Unknown discriminator | The transaction fails as invalid instruction data. | Keep client operation constants aligned with program dispatch. |
| Operand bytes do not match the required layout | The transaction fails with an invalid input length error. | Encode complete points and scalars in the required order. |
| Aggregate payload has no G2 public key or has a partial final key | Aggregate verification does not run. | Require at least one complete 128-byte G2 key after the two G1 values. |
| Aggregate signature does not match the full public-key set | The transaction fails with aggregate verification failure. | Build the aggregate signature and key list from the same signer set and message. |
| Required runtime feature is not active | The curve syscall fails or is unavailable on the target environment. | Check LiteSVM support and the relevant public-cluster feature gate before deployment. |
Key files
| File | Why to read it |
|---|---|
| cryptography/…/lib.rs | Shows the BLS12-381 instruction contract and the six supported G1 and G2 operations. |
| cryptography/…/bls12-381.test.ts | Shows how clients encode BLS12-381 operands and compare returned points. |
| cryptography/…/lib.rs | Shows BN254 point operations and the full aggregate signature verification flow. |
| cryptography/…/bn254.test.ts | Shows the BN254 wire codec and valid and invalid aggregate verification calls. |
| cryptography/…/test.rs | Provides Rust-side known vectors for G2 arithmetic and aggregate signature acceptance or rejection. |
| cryptography/…/README.md | States the BLS12-381 input sizes and runtime availability in a short form. |
| cryptography/…/README.md | States the BN254 instruction variants and pairing use in a short form. |