-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathinput.rs
263 lines (230 loc) · 7.07 KB
/
input.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use core::{fmt::Debug, str::FromStr};
#[cfg(feature = "std")]
use std::path::PathBuf;
use alloy_sol_types::sol;
use anyhow::{anyhow, Error, Result};
use reth_primitives::{
revm_primitives::{Address, Bytes, HashMap, B256, U256},
Block, Header, TransactionSigned,
};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
#[cfg(not(feature = "std"))]
use crate::no_std::*;
use crate::{
consts::ChainSpec, primitives::mpt::MptNode, prover::Proof, utils::zlib_compress_data,
};
/// Represents the state of an account's storage.
/// The storage trie together with the used storage slots allow us to reconstruct all the
/// required values.
pub type StorageEntry = (MptNode, Vec<U256>);
/// External block input.
#[serde_as]
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct GuestInput {
/// Reth block
pub block: Block,
/// The network to generate the proof for
pub chain_spec: ChainSpec,
/// Previous block header
pub parent_header: Header,
/// State trie of the parent block.
pub parent_state_trie: MptNode,
/// Maps each address with its storage trie and the used storage slots.
pub parent_storage: HashMap<Address, StorageEntry>,
/// The code of all unique contracts.
pub contracts: Vec<Bytes>,
/// List of at most 256 previous block headers
pub ancestor_headers: Vec<Header>,
/// Taiko specific data
pub taiko: TaikoGuestInput,
}
/// External aggregation input.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct AggregationGuestInput {
/// All block proofs to prove
pub proofs: Vec<Proof>,
}
/// The raw proof data necessary to verify a proof
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct RawProof {
/// The actual proof
pub proof: Vec<u8>,
/// The resulting hash
pub input: B256,
}
/// External aggregation input.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct RawAggregationGuestInput {
/// All block proofs to prove
pub proofs: Vec<RawProof>,
}
/// External aggregation input.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct AggregationGuestOutput {
/// The resulting hash
pub hash: B256,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct ZkAggregationGuestInput {
pub image_id: [u32; 8],
pub block_inputs: Vec<B256>,
}
impl From<(Block, Header, ChainSpec, TaikoGuestInput)> for GuestInput {
fn from(
(block, parent_header, chain_spec, taiko): (Block, Header, ChainSpec, TaikoGuestInput),
) -> Self {
Self {
block,
chain_spec,
taiko,
parent_header,
..Self::default()
}
}
}
#[serde_as]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct TaikoGuestInput {
/// header
pub l1_header: Header,
pub tx_data: Vec<u8>,
pub anchor_tx: Option<TransactionSigned>,
pub block_proposed: BlockProposed,
pub prover_data: TaikoProverData,
pub blob_commitment: Option<Vec<u8>>,
pub blob_proof_type: BlobProofType,
}
pub struct ZlibCompressError(pub String);
impl TryFrom<Vec<TransactionSigned>> for TaikoGuestInput {
type Error = ZlibCompressError;
fn try_from(value: Vec<TransactionSigned>) -> Result<Self, Self::Error> {
let tx_data = zlib_compress_data(&alloy_rlp::encode(&value))
.map_err(|e| ZlibCompressError(e.to_string()))?;
Ok(Self {
tx_data,
..Self::default()
})
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum BlobProofType {
/// Guest runs through the entire computation from blob to Kzg commitment
/// then to version hash
#[default]
KzgVersionedHash,
/// Simplified Proof of Equivalence with fiat input in non-aligned field
/// Referencing https://notes.ethereum.org/@dankrad/kzg_commitments_in_proofs
/// with impl details in https://github.com/taikoxyz/raiko/issues/292
/// Guest proves the KZG evaluation of the a fiat-shamir input x and output result y
/// x = sha256(sha256(blob), kzg_commit(blob))
/// y = f(x)
/// where f is the KZG polynomial
ProofOfEquivalence,
}
impl FromStr for BlobProofType {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim() {
"proof_of_equivalence" => Ok(BlobProofType::ProofOfEquivalence),
"kzg_versioned_hash" => Ok(BlobProofType::KzgVersionedHash),
_ => Err(anyhow!("invalid blob proof type")),
}
}
}
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
pub struct TaikoProverData {
pub prover: Address,
pub graffiti: B256,
}
pub type RawGuestOutput = sol! {
tuple(uint64, address, Transition, address, address, bytes32)
};
#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GuestOutput {
pub header: Header,
pub hash: B256,
}
sol! {
#[derive(Debug, Default, Deserialize, Serialize)]
struct EthDeposit {
address recipient;
uint96 amount;
uint64 id;
}
#[derive(Debug, Default, Deserialize, Serialize)]
struct BlockMetadata {
bytes32 l1Hash;
bytes32 difficulty;
bytes32 blobHash; //or txListHash (if Blob not yet supported)
bytes32 extraData;
bytes32 depositsHash;
address coinbase; // L2 coinbase
uint64 id;
uint32 gasLimit;
uint64 timestamp;
uint64 l1Height;
uint16 minTier;
bool blobUsed;
bytes32 parentMetaHash;
address sender;
}
#[derive(Debug, Default, Deserialize, Serialize)]
struct BlockParams {
address assignedProver;
address coinbase;
bytes32 extraData;
bytes32 parentMetaHash;
HookCall[] hookCalls;
}
#[derive(Debug, Default, Deserialize, Serialize)]
struct HookCall {
address hook;
bytes data;
}
#[derive(Debug, Default, Deserialize, Serialize)]
struct Transition {
bytes32 parentHash;
bytes32 blockHash;
bytes32 stateRoot;
bytes32 graffiti;
}
#[derive(Debug, Default, Deserialize, Serialize)]
event BlockProposed(
uint256 indexed blockId,
address indexed assignedProver,
uint96 livenessBond,
BlockMetadata meta,
EthDeposit[] depositsProcessed
);
#[derive(Debug)]
struct TierProof {
uint16 tier;
bytes data;
}
#[derive(Debug)]
function proposeBlock(
bytes calldata params,
bytes calldata txList
)
{}
function proveBlock(uint64 blockId, bytes calldata input) {}
}
#[cfg(feature = "std")]
use std::path::Path;
#[cfg(feature = "std")]
pub fn get_input_path(dir: &Path, block_number: u64, network: &str) -> PathBuf {
dir.join(format!("input-{network}-{block_number}.bin"))
}
#[cfg(test)]
mod tests {
extern crate alloc;
use super::*;
#[test]
fn input_serde_roundtrip() {
let input = GuestInput::default();
let _: GuestInput = bincode::deserialize(&bincode::serialize(&input).unwrap()).unwrap();
}
}