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
use crate::{black_box::BlackBoxOp, Value};
use serde::{Deserialize, Serialize};
pub type Label = usize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct MemoryAddress(pub usize);
/// `MemoryAddress` refers to the index in VM memory.
impl MemoryAddress {
pub fn to_usize(self) -> usize {
self.0
}
}
impl From<usize> for MemoryAddress {
fn from(value: usize) -> Self {
MemoryAddress(value)
}
}
/// Describes the memory layout for an array/vector element
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum HeapValueType {
// A single field element is enough to represent the value
Simple,
// The value read should be interpreted as a pointer to a heap array, which
// consists of a pointer to a slice of memory of size elements, and a
// reference count
Array { value_types: Vec<HeapValueType>, size: usize },
// The value read should be interpreted as a pointer to a heap vector, which
// consists of a pointer to a slice of memory, a number of elements in that
// slice, and a reference count
Vector { value_types: Vec<HeapValueType> },
}
impl HeapValueType {
pub fn all_simple(types: &[HeapValueType]) -> bool {
types.iter().all(|typ| matches!(typ, HeapValueType::Simple))
}
}
/// A fixed-sized array starting from a Brillig memory location.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Copy)]
pub struct HeapArray {
pub pointer: MemoryAddress,
pub size: usize,
}
/// A memory-sized vector passed starting from a Brillig memory location and with a memory-held size
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Copy)]
pub struct HeapVector {
pub pointer: MemoryAddress,
pub size: MemoryAddress,
}
/// Lays out various ways an external foreign call's input and output data may be interpreted inside Brillig.
/// This data can either be an individual value or memory.
///
/// While we are usually agnostic to how memory is passed within Brillig,
/// this needs to be encoded somehow when dealing with an external system.
/// For simplicity, the extra type information is given right in the ForeignCall instructions.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Copy)]
pub enum ValueOrArray {
/// A single value passed to or from an external call
/// It is an 'immediate' value - used without dereferencing.
/// For a foreign call input, the value is read directly from memory.
/// For a foreign call output, the value is written directly to memory.
MemoryAddress(MemoryAddress),
/// An array passed to or from an external call
/// In the case of a foreign call input, the array is read from this Brillig memory location + usize more cells.
/// In the case of a foreign call output, the array is written to this Brillig memory location with the usize being here just as a sanity check for the size write.
HeapArray(HeapArray),
/// A vector passed to or from an external call
/// In the case of a foreign call input, the vector is read from this Brillig memory location + as many cells as the 2nd address indicates.
/// In the case of a foreign call output, the vector is written to this Brillig memory location and as 'size' cells, with size being stored in the second address.
HeapVector(HeapVector),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BrilligOpcode {
/// Takes the fields in addresses `lhs` and `rhs`
/// Performs the specified binary operation
/// and stores the value in the `result` address.
BinaryFieldOp {
destination: MemoryAddress,
op: BinaryFieldOp,
lhs: MemoryAddress,
rhs: MemoryAddress,
},
/// Takes the `bit_size` size integers in addresses `lhs` and `rhs`
/// Performs the specified binary operation
/// and stores the value in the `result` address.
BinaryIntOp {
destination: MemoryAddress,
op: BinaryIntOp,
bit_size: u32,
lhs: MemoryAddress,
rhs: MemoryAddress,
},
Cast {
destination: MemoryAddress,
source: MemoryAddress,
bit_size: u32,
},
JumpIfNot {
condition: MemoryAddress,
location: Label,
},
/// Sets the program counter to the value located at `destination`
/// If the value at `condition` is non-zero
JumpIf {
condition: MemoryAddress,
location: Label,
},
/// Sets the program counter to the label.
Jump {
location: Label,
},
/// Copies calldata after the offset to the specified address and length
CalldataCopy {
destination_address: MemoryAddress,
size: usize,
offset: usize,
},
/// We don't support dynamic jumps or calls
/// See https://github.com/ethereum/aleth/issues/3404 for reasoning
Call {
location: Label,
},
Const {
destination: MemoryAddress,
bit_size: u32,
value: Value,
},
Return,
/// Used to get data from an outside source.
/// Also referred to as an Oracle. However, we don't use that name as
/// this is intended for things like state tree reads, and shouldn't be confused
/// with e.g. blockchain price oracles.
ForeignCall {
/// Interpreted by caller context, ie this will have different meanings depending on
/// who the caller is.
function: String,
/// Destination addresses (may be single values or memory pointers).
destinations: Vec<ValueOrArray>,
/// Destination value types
destination_value_types: Vec<HeapValueType>,
/// Input addresses (may be single values or memory pointers).
inputs: Vec<ValueOrArray>,
/// Input value types (for heap allocated structures indicates how to
/// retrieve the elements)
input_value_types: Vec<HeapValueType>,
},
Mov {
destination: MemoryAddress,
source: MemoryAddress,
},
Load {
destination: MemoryAddress,
source_pointer: MemoryAddress,
},
Store {
destination_pointer: MemoryAddress,
source: MemoryAddress,
},
BlackBox(BlackBoxOp),
/// Used to denote execution failure
Trap,
/// Stop execution, returning data after the offset
Stop {
return_data_offset: usize,
return_data_size: usize,
},
}
/// Binary fixed-length field expressions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BinaryFieldOp {
Add,
Sub,
Mul,
Div,
/// (==) equal
Equals,
}
/// Binary fixed-length integer expressions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BinaryIntOp {
Add,
Sub,
Mul,
SignedDiv,
UnsignedDiv,
/// (==) equal
Equals,
/// (<) Field less than
LessThan,
/// (<=) field less or equal
LessThanEquals,
/// (&) Bitwise AND
And,
/// (|) Bitwise OR
Or,
/// (^) Bitwise XOR
Xor,
/// (<<) Shift left
Shl,
/// (>>) Shift right
Shr,
}