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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use std::{
    collections::BTreeMap,
    ffi::OsStr,
    path::{Path, PathBuf},
};

use miden_processor::{AdviceInputs, ExecutionOptions, Felt as RawFelt, StackInputs};
use serde::Deserialize;

use crate::Felt;

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(try_from = "DebuggerConfigFile")]
pub struct DebuggerConfig {
    pub inputs: StackInputs,
    pub advice_inputs: AdviceInputs,
    pub options: ExecutionOptions,
}

impl TryFrom<DebuggerConfigFile> for DebuggerConfig {
    type Error = String;

    #[inline]
    fn try_from(mut file: DebuggerConfigFile) -> Result<Self, Self::Error> {
        Self::from_inputs_file(file, None)
    }
}

impl DebuggerConfig {
    pub fn parse_file<P>(path: P) -> std::io::Result<Self>
    where
        P: AsRef<std::path::Path>,
    {
        let path = path.as_ref();
        let content = std::fs::read_to_string(path)?;

        let file = toml::from_str::<DebuggerConfigFile>(&content).map_err(std::io::Error::other)?;
        Self::from_inputs_file(file, path.parent().map(|p| p.to_path_buf()))
            .map_err(std::io::Error::other)
    }

    pub fn parse_str(content: &str) -> Result<Self, String> {
        let file = toml::from_str::<DebuggerConfigFile>(content).map_err(|err| err.to_string())?;

        Self::from_inputs_file(file, None)
    }

    fn from_inputs_file(
        mut file: DebuggerConfigFile,
        _cwd: Option<PathBuf>,
    ) -> Result<Self, String> {
        let inputs = StackInputs::new(file.inputs.stack.into_iter().map(|felt| felt.0).collect())
            .map_err(|err| format!("invalid value for 'stack': {err}"))?;
        let advice_inputs = AdviceInputs::default()
            .with_stack(file.inputs.advice.stack.into_iter().rev().map(|felt| felt.0))
            .with_map(file.inputs.advice.map.into_iter().map(|entry| {
                (entry.digest.0, entry.values.into_iter().map(|felt| felt.0).collect::<Vec<_>>())
            }));

        Ok(Self {
            inputs,
            advice_inputs,
            options: file.options,
        })
    }
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
struct DebuggerConfigFile {
    inputs: Inputs,
    #[serde(deserialize_with = "deserialize_execution_options")]
    options: ExecutionOptions,
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
struct Inputs {
    /// The contents of the operand stack, top is leftmost
    stack: Vec<crate::Felt>,
    /// The inputs to the advice provider
    advice: Advice,
}

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
struct Advice {
    /// The contents of the advice stack, top is leftmost
    stack: Vec<crate::Felt>,
    /// Entries to populate the advice map with
    map: Vec<AdviceMapEntry>,
}

#[derive(Debug, Clone, Deserialize)]
struct AdviceMapEntry {
    digest: Digest,
    /// Values that will be pushed to the advice stack when this entry is requested
    #[serde(default)]
    values: Vec<crate::Felt>,
}

impl clap::builder::ValueParserFactory for DebuggerConfig {
    type Parser = DebuggerConfigParser;

    fn value_parser() -> Self::Parser {
        DebuggerConfigParser
    }
}

#[doc(hidden)]
#[derive(Clone)]
pub struct DebuggerConfigParser;
impl clap::builder::TypedValueParser for DebuggerConfigParser {
    type Value = DebuggerConfig;

    fn parse_ref(
        &self,
        _cmd: &clap::Command,
        _arg: Option<&clap::Arg>,
        value: &OsStr,
    ) -> Result<Self::Value, clap::error::Error> {
        use clap::error::{Error, ErrorKind};

        let inputs_path = Path::new(value);
        if !inputs_path.is_file() {
            return Err(Error::raw(
                ErrorKind::InvalidValue,
                format!("invalid inputs file: '{}' is not a file", inputs_path.display()),
            ));
        }

        let content = std::fs::read_to_string(inputs_path).map_err(|err| {
            Error::raw(ErrorKind::ValueValidation, format!("failed to read inputs file: {err}"))
        })?;
        let inputs_file = toml::from_str::<DebuggerConfigFile>(&content).map_err(|err| {
            Error::raw(ErrorKind::ValueValidation, format!("invalid inputs file: {err}"))
        })?;

        DebuggerConfig::from_inputs_file(inputs_file, Some(inputs_path.to_path_buf())).map_err(
            |err| Error::raw(ErrorKind::ValueValidation, format!("invalid inputs file: {err}")),
        )
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Digest(miden_processor::Digest);
impl<'de> Deserialize<'de> for Digest {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let digest = String::deserialize(deserializer)?;
        miden_processor::Digest::try_from(&digest)
            .map_err(|err| serde::de::Error::custom(format!("invalid digest: {err}")))
            .map(Self)
    }
}

fn deserialize_rodata_bytes<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use midenc_hir::ConstantData;

    String::deserialize(deserializer).and_then(|hex| {
        ConstantData::from_str_be(hex.as_str())
            .map_err(|err| serde::de::Error::custom(format!("invalid rodata: {err}")))
            .map(Vec::<u8>::from)
    })
}

fn deserialize_execution_options<'de, D>(deserializer: D) -> Result<ExecutionOptions, D::Error>
where
    D: serde::Deserializer<'de>,
{
    #[derive(Default, Deserialize)]
    #[serde(default)]
    struct ExecOptions {
        max_cycles: Option<u32>,
        expected_cycles: u32,
    }

    ExecOptions::deserialize(deserializer).and_then(|opts| {
        ExecutionOptions::new(
            opts.max_cycles,
            opts.expected_cycles,
            /* enable_tracing= */ true,
        )
        .map(|exec_opts| exec_opts.with_debugging())
        .map_err(|err| serde::de::Error::custom(format!("invalid execution options: {err}")))
    })
}

#[cfg(test)]
mod tests {
    use toml::toml;

    use super::*;

    #[test]
    fn debugger_config_empty() {
        let text = toml::to_string_pretty(&toml! {
            [inputs]
            [options]
        })
        .unwrap();

        let file = toml::from_str::<DebuggerConfig>(&text).unwrap();
        assert!(file.inputs.values().is_empty());
        assert!(file.advice_inputs.stack().is_empty());
        assert_eq!(file.options.enable_tracing(), true);
        assert_eq!(file.options.enable_debugging(), true);
        assert_eq!(file.options.max_cycles(), u32::MAX);
        assert_eq!(file.options.expected_cycles(), 64);
    }

    #[test]
    fn debugger_config_with_options() {
        let text = toml::to_string_pretty(&toml! {
            [inputs]
            [options]
            max_cycles = 1000
        })
        .unwrap();

        let file = DebuggerConfig::parse_str(&text).unwrap();
        assert!(file.inputs.values().is_empty());
        assert!(file.advice_inputs.stack().is_empty());
        assert_eq!(file.options.enable_tracing(), true);
        assert_eq!(file.options.enable_debugging(), true);
        assert_eq!(file.options.max_cycles(), 1000);
        assert_eq!(file.options.expected_cycles(), 64);
    }

    #[test]
    fn debugger_config_with_operands() {
        let text = toml::to_string_pretty(&toml! {
            [inputs]
            stack = [1, 2, 3]

            [options]
            max_cycles = 1000
        })
        .unwrap();

        let file = DebuggerConfig::parse_str(&text).unwrap();
        assert_eq!(file.inputs.values(), &[RawFelt::new(3), RawFelt::new(2), RawFelt::new(1)]);
        assert!(file.advice_inputs.stack().is_empty());
        assert_eq!(file.options.enable_tracing(), true);
        assert_eq!(file.options.enable_debugging(), true);
        assert_eq!(file.options.max_cycles(), 1000);
        assert_eq!(file.options.expected_cycles(), 64);
    }

    #[test]
    fn debugger_config_with_advice() {
        let text = toml::to_string_pretty(&toml! {
            [inputs]
            stack = [1, 2, 0x3]

            [inputs.advice]
            stack = [1, 2, 3, 4]

            [[inputs.advice.map]]
            digest = "0x3cff5b58a573dc9d25fd3c57130cc57e5b1b381dc58b5ae3594b390c59835e63"
            values = [1, 2, 3, 4]

            [options]
            max_cycles = 1000
        })
        .unwrap();
        let digest = miden_processor::Digest::try_from(
            "0x3cff5b58a573dc9d25fd3c57130cc57e5b1b381dc58b5ae3594b390c59835e63",
        )
        .unwrap();
        let file = DebuggerConfig::parse_str(&text).unwrap_or_else(|err| panic!("{err}"));
        assert_eq!(file.inputs.values(), &[RawFelt::new(3), RawFelt::new(2), RawFelt::new(1)]);
        assert_eq!(
            file.advice_inputs.stack(),
            &[RawFelt::new(4), RawFelt::new(3), RawFelt::new(2), RawFelt::new(1)]
        );
        assert_eq!(
            file.advice_inputs.mapped_values(&digest),
            Some([RawFelt::new(1), RawFelt::new(2), RawFelt::new(3), RawFelt::new(4)].as_slice())
        );
        assert_eq!(file.options.enable_tracing(), true);
        assert_eq!(file.options.enable_debugging(), true);
        assert_eq!(file.options.max_cycles(), 1000);
        assert_eq!(file.options.expected_cycles(), 64);
    }
}