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
use crate::Felt;
use core::fmt;
// ADVICE INJECTORS
// ================================================================================================
/// Defines a set of actions which can be initiated from the VM to inject new data into the advice
/// provider.
///
/// These actions can affect all 3 components of the advice provider: Merkle store, advice stack,
/// and advice map.
///
/// All actions, except for `MerkleNodeMerge` and `Ext2Inv`, can be invoked directly from Miden
/// assembly via dedicated instructions.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum AdviceInjector {
// MERKLE STORE INJECTORS
// --------------------------------------------------------------------------------------------
/// Creates a new Merkle tree in the advice provider by combining Merkle trees with the
/// specified roots. The root of the new tree is defined as `Hash(LEFT_ROOT, RIGHT_ROOT)`.
///
/// Inputs:
/// Operand stack: [RIGHT_ROOT, LEFT_ROOT, ...]
/// Merkle store: {RIGHT_ROOT, LEFT_ROOT}
///
/// Outputs:
/// Operand stack: [RIGHT_ROOT, LEFT_ROOT, ...]
/// Merkle store: {RIGHT_ROOT, LEFT_ROOT, hash(LEFT_ROOT, RIGHT_ROOT)}
///
/// After the operation, both the original trees and the new tree remains in the advice
/// provider (i.e., the input trees are not removed).
MerkleNodeMerge,
// ADVICE STACK INJECTORS
// --------------------------------------------------------------------------------------------
/// Pushes a node of the Merkle tree specified by the values on the top of the operand stack
/// onto the advice stack.
///
/// Inputs:
/// Operand stack: [depth, index, TREE_ROOT, ...]
/// Advice stack: [...]
/// Merkle store: {TREE_ROOT<-NODE}
///
/// Outputs:
/// Operand stack: [depth, index, TREE_ROOT, ...]
/// Advice stack: [NODE, ...]
/// Merkle store: {TREE_ROOT<-NODE}
MerkleNodeToStack,
/// Pushes a list of field elements onto the advice stack. The list is looked up in the advice
/// map using the specified word from the operand stack as the key. If `include_len` is set to
/// true, the number of elements in the value is also pushed onto the advice stack.
///
/// Inputs:
/// Operand stack: [..., KEY, ...]
/// Advice stack: [...]
/// Advice map: {KEY: values}
///
/// Outputs:
/// Operand stack: [..., KEY, ...]
/// Advice stack: [values_len?, values, ...]
/// Advice map: {KEY: values}
///
/// The `key_offset` value specifies the location of the `KEY` on the stack. For example,
/// offset value of 0 indicates that the top word on the stack should be used as the key, the
/// offset value of 4, indicates that the second word on the stack should be used as the key
/// etc.
///
/// The valid values of `key_offset` are 0 through 12 (inclusive).
MapValueToStack {
include_len: bool,
key_offset: usize,
},
/// Pushes the result of [u64] division (both the quotient and the remainder) onto the advice
/// stack.
///
/// Inputs:
/// Operand stack: [b1, b0, a1, a0, ...]
/// Advice stack: [...]
///
/// Outputs:
/// Operand stack: [b1, b0, a1, a0, ...]
/// Advice stack: [q0, q1, r0, r1, ...]
///
/// Where (a0, a1) and (b0, b1) are the 32-bit limbs of the dividend and the divisor
/// respectively (with a0 representing the 32 lest significant bits and a1 representing the
/// 32 most significant bits). Similarly, (q0, q1) and (r0, r1) represent the quotient and
/// the remainder respectively.
DivU64,
/// Given an element in a quadratic extension field on the top of the stack (i.e., a0, b1),
/// computes its multiplicative inverse and push the result onto the advice stack.
///
/// Inputs:
/// Operand stack: [a1, a0, ...]
/// Advice stack: [...]
///
/// Outputs:
/// Operand stack: [a1, a0, ...]
/// Advice stack: [b0, b1...]
///
/// Where (b0, b1) is the multiplicative inverse of the extension field element (a0, a1) at the
/// top of the stack.
Ext2Inv,
/// Given evaluations of a polynomial over some specified domain, interpolates the evaluations
/// into a polynomial in coefficient form and pushes the result into the advice stack.
///
/// The interpolation is performed using the iNTT algorithm. The evaluations are expected to be
/// in the quadratic extension.
///
/// Inputs:
/// Operand stack: [output_size, input_size, input_start_ptr, ...]
/// Advice stack: [...]
///
/// Outputs:
/// Operand stack: [output_size, input_size, input_start_ptr, ...]
/// Advice stack: [coefficients...]
///
/// - `input_size` is the number of evaluations (each evaluation is 2 base field elements).
/// Must be a power of 2 and greater 1.
/// - `output_size` is the number of coefficients in the interpolated polynomial (each
/// coefficient is 2 base field elements). Must be smaller than or equal to the number of
/// input evaluations.
/// - `input_start_ptr` is the memory address of the first evaluation.
/// - `coefficients` are the coefficients of the interpolated polynomial such that lowest
/// degree coefficients are located at the top of the advice stack.
Ext2Intt,
/// Pushes values onto the advice stack which are required for successful retrieval of a
/// value from a Sparse Merkle Tree data structure.
///
/// The Sparse Merkle Tree is tiered, meaning it will have leaf depths in `{16, 32, 48, 64}`.
/// The depth flags define the tier on which the leaf is located.
///
/// Inputs:
/// Operand stack: [KEY, ROOT, ...]
/// Advice stack: [...]
///
/// Outputs:
/// Operand stack: [KEY, ROOT, ...]
/// Advice stack: [f0, f1, K, V, f2]
///
/// Where:
/// - f0 is a boolean flag set to `1` if the depth is `16` or `48`.
/// - f1 is a boolean flag set to `1` if the depth is `16` or `32`.
/// - K is the remaining key word; will be zeroed if the tree don't contain a mapped value
/// for the key.
/// - V is the value word; will be zeroed if the tree don't contain a mapped value for the key.
/// - f2 is a boolean flag set to `1` if a remaining key is not zero.
SmtGet,
// ADVICE MAP INJECTORS
// --------------------------------------------------------------------------------------------
/// Reads words from memory at the specified range and inserts them into the advice map under
/// the key `KEY` located at the top of the stack.
///
/// Inputs:
/// Operand stack: [KEY, start_addr, end_addr, ...]
/// Advice map: {...}
///
/// Outputs:
/// Operand stack: [KEY, start_addr, end_addr, ...]
/// Advice map: {KEY: values}
///
/// Where `values` are the elements located in memory[start_addr..end_addr].
MemToMap,
/// Reads two word from the operand stack and inserts them into the advice map under the key
/// defined by the hash of these words.
///
/// Inputs:
/// Operand stack: [B, A, ...]
/// Advice map: {...}
///
/// Outputs:
/// Operand stack: [B, A, ...]
/// Advice map: {KEY: [a0, a1, a2, a3, b0, b1, b2, b3]}
///
/// Where KEY is computed as hash(A || B, domain), where domain is provided via the immediate
/// value.
HdwordToMap { domain: Felt },
}
impl fmt::Display for AdviceInjector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MerkleNodeMerge => write!(f, "merkle_node_merge"),
Self::MerkleNodeToStack => write!(f, "merkle_node_to_stack"),
Self::MapValueToStack {
include_len,
key_offset,
} => {
if *include_len {
write!(f, "map_value_to_stack_with_len.{key_offset}")
} else {
write!(f, "map_value_to_stack.{key_offset}")
}
}
Self::DivU64 => write!(f, "div_u64"),
Self::Ext2Inv => write!(f, "ext2_inv"),
Self::Ext2Intt => write!(f, "ext2_intt"),
Self::SmtGet => write!(f, "smt_get"),
Self::MemToMap => write!(f, "mem_to_map"),
Self::HdwordToMap { domain } => write!(f, "hdword_to_map.{domain}"),
}
}
}