Skip to content
MathCoprocessor

MathCoprocessor

Cartesi Coprocessor that allows smart contracts to evaluate math functions using Python

Created on 16th February 2025

•

MathCoprocessor

MathCoprocessor

Cartesi Coprocessor that allows smart contracts to evaluate math functions using Python

The problem MathCoprocessor solves

Smart contracts written in Solidity often need to perform mathematical operations that are either too resource intensive, or are simply unavailble in Solidity. A trivial example of the latter is to perform an exponentiataion with a floating-point base (i.e., "x^y" where "x" is not an integer).
This project proposes a simple solution using Cartesi Coprocessors, that allows smart contracts to send an arbitrary Python mathematical expression along with a callback to be called with the ABI-encoded result. This implementation includes support for NumPy and arbitrary ABI encodings.

Challenges I ran into

The devnet crashes if an input is rejected, which is unexpected. Since the devnet takes a bit of time to spin up, this is a bit clumsy. I worked around it by being a bit careful with my inputs :). I admit I didn't try Nonodo-x, maybe it would have helped.
I used the cartesi-coprocessor CLI tool in general. It works great for a first go, but lacks some features to make it nice to use, mainly:

  • Better way to reuse things like the published machine's hash when deploying the coprocessor contract. Right now you need to observe the logs and copy that hash from there
  • Proper caching when publishing: it seems to build the Cartesi Machine's Docker from scratch every time I want to publish, I feel that it could cache some things (e.g., if I only change my app's code, it should execute only those last steps)

Discussion

Builders also viewed

See more projects on Devfolio