Skip to content

Commit 43f4ba4

Browse files
set the encoded instruction to be u128 for opcode_extensions to come
1 parent 1a135db commit 43f4ba4

File tree

6 files changed

+73
-53
lines changed

6 files changed

+73
-53
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* feat: set `encoded_instruction` to be u128 for opcode_extensions to come [#1940](https://github.com/lambdaclass/cairo-vm/pull/1940)
6+
57
* feat: add `get_u32_range` to `impl VirtualMachine` add `get_u32` and `get_u32_range` to `impl Memory` [#1936](https://github.com/lambdaclass/cairo-vm/pull/1936)
68

79
* feat: add the field `opcode_extension` to the structure of `Instruction` [#1933](https://github.com/lambdaclass/cairo-vm/pull/1933)

cairo-vm-tracer/src/tracer_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl TracerData {
143143
let (instruction_encoding, _) =
144144
get_instruction_encoding(entry.pc, &memory, program.prime())?;
145145

146-
let instruction_encoding = instruction_encoding.to_u64();
146+
let instruction_encoding = instruction_encoding.to_u128();
147147
if instruction_encoding.is_none() {
148148
return Err(TraceDataError::FailedToConvertInstructionEncoding);
149149
}

vm/src/types/instruction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ impl Instruction {
9393

9494
// Returns True if the given instruction looks like a call instruction
9595
pub(crate) fn is_call_instruction(encoded_instruction: &Felt252) -> bool {
96-
let encoded_i64_instruction = match encoded_instruction.to_u64() {
96+
let encoded_u128_instruction = match encoded_instruction.to_u128() {
9797
Some(num) => num,
9898
None => return false,
9999
};
100-
let instruction = match decode_instruction(encoded_i64_instruction) {
100+
let instruction = match decode_instruction(encoded_u128_instruction) {
101101
Ok(inst) => inst,
102102
Err(_) => return false,
103103
};
@@ -140,7 +140,7 @@ mod tests {
140140
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
141141
fn instruction_size() {
142142
let encoded_instruction = Felt252::from(1226245742482522112_i64);
143-
let instruction = decode_instruction(encoded_instruction.to_u64().unwrap()).unwrap();
143+
let instruction = decode_instruction(encoded_instruction.to_u128().unwrap()).unwrap();
144144
assert_eq!(instruction.size(), 2);
145145
}
146146
}

vm/src/vm/decoding/decoder.rs

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,42 @@ use crate::{
55
vm::errors::vm_errors::VirtualMachineError,
66
};
77

8-
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
9-
// ... 15|14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
8+
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
9+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
1010

11-
/// Decodes an instruction. The encoding is little endian, so flags go from bit 63 to 48.
12-
/// The bits 64 and beyond are reserved for the opcode extension.
11+
/// Decodes an instruction. The encoding is little endian, so flags go from bit 127 to 48.
12+
/// The bits 63 and beyond are reserved for the opcode extension.
1313
/// opcode_extension_num=0 means the instruction is a Stone instruction.
14-
/// opcode_extension_num>1 is for new Stwo opcodes.
15-
pub fn decode_instruction(encoded_instr: u64) -> Result<Instruction, VirtualMachineError> {
16-
const DST_REG_MASK: u64 = 0x0001;
17-
const DST_REG_OFF: u64 = 0;
18-
const OP0_REG_MASK: u64 = 0x0002;
19-
const OP0_REG_OFF: u64 = 1;
20-
const OP1_SRC_MASK: u64 = 0x001C;
21-
const OP1_SRC_OFF: u64 = 2;
22-
const RES_LOGIC_MASK: u64 = 0x0060;
23-
const RES_LOGIC_OFF: u64 = 5;
24-
const PC_UPDATE_MASK: u64 = 0x0380;
25-
const PC_UPDATE_OFF: u64 = 7;
26-
const AP_UPDATE_MASK: u64 = 0x0C00;
27-
const AP_UPDATE_OFF: u64 = 10;
28-
const OPCODE_MASK: u64 = 0x7000;
29-
const OPCODE_OFF: u64 = 12;
30-
const OPCODE_EXTENSION_OFF: u64 = 63;
14+
/// opcode_extension_num>0 is for new Stwo opcodes.
15+
pub fn decode_instruction(encoded_instr: u128) -> Result<Instruction, VirtualMachineError> {
16+
// HIGH_BITS_MASK is a mask to extract the high bits that are yet to be used in any opcode extension.
17+
const HIGH_BITS_MASK: u128 = ((1 << 127) - (1 << 64)) << 1;
18+
const DST_REG_MASK: u128 = 0x0001;
19+
const DST_REG_OFF: u128 = 0;
20+
const OP0_REG_MASK: u128 = 0x0002;
21+
const OP0_REG_OFF: u128 = 1;
22+
const OP1_SRC_MASK: u128 = 0x001C;
23+
const OP1_SRC_OFF: u128 = 2;
24+
const RES_LOGIC_MASK: u128 = 0x0060;
25+
const RES_LOGIC_OFF: u128 = 5;
26+
const PC_UPDATE_MASK: u128 = 0x0380;
27+
const PC_UPDATE_OFF: u128 = 7;
28+
const AP_UPDATE_MASK: u128 = 0x0C00;
29+
const AP_UPDATE_OFF: u128 = 10;
30+
const OPCODE_MASK: u128 = 0x7000;
31+
const OPCODE_OFF: u128 = 12;
32+
const OPCODE_EXTENSION_OFF: u128 = 63;
3133

3234
// Flags start on the 48th bit.
33-
const FLAGS_OFFSET: u64 = 48;
34-
const OFF0_OFF: u64 = 0;
35-
const OFF1_OFF: u64 = 16;
36-
const OFF2_OFF: u64 = 32;
37-
const OFFX_MASK: u64 = 0xFFFF;
35+
const FLAGS_OFFSET: u128 = 48;
36+
const OFF0_OFF: u128 = 0;
37+
const OFF1_OFF: u128 = 16;
38+
const OFF2_OFF: u128 = 32;
39+
const OFFX_MASK: u128 = 0xFFFF;
40+
41+
if (encoded_instr & HIGH_BITS_MASK) != 0 {
42+
return Err(VirtualMachineError::NonZeroReservedBits);
43+
}
3844

3945
// Grab offsets and convert them from little endian format.
4046
let off0 = decode_offset(encoded_instr >> OFF0_OFF & OFFX_MASK);
@@ -160,8 +166,8 @@ pub fn decode_instruction(encoded_instr: u64) -> Result<Instruction, VirtualMach
160166
})
161167
}
162168

163-
fn decode_offset(offset: u64) -> isize {
164-
let vectorized_offset: [u8; 8] = offset.to_le_bytes();
169+
fn decode_offset(offset: u128) -> isize {
170+
let vectorized_offset: [u8; 16] = offset.to_le_bytes();
165171
let offset_16b_encoded = u16::from_le_bytes([vectorized_offset[0], vectorized_offset[1]]);
166172
let complement_const = 0x8000u16;
167173
let (offset_16b, _) = offset_16b_encoded.overflowing_sub(complement_const);
@@ -177,6 +183,16 @@ mod decoder_test {
177183
#[cfg(target_arch = "wasm32")]
178184
use wasm_bindgen_test::*;
179185

186+
#[test]
187+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
188+
fn non_zero_high_bits() {
189+
let error = decode_instruction(0x214a7800080008000);
190+
assert_eq!(
191+
error.unwrap_err().to_string(),
192+
"Reserved instruction bits must be 0",
193+
)
194+
}
195+
180196
#[test]
181197
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
182198
fn invalid_op1_reg() {
@@ -224,7 +240,7 @@ mod decoder_test {
224240
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
225241
fn decode_flags_nop_add_jmp_add_imm_fp_fp() {
226242
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
227-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
243+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
228244
// Stone| NOp| ADD| JUMP| ADD| IMM| FP| FP
229245
// 0 0 0 0 0 1 0 0 1 0 1 0 0 1 1 1
230246
// 0000 0100 1010 0111 = 0x04A7; offx = 0
@@ -244,7 +260,7 @@ mod decoder_test {
244260
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
245261
fn decode_flags_nop_add1_jmp_rel_mul_fp_ap_ap() {
246262
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
247-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
263+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
248264
// Stone| NOp| ADD1| JUMP_REL| MUL| FP| AP| AP
249265
// 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0
250266
// 0000 1001 0100 1000 = 0x0948; offx = 0
@@ -264,7 +280,7 @@ mod decoder_test {
264280
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
265281
fn decode_flags_assrt_add_regular_mul_ap_ap_ap() {
266282
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
267-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
283+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
268284
// Stone| ASSRT_EQ| ADD| REGULAR| MUL| AP| AP| AP
269285
// 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0
270286
// 0100 1000 0101 0000 = 0x4850; offx = 0
@@ -284,7 +300,7 @@ mod decoder_test {
284300
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
285301
fn decode_flags_assrt_add2_jnz_uncon_op0_ap_ap() {
286302
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
287-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
303+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
288304
// Stone| ASSRT_EQ| ADD2| JNZ|UNCONSTRD| OP0| AP| AP
289305
// 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0
290306
// 0100 0010 0000 0000 = 0x4200; offx = 0
@@ -304,7 +320,7 @@ mod decoder_test {
304320
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
305321
fn decode_flags_nop_regu_regu_op1_op0_ap_ap() {
306322
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
307-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
323+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
308324
// Stone| NOP| REGULAR| REGULAR| OP1| OP0| AP| AP
309325
// 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
310326
// 0000 0000 0000 0000 = 0x0000; offx = 0
@@ -324,7 +340,7 @@ mod decoder_test {
324340
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
325341
fn decode_offset_negative() {
326342
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
327-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
343+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
328344
// Stone| NOP| REGULAR| REGULAR| OP1| OP0| AP| AP
329345
// 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
330346
// 0000 0000 0000 0000 = 0x0000; offx = 0
@@ -338,7 +354,7 @@ mod decoder_test {
338354
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
339355
fn decode_ret_cairo_standard() {
340356
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
341-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
357+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
342358
// Stone| RET| REGULAR| JUMP| Op1| FP| FP| FP
343359
// 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 1
344360
// 0010 0000 1000 1011 = 0x208b; off0 = -2, off1 = -1
@@ -360,8 +376,8 @@ mod decoder_test {
360376
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
361377
fn decode_call_cairo_standard() {
362378
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
363-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
364-
// Stone| CALL| Add2| JumpRel| Op1| IMM| FP| FP
379+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
380+
// Stone| CALL| Regular| JumpRel| Op1| FP| FP| FP
365381
// 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0
366382
// 0001 0001 0000 0100 = 0x1104; off0 = 0, off1 = 1
367383
let inst = decode_instruction(0x1104800180018000).unwrap();
@@ -382,7 +398,7 @@ mod decoder_test {
382398
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
383399
fn decode_ret_opcode_error() {
384400
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
385-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
401+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
386402
// Stone| RET| REGULAR| JUMP| Op1| FP| FP| FP
387403
// 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 1
388404
// 0010 0000 1000 1011 = 0x208b; off0 = -1, off1 = -1
@@ -394,8 +410,8 @@ mod decoder_test {
394410
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
395411
fn decode_call_opcode_error() {
396412
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
397-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
398-
// Stone| CALL| Add2| JumpRel| Op1| FP| FP| FP
413+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
414+
// Stone| CALL| REGULAR| JumpRel| Op1| IMM| AP| AP
399415
// 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0
400416
// 0001 0001 0000 0100 = 0x1104; off0 = 1, off1 = 1
401417
let error = decode_instruction(0x1104800180018001);
@@ -406,7 +422,7 @@ mod decoder_test {
406422
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
407423
fn decode_invalid_opcode_extension_error() {
408424
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
409-
// ... 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
425+
// 79 ... 17 16 15| 14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0
410426
// ???| CALL| Add2| JumpRel| Op1| IMM| FP| FP
411427
// 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0
412428
// 1001 0001 0000 0100 = 0x9104; off0 = 0, off1 = 1

vm/src/vm/errors/vm_errors.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ pub enum VirtualMachineError {
3434
MainScopeError(#[from] ExecScopeError),
3535
#[error(transparent)]
3636
Other(anyhow::Error),
37+
#[error("Reserved instruction bits must be 0")]
38+
NonZeroReservedBits,
3739
#[error("Instruction should be an int")]
3840
InvalidInstructionEncoding,
3941
#[error("Invalid op1_register value: {0}")]
40-
InvalidOp1Reg(u64),
42+
InvalidOp1Reg(u128),
4143
#[error("In immediate mode, off2 should be 1")]
4244
ImmShouldBe1,
4345
#[error("op0 must be known in double dereference")]
4446
UnknownOp0,
4547
#[error("Invalid ap_update value: {0}")]
46-
InvalidApUpdate(u64),
48+
InvalidApUpdate(u128),
4749
#[error("Invalid pc_update value: {0}")]
48-
InvalidPcUpdate(u64),
50+
InvalidPcUpdate(u128),
4951
#[error("Res.UNCONSTRAINED cannot be used with ApUpdate.ADD")]
5052
UnconstrainedResAdd,
5153
#[error("Res.UNCONSTRAINED cannot be used with PcUpdate.JUMP")]
@@ -71,11 +73,11 @@ pub enum VirtualMachineError {
7173
#[error("Couldn't get or load dst")]
7274
NoDst,
7375
#[error("Invalid res value: {0}")]
74-
InvalidRes(u64),
76+
InvalidRes(u128),
7577
#[error("Invalid opcode value: {0}")]
76-
InvalidOpcode(u64),
78+
InvalidOpcode(u128),
7779
#[error("Invalid opcode extension value: {0}")]
78-
InvalidOpcodeExtension(u64),
80+
InvalidOpcodeExtension(u128),
7981
#[error("This is not implemented")]
8082
NotImplemented,
8183
#[error("Inconsistent auto-deduction for {}, expected {}, got {:?}", (*.0).0, (*.0).1, (*.0).2)]

vm/src/vm/vm_core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ impl VirtualMachine {
452452
.segments
453453
.memory
454454
.get_integer(self.run_context.pc)?
455-
.to_u64()
455+
.to_u128()
456456
.ok_or(VirtualMachineError::InvalidInstructionEncoding)?;
457457
decode_instruction(instruction)
458458
}
@@ -4187,7 +4187,7 @@ mod tests {
41874187
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
41884188
fn decode_current_instruction_invalid_encoding() {
41894189
let mut vm = vm!();
4190-
vm.segments = segments![((0, 0), ("112233445566778899", 16))];
4190+
vm.segments = segments![((0, 0), ("112233445566778899112233445566778899", 16))];
41914191
assert_matches!(
41924192
vm.decode_current_instruction(),
41934193
Err(VirtualMachineError::InvalidInstructionEncoding)

0 commit comments

Comments
 (0)