C, Go, and Rust integrations
Constantine exposes its Nim cryptography through a C-compatible boundary. The C headers describe the shared data and function contract, while the Go and Rust packages turn that contract into language-level APIs. This lets both languages use the same core operations and check their results against the same protocol vectors.
Integration model
The integration path has one source of behavior and several language-facing layers. Nim exports the public operations, generated C headers describe them, and each consumer maps C data and status values into its own call and error model.
This diagram shows how a request moves from a language package to the Nim core and back as a value or status.
flowchart LR
N[Nim core] --> E[Nim C exports]
E --> H[Generated C headers]
E --> L[Static or dynamic library]
H --> G[Go cgo package]
L --> G
H --> S[Rust bindgen declarations]
L --> S
G --> GT[Go integration tests]
S --> RT[Rust integration tests]
H --> CT[C ABI examples and tests]
L --> CT
C ABI boundary
The C ABI is the common contract between the Nim library and foreign callers. Exported names use the ctt_ prefix, arguments use C-compatible values or pointers, and operations report invalid input through return values or status enums rather than Nim exceptions. The generated declarations cover hashes, random bytes, curve and field operations, thread pools, and Ethereum protocol calls through the umbrella header include/constantine.h.
References: bindings/c_curve_decls.nim• L28-45 bindings/c_curve_decls_parallel.nim• L15-29 include/constantine.h• L12-47
Consumer layers
The Go package calls the C ABI through cgo and presents Go arrays, slices, contexts, and errors. The Rust stack separates raw declarations in constantine-sys from higher-level crates that provide Rust results, ownership, and typed protocol APIs. Both layers keep the C ABI as the compatibility point instead of reimplementing the cryptographic behavior.
References: constantine-go/constantine.go• L10-28 constantine-rust/…/lib.rs• L1-8 constantine-rust/…/lib.rs• L9-22
C ABI surface
The C surface groups the core by use rather than by Nim module. Callers include one umbrella header for the full public surface or include a narrower header for a focused operation such as Keccak-256. The library can be linked as a static or dynamic library, so C, Go, and Rust use the same exported behavior with different link steps.
Data and status contracts
The ABI uses fixed-size structures for fields and curve points, byte buffers with explicit lengths, and pointers for resources that have a life cycle. constantine/…/datatypes.h defines the portable base types and word sizing rules that keep these structures consistent across C compilers. Protocol operations return status values that distinguish success, invalid input, and verification failure.
References: include/…/datatypes.h• L16-59 bindings/c_typedefs.nim• L53-100 constantine-rust/…/lib.rs• L10-59
Resource lifecycles
Thread pools and protocol contexts cross the ABI as pointers. A caller creates them through a constructor, uses the context for related operations, and releases it through the matching shutdown or delete call. Parallel operations require a configured thread pool, while trusted-setup operations require a loaded KZG context before proof work can start.
References: constantine-go/constantine.go• L30-45 constantine-go/constantine.go• L58-134 constantine-rust/…/lib.rs• L37-69 constantine-rust/…/lib.rs• L15-33
Header and declaration generation
Headers and foreign declarations are derived from the same binding descriptions as the exported Nim functions. This keeps the C names, parameter shapes, data layouts, and supported curve set aligned with the compiled library.
Nim binding descriptions
The binding templates define families of exported operations for big integers, fields, extension fields, elliptic-curve points, hash-to-curve, and parallel multi-scalar multiplication. Curve-specific declarations select the supported types and operations for BLS12-381, BN254-Snarks, Pallas, Vesta, and Banderwagon. The main library imports these binding modules together with the protocol exports.
References: bindings/lib_curves.nim• L15-19 bindings/lib_curves.nim• L21-65 bindings/lib_curves.nim• L70-123 bindings/lib_curves.nim• L146-157 bindings/lib_constantine.nim• L15-33
C header generation
The header generator turns the selected C declarations into guarded headers under include/constantine. It emits base typedefs, curve data structures, function declarations, and C++ linkage guards. Parallel headers include the thread-pool and serial curve headers so a caller sees one consistent type contract.
Run nimble make_headers when the public binding descriptions change. The generator requires header-collection mode and writes both regular and parallel curve headers plus the shared big-integer header.
References: bindings/lib_headers.nim• L19-38 bindings/lib_headers.nim• L40-70 bindings/lib_headers.nim• L86-109 bindings/lib_headers.nim• L130-170 constantine.nimble• L314-321
Rust declaration generation
Rust raw declarations come from include/constantine.h through bindgen. scripts/gen_rust_bindings.sh generates separate 64-bit and 32-bit files and applies safety-related type rules such as keeping thread pools non-copyable and avoiding debug output for secret-bearing types. constantine-sys selects the file that matches the target pointer width.
The Rust build also compiles a position-independent static Constantine library into Cargo’s output directory. Changes to Nim sources, binding descriptions, headers, or the Nim build file cause the build script to rebuild that library.
References: scripts/gen_rust_bindings.sh• L1-46 constantine-rust/…/lib.rs• L1-8 constantine-rust/…/build.rs• L5-57 constantine.nimble• L327-333
Go integration
The Go integration is a cgo package that links the Constantine library and includes constantine.h. Its public methods convert Go values into the fixed-size C records and byte spans expected by the ABI. It then converts C status values into Go errors while keeping a verification failure distinct from an API or input error.
Go protocol APIs
The package exposes thread-pool use, Ethereum BLS signatures, EIP-4844 KZG, EIP-7594 PeerDAS, and EVM precompile calls. KZG methods use an explicit context for trusted setup state. Parallel KZG methods also require a thread pool, and the package checks that requirement before it crosses the ABI.
References: constantine-go/constantine.go• L12-24 constantine-go/constantine.go• L47-147 constantine-go/constantine.go• L249-354 constantine-go/constantine.go• L357-460 constantine-go/constantine.go• L840-890
Go hash package
The constantine-go/…/sha256.go package offers both one-shot hashing and a stateful context. It uses the same umbrella header and library as the main Go package, which makes hashing available without changing the lower C contract. A non-cgo stub keeps the package type-checkable for linting, but it is not a cryptographic implementation.
References: constantine-go/…/sha256.go• L4-16 constantine-go/…/sha256.go• L30-65 constantine-go/…/sha256_stub.go• L1-12 constantine-go/…/sha256_stub.go• L40-45
Rust integration
Rust uses a layered API. constantine-sys owns the generated declarations and raw C calls, constantine-core adds shared services such as secure random bytes and thread-pool ownership, and protocol crates expose typed operations that return Result values. The higher layers still preserve the C status contract so callers can handle invalid input and verification failure without inspecting raw pointers.
Rust core services
constantine-core wraps system randomness, hardware thread-count queries, and thread-pool creation and shutdown. Its Threadpool owns the C resource and shuts it down when the Rust value is dropped. Protocol crates borrow this service when they call parallel C entry points.
References: constantine-rust/…/lib.rs• L11-35 constantine-rust/…/lib.rs• L37-69 constantine-rust/…/lib.rs• L9-18
Rust protocol crates
The BLS crate maps key and signature types to the generated C types and provides serialization, signing, verification, aggregation, and batch verification. The KZG crate owns trusted-setup contexts and provides serial, parallel, and PeerDAS operations. The EVM crate maps byte input to the EVM precompile calls and reexports the KZG context needed for point evaluation.
These crates use Rust slices and fixed-size arrays at their public boundary. They allocate result storage in Rust, pass pointers only for the duration of a call, and map C status values to Result values or boolean verification results.
References: constantine-rust/…/lib.rs• L14-30 constantine-rust/…/lib.rs• L65-95 constantine-rust/…/lib.rs• L15-33 constantine-rust/…/lib.rs• L155-232 constantine-rust/…/lib.rs• L13-18 constantine-rust/…/lib.rs• L20-79
Rust accelerator integration
The Halo2 ZK accelerator crate adapts Constantine’s parallel BN254 multi-scalar multiplication to the Halo2 middleware accelerator trait. It owns a Constantine thread pool and checks the result against the middleware’s independent multi-scalar multiplication in its integration test.
References: constantine-rust/…/lib.rs• L18-29 constantine-rust/…/lib.rs• L42-55 constantine-rust/…/t_zal_msm_accel.rs• L28-69
Integration tests
The integration tests check the complete boundary: a consumer reads the public contract, links the library, passes encoded data, and compares the returned value or status with an independent expected result. They test both successful work and rejected input, so a passing unit test inside Nim is not enough to prove that a language binding still matches the ABI.
C integration tests
The C examples exercise field arithmetic against GMP, serialization and deserialization, Keccak hashing, BLS signatures, and EVM precompiles. The thread-pool C test checks creation and shutdown on supported platforms and confirms that library initialization does not require a manual NimMain call. nimble test_lib builds each case against both dynamic and static library forms.
References: examples-c/README.md• L1-24 examples-c/t_libctt_bls12_381.c• L10-18 examples-c/t_libctt_bls12_381.c• L64-74 examples-c/t_libctt_keccak.c• L49-67 tests/…/t_threadpool.c• L10-29 constantine.nimble• L343-376
Go integration tests
The Go tests load the shared Ethereum BLS, KZG, PeerDAS, and EVM vector sets and compare binding results with their expected serialized output. They cover codec rejection, signature and proof verification, batch length checks, and serial versus parallel KZG behavior. Parallel tests keep C calls on one locked OS thread because the thread-pool state uses thread-local storage.
Run the Go tests from constantine-go with go test -modfile=../go_test.mod.
References: constantine-go/README.md• L1-9 constantine-go/eth_bls_signatures_test.go• L24-40 constantine-go/eth_bls_signatures_test.go• L551-650 constantine-go/eth_kzg4844_test.go• L27-49 constantine-go/eth_kzg4844_test.go• L449-465 constantine-go/eth_evm_precompiles_test.go• L131-165
Rust integration tests
The Rust tests read the same protocol vector families through the Rust-facing crates. BLS tests cover encoding, signing, verification, aggregation, and batch verification. KZG tests cover trusted-setup loading, proof and cell flows, parallel variants, and direct length or ordering errors. EVM tests map each precompile vector to its wrapper and check either the expected bytes or the expected error.
References: constantine-rust/…/t_ethereum_bls_sig_vectors.rs• L40-80 constantine-rust/…/t_ethereum_bls_sig_vectors.rs• L618-725 constantine-rust/…/t_eth_kzg4844.rs• L20-49 constantine-rust/…/t_eth_kzg7594.rs• L63-167 constantine-rust/…/t_eth_kzg7594.rs• L422-488 constantine-rust/…/t_ethereum_evm_precompiles.rs• L99-135
Change checklist
When these files or sections are changed, remember to consider these:
- When an exported Nim operation changes, regenerate the affected C headers and Rust declarations, then check the Go and Rust wrapper signatures against the new ABI.
- When a C type, enum, struct layout, or word-width rule changes, check
bindings/c_typedefs.nim, the generated headers, both Rust pointer-width declaration files, and all language conversions. - When a context or parallel operation changes, preserve its create, borrow, thread-pool, and release contract in both language wrappers and their integration tests.
- When a status enum changes, update the Rust display mapping and the Go status-to-error paths so verification failure remains distinct from an operational error.
- When library output names or link rules change, update cgo flags, the Rust build script, and the C test commands together.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Generated headers do not match the compiled Nim exports | C compilation or link errors, or a caller passes the wrong data shape | Run the header generation task after binding changes and build the C examples against the same library output |
| Rust declarations are generated for the wrong pointer width | Struct sizes or function calls do not match the linked library | Generate both bindings32.rs and bindings64.rs; let constantine-sys select by target width |
| A Go or Rust caller uses a parallel operation without a thread pool | Go returns a configuration error, while Rust’s parallel wrapper requires a configured pool | Create and retain the thread pool before calling parallel methods |
| Parallel C calls move between OS threads | Thread-local thread-pool state can be invalid or produce incorrect behavior | Keep the calls on the required OS thread, as the Go parallel tests do |
| A KZG context is missing its trusted setup or is released too early | Proof, cell, or point-evaluation calls cannot produce valid results | Load the setup before use and keep the context alive for all dependent calls |
| Batch inputs have different lengths | The ABI rejects the request with an input-length status or wrapper error | Validate related slice lengths before crossing the C boundary |
Key files
| File | Why to read it |
|---|---|
| include/constantine.h | The public C entry point and the map of exposed capability groups |
| include/…/datatypes.h | The portable C types and word-size rules shared by all bindings |
bindings/lib_constantine.nim | The set of Nim modules included in the exported library |
bindings/lib_headers.nim | The flow that turns binding declarations into public headers |
bindings/c_typedefs.nim | The shared rules for C data types, parameters, and linkage guards |
bindings/c_curve_decls.nim | The common C export families for fields, curves, and hash-to-curve |
constantine.nimble | Library, header, Rust-library, and C integration-test build tasks |
| constantine-go/constantine.go | The main Go cgo bridge and its protocol-level resource and error model |
| constantine-go/…/sha256.go | A small stateful and one-shot example of the Go-to-C boundary |
| scripts/gen_rust_bindings.sh | The policy and commands used to generate Rust declarations |
| constantine-rust/…/build.rs | The Rust build path from Nim sources to a linked static library |
| constantine-rust/…/lib.rs | Rust ownership of shared C services such as thread pools and randomness |
| constantine-rust/…/lib.rs | A typed Rust wrapper around the BLS C surface |
| constantine-rust/…/lib.rs | Trusted setup, serial and parallel KZG, and PeerDAS integration |
| tests/…/t_threadpool.c | The smallest C ABI lifecycle and initialization check |
| constantine-go/eth_kzg4844_test.go | Go vector tests for serial and parallel KZG boundary behavior |
| constantine-rust/…/t_eth_kzg7594.rs | Rust vector and error-path tests for PeerDAS calls |