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
use super::{
    super::{
        ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
        MAX_STACK_WORD_OFFSET,
    },
    serde::signatures,
};
use alloc::string::ToString;
use core::fmt;
use vm_core::{AdviceInjector, Felt, SignatureKind, ZERO};

// ADVICE INJECTORS
// ================================================================================================

/// Instructions which inject data into the advice provider.
///
/// These instructions can be used to perform two broad sets of operations:
/// - Push new data onto the advice stack.
/// - Insert new data into the advice map.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AdviceInjectorNode {
    PushU64Div,
    PushExt2intt,
    PushSmtGet,
    PushSmtSet,
    PushSmtPeek,
    PushMapVal,
    PushMapValImm { offset: u8 },
    PushMapValN,
    PushMapValNImm { offset: u8 },
    PushMtNode,
    InsertMem,
    InsertHdword,
    InsertHdwordImm { domain: u8 },
    InsertHperm,
    PushSignature { kind: SignatureKind },
}

impl From<&AdviceInjectorNode> for AdviceInjector {
    fn from(value: &AdviceInjectorNode) -> Self {
        use AdviceInjectorNode::*;
        match value {
            PushU64Div => Self::U64Div,
            PushExt2intt => Self::Ext2Intt,
            PushSmtGet => Self::SmtGet,
            PushSmtSet => Self::SmtSet,
            PushSmtPeek => Self::SmtPeek,
            PushMapVal => Self::MapValueToStack {
                include_len: false,
                key_offset: 0,
            },
            PushMapValImm { offset } => Self::MapValueToStack {
                include_len: false,
                key_offset: (*offset) as usize,
            },
            PushMapValN => Self::MapValueToStack {
                include_len: true,
                key_offset: 0,
            },
            PushMapValNImm { offset } => Self::MapValueToStack {
                include_len: true,
                key_offset: (*offset) as usize,
            },
            PushMtNode => Self::MerkleNodeToStack,
            InsertMem => Self::MemToMap,
            InsertHdword => Self::HdwordToMap { domain: ZERO },
            InsertHdwordImm { domain } => Self::HdwordToMap {
                domain: Felt::from(*domain),
            },
            InsertHperm => Self::HpermToMap,
            PushSignature { kind } => Self::SigToStack { kind: *kind },
        }
    }
}

impl fmt::Display for AdviceInjectorNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use AdviceInjectorNode::*;
        match self {
            PushU64Div => write!(f, "push_u64div"),
            PushExt2intt => write!(f, "push_ext2intt"),
            PushSmtGet => write!(f, "push_smtget"),
            PushSmtSet => write!(f, "push_smtset"),
            PushSmtPeek => write!(f, "push_smtpeek"),
            PushMapVal => write!(f, "push_mapval"),
            PushMapValImm { offset } => write!(f, "push_mapval.{offset}"),
            PushMapValN => write!(f, "push_mapvaln"),
            PushMapValNImm { offset } => write!(f, "push_mapvaln.{offset}"),
            PushMtNode => write!(f, "push_mtnode"),
            InsertMem => write!(f, "insert_mem"),
            InsertHdword => write!(f, "insert_hdword"),
            InsertHdwordImm { domain } => write!(f, "insert_hdword.{domain}"),
            InsertHperm => writeln!(f, "insert_hperm"),
            PushSignature { kind } => write!(f, "push_sig.{kind}"),
        }
    }
}

// SERIALIZATION / DESERIALIZATION
// ================================================================================================

const PUSH_U64DIV: u8 = 0;
const PUSH_EXT2INTT: u8 = 1;
const PUSH_SMTGET: u8 = 2;
const PUSH_SMTSET: u8 = 3;
const PUSH_SMTPEEK: u8 = 4;
const PUSH_MAPVAL: u8 = 5;
const PUSH_MAPVAL_IMM: u8 = 6;
const PUSH_MAPVALN: u8 = 7;
const PUSH_MAPVALN_IMM: u8 = 8;
const PUSH_MTNODE: u8 = 9;
const INSERT_MEM: u8 = 10;
const INSERT_HDWORD: u8 = 11;
const INSERT_HDWORD_IMM: u8 = 12;
const INSERT_HPERM: u8 = 13;
const PUSH_SIG: u8 = 14;

impl Serializable for AdviceInjectorNode {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        use AdviceInjectorNode::*;
        match self {
            PushU64Div => target.write_u8(PUSH_U64DIV),
            PushExt2intt => target.write_u8(PUSH_EXT2INTT),
            PushSmtGet => target.write_u8(PUSH_SMTGET),
            PushSmtSet => target.write_u8(PUSH_SMTSET),
            PushSmtPeek => target.write_u8(PUSH_SMTPEEK),
            PushMapVal => target.write_u8(PUSH_MAPVAL),
            PushMapValImm { offset } => {
                target.write_u8(PUSH_MAPVAL_IMM);
                target.write_u8(*offset);
            }
            PushMapValN => target.write_u8(PUSH_MAPVALN),
            PushMapValNImm { offset } => {
                target.write_u8(PUSH_MAPVALN_IMM);
                target.write_u8(*offset);
            }
            PushMtNode => target.write_u8(PUSH_MTNODE),
            InsertMem => target.write_u8(INSERT_MEM),
            InsertHdword => target.write_u8(INSERT_HDWORD),
            InsertHdwordImm { domain } => {
                target.write_u8(INSERT_HDWORD_IMM);
                target.write_u8(*domain);
            }
            InsertHperm => target.write_u8(INSERT_HPERM),
            PushSignature { kind } => {
                target.write_u8(PUSH_SIG);
                signatures::write_options_into(target, kind)
            }
        }
    }
}

impl Deserializable for AdviceInjectorNode {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        match source.read_u8()? {
            PUSH_U64DIV => Ok(AdviceInjectorNode::PushU64Div),
            PUSH_EXT2INTT => Ok(AdviceInjectorNode::PushExt2intt),
            PUSH_SMTGET => Ok(AdviceInjectorNode::PushSmtGet),
            PUSH_SMTSET => Ok(AdviceInjectorNode::PushSmtSet),
            PUSH_SMTPEEK => Ok(AdviceInjectorNode::PushSmtPeek),
            PUSH_MAPVAL => Ok(AdviceInjectorNode::PushMapVal),
            PUSH_MAPVAL_IMM => {
                let offset = source.read_u8()?;
                if offset > MAX_STACK_WORD_OFFSET {
                    return Err(DeserializationError::InvalidValue("invalid offset".to_string()));
                }
                Ok(AdviceInjectorNode::PushMapValImm { offset })
            }
            PUSH_MAPVALN => Ok(AdviceInjectorNode::PushMapValN),
            PUSH_MAPVALN_IMM => {
                let offset = source.read_u8()?;
                if offset > MAX_STACK_WORD_OFFSET {
                    return Err(DeserializationError::InvalidValue("invalid offset".to_string()));
                }
                Ok(AdviceInjectorNode::PushMapValNImm { offset })
            }
            PUSH_MTNODE => Ok(AdviceInjectorNode::PushMtNode),
            INSERT_MEM => Ok(AdviceInjectorNode::InsertMem),
            INSERT_HDWORD => Ok(AdviceInjectorNode::InsertHdword),
            INSERT_HDWORD_IMM => {
                let domain = source.read_u8()?;
                Ok(AdviceInjectorNode::InsertHdwordImm { domain })
            }
            INSERT_HPERM => Ok(AdviceInjectorNode::InsertHperm),
            PUSH_SIG => Ok(AdviceInjectorNode::PushSignature {
                kind: signatures::read_options_from(source)?,
            }),
            val => Err(DeserializationError::InvalidValue(val.to_string())),
        }
    }
}