Skip to content

Commit 45ad637

Browse files
vezenovmTomAFrench
andauthored
chore(docs): Brillig opcodes (#7722)
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
1 parent 1bfced8 commit 45ad637

File tree

3 files changed

+61
-70
lines changed

3 files changed

+61
-70
lines changed

acvm-repo/brillig/src/black_box.rs

+31-63
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,13 @@ use serde::{Deserialize, Serialize};
77
#[cfg_attr(feature = "arb", derive(proptest_derive::Arbitrary))]
88
pub enum BlackBoxOp {
99
/// Encrypts a message using AES128.
10-
AES128Encrypt {
11-
inputs: HeapVector,
12-
iv: HeapArray,
13-
key: HeapArray,
14-
outputs: HeapVector,
15-
},
10+
AES128Encrypt { inputs: HeapVector, iv: HeapArray, key: HeapArray, outputs: HeapVector },
1611
/// Calculates the Blake2s hash of the inputs.
17-
Blake2s {
18-
message: HeapVector,
19-
output: HeapArray,
20-
},
12+
Blake2s { message: HeapVector, output: HeapArray },
2113
/// Calculates the Blake3 hash of the inputs.
22-
Blake3 {
23-
message: HeapVector,
24-
output: HeapArray,
25-
},
14+
Blake3 { message: HeapVector, output: HeapArray },
2615
/// Keccak Permutation function of 1600 width
27-
Keccakf1600 {
28-
input: HeapArray,
29-
output: HeapArray,
30-
},
16+
Keccakf1600 { input: HeapArray, output: HeapArray },
3117
/// Verifies a ECDSA signature over the secp256k1 curve.
3218
EcdsaSecp256k1 {
3319
hashed_msg: HeapVector,
@@ -44,13 +30,8 @@ pub enum BlackBoxOp {
4430
signature: HeapArray,
4531
result: MemoryAddress,
4632
},
47-
4833
/// Performs multi scalar multiplication over the embedded curve.
49-
MultiScalarMul {
50-
points: HeapVector,
51-
scalars: HeapVector,
52-
outputs: HeapArray,
53-
},
34+
MultiScalarMul { points: HeapVector, scalars: HeapVector, outputs: HeapArray },
5435
/// Performs addition over the embedded curve.
5536
EmbeddedCurveAdd {
5637
input1_x: MemoryAddress,
@@ -61,45 +42,32 @@ pub enum BlackBoxOp {
6142
input2_infinite: MemoryAddress,
6243
result: HeapArray,
6344
},
64-
BigIntAdd {
65-
lhs: MemoryAddress,
66-
rhs: MemoryAddress,
67-
output: MemoryAddress,
68-
},
69-
BigIntSub {
70-
lhs: MemoryAddress,
71-
rhs: MemoryAddress,
72-
output: MemoryAddress,
73-
},
74-
BigIntMul {
75-
lhs: MemoryAddress,
76-
rhs: MemoryAddress,
77-
output: MemoryAddress,
78-
},
79-
BigIntDiv {
80-
lhs: MemoryAddress,
81-
rhs: MemoryAddress,
82-
output: MemoryAddress,
83-
},
84-
BigIntFromLeBytes {
85-
inputs: HeapVector,
86-
modulus: HeapVector,
87-
output: MemoryAddress,
88-
},
89-
BigIntToLeBytes {
90-
input: MemoryAddress,
91-
output: HeapVector,
92-
},
93-
Poseidon2Permutation {
94-
message: HeapVector,
95-
output: HeapArray,
96-
len: MemoryAddress,
97-
},
98-
Sha256Compression {
99-
input: HeapArray,
100-
hash_values: HeapArray,
101-
output: HeapArray,
102-
},
45+
/// BigInt addition
46+
BigIntAdd { lhs: MemoryAddress, rhs: MemoryAddress, output: MemoryAddress },
47+
/// BigInt subtraction
48+
BigIntSub { lhs: MemoryAddress, rhs: MemoryAddress, output: MemoryAddress },
49+
/// BigInt multiplication
50+
BigIntMul { lhs: MemoryAddress, rhs: MemoryAddress, output: MemoryAddress },
51+
/// BigInt division
52+
BigIntDiv { lhs: MemoryAddress, rhs: MemoryAddress, output: MemoryAddress },
53+
/// BigInt from le bytes
54+
BigIntFromLeBytes { inputs: HeapVector, modulus: HeapVector, output: MemoryAddress },
55+
/// BigInt to le bytes
56+
BigIntToLeBytes { input: MemoryAddress, output: HeapVector },
57+
/// Applies the Poseidon2 permutation function to the given state,
58+
/// outputting the permuted state.
59+
Poseidon2Permutation { message: HeapVector, output: HeapArray, len: MemoryAddress },
60+
/// Applies the SHA-256 compression function to the input message
61+
Sha256Compression { input: HeapArray, hash_values: HeapArray, output: HeapArray },
62+
/// Returns a decomposition in `num_limbs` limbs of the given input over the given radix.
63+
///
64+
/// - The value stored in `radix` must be in the range [2, 256]
65+
/// - `num_limbs` must be at least one if the value stored in `input` is not zero.
66+
/// - The value stored in `output_bits` must have a `bit_size` of one.
67+
/// That value specifies whether we should decompose into bits. The value stored in
68+
/// the `radix` address must be two if the value stored in `output_bits` is equal to one.
69+
///
70+
/// Native to the Brillig VM and not supported as an ACIR black box function.
10371
ToRadix {
10472
input: MemoryAddress,
10573
radix: MemoryAddress,

acvm-repo/brillig/src/opcodes.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ use serde::{Deserialize, Serialize};
44

55
pub type Label = usize;
66

7+
/// Represents an address in the VM's memory.
8+
/// Supports both direct and relative addressing.
79
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
810
#[cfg_attr(feature = "arb", derive(proptest_derive::Arbitrary))]
911
pub enum MemoryAddress {
12+
/// Specifies an exact index in the VM's memory
1013
Direct(usize),
14+
/// Specifies an index relative to the stack pointer.
15+
///
16+
/// It is resolved as the current stack pointer plus the offset stored here.
1117
Relative(usize),
1218
}
1319

14-
/// `MemoryAddress` refers to the index in VM memory.
1520
impl MemoryAddress {
1621
pub fn direct(address: usize) -> Self {
1722
MemoryAddress::Direct(address)
@@ -209,7 +214,7 @@ pub enum ValueOrArray {
209214
pub enum BrilligOpcode<F> {
210215
/// Takes the fields in addresses `lhs` and `rhs`
211216
/// Performs the specified binary operation
212-
/// and stores the value in the `result` address.
217+
/// and stores the value in the `destination` address.
213218
BinaryFieldOp {
214219
destination: MemoryAddress,
215220
op: BinaryFieldOp,
@@ -218,7 +223,7 @@ pub enum BrilligOpcode<F> {
218223
},
219224
/// Takes the `bit_size` size integers in addresses `lhs` and `rhs`
220225
/// Performs the specified binary operation
221-
/// and stores the value in the `result` address.
226+
/// and stores the value in the `destination` address.
222227
BinaryIntOp {
223228
destination: MemoryAddress,
224229
op: BinaryIntOp,
@@ -236,17 +241,19 @@ pub enum BrilligOpcode<F> {
236241
source: MemoryAddress,
237242
bit_size: BitSize,
238243
},
244+
/// Sets the program counter to the value of `location` if
245+
/// the value at the `condition` address is zero.
239246
JumpIfNot {
240247
condition: MemoryAddress,
241248
location: Label,
242249
},
243-
/// Sets the program counter to the value located at `destination`
250+
/// Sets the program counter to the value of `location`
244251
/// If the value at `condition` is non-zero
245252
JumpIf {
246253
condition: MemoryAddress,
247254
location: Label,
248255
},
249-
/// Sets the program counter to the label.
256+
/// Sets the program counter to the value of `location`.
250257
Jump {
251258
location: Label,
252259
},
@@ -258,19 +265,27 @@ pub enum BrilligOpcode<F> {
258265
},
259266
/// We don't support dynamic jumps or calls
260267
/// See <https://github.com/ethereum/aleth/issues/3404> for reasoning
268+
///
269+
/// Pushes the current program counter to the call stack as to set a return location.
270+
/// Sets the program counter to the value of `location`.
261271
Call {
262272
location: Label,
263273
},
274+
/// Stores a constant `value` with a `bit_size` in the `destination` address.
264275
Const {
265276
destination: MemoryAddress,
266277
bit_size: BitSize,
267278
value: F,
268279
},
280+
/// Reads the address from `destination_pointer`, then stores a constant `value` with a `bit_size` at that address.
269281
IndirectConst {
270282
destination_pointer: MemoryAddress,
271283
bit_size: BitSize,
272284
value: F,
273285
},
286+
/// Pops the top element from the call stack, which represents the return location,
287+
/// and sets the program counter to that value. This operation is used to return
288+
/// from a function call.
274289
Return,
275290
/// Used to get data from an outside source.
276291
/// Also referred to as an Oracle. However, we don't use that name as
@@ -290,6 +305,7 @@ pub enum BrilligOpcode<F> {
290305
/// retrieve the elements)
291306
input_value_types: Vec<HeapValueType>,
292307
},
308+
/// Moves the content in the `source` address to the `destination` address.
293309
Mov {
294310
destination: MemoryAddress,
295311
source: MemoryAddress,
@@ -301,20 +317,26 @@ pub enum BrilligOpcode<F> {
301317
source_b: MemoryAddress,
302318
condition: MemoryAddress,
303319
},
320+
/// Reads the `source_pointer` to obtain a memory address, then retrieves the data
321+
/// stored at that address and writes it to the `destination` address.
304322
Load {
305323
destination: MemoryAddress,
306324
source_pointer: MemoryAddress,
307325
},
326+
/// Reads the `destination_pointer` to obtain a memory address, then stores the value
327+
/// from the `source` address at that location.
308328
Store {
309329
destination_pointer: MemoryAddress,
310330
source: MemoryAddress,
311331
},
332+
/// Native functions in the VM.
333+
/// These are equivalent to the black box functions in ACIR.
312334
BlackBox(BlackBoxOp),
313-
/// Used to denote execution failure, returning data after the offset
335+
/// Used to denote execution failure, halting the VM and returning data specified by a dynamically-sized vector.
314336
Trap {
315337
revert_data: HeapVector,
316338
},
317-
/// Stop execution, returning data after the offset
339+
/// Halts execution and returns data specified by a dynamically-sized vector.
318340
Stop {
319341
return_data: HeapVector,
320342
},

cspell.json

+1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@
261261
"vecs",
262262
"Vecs",
263263
"vitkov",
264+
"VM",
264265
"walkdir",
265266
"wasi",
266267
"wasmer",

0 commit comments

Comments
 (0)