Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Sep 8, 2024
1 parent 866c81b commit 5b15916
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 53 deletions.
6 changes: 6 additions & 0 deletions avm-transpiler/src/bit_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ impl BitsQueryable for u128 {
}
}

impl BitsQueryable for usize {
fn num_bits(&self) -> usize {
get_msb(*self as u128)
}
}

pub fn bits_needed_for<T: BitsQueryable>(val: &T) -> usize {
let num_bits = val.num_bits();
if num_bits < 8 {
Expand Down
8 changes: 4 additions & 4 deletions avm-transpiler/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub enum AvmOpcode {
L2GASLEFT,
DAGASLEFT,
// Control flow
JUMP,
JUMPI,
JUMP_16,
JUMPI_16,
INTERNALCALL,
INTERNALRETURN,
// Memory
Expand Down Expand Up @@ -129,8 +129,8 @@ impl AvmOpcode {
AvmOpcode::L2GASLEFT => "L2GASLEFT",
AvmOpcode::DAGASLEFT => "DAGASLEFT",
// Machine State - Internal Control Flow
AvmOpcode::JUMP => "JUMP",
AvmOpcode::JUMPI => "JUMPI",
AvmOpcode::JUMP_16 => "JUMP_16",
AvmOpcode::JUMPI_16 => "JUMPI_16",
AvmOpcode::INTERNALCALL => "INTERNALCALL",
AvmOpcode::INTERNALRETURN => "INTERNALRETURN",
// Machine State - Memory
Expand Down
11 changes: 4 additions & 7 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,17 @@ pub fn brillig_to_avm(
BrilligOpcode::Jump { location } => {
let avm_loc = brillig_pcs_to_avm_pcs[*location];
avm_instrs.push(AvmInstruction {
opcode: AvmOpcode::JUMP,
operands: vec![AvmOperand::U32 { value: avm_loc as u32 }],
opcode: AvmOpcode::JUMP_16,
operands: vec![make_operand(16, &avm_loc)],
..Default::default()
});
}
BrilligOpcode::JumpIf { condition, location } => {
let avm_loc = brillig_pcs_to_avm_pcs[*location];
avm_instrs.push(AvmInstruction {
opcode: AvmOpcode::JUMPI,
opcode: AvmOpcode::JUMPI_16,
indirect: Some(ALL_DIRECT),
operands: vec![
AvmOperand::U32 { value: avm_loc as u32 },
AvmOperand::U32 { value: condition.to_usize() as u32 },
],
operands: vec![make_operand(16, &avm_loc), make_operand(16, &condition.0)],
..Default::default()
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ TEST_F(AvmExecutionTests, jumpAndCalldatacopy)
"00000000" // cd_offset
"00000002" // copy_size
"0000000A" // dst_offset // M[10] = 13, M[11] = 156
+ to_hex(OpCode::JUMP) + // opcode JUMP
"00000003" // jmp_dest (FDIV located at 3)
+ to_hex(OpCode::JUMP_16) + // opcode JUMP
"0003" // jmp_dest (FDIV located at 3)
+ to_hex(OpCode::SUB) + // opcode SUB
"00" // Indirect flag
"06" // FF
Expand Down Expand Up @@ -444,8 +444,8 @@ TEST_F(AvmExecutionTests, jumpAndCalldatacopy)

// JUMP
EXPECT_THAT(instructions.at(1),
AllOf(Field(&Instruction::op_code, OpCode::JUMP),
Field(&Instruction::operands, ElementsAre(VariantWith<uint32_t>(3)))));
AllOf(Field(&Instruction::op_code, OpCode::JUMP_16),
Field(&Instruction::operands, ElementsAre(VariantWith<uint16_t>(3)))));

std::vector<FF> returndata{};
auto trace = Execution::gen_trace(instructions, returndata, std::vector<FF>{ 13, 156 }, public_inputs_vec);
Expand Down Expand Up @@ -490,10 +490,10 @@ TEST_F(AvmExecutionTests, jumpiAndCalldatacopy)
"02" // U16
"0014" // val 20
"0065" // dst_offset 101
+ to_hex(OpCode::JUMPI) + // opcode JUMPI
+ to_hex(OpCode::JUMPI_16) + // opcode JUMPI
"00" // Indirect flag
"00000004" // jmp_dest (MUL located at 4)
"0000000A" // cond_offset 10
"0004" // jmp_dest (MUL located at 4)
"000A" // cond_offset 10
+ to_hex(OpCode::ADD) + // opcode ADD
"00" // Indirect flag
"02" // U16
Expand Down Expand Up @@ -522,9 +522,9 @@ TEST_F(AvmExecutionTests, jumpiAndCalldatacopy)
// JUMPI
EXPECT_THAT(
instructions.at(2),
AllOf(Field(&Instruction::op_code, OpCode::JUMPI),
AllOf(Field(&Instruction::op_code, OpCode::JUMPI_16),
Field(&Instruction::operands,
ElementsAre(VariantWith<uint8_t>(0), VariantWith<uint32_t>(4), VariantWith<uint32_t>(10)))));
ElementsAre(VariantWith<uint8_t>(0), VariantWith<uint16_t>(4), VariantWith<uint16_t>(10)))));

std::vector<FF> returndata{};
auto trace_jump = Execution::gen_trace(instructions, returndata, std::vector<FF>{ 9873123 }, public_inputs_vec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ const std::unordered_map<OpCode, std::vector<OperandType>> OPCODE_WIRE_FORMAT =
{ OpCode::DAGASLEFT, getter_format },

// Machine State - Internal Control Flow
{ OpCode::JUMP, { OperandType::UINT32 } },
{ OpCode::JUMPI, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } },
{ OpCode::JUMP_16, { OperandType::UINT16 } },
{ OpCode::JUMPI_16, { OperandType::INDIRECT, OperandType::UINT16, OperandType::UINT16 } },
{ OpCode::INTERNALCALL, { OperandType::UINT32 } },
{ OpCode::INTERNALRETURN, {} },

Expand Down
10 changes: 5 additions & 5 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,13 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
break;

// Machine State - Internal Control Flow
case OpCode::JUMP:
trace_builder.op_jump(std::get<uint32_t>(inst.operands.at(0)));
case OpCode::JUMP_16:
trace_builder.op_jump(std::get<uint16_t>(inst.operands.at(0)));
break;
case OpCode::JUMPI:
case OpCode::JUMPI_16:
trace_builder.op_jumpi(std::get<uint8_t>(inst.operands.at(0)),
std::get<uint32_t>(inst.operands.at(1)),
std::get<uint32_t>(inst.operands.at(2)));
std::get<uint16_t>(inst.operands.at(1)),
std::get<uint16_t>(inst.operands.at(2)));
break;
case OpCode::INTERNALCALL:
trace_builder.op_internal_call(std::get<uint32_t>(inst.operands.at(0)));
Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/fixed_gas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const std::unordered_map<OpCode, FixedGasTable::GasRow> GAS_COST_TABLE = {
{ OpCode::CALLDATACOPY, make_cost(AVM_CALLDATACOPY_BASE_L2_GAS, 0, AVM_CALLDATACOPY_DYN_L2_GAS, 0) },
{ OpCode::L2GASLEFT, make_cost(AVM_L2GASLEFT_BASE_L2_GAS, 0, AVM_L2GASLEFT_DYN_L2_GAS, 0) },
{ OpCode::DAGASLEFT, make_cost(AVM_DAGASLEFT_BASE_L2_GAS, 0, AVM_DAGASLEFT_DYN_L2_GAS, 0) },
{ OpCode::JUMP, make_cost(AVM_JUMP_BASE_L2_GAS, 0, AVM_JUMP_DYN_L2_GAS, 0) },
{ OpCode::JUMPI, make_cost(AVM_JUMPI_BASE_L2_GAS, 0, AVM_JUMPI_DYN_L2_GAS, 0) },
{ OpCode::JUMP_16, make_cost(AVM_JUMP_BASE_L2_GAS, 0, AVM_JUMP_DYN_L2_GAS, 0) },
{ OpCode::JUMPI_16, make_cost(AVM_JUMPI_BASE_L2_GAS, 0, AVM_JUMPI_DYN_L2_GAS, 0) },
{ OpCode::INTERNALCALL, make_cost(AVM_INTERNALCALL_BASE_L2_GAS, 0, AVM_INTERNALCALL_DYN_L2_GAS, 0) },
{ OpCode::INTERNALRETURN, make_cost(AVM_INTERNALRETURN_BASE_L2_GAS, 0, AVM_INTERNALRETURN_DYN_L2_GAS, 0) },
{ OpCode::SET_8, make_cost(AVM_SET_BASE_L2_GAS, 0, AVM_SET_DYN_L2_GAS, 0) },
Expand Down
8 changes: 4 additions & 4 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/opcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ std::string to_string(OpCode opcode)
case OpCode::DAGASLEFT:
return "DAGASLEFT";
// Machine State - Internal Control Flow
case OpCode::JUMP:
return "JUMP";
case OpCode::JUMPI:
return "JUMPI";
case OpCode::JUMP_16:
return "JUMP_16";
case OpCode::JUMPI_16:
return "JUMPI_16";
case OpCode::INTERNALCALL:
return "INTERNALCALL";
case OpCode::INTERNALRETURN:
Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/opcode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ enum class OpCode : uint8_t {
L2GASLEFT,
DAGASLEFT,
// Machine State - Internal Control Flow
JUMP,
JUMPI,
JUMP_16,
JUMPI_16,
INTERNALCALL,
INTERNALRETURN,
// Machine State - Memory
Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ void AvmTraceBuilder::op_jump(uint32_t jmp_dest)
auto clk = static_cast<uint32_t>(main_trace.size()) + 1;

// Constrain gas cost
gas_trace_builder.constrain_gas(clk, OpCode::JUMP);
gas_trace_builder.constrain_gas(clk, OpCode::JUMP_16);

main_trace.push_back(Row{
.main_clk = clk,
Expand Down Expand Up @@ -1602,7 +1602,7 @@ void AvmTraceBuilder::op_jumpi(uint8_t indirect, uint32_t jmp_dest, uint32_t con
uint32_t next_pc = !id_zero ? jmp_dest : pc + 1;

// Constrain gas cost
gas_trace_builder.constrain_gas(clk, OpCode::JUMPI);
gas_trace_builder.constrain_gas(clk, OpCode::JUMPI_16);

main_trace.push_back(Row{
.main_clk = clk,
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/simulator/src/avm/avm_gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ const BaseGasCosts: Record<Opcode, Gas> = {
[Opcode.CALLDATACOPY]: makeCost(c.AVM_CALLDATACOPY_BASE_L2_GAS, 0),
[Opcode.L2GASLEFT]: makeCost(c.AVM_L2GASLEFT_BASE_L2_GAS, 0),
[Opcode.DAGASLEFT]: makeCost(c.AVM_DAGASLEFT_BASE_L2_GAS, 0),
[Opcode.JUMP]: makeCost(c.AVM_JUMP_BASE_L2_GAS, 0),
[Opcode.JUMPI]: makeCost(c.AVM_JUMPI_BASE_L2_GAS, 0),
[Opcode.JUMP_16]: makeCost(c.AVM_JUMP_BASE_L2_GAS, 0),
[Opcode.JUMPI_16]: makeCost(c.AVM_JUMPI_BASE_L2_GAS, 0),
[Opcode.INTERNALCALL]: makeCost(c.AVM_INTERNALCALL_BASE_L2_GAS, 0),
[Opcode.INTERNALRETURN]: makeCost(c.AVM_INTERNALRETURN_BASE_L2_GAS, 0),
[Opcode.SET_8]: makeCost(c.AVM_SET_BASE_L2_GAS, 0),
Expand Down Expand Up @@ -157,8 +157,8 @@ const DynamicGasCosts: Record<Opcode, Gas> = {
[Opcode.CALLDATACOPY]: makeCost(c.AVM_CALLDATACOPY_DYN_L2_GAS, 0),
[Opcode.L2GASLEFT]: makeCost(c.AVM_L2GASLEFT_DYN_L2_GAS, 0),
[Opcode.DAGASLEFT]: makeCost(c.AVM_DAGASLEFT_DYN_L2_GAS, 0),
[Opcode.JUMP]: makeCost(c.AVM_JUMP_DYN_L2_GAS, 0),
[Opcode.JUMPI]: makeCost(c.AVM_JUMPI_DYN_L2_GAS, 0),
[Opcode.JUMP_16]: makeCost(c.AVM_JUMP_DYN_L2_GAS, 0),
[Opcode.JUMPI_16]: makeCost(c.AVM_JUMPI_DYN_L2_GAS, 0),
[Opcode.INTERNALCALL]: makeCost(c.AVM_INTERNALCALL_DYN_L2_GAS, 0),
[Opcode.INTERNALRETURN]: makeCost(c.AVM_INTERNALRETURN_DYN_L2_GAS, 0),
[Opcode.SET_8]: makeCost(c.AVM_SET_DYN_L2_GAS, 0),
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/simulator/src/avm/opcodes/control_flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ describe('Control Flow Opcodes', () => {
it('Should (de)serialize correctly', () => {
const buf = Buffer.from([
Jump.opcode, // opcode
...Buffer.from('12345678', 'hex'), // loc
...Buffer.from('1234', 'hex'), // loc
]);
const inst = new Jump(/*loc=*/ 0x12345678);
const inst = new Jump(/*loc=*/ 0x1234);

expect(Jump.deserialize(buf)).toEqual(inst);
expect(inst.serialize()).toEqual(buf);
Expand All @@ -39,10 +39,10 @@ describe('Control Flow Opcodes', () => {
const buf = Buffer.from([
JumpI.opcode, // opcode
0x01, // indirect
...Buffer.from('12345678', 'hex'), // loc
...Buffer.from('a2345678', 'hex'), // condOffset
...Buffer.from('1234', 'hex'), // loc
...Buffer.from('a234', 'hex'), // condOffset
]);
const inst = new JumpI(/*indirect=*/ 1, /*loc=*/ 0x12345678, /*condOffset=*/ 0xa2345678);
const inst = new JumpI(/*indirect=*/ 1, /*loc=*/ 0x1234, /*condOffset=*/ 0xa234);

expect(JumpI.deserialize(buf)).toEqual(inst);
expect(inst.serialize()).toEqual(buf);
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/simulator/src/avm/opcodes/control_flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { Instruction } from './instruction.js';

export class Jump extends Instruction {
static type: string = 'JUMP';
static readonly opcode: Opcode = Opcode.JUMP;
static readonly opcode: Opcode = Opcode.JUMP_16;
// Informs (de)serialization. See Instruction.deserialize.
static readonly wireFormat: OperandType[] = [OperandType.UINT8, OperandType.UINT32];
static readonly wireFormat: OperandType[] = [OperandType.UINT8, OperandType.UINT16];

constructor(private jumpOffset: number) {
super();
Expand All @@ -26,14 +26,14 @@ export class Jump extends Instruction {

export class JumpI extends Instruction {
static type: string = 'JUMPI';
static readonly opcode: Opcode = Opcode.JUMPI;
static readonly opcode: Opcode = Opcode.JUMPI_16;

// Instruction wire format with opcode.
static readonly wireFormat: OperandType[] = [
OperandType.UINT8,
OperandType.UINT8,
OperandType.UINT32,
OperandType.UINT32,
OperandType.UINT16,
OperandType.UINT16,
];

constructor(private indirect: number, private loc: number, private condOffset: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export enum Opcode {
L2GASLEFT,
DAGASLEFT,
// Control flow
JUMP,
JUMPI,
JUMP_16,
JUMPI_16,
INTERNALCALL,
INTERNALRETURN,
// Memory
Expand Down

0 comments on commit 5b15916

Please sign in to comment.