Skip to content

Commit 39440ad

Browse files
taturosatientropidelicNicolasRampoldi
authored
feat (risc0) add public input as separate parameter on verification data (#604)
Co-authored-by: Mariano A. Nicolini <mariano.nicolini.91@gmail.com> Co-authored-by: NicolasRampoldi <58613770+NicolasRampoldi@users.noreply.github.com>
1 parent 1ac7d2a commit 39440ad

17 files changed

Lines changed: 164 additions & 47 deletions

File tree

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ batcher_send_risc0_task:
230230
--proving_system Risc0 \
231231
--proof ../../scripts/test_files/risc_zero/fibonacci_proof_generator/risc_zero_fibonacci.proof \
232232
--vm_program ../../scripts/test_files/risc_zero/fibonacci_proof_generator/fibonacci_id.bin \
233+
--public_input ../../scripts/test_files/risc_zero/fibonacci_proof_generator/risc_zero_fibonacci.pub \
233234
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
234235

235236
batcher_send_risc0_burst:
@@ -238,6 +239,7 @@ batcher_send_risc0_burst:
238239
--proving_system Risc0 \
239240
--proof ../../scripts/test_files/risc_zero/fibonacci_proof_generator/risc_zero_fibonacci.proof \
240241
--vm_program ../../scripts/test_files/risc_zero/fibonacci_proof_generator/fibonacci_id.bin \
242+
--public_input ../../scripts/test_files/risc_zero/fibonacci_proof_generator/risc_zero_fibonacci.pub \
241243
--repetitions 15 \
242244
--proof_generator_addr 0x66f9664f97F2b50F62D13eA064982f936dE76657
243245

@@ -470,7 +472,7 @@ test_risc_zero_go_bindings_linux: build_risc_zero_linux
470472
generate_risc_zero_fibonacci_proof:
471473
@cd scripts/test_files/risc_zero/fibonacci_proof_generator && \
472474
RUST_LOG=info cargo run --release && \
473-
echo "Fibonacci proof and image ID generated in scripts/test_files/risc_zero folder"
475+
echo "Fibonacci proof, pub input and image ID generated in scripts/test_files/risc_zero folder"
474476

475477
__MERKLE_TREE_FFI__: ##
476478
build_merkle_tree_macos:

batcher/aligned-batcher/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,10 @@ impl Batcher {
386386
let mut last_uploaded_batch_block = self.last_uploaded_batch_block.lock().await;
387387
// update last uploaded batch block
388388
*last_uploaded_batch_block = block_number;
389-
info!("Batch Finalizer: Last uploaded batch block updated to: {}. Lock unlocked", block_number);
389+
info!(
390+
"Batch Finalizer: Last uploaded batch block updated to: {}. Lock unlocked",
391+
block_number
392+
);
390393
}
391394
// Moving this outside the previous scope is a hotfix until we merge https://github.com/yetanotherco/aligned_layer/pull/365
392395
self.submit_batch(&batch_bytes, &batch_merkle_tree.root, submitter_addresses)

batcher/aligned-batcher/src/risc_zero/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
use risc0_zkvm::Receipt;
22

3-
pub fn verify_risc_zero_proof(receipt_bytes: &[u8], image_id: &[u8; 32]) -> bool {
3+
pub fn verify_risc_zero_proof(
4+
receipt_bytes: &[u8],
5+
image_id: &[u8; 32],
6+
public_input: &[u8],
7+
) -> bool {
48
if let Ok(receipt) = bincode::deserialize::<Receipt>(receipt_bytes) {
9+
if public_input != receipt.journal.bytes {
10+
return false;
11+
}
12+
513
return receipt.verify(*image_id).is_ok();
614
}
715
false

batcher/aligned-batcher/src/zk_utils/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use log::{debug, warn};
2+
3+
use aligned_sdk::types::{ProvingSystemId, VerificationData};
4+
15
use crate::gnark::verify_gnark;
26
use crate::halo2::ipa::verify_halo2_ipa;
37
use crate::halo2::kzg::verify_halo2_kzg;
48
use crate::risc_zero::verify_risc_zero_proof;
59
use crate::sp1::verify_sp1_proof;
6-
use aligned_sdk::types::{ProvingSystemId, VerificationData};
7-
use log::{debug, warn};
810

911
pub(crate) fn verify(verification_data: &VerificationData) -> bool {
1012
match verification_data.proving_system {
@@ -44,12 +46,20 @@ pub(crate) fn verify(verification_data: &VerificationData) -> bool {
4446
is_valid
4547
}
4648
ProvingSystemId::Risc0 => {
47-
if let Some(image_id_slice) = &verification_data.vm_program_code {
49+
if let (Some(image_id_slice), Some(pub_input)) = (
50+
&verification_data.vm_program_code,
51+
&verification_data.pub_input,
52+
) {
4853
let mut image_id = [0u8; 32];
4954
image_id.copy_from_slice(image_id_slice.as_slice());
50-
return verify_risc_zero_proof(verification_data.proof.as_slice(), &image_id);
55+
return verify_risc_zero_proof(
56+
verification_data.proof.as_slice(),
57+
&image_id,
58+
pub_input,
59+
);
5160
}
52-
warn!("Trying to verify Risc0 proof but image ID was not provided. Returning false");
61+
62+
warn!("Trying to verify Risc0 proof but image id or public input was not provided. Returning false");
5363
false
5464
}
5565
ProvingSystemId::GnarkPlonkBls12_381

batcher/aligned-sdk/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ impl From<VerificationData> for VerificationDataCommitment {
6262
// FIXME(marian): This should probably be reworked, for the moment when the proving
6363
// system is SP1, `proving_system_aux_data` stands for the compiled ELF, while in the case
6464
// of Groth16 and PLONK, stands for the verification key.
65+
6566
if let Some(vm_program_code) = &verification_data.vm_program_code {
66-
debug_assert_eq!(verification_data.proving_system, ProvingSystemId::SP1);
6767
hasher.update(vm_program_code);
6868
proving_system_aux_data_commitment = hasher.finalize_reset().into();
6969
} else if let Some(verification_key) = &verification_data.verification_key {

batcher/aligned/src/main.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@ use std::io::Write;
55
use std::path::PathBuf;
66
use std::str::FromStr;
77

8-
use aligned_sdk::errors::{AlignedError, SubmitError};
9-
use aligned_sdk::types::AlignedVerificationData;
10-
use aligned_sdk::types::Chain;
11-
use aligned_sdk::types::ProvingSystemId;
12-
use aligned_sdk::types::VerificationData;
138
use clap::Parser;
149
use clap::Subcommand;
1510
use clap::ValueEnum;
1611
use env_logger::Env;
1712
use ethers::prelude::*;
18-
use log::warn;
19-
use log::{error, info};
20-
21-
use aligned_sdk::sdk::{get_verification_key_commitment, submit_multiple, verify_proof_onchain};
22-
2313
use ethers::utils::format_ether;
2414
use ethers::utils::hex;
2515
use ethers::utils::parse_ether;
16+
use log::warn;
17+
use log::{error, info};
2618
use transaction::eip2718::TypedTransaction;
2719

20+
use aligned_sdk::errors::{AlignedError, SubmitError};
21+
use aligned_sdk::sdk::{get_verification_key_commitment, submit_multiple, verify_proof_onchain};
22+
use aligned_sdk::types::AlignedVerificationData;
23+
use aligned_sdk::types::Chain;
24+
use aligned_sdk::types::ProvingSystemId;
25+
use aligned_sdk::types::VerificationData;
26+
2827
use crate::AlignedCommands::DepositToBatcher;
2928
use crate::AlignedCommands::GetUserBalance;
3029
use crate::AlignedCommands::GetVerificationKeyCommitment;
@@ -483,11 +482,21 @@ fn verification_data_from_args(args: SubmitArgs) -> Result<VerificationData, Sub
483482
let mut vm_program_code: Option<Vec<u8>> = None;
484483

485484
match proving_system {
486-
ProvingSystemId::SP1 | ProvingSystemId::Risc0 => {
485+
ProvingSystemId::SP1 => {
486+
vm_program_code = Some(read_file_option(
487+
"--vm_program",
488+
args.vm_program_code_file_name,
489+
)?);
490+
}
491+
ProvingSystemId::Risc0 => {
487492
vm_program_code = Some(read_file_option(
488493
"--vm_program",
489494
args.vm_program_code_file_name,
490495
)?);
496+
pub_input = Some(read_file_option(
497+
"--public_input",
498+
args.pub_input_file_name,
499+
)?);
491500
}
492501
ProvingSystemId::Halo2KZG
493502
| ProvingSystemId::Halo2IPA

docs/guides/0_submitting_proofs.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ You need to have installed [Foundry](https://book.getfoundry.sh/getting-started/
3838
```
3939
Phrase:
4040
test test test test test test test test test test test test
41-
41+
4242
Accounts:
4343
- Account 0:
4444
Address: 0xabcd...1234
4545
Private key: 0x1234...abcd
4646
```
47-
47+
4848
- Import the wallet using the private key previously generated, or whichever you want to use, and write a password to use it.
4949

5050
```bash
5151
mkdir -p ~/.aligned_keystore/
5252
cast wallet import ~/.aligned_keystore/keystore0 --interactive
5353
```
54-
54+
5555
You have to paste your private key and set a password for the keystore file.
5656

5757
This will create the ECDSA keystore file in `~/.aligned_keystore/keystore0`
@@ -75,7 +75,7 @@ aligned deposit-to-batcher \
7575
--amount 0.1ether
7676
```
7777

78-
This commands allows the usage of the following flags:
78+
This commands allows the usage of the following flags:
7979
- `--batcher_addr` to specify the address of the Batcher Payment Service smart contract.
8080
- `--rpc` to specify the rpc url to be used.
8181
- `--chain` to specify the chain id to be used. Could be holesky or devnet.
@@ -92,7 +92,7 @@ aligned get-user-balance \
9292
--user_addr <user_addr>
9393
```
9494

95-
This commands allows the usage of the following flags:
95+
This commands allows the usage of the following flags:
9696
- `--batcher_addr` to specify the address of the Batcher Payment Service smart contract.
9797
- `--rpc` to specify the rpc url to be used.
9898
- `--user_addr` the address of the user that funded the Batcher.
@@ -114,7 +114,7 @@ aligned submit \
114114
--conn wss://batcher.alignedlayer.com \
115115
--proof_generator_addr [proof_generator_addr] \
116116
--batch_inclusion_data_directory_path [batch_inclusion_data_directory_path] \
117-
--keystore_path <path_to_ecdsa_keystore>
117+
--keystore_path <path_to_ecdsa_keystore>
118118
```
119119

120120
**Example**
@@ -141,6 +141,7 @@ aligned submit \
141141
--proving_system Risc0 \
142142
--proof <proof_file> \
143143
--vm_program <vm_program_file> \
144+
--pub_input <pub_input_file> \
144145
--conn wss://batcher.alignedlayer.com \
145146
--proof_generator_addr [proof_generator_addr] \
146147
--batch_inclusion_data_directory_path [batch_inclusion_data_directory_path] \
@@ -150,11 +151,12 @@ aligned submit \
150151
**Example**
151152

152153
```bash
153-
rm -rf ~/.aligned/aligned_verification_data/ &&
154+
rm -rf ~/.aligned/aligned_verification_data/ &&
154155
aligned submit \
155156
--proving_system Risc0 \
156157
--proof ./scripts/test_files/risc_zero/fibonacci_proof_generator/risc_zero_fibonacci.proof \
157158
--vm_program ./scripts/test_files/risc_zero/fibonacci_proof_generator/fibonacci_id.bin \
159+
--public_input ./scripts/test_files/risc_zero/fibonacci_proof_generator/risc_zero_fibonacci.pub \
158160
--conn wss://batcher.alignedlayer.com \
159161
--aligned_verification_data_path ~/.aligned/aligned_verification_data \
160162
--keystore_path ~/.aligned_keystore/keystore0

docs/guides/3_generating_proofs.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,71 @@ aligned submit \
9090
```
9191
Where proof path is the path to the proof file, public input path is the path to the public input file, and verification key path is the path to the verification key file.
9292

93-
For more instructions on how to submit proofs, check the [Submitting proofs guide](../guides/0_submitting_proofs.md).
93+
For more instructions on how to submit proofs, check the [Submitting proofs guide](../guides/0_submitting_proofs.md).
94+
95+
## Risc0
96+
97+
### Dependencies
98+
99+
This guide assumes that:
100+
- Risc0 toolchain installed (instructions [here](https://dev.risczero.com/api/zkvm/quickstart#1-install-the-risc-zero-toolchain))
101+
- Risc0 project to generate the proofs (instructions [here](https://dev.risczero.com/api/zkvm/quickstart#2-create-a-new-project))
102+
- Aligned installed (instructions [here](../introduction/1_getting_started.md#quickstart))
103+
104+
### How to generate a proof
105+
106+
First, open the risc0 host file and add the following code to export image id & public input needed by Aligned.
107+
108+
```rust
109+
fn main() {
110+
// your code here
111+
112+
// <METHOD_ID> is the method id of the function you want to prove
113+
// <method_id_file_path> is the path where the method id will be saved
114+
std::fs::write("<method_id_file_path>", convert(&<METHOD_ID>))
115+
.expect("Failed to write method_id file");
116+
117+
// <pub_input_file_path> is the path where the public input will be saved
118+
std::fs::write("<pub_input_file_path>", receipt.journal.bytes)
119+
.expect("Failed to write pub_input file");
120+
}
121+
122+
123+
// Convert u32 array to u8 array for storage
124+
pub fn convert(data: &[u32; 8]) -> [u8; 32] {
125+
let mut res = [0; 32];
126+
for i in 0..8 {
127+
res[4 * i..4 * (i + 1)].copy_from_slice(&data[i].to_le_bytes());
128+
}
129+
res
130+
}
131+
```
132+
133+
Note that METHOD_ID will be imported from guest but it will be under a different name.
134+
135+
Then run the following command to generate the proof:
136+
137+
```bash
138+
cargo run --release
139+
```
140+
141+
### How to get the proof verified by Aligned
142+
143+
After generating the proof, you will have to find three different files:
144+
- Proof file
145+
- Image id file
146+
- Public input file
147+
148+
Then, you can send the proof to the Aligned network by running the following command
149+
150+
```bash
151+
aligned submit \
152+
--proving_system Risc0 \
153+
--proof <proof_file_path> \
154+
--vm_program <method_id_file_path> \
155+
--public_input <pub_input_file_path> \
156+
--conn wss://batcher.alignedlayer.com \
157+
--proof_generator_addr <proof_generator_addr>
158+
```
159+
160+
For more instructions on how to submit proofs, check the [Submitting proofs guide](../guides/0_submitting_proofs.md).

operator/pkg/operator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,10 @@ func (o *Operator) verify(verificationData VerificationData, results chan bool)
342342
case common.Risc0:
343343
proofLen := (uint32)(len(verificationData.Proof))
344344
imageIdLen := (uint32)(len(verificationData.VmProgramCode))
345+
pubInputLen := (uint32)(len(verificationData.PubInput))
345346

346-
verificationResult := risc_zero.VerifyRiscZeroReceipt(verificationData.Proof, proofLen, verificationData.VmProgramCode, imageIdLen)
347+
verificationResult := risc_zero.VerifyRiscZeroReceipt(verificationData.Proof, proofLen,
348+
verificationData.VmProgramCode, imageIdLen, verificationData.PubInput, pubInputLen)
347349

348350
o.Logger.Infof("Risc0 proof verification result: %t", verificationResult)
349351
results <- verificationResult

operator/risc_zero/lib/risc_zero.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#include <stdbool.h>
22
#include <stdint.h>
33

4-
bool verify_risc_zero_receipt_ffi(unsigned char *receipt_bytes, uint32_t receipt_len, unsigned char *image_id, uint32_t image_id_len);
4+
bool verify_risc_zero_receipt_ffi(unsigned char *receipt_bytes, uint32_t receipt_len, unsigned char *image_id, uint32_t image_id_len, unsigned char *public_input, uint32_t public_input_len);

0 commit comments

Comments
 (0)