Cairo Hints is an extension to Cairo language that makes programs easier to implement and cheaper to execute. They allow supplementing programs with data that is difficult to obtain in ZK circuits.
For example, calculating a square root in circuit is difficult, but verifying the result requires only a single multiplication. Therefore, it's a good candidate to be optimized by Cairo Hints. We can offload sqrt
calculation to an external server, and only assert that result * result == input
.
Cairo Hints uses protocol buffers to define messages shared between Cairo and an external RPC server. Our scarb hints-run
code runner is used to execute Cairo code with hints.
// Oracle definition using Protocol Buffers 3
message Request {
uint64 n = 1;
}
message Response {
uint64 n = 1;
}
service SqrtOracle {
rpc Sqrt(Request) returns (Response);
}
// Using the oracle in Cairo code
let result = SqrtOracle::sqrt(Request { n: input });
// Constraining the result of SqrtOracle in Cairo
result.n * result.n == input
protoc
from herescarb
from herelambdaworks/provers/cairo
from here for proving only. As of February 2024, the tested revision isfed12d6
.
Clone this repository and run:
cargo install --path cairo-hints --locked
- Create a new project using
scarb hints-new --lang rust <PROJECT_NAME>
- Define messages in a
.proto
file - Run
scarb hints-generate
- In another tab,
cd rust
and start the RPC server with the commandcargo run
- Run
scarb hints-run --oracle-server http://127.0.0.1:3000 --layout all_cairo
- Integration tests can be run with
scarb hints-test --oracle-server http://127.0.0.1:3000 --layout all_cairo
To run all tests in this crate execute the following command cargo test --workspace --no-fail-fast
.