Skip to content

Commit 7e8b49a

Browse files
authored
Merge pull request #157 from HerodotusDev/update_scarb
Export types with no features enables
2 parents db78e0e + 6e27944 commit 7e8b49a

File tree

197 files changed

+57075
-564
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

197 files changed

+57075
-564
lines changed

.gitattributes

-1
This file was deleted.
+30-32
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
name: Continuous Integration - proof verification tests
22

33
on:
4-
push:
5-
branches:
6-
- main
7-
pull_request:
8-
branches:
9-
- main
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
1010

1111
jobs:
12-
verify-example-proofs:
13-
runs-on: ubuntu-latest
14-
strategy:
15-
matrix:
16-
cairo_version: ["cairo0", "cairo1"]
17-
layout: ["recursive", "recursive_with_poseidon", "small", "dex", "starknet", "starknet_with_keccak"]
18-
hash_function: ["keccak"]
19-
hasher_bit_length: ["160_lsb"]
20-
stone_version: ["stone5"]
21-
steps:
22-
- name: Checkout repository
23-
uses: actions/checkout@v3
24-
with:
25-
lfs: true
26-
27-
- name: Setup Scarb
28-
uses: software-mansion/setup-scarb@v1
29-
30-
- name: Setup Rust toolchain
31-
uses: actions-rust-lang/setup-rust-toolchain@v1
32-
33-
- name: Build project
34-
run: scarb build --no-default-features --features monolith,${{ matrix.layout }},${{ matrix.hash_function }}
35-
36-
- name: Run verification
37-
run: cargo run --release --bin runner -- --program target/dev/cairo_verifier.sierra.json --cairo-version ${{ matrix.cairo_version }} --stone-version ${{ matrix.stone_version }} --hasher-bit-length ${{ matrix.hasher_bit_length }} < examples/proofs/${{ matrix.layout }}/${{ matrix.cairo_version }}_${{ matrix.stone_version }}_${{ matrix.hash_function }}_${{ matrix.hasher_bit_length }}_example_proof.json
12+
verify-example-proofs:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
memory_verification: ['cairo0', 'cairo1']
17+
layout: ['recursive', 'recursive_with_poseidon', 'small', 'dex', 'starknet', 'starknet_with_keccak']
18+
hash_function: ['keccak']
19+
hasher_bit_length: ['160_lsb']
20+
stone_version: ['stone5']
21+
steps:
22+
- name: Checkout repository
23+
uses: actions/checkout@v3
24+
25+
- name: Setup Scarb
26+
uses: software-mansion/setup-scarb@v1
27+
28+
- name: Setup Rust toolchain
29+
uses: actions-rust-lang/setup-rust-toolchain@v1
30+
31+
- name: Build project
32+
run: scarb build --no-default-features --features monolith,${{ matrix.layout }},${{ matrix.hash_function }}
33+
34+
- name: Run verification
35+
run: cargo run --release --bin runner -- --program target/dev/integrity.sierra.json --memory-verification ${{ matrix.memory_verification == 'cairo0' && 'strict' || 'cairo1' }} --stone-version ${{ matrix.stone_version }} --hasher-bit-length ${{ matrix.hasher_bit_length }} < examples/proofs/${{ matrix.layout }}/${{ matrix.memory_verification }}_${{ matrix.stone_version }}_${{ matrix.hash_function }}_${{ matrix.hasher_bit_length }}_example_proof.json

.tool-versions

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
scarb 2.8.2
2-
starknet-foundry 0.30.0
1+
scarb 2.8.4
2+
starknet-foundry 0.32.0

README.md

+134-36
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
# Cairo Verifier
1+
# Integrity
22

33
![Integrity](.github/readme.png)
44

5-
[![Continuous Integration - tests](https://github.com/HerodotusDev/cairo-verifier/actions/workflows/tests.yml/badge.svg)](https://github.com/HerodotusDev/cairo-verifier/actions/workflows/tests.yml)
5+
[![Continuous Integration - tests](https://github.com/HerodotusDev/integrity/actions/workflows/tests.yml/badge.svg)](https://github.com/HerodotusDev/cairo-verifier/actions/workflows/tests.yml)
66

7-
[![Continuous Integration - proof verification tests](https://github.com/HerodotusDev/cairo-verifier/actions/workflows/proof_verification_tests.yml/badge.svg)](https://github.com/HerodotusDev/cairo-verifier/actions/workflows/proof_verification_tests.yml)
7+
[![Continuous Integration - proof verification tests](https://github.com/HerodotusDev/integrity/actions/workflows/proof_verification_tests.yml/badge.svg)](https://github.com/HerodotusDev/cairo-verifier/actions/workflows/proof_verification_tests.yml)
8+
9+
Integrity is a STARK proof verifier written in cairo language and deployed on Starknet.
810

911
## Table of contents
1012

1113
- [Prerequisites](#prerequisites)
1214
- [Using Verifier contracts on Starknet](#using-verifier-contracts-on-starknet)
15+
- [FactRegistry and Proxy contract](#factregistry-and-proxy-contract)
16+
- [Calls from Starknet contracts](#calls-from-starknet-contracts)
1317
- [Running locally](#running-locally)
1418
- [Creating a Proof](#creating-a-proof)
1519
- [Deployment](#deployment)
@@ -21,10 +25,6 @@ To use the verifier with contracts deployed on Starknet, you need to have [Rust]
2125

2226
For running locally and development, you will need [scarb](https://docs.swmansion.com/scarb/) (we recommend using [asdf](https://asdf-vm.com/) version manager).
2327

24-
### Getting example proofs
25-
26-
Because of large size of proofs, we don't store example proofs directly in this repository, but rather in [Large File Storage](https://git-lfs.com/), so you need to have it installed and then run `git lfs pull` to get all example proofs.
27-
2828
## Using Verifier contracts on Starknet
2929

3030
Integrity verifier is deployed on Starknet and can be used for verifying proofs onchain. The intended way of using the verifier is through FactRegistry contract, which besides running the verification process, also stores data for all verified proofs. (For more information see [FactRegistry and Proxy contract](#factregistry-and-proxy-contract))
@@ -44,7 +44,7 @@ After that, you can use `verify-on-starknet.sh` script to send the transaction t
4444
For example, run:
4545

4646
```bash
47-
./verify-on-starknet.sh 0x16409cfef9b6c3e6002133b61c59d09484594b37b8e4daef7dcba5495a0ef1a examples/calldata recursive keccak_248_lsb stone5 cairo0
47+
./verify-on-starknet.sh 0x4ce7851f00b6c3289674841fd7a1b96b6fd41ed1edc248faccd672c26371b8c examples/calldata recursive keccak_248_lsb stone5 strict
4848
```
4949

5050
This bash script internally calls `verify_proof_full_and_register_fact` function on FactRegistry contract.
@@ -53,6 +53,101 @@ This bash script internally calls `verify_proof_full_and_register_fact` function
5353

5454
To generate split calldata, please refer to [Calldata Generator README](https://github.com/HerodotusDev/integrity-calldata-generator/blob/main/README.md). This repository also provides script for automatic transaction sending (proof verification is split into multiple transactions, for more information see [Split Verifier Architecture](#split-verifier-architecture)).
5555

56+
## FactRegistry and Proxy contract
57+
58+
Since verifier can be configured in many ways and some parts of the logic changes with new stone versions, a contract which routes calls to the correct verifier is needed. This task is handled by FactRegistry contract that also stores data for all verified proofs.
59+
60+
After proof is verified, `FactRegistered` event is emitted which contains `fact_hash`, `verification_hash`, `security_bits` and `settings`. `fact_hash` is a value that represents proven program and its output (formally `fact_hash = poseidon_hash(program_hash, output_hash)`). Remember that registration of some `fact_hash` doesn't necessary mean that it has been verified by someone with secure enough proof. You always need to check `security_bits` and `settings` which is part of `verification_hash` (formally `verification_hash = poseidon_hash(fact_hash, security_bits, settings)`).
61+
62+
For more detailed and visual representation of those hash calculations, check out [Integrity Hashes Calculator](https://integrity-hashes-calculator.vercel.app/) tool. It generates all mentioned hashes for arbitrary user input and even proof JSON file.
63+
64+
`FactRegistry` provides two methods for checking verified proofs:
65+
66+
- `get_verification(verification_hash)` - returns fact hash, security bits and settings for given `verification_hash`.
67+
- `get_all_verifications_for_fact_hash(fact_hash)` - returns list of all verification hashes, security bits and settings for given `fact_hash`. This method is useful for checking if given program has been verified by someone with secure enough proof.
68+
69+
FactRegistry contract is trustless which means that the owner of the contract can't override or change any existing behavior, they can only add new verifiers. Proxy contract on the other hand is upgradable, so every function can be changed or removed. It has the advantage of having all future updates of the verifier logic without having to replace the address of FactRegistry contract. Proxy contract provides the same interface as FactRegistry with additional `get_fact_registry` method which returns address of FactRegistry contract.
70+
71+
## Calls from Starknet contracts
72+
73+
Since integrity is deployed on Starknet, other contracts can call FactRegistry to check whether certain proof has been verified. Integrity can be used as a dependency of your cairo1 project by including it in project's `Scarb.toml`:
74+
75+
```toml
76+
[dependencies]
77+
integrity = { git = "https://github.com/HerodotusDev/integrity" }
78+
```
79+
80+
The package provides many utility functions for interacting with the verifier. For contract calls, you can use `Integrity` struct which provides following methods:
81+
82+
- `new() -> Integrity` - creates new interface for interacting with official FactRegistry (contract address is set automatically).
83+
- `new_proxy() -> Integrity` - creates new interface using official Proxy contract (contract address is set automatically).
84+
- `from_address(address: ContractAddress) -> Integrity` - create new interface using custom FactRegistry deployment.
85+
- `is_fact_hash_valid_with_security(self: Integrity, fact_hash: felt252, security_bits: u32) -> bool` - checks if given `fact_hash` has been verified with at least `security_bits` number of security bits.
86+
- `is_verification_hash_valid(self: Integrity, verification_hash: felt252) -> bool` - checks if given `verification_hash` has been verified.
87+
- `with_config(self: Integrity, verifier_config: VerifierConfiguration, security_bits: u32) -> IntegrityWithConfig` - returns new interface with custom verifier configuration.
88+
- `with_hashed_config(self: Integrity, verifier_config_hash: felt252, security_bits: u32) -> IntegrityWithConfig` - returns new interface with custom verifier configuration given its hash.
89+
90+
On `IntegrityWithConfig` interface you can call:
91+
92+
- `is_fact_hash_valid(self: IntegrityWithConfig, fact_hash: felt252) -> bool` - checks if given `fact_hash` has been verified with selected config.
93+
94+
There are also few utility function for calculating hashes:
95+
96+
- `get_verifier_config_hash(verifier_config: VerifierConfiguration) -> felt252` - calculates hash for given verifier configuration, which is used necessary for calculating verification hash.
97+
- `get_verification_hash(fact_hash: felt252, verifier_config_hash: felt252, security_bits: u32) -> felt252` - calculates verification hash for given `fact_hash`, `verifier_config_hash` and `security_bits`.
98+
- `calculate_fact_hash(program_hash: felt256, output: Span<felt252>) -> felt252` - calculates fact hash for given `program_hash` and `output` array.
99+
- `calculate_bootloaded_fact_hash(bootloader_program_hash: felt252, child_program_hash: felt252, child_output: Span<felt252>) -> felt252` - calculates fact hash for program that was bootloaded with standard bootloader.
100+
101+
Available constants are:
102+
103+
- `INTEGRITY_ADDRESS` - address of official FactRegistry contract deployed on Starknet Sepolia
104+
- `PROXY_ADDRESS` - address of official Proxy contract deployed on Starknet Sepolia
105+
- `SHARP_BOOTLOADER_PROGRAM_HASH` - program hash of the bootloader used by SHARP prover
106+
- `STONE_BOOTLOADER_PROGRAM_HASH` - program hash of [TODO LINK]
107+
108+
Example:
109+
110+
```
111+
use integrity::{Integrity, IntegrityWithConfig, calculate_bootloaded_fact_hash, SHARP_BOOTLOADER_PROGRAM_HASH, VerifierConfiguration};
112+
113+
fn is_fibonacci_verified(fib_index: felt252, fib_value: felt252) -> bool {
114+
let SECURITY_BITS = 70;
115+
let fibonacci_program_hash = 0x59874649ccc5a0a15ee77538f1eb760acb88cab027a2d48f4246bf17b7b7694;
116+
let fact_hash = calculate_bootloaded_fact_hash(
117+
SHARP_BOOTLOADER_PROGRAM_HASH, fibonacci_program_hash, [fib_index, fib_value].span()
118+
);
119+
120+
let integrity = Integrity::new();
121+
integrity.is_fact_hash_valid_with_security(fact_hash, SECURITY_BITS)
122+
}
123+
124+
fn is_multi_fibonacci_verified(fib: Span<(felt252, felt252)>) -> bool {
125+
let config = VerifierConfiguration {
126+
layout: 'recursive_with_poseidon',
127+
hasher: 'keccak_160_lsb',
128+
stone_version: 'stone6',
129+
memory_verification: 'relaxed',
130+
};
131+
let SECURITY_BITS = 96;
132+
let fibonacci_program_hash = 0x59874649ccc5a0a15ee77538f1eb760acb88cab027a2d48f4246bf17b7b7694;
133+
134+
let integrity = Integrity::new().with_config(config, SECURITY_BITS);
135+
136+
let mut ret = true;
137+
for f in fib {
138+
let (fib_index, fib_value) = *f;
139+
let fact_hash = calculate_bootloaded_fact_hash(
140+
SHARP_BOOTLOADER_PROGRAM_HASH, fibonacci_program_hash, [fib_index, fib_value].span()
141+
);
142+
143+
if !integrity.is_fact_hash_valid(fact_hash) {
144+
ret = false;
145+
}
146+
};
147+
ret
148+
}
149+
```
150+
56151
## Running locally
57152

58153
To run the verifier locally, first you need to build cairo project using:
@@ -69,8 +164,8 @@ You can use cairo runner to run the verifier on example proof:
69164

70165
```bash
71166
cargo run --release --bin runner -- \
72-
--program target/dev/cairo_verifier.sierra.json \
73-
--cairo-version cairo0 \
167+
--program target/dev/integrity.sierra.json \
168+
--memory-verification strict \
74169
--stone-version stone5 \
75170
--hasher-bit-length 160_lsb \
76171
< examples/proofs/recursive/cairo0_stone5_keccak_160_lsb_example_proof.json
@@ -84,21 +179,41 @@ By default, the verifier is configured for monolith version, recursive layout an
84179
scarb build --no-default-features --features small,blake2s,monolith
85180
```
86181

87-
`layout`: [`dex`, `recursive`, `recursive_with_poseidon`, `small`, `starknet`, `starknet_with_keccak`]<br />
88-
hash functions: [`keccak`, `blake2s`]<br />
89-
verifier types: [`monolith`, `split`]
182+
- `layout`
183+
- `dex`
184+
- `recursive`
185+
- `recursive_with_poseidon`
186+
- `small`
187+
- `starknet`
188+
- `starknet_with_keccak`
189+
- hash functions:
190+
- `keccak`
191+
- `blake2s`
192+
- verifier types
193+
- `monolith`
194+
- `split`
90195

91196
There are also additional settings that can be configured at runtime:
92197

93-
`cairo_version`: [`cairo0`, `cairo1`]<br />
94-
`stone_version`: [`stone5`, `stone6`]<br />
95-
hasher bit length: [`160_lsb`, `248_lsb`]
198+
- `memory_verification`
199+
- `strict`
200+
- `relaxed`
201+
- `cairo1`
202+
- `stone_version`
203+
- `stone5`
204+
- `stone6`
205+
- hasher bit length
206+
- `160_lsb`
207+
- `248_lsb`
96208

97209
Hash function and hasher bit length are combined into one setting:
98210

99-
`hasher`: [`keccak_160_lsb`, `blake2s_160`, `blake2s_248_lsb`]
211+
- `hasher`
212+
- `keccak_160_lsb`
213+
- `blake2s_160`
214+
- `blake2s_248_lsb`
100215

101-
For `stone5` available `hasher`s are `keccak_160_lsb` and `blake2s_160`, for `stone6` - `keccak_160_lsb` and `blake2s_248_lsb`.
216+
For `stone5` available hashers are `keccak_160_lsb` and `blake2s_160`, for `stone6` - `keccak_160_lsb` and `blake2s_248_lsb`.
102217

103218
### Running tests
104219

@@ -113,7 +228,7 @@ scarb test
113228
In order to launch benchmarking, just run this (it requires recursive layout configuration):
114229

115230
```bash
116-
cargo run --release --bin benches -- target/dev/cairo_verifier.sierra.json
231+
cargo run --release --bin benches -- target/dev/integrity.sierra.json
117232
```
118233

119234
## Creating a Proof
@@ -154,27 +269,10 @@ bash deployment/verifiers/<layout>/<hasher>/register.sh
154269

155270
## Split Verifier Architecture
156271

157-
### Background information
158-
159272
Because of great complexity of the verifier compared to standard starknet contracts, we encounter some limitations enforced by starknet. The most important ones are:
160273

161274
- Contract classhash size limit
162275
- Transaction calldata limit
163276
- Transaction steps limit
164277

165278
To overcome these limitations, we split the verifier into multiple contracts and transactions. The biggest part of classhash size is autogenerated (e.g. [recursive autogenerated](/src/air/layouts/recursive/autogenerated.cairo)), so we extracted that part into separate contract (or many contracts in case of `starknet_with_keccak` layout), which is called automatically by the main verifier contract. On the other hand the biggest part of calldata is fri witness, so user can send subsequent chunks of fri witness in separate step transactions.
166-
167-
### FactRegistry and Proxy contract
168-
169-
Since verifier can be configured in many ways and some parts of the logic changes with new stone versions, a contract which routes calls to the correct verifier is needed. This task is handled by FactRegistry contract that also stores data for all verified proofs.
170-
171-
After proof is verified, `FactRegistered` event is emitted which contains `fact_hash`, `verification_hash`, `security_bits` and `settings`. `fact_hash` is a value that represents proven program and its output (formally `fact_hash = poseidon_hash(program_hash, output_hash)`). Remember that registration of some `fact_hash` doesn't necessary mean that it has been verified by someone with secure enough proof. You always need to check `security_bits` and `settings` which is part of `verification_hash` (formally `verification_hash = poseidon_hash(fact_hash, security_bits, settings)`).
172-
173-
`FactRegistry` provides two methods for checking verified proofs:
174-
175-
- `get_verification(verification_hash)` - returns fact hash, security bits and settings for given `verification_hash`.
176-
- `get_all_verifications_for_fact_hash(fact_hash)` - returns list of all verification hashes, security bits and settings for given `fact_hash`. This method is useful for checking if given program has been verified by someone with secure enough proof.
177-
178-
FactRegistry contract is trustless which means that the owner of the contract can't override or change any existing behavior, they can only add new verifiers. Proxy contract on the other hand is upgradable, so every function can be changed or removed. It has the advantage of having all future updates of the verifier logic without having to replace the address of FactRegistry contract.
179-
180-
TODO: how to read FactRegistered event

Scarb.toml

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
[package]
2-
name = "cairo_verifier"
2+
name = "integrity"
33
version = "0.1.0"
44

55
[dependencies]
6-
starknet = "2.8.2"
6+
starknet = "2.8.4"
77

88
[dev-dependencies]
9-
cairo_test = "2.8.2"
10-
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.27.0" }
9+
cairo_test = "2.8.4"
1110

1211
[[target.starknet-contract]]
1312
casm = true
@@ -19,12 +18,14 @@ casm = false
1918

2019
[features]
2120

22-
recursive = []
23-
recursive_with_poseidon = []
24-
dex = []
25-
small = []
26-
starknet = []
27-
starknet_with_keccak = []
21+
_verifier_logic = []
22+
23+
recursive = ["_verifier_logic"]
24+
recursive_with_poseidon = ["_verifier_logic"]
25+
dex = ["_verifier_logic"]
26+
small = ["_verifier_logic"]
27+
starknet = ["_verifier_logic"]
28+
starknet_with_keccak = ["_verifier_logic"]
2829

2930
keccak = []
3031
blake2s = []

benches/src/bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn bench(sierra_program: Program, function: &str) -> Result<RunResultStarkne
1515
let func = runner.find_function(function).unwrap();
1616
runner.run_function_with_starknet_context(
1717
func,
18-
&[],
18+
vec![],
1919
Some(u32::MAX as usize),
2020
Default::default(),
2121
)

0 commit comments

Comments
 (0)