|
| 1 | +use kimchi::mina_curves::pasta::Pallas; |
| 2 | +use serde::Deserialize; |
| 3 | + |
| 4 | +use crate::type_aliases::WrapPolyComm; |
| 5 | + |
| 6 | +#[derive(Deserialize)] |
| 7 | +pub struct StateProof { |
| 8 | + pub proof: Proof, |
| 9 | + pub statement: Statement, |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Deserialize)] |
| 13 | +pub struct Proof { |
| 14 | + pub bulletproof: Bulletproof, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Deserialize)] |
| 18 | +pub struct Bulletproof { |
| 19 | + pub challenge_polynomial_commitment: Point, |
| 20 | + pub delta: Point, |
| 21 | + pub lr: [[Point; 2]; 15], |
| 22 | + pub z_1: Scalar, |
| 23 | + pub z_2: Scalar, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Deserialize)] |
| 27 | +pub struct Commitments { |
| 28 | + pub t_comm: [Point; 7], |
| 29 | + pub w_comm: [Point; 15], |
| 30 | + pub z_comm: Point, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Deserialize)] |
| 34 | +pub struct Evaluations { |
| 35 | + pub coefficients: [Point; 15], |
| 36 | + pub complete_add_selector: Point, |
| 37 | + pub emul_selector: Point, |
| 38 | + pub endomul_scalar_selector: Point, |
| 39 | + pub generic_selector: Point, |
| 40 | + pub mul_selector: Point, |
| 41 | + pub poseidon_selector: Point, |
| 42 | + pub s: [Point; 6], |
| 43 | + pub w: [Point; 15], |
| 44 | + pub z: Point, |
| 45 | + pub ft_eval1: Scalar, |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Deserialize)] |
| 49 | +pub struct Statement { |
| 50 | + pub messages_for_next_step_proof: MessagesForNextStepProof, |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Deserialize)] |
| 54 | +pub struct MessagesForNextStepProof { |
| 55 | + pub challenge_polynomial_commitments: [Point; 2], |
| 56 | + pub old_bulletproof_challenges: [[BulletproofChallenge; 16]; 2], |
| 57 | +} |
| 58 | + |
| 59 | +#[derive(Deserialize)] |
| 60 | +pub struct BulletproofChallenge { |
| 61 | + pub prechallenge: Prechallenge, |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Deserialize)] |
| 65 | +pub struct Prechallenge { |
| 66 | + // OCaml doesn't support unsigned integers, these should |
| 67 | + // be two u64 limbs but are encoded with a sign. |
| 68 | + // We just need to do a cast to u64. |
| 69 | + pub inner: [I64; 2], |
| 70 | +} |
| 71 | + |
| 72 | +#[derive(Deserialize)] |
| 73 | +pub struct ProofState { |
| 74 | + pub deferred_values: DeferredValues, |
| 75 | + pub messages_for_next_wrap_proof: MessagesForNextWrapProof, |
| 76 | + pub sponge_digest_before_evaluations: [Scalar; 4], |
| 77 | +} |
| 78 | + |
| 79 | +#[derive(Deserialize)] |
| 80 | +pub struct DeferredValues { |
| 81 | + pub branch_data: BranchData, |
| 82 | + pub bulletproof_challenges: [BulletproofChallenge; 16], |
| 83 | + pub plonk: Plonk, |
| 84 | +} |
| 85 | + |
| 86 | +#[derive(Deserialize)] |
| 87 | +pub struct BranchData { |
| 88 | + pub domain_log2: String, |
| 89 | + pub proofs_verified: [String; 1], |
| 90 | +} |
| 91 | + |
| 92 | +#[derive(Deserialize)] |
| 93 | +pub struct Plonk { |
| 94 | + pub alpha: Prechallenge, |
| 95 | + pub beta: Point, |
| 96 | + pub feature_flags: FeatureFlags, |
| 97 | + pub gamma: Point, |
| 98 | + pub zeta: Prechallenge, |
| 99 | +} |
| 100 | + |
| 101 | +#[derive(Deserialize)] |
| 102 | +pub struct FeatureFlags { |
| 103 | + pub foreign_field_add: bool, |
| 104 | + pub foreign_field_mul: bool, |
| 105 | + pub lookup: bool, |
| 106 | + pub range_check0: bool, |
| 107 | + pub range_check1: bool, |
| 108 | + pub rot: bool, |
| 109 | + pub runtime_tables: bool, |
| 110 | + pub xor: bool, |
| 111 | +} |
| 112 | + |
| 113 | +#[derive(Deserialize)] |
| 114 | +pub struct MessagesForNextWrapProof { |
| 115 | + pub challenge_polynomial_commitment: Point, |
| 116 | + pub old_bulletproof_challenges: [[BulletproofChallenge; 16]; 2], |
| 117 | +} |
| 118 | + |
| 119 | +pub type Point = [String; 2]; // hex |
| 120 | +pub type Scalar = String; // hex |
| 121 | +pub type I64 = String; // decimal signed |
| 122 | + |
| 123 | +pub fn parse(proof_json: &serde_json::Value) -> Result<StateProof, String> { |
| 124 | + serde_json::from_value(proof_json.to_owned()) |
| 125 | + .map_err(|err| format!("Could not parse proof: {err}")) |
| 126 | +} |
| 127 | + |
| 128 | +impl Into<WrapPolyComm> for Point { |
| 129 | + fn into(self) -> WrapPolyComm { |
| 130 | + from hex |
| 131 | + basefield from big endian |
| 132 | + Pallas |
| 133 | + } |
| 134 | +} |
0 commit comments