stark_evm_adapter/
annotated_proof.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
use std::collections::HashMap;

use ethers::types::U256;
use regex::Regex;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
/// [AnnotatedProof] maps annotated proof json file which contains the original proof
/// and the annotations generated by verifier of stone-prover
pub struct AnnotatedProof {
    pub proof_hex: String,
    pub annotations: Vec<String>,
    pub extra_annotations: Vec<String>,
    pub proof_parameters: ProofParameters,
    pub public_input: PublicInput,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProofParameters {
    pub field: String,
    pub stark: StarkParameters,
    pub use_extension_field: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StarkParameters {
    pub fri: FriParameters,
    pub log_n_cosets: u32,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FriParameters {
    pub fri_step_list: Vec<u32>,
    pub last_layer_degree_bound: u32,
    pub n_queries: u32,
    pub proof_of_work_bits: u32,
}

/// Public input for a cairo execution
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PublicInput {
    pub layout: String,
    pub memory_segments: HashMap<String, MemorySegment>,
    pub n_steps: u32,
    pub public_memory: Vec<PublicMemory>,
    pub rc_max: u32,
    pub rc_min: u32,
}

/// Memory segments for cairo builtins
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MemorySegment {
    pub begin_addr: u32,
    pub stop_ptr: u32,
}

/// Public memory for a cairo execution
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PublicMemory {
    pub address: u32,
    pub page: u32,
    // todo refactor to u256
    pub value: String,
}

impl AnnotatedProof {
    pub fn extract_interaction_elements(&self) -> (U256, U256) {
        let re = Regex::new(r"V->P: /cpu air/STARK/Interaction: Interaction element #\d+: Field Element\(0x([0-9a-f]+)\)").unwrap();
        let annotations = self.annotations.join("\n");

        let interaction_elements: Vec<U256> = re
            .captures_iter(&annotations)
            .filter_map(|cap| U256::from_str_radix(&cap[1], 16).ok())
            .collect();

        assert!(interaction_elements.len() == 3 || interaction_elements.len() == 6);

        (interaction_elements[0], interaction_elements[1])
    }
}