A Cartesi application that performs matrix multiplication using NumPy. Send any two matrices as input and receive the result as an on-chain notice.
The application receives a JSON payload containing two matrices, multiplies them using numpy.matmul, and emits the result as a rollup notice.
- Advance (state-changing): result is emitted as a notice
- Inspect (read-only): result is emitted as a report
- Cartesi CLI
- Docker with QEMU support (for riscv64 emulation)
Because the Cartesi machine runs on linux/riscv64, NumPy must be compiled for that architecture. A helper script handles this by building a pre-compiled wheel using Docker + QEMU emulation.
Step 1 — Build the NumPy wheel (once):
./build-wheels.shThis compiles NumPy for linux/riscv64 and saves the wheel to wheels/. Only needs to be re-run if you change the NumPy version in requirements.txt.
Step 2 — Build the Cartesi machine:
cartesi buildStart the local Cartesi node:
cartesi runUse the Cartesi CLI to send an advance-state input. The payload must be a JSON object with matrix_a and matrix_b keys, each containing a 2D array:
cartesi send genericWhen prompted for the hex payload, encode the following JSON:
{"matrix_a": [[1, 2], [3, 4]], "matrix_b": [[5, 6], [7, 8]]}The application emits a notice with:
{"result": [[19.0, 22.0], [43.0, 50.0]]}- The number of columns in
matrix_amust equal the number of rows inmatrix_b - Matrices can be any valid shape, not just square
- On invalid input the application rejects the input and emits an error report
dapp.py # App logic — decodes input, runs matmul, emits notice
requirements.txt # Python dependencies (requests, numpy)
Dockerfile # Cartesi machine image definition
build-wheels.sh # Builds the riscv64 NumPy wheel using Docker + QEMU
wheels/ # Pre-built riscv64 wheels (generated by build-wheels.sh)
PyPI does not distribute pre-built NumPy wheels for linux/riscv64, so pip would normally compile it from source — requiring a full build toolchain inside the Docker image. The build-wheels.sh script solves this by building the wheel once on your host using QEMU emulation and committing it to the wheels/ folder. The Dockerfile then installs from that local wheel with no compilation required at build time.
NumPy is built without OpenBLAS (NPY_BLAS_ORDER="") to produce a self-contained wheel with no external shared library dependencies, which is required for the slim Cartesi base image.