Skip to content

Commit 58917e8

Browse files
committed
Add base for protocol state proof deserialization
1 parent fadd571 commit 58917e8

4 files changed

Lines changed: 220 additions & 0 deletions

File tree

operator/mina/lib/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

operator/mina/lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rmp-serde = "1.1.2"
1818
serde_json = "1.0.118"
1919
hex = "0.4.3"
2020
mina-tree = { git = "https://github.com/lambdaclass/openmina/", branch = "mina_bridge" }
21+
mina-p2p-messages = { git = "https://github.com/lambdaclass/openmina/", branch = "mina_bridge" }
2122

2223
[patch.crates-io]
2324
ark-ff = { git = "https://github.com/openmina/algebra", branch = "openmina" }

operator/mina/lib/src/openmina_block_verifier/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod protocol_state_proof;
2+
13
pub fn parse_query_to_mina_block_header(mina_state_proof_vk_query: &str) {
24
todo!()
35
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
use mina_p2p_messages::v2::{
2+
MinaBaseProofStableV2, PicklesProofProofsVerified2ReprStableV2,
3+
PicklesProofProofsVerified2ReprStableV2PrevEvals,
4+
PicklesProofProofsVerified2ReprStableV2Statement, PicklesWrapWireProofCommitmentsStableV1,
5+
PicklesWrapWireProofEvaluationsStableV1, PicklesWrapWireProofStableV1,
6+
PicklesWrapWireProofStableV1Bulletproof,
7+
};
8+
use serde::Deserialize;
9+
10+
pub const WRAP_PREV_CHALLENGES: usize = 2;
11+
pub const WRAP_SCALARS_PER_CHALLENGE: usize = 15;
12+
13+
pub type DecimalSigned = String;
14+
pub type HexScalar = String;
15+
pub type HexPointCoordinates = [String; 2];
16+
pub type HexPointEvaluations = [String; 2];
17+
18+
#[derive(Deserialize)]
19+
pub struct StateProof {
20+
pub proof: Proof,
21+
pub statement: Statement,
22+
}
23+
24+
#[derive(Deserialize)]
25+
pub struct Proof {
26+
pub commitments: Commitments,
27+
pub evaluations: Evaluations,
28+
pub ft_eval1: HexScalar,
29+
pub bulletproof: Bulletproof,
30+
}
31+
32+
#[derive(Deserialize)]
33+
pub struct Bulletproof {
34+
pub challenge_polynomial_commitment: HexPointCoordinates,
35+
pub delta: HexPointCoordinates,
36+
pub lr: Vec<(HexPointCoordinates, HexPointCoordinates)>,
37+
pub z_1: HexScalar,
38+
pub z_2: HexScalar,
39+
}
40+
41+
#[derive(Deserialize)]
42+
pub struct Commitments {
43+
pub w_comm: [HexPointCoordinates; 15],
44+
pub z_comm: HexPointCoordinates,
45+
pub t_comm: Vec<HexPointCoordinates>,
46+
}
47+
48+
#[derive(Deserialize)]
49+
pub struct Evaluations {
50+
pub coefficients: [HexPointEvaluations; 15],
51+
pub complete_add_selector: HexPointEvaluations,
52+
pub emul_selector: HexPointEvaluations,
53+
pub endomul_scalar_selector: HexPointEvaluations,
54+
pub generic_selector: HexPointEvaluations,
55+
pub mul_selector: HexPointEvaluations,
56+
pub poseidon_selector: HexPointEvaluations,
57+
pub s: [HexPointEvaluations; 6],
58+
pub w: [HexPointEvaluations; 15],
59+
pub z: HexPointEvaluations,
60+
}
61+
62+
#[derive(Deserialize)]
63+
pub struct Statement {
64+
pub proof_state: ProofState,
65+
pub messages_for_next_step_proof: MessagesForNextStepProof,
66+
}
67+
68+
#[derive(Deserialize)]
69+
pub struct MessagesForNextStepProof {
70+
pub challenge_polynomial_commitments: [HexPointCoordinates; 2],
71+
pub old_bulletproof_challenges: [[BulletproofChallenge; 16]; 2],
72+
}
73+
74+
#[derive(Deserialize)]
75+
pub struct BulletproofChallenge {
76+
pub prechallenge: Prechallenge,
77+
}
78+
79+
#[derive(Deserialize)]
80+
pub struct Prechallenge {
81+
// OCaml doesn't support unsigned integers, these should
82+
// be two u64 limbs but are encoded with a sign.
83+
// We just need to do a cast to u64.
84+
pub inner: [DecimalSigned; 2],
85+
}
86+
87+
#[derive(Deserialize)]
88+
pub struct ProofState {
89+
pub deferred_values: DeferredValues,
90+
pub messages_for_next_wrap_proof: MessagesForNextWrapProof,
91+
pub sponge_digest_before_evaluations: [HexScalar; 4],
92+
}
93+
94+
#[derive(Deserialize)]
95+
pub struct DeferredValues {
96+
pub branch_data: BranchData,
97+
pub bulletproof_challenges: [BulletproofChallenge; 16],
98+
pub plonk: Plonk,
99+
}
100+
101+
#[derive(Deserialize)]
102+
pub struct BranchData {
103+
pub domain_log2: String,
104+
pub proofs_verified: [String; 1],
105+
}
106+
107+
#[derive(Deserialize)]
108+
pub struct Plonk {
109+
pub alpha: Prechallenge,
110+
pub beta: HexPointCoordinates,
111+
pub feature_flags: FeatureFlags,
112+
pub gamma: HexPointCoordinates,
113+
pub zeta: Prechallenge,
114+
}
115+
116+
#[derive(Deserialize)]
117+
pub struct FeatureFlags {
118+
pub foreign_field_add: bool,
119+
pub foreign_field_mul: bool,
120+
pub lookup: bool,
121+
pub range_check0: bool,
122+
pub range_check1: bool,
123+
pub rot: bool,
124+
pub runtime_tables: bool,
125+
pub xor: bool,
126+
}
127+
128+
#[derive(Deserialize)]
129+
pub struct MessagesForNextWrapProof {
130+
pub challenge_polynomial_commitment: HexPointCoordinates,
131+
pub old_bulletproof_challenges:
132+
[[BulletproofChallenge; WRAP_SCALARS_PER_CHALLENGE]; WRAP_PREV_CHALLENGES],
133+
}
134+
135+
pub fn parse_json(mina_state_proof_vk_query_str: &str) -> Result<StateProof, String> {
136+
let mina_state_proof_vk_query: serde_json::Map<String, serde_json::Value> =
137+
serde_json::from_str(mina_state_proof_vk_query_str)
138+
.map_err(|err| format!("Could not parse mina state proof vk query: {err}"))?;
139+
let protocol_state_proof_json = mina_state_proof_vk_query
140+
.get("data")
141+
.and_then(|d| d.get("bestChain"))
142+
.and_then(|d| d.get(0))
143+
.and_then(|d| d.get("protocolStateProof"))
144+
.and_then(|d| d.get("json"))
145+
.ok_or("Could not parse protocol state proof: JSON structure upto protocolStateProof is unexpected")?;
146+
147+
serde_json::from_value(protocol_state_proof_json.to_owned())
148+
.map_err(|err| format!("Could not parse mina state proof: {err}"))
149+
}
150+
151+
pub fn parse_state_proof(state_proof: StateProof) -> Result<MinaBaseProofStableV2, String> {
152+
// proof fields
153+
let commitments = PicklesWrapWireProofCommitmentsStableV1 {
154+
w_comm: (),
155+
z_comm: (),
156+
t_comm: (),
157+
};
158+
let evaluations = PicklesWrapWireProofEvaluationsStableV1 {
159+
w: (),
160+
coefficients: (),
161+
z: (),
162+
s: (),
163+
generic_selector: (),
164+
poseidon_selector: (),
165+
complete_add_selector: (),
166+
mul_selector: (),
167+
emul_selector: (),
168+
endomul_scalar_selector: (),
169+
};
170+
//let ft_eval1 = bigint
171+
let bulletproof = PicklesWrapWireProofStableV1Bulletproof {
172+
lr: (),
173+
z_1: (),
174+
z_2: (),
175+
delta: (),
176+
challenge_polynomial_commitment: (),
177+
};
178+
179+
// protocol_state_proof fields
180+
let statement = PicklesProofProofsVerified2ReprStableV2Statement {
181+
proof_state: (),
182+
messages_for_next_step_proof: (),
183+
};
184+
let prev_evals = PicklesProofProofsVerified2ReprStableV2PrevEvals {
185+
evals: (),
186+
ft_eval1: (),
187+
};
188+
let proof = PicklesWrapWireProofStableV1 {
189+
commitments: (),
190+
evaluations: (),
191+
ft_eval1: (),
192+
bulletproof: (),
193+
};
194+
195+
let protocol_state_proof = MinaBaseProofStableV2(PicklesProofProofsVerified2ReprStableV2 {
196+
statement: (),
197+
prev_evals: (),
198+
proof: (),
199+
});
200+
201+
protocol_state_proof
202+
}
203+
204+
#[cfg(test)]
205+
mod tests {
206+
use super::parse_json;
207+
208+
const MINA_STATE_PROOF_VK_QUERY: &str = include_str!(
209+
"../../../../../batcher/aligned/test_files/mina/mina_state_proof_vk_query.json"
210+
);
211+
212+
#[test]
213+
fn parse_protocol_state_proof() {
214+
parse_json(MINA_STATE_PROOF_VK_QUERY).unwrap();
215+
}
216+
}

0 commit comments

Comments
 (0)