Skip to content

Commit

Permalink
MOD: vm inf
Browse files Browse the repository at this point in the history
MOD: calldata order
MOD: fmt
  • Loading branch information
hongyuanyang-uu committed Dec 13, 2023
1 parent 1470efb commit 21b70b1
Show file tree
Hide file tree
Showing 26 changed files with 391 additions and 146 deletions.
2 changes: 1 addition & 1 deletion assembler/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub(crate) fn encode_to_binary(bundle: RelocatedAsmBundle) -> Result<BinaryProgr
binary_instructions.push(instruction);
binary_counter += asm.binary_length() as usize;
}
BinaryProgram::from_instructions(binary_instructions, Some(origin_asm), false)
BinaryProgram::from_instructions(binary_instructions, Some(origin_asm), true)
}

fn is_adjusted_operand(asm: &OlaAsmInstruction) -> bool {
Expand Down
199 changes: 197 additions & 2 deletions assembler/test_data/asm/fibo_loop.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,199 @@
{
"program": "main:\n.LBL0_0:\nadd r9 r9 4\nmstore [r9,-2] r9\nmov r1 10\ncall fib_non_recursive\nadd r9 r9 -4\nend\nfib_non_recursive:\n.LBL2_0:\nadd r9 r9 5\nmov r0 r1\nmstore [r9,-1] r0\nmov r0 0\nmstore [r9,-2] r0\nmov r0 1\nmstore [r9,-3] r0\nmov r0 1\nmstore [r9,-4] r0\nmov r0 2\nmstore [r9,-5] r0\njmp .LBL2_1\n.LBL2_1:\nmload r0 [r9,-5]\nmload r1 [r9,-1]\ngte r0 r1 r0\ncjmp r0 .LBL2_2\njmp .LBL2_4\n.LBL2_2:\nmload r1 [r9,-2]\nmload r2 [r9,-3]\nadd r0 r1 r2\nmstore [r9,-4] r0\nmload r0 [r9,-3]\nmstore [r9,-2] r0\nmload r0 [r9,-4]\nmstore [r9,-3] r0\njmp .LBL2_3\n.LBL2_3:\nmload r1 [r9,-5]\nadd r0 r1 1\nmstore [r9,-5] r0\njmp .LBL2_1\n.LBL2_4:\nmload r0 [r9,-4]\nadd r9 r9 -5\nret",
"prophets": []
"program": "chain_id:\n.LBL8_0:\n add r9 r9 2\n mov r0 7\n mstore [r9,-1] r0\n mload r0 [r9,-1]\n add r1 r9 -2\n mov r2 0\n tload r1 r2 r0\n mload r0 [r9,-2]\n add r9 r9 -2\n ret\nexe_address:\n.LBL9_0:\n add r9 r9 6\n mov r0 8\n mstore [r9,-1] r0\n mload r1 [r9,-1]\n.PROPHET9_0:\n mov r0 psp\n mload r0 [r0]\n mstore [r9,-6] r0\n mload r0 [r9,-6]\n mload r1 [r9,-1]\n mov r2 1\n tload r0 r2 r1\n mload r0 [r9,-6]\n mload r0 [r0]\n mstore [r9,-5] r0\n mload r0 [r9,-6]\n mload r0 [r0,+1]\n mstore [r9,-4] r0\n mload r0 [r9,-6]\n mload r0 [r0,+2]\n mstore [r9,-3] r0\n mload r0 [r9,-6]\n mload r0 [r0,+3]\n mstore [r9,-2] r0\n mload r0 [r9,-6]\n add r9 r9 -6\n ret\nfib_non_recursive:\n.LBL10_0:\n add r9 r9 5\n mov r0 r1\n mstore [r9,-1] r0\n mload r0 [r9,-1]\n eq r0 r0 0\n gte r1 r0 0\n neq r0 r0 0\n and r1 r1 r0\n cjmp r1 .LBL10_1\n jmp .LBL10_2\n.LBL10_1:\n mov r0 0\n add r9 r9 -5\n ret\n.LBL10_2:\n jmp .LBL10_3\n.LBL10_3:\n mov r0 0\n mstore [r9,-2] r0\n mov r0 1\n mstore [r9,-3] r0\n mov r0 2\n mstore [r9,-4] r0\n mov r0 2\n mstore [r9,-5] r0\n jmp .LBL10_4\n.LBL10_4:\n mload r0 [r9,-5]\n mload r1 [r9,-1]\n gte r0 r1 r0\n gte r1 r0 0\n neq r0 r0 0\n and r1 r1 r0\n cjmp r1 .LBL10_5\n jmp .LBL10_6\n.LBL10_5:\n mload r1 [r9,-2]\n mload r2 [r9,-3]\n add r0 r1 r2\n mstore [r9,-4] r0\n mload r1 [r9,-4]\n mov r2 3\n.PROPHET10_0:\n mload r0 [r9,-3]\n mstore [r9,-2] r0\n mload r0 [r9,-4]\n mstore [r9,-3] r0\n mload r1 [r9,-5]\n mov r2 3\n.PROPHET10_1:\n mload r0 [r9,-5]\n add r3 r0 1\n mstore [r9,-5] r3\n jmp .LBL10_4\n.LBL10_6:\n mload r0 [r9,-4]\n add r9 r9 -5\n ret\nbench_fib_non_recursive:\n.LBL11_0:\n add r9 r9 11\n mstore [r9,-2] r9\n mov r0 r1\n mov r1 r2\n mstore [r9,-3] r0\n mstore [r9,-4] r1\n mov r0 0\n mstore [r9,-5] r0\n mload r1 [r9,-3]\n mov r2 3\n.PROPHET11_0:\n mload r1 [r9,-4]\n mov r2 3\n.PROPHET11_1:\n mov r0 0\n mstore [r9,-6] r0\n jmp .LBL11_1\n.LBL11_1:\n mload r0 [r9,-6]\n mload r1 [r9,-4]\n gte r2 r1 r0\n neq r0 r0 r1\n and r2 r2 r0\n gte r0 r2 0\n neq r1 r2 0\n and r0 r0 r1\n cjmp r0 .LBL11_2\n jmp .LBL11_3\n.LBL11_2:\n mload r1 [r9,-3]\n call fib_non_recursive\n mstore [r9,-7] r0\n mload r1 [r9,-7]\n mov r2 3\n.PROPHET11_2:\n mload r0 [r9,-5]\n add r0 r0 1\n mstore [r9,-9] r0\n mload r0 [r9,-9]\n mstore [r9,-5] r0\n mload r1 [r9,-5]\n mov r2 3\n.PROPHET11_3:\n mload r0 [r9,-6]\n add r0 r0 1\n mstore [r9,-8] r0\n mload r0 [r9,-8]\n mstore [r9,-6] r0\n jmp .LBL11_1\n.LBL11_3:\n mload r0 [r9,-5]\n add r9 r9 -11\n ret\nmain:\n.LBL12_0:\n add r9 r9 10\n mstore [r9,-2] r9\n mov r0 14\n mstore [r9,-3] r0\n mov r0 1\n mstore [r9,-8] r0\n mload r0 [r9,-3]\n mstore [r9,-5] r0\n mload r1 [r9,-5]\n.PROPHET12_0:\n mov r0 psp\n mload r0 [r0]\n mstore [r9,-6] r0\n mload r0 [r9,-6]\n mload r1 [r9,-5]\n mov r3 0\n not r1 r1\n add r1 r1 1\n add r1 r3 r1\n add r0 r0 r1\n mstore [r9,-7] r0\n mload r0 [r9,-7]\n mload r1 [r9,-5]\n mov r3 1\n tload r0 r3 r1\n mload r0 [r9,-7]\n mload r0 [r0]\n mstore [r9,-5] r0\n mload r0 [r9,-7]\n add r0 r0 1\n mstore [r9,-7] r0\n mload r0 [r9,-7]\n mload r0 [r0]\n mstore [r9,-4] r0\n mload r0 [r9,-5]\n mload r1 [r9,-3]\n add r2 r0 r1\n mstore [r9,-5] r2\n mload r1 [r9,-5]\n.PROPHET12_1:\n mov r0 psp\n mload r0 [r0]\n mstore [r9,-6] r0\n mload r0 [r9,-6]\n mload r1 [r9,-5]\n mov r2 0\n not r1 r1\n add r1 r1 1\n add r1 r2 r1\n add r0 r0 r1\n mstore [r9,-7] r0\n mload r0 [r9,-7]\n mload r1 [r9,-5]\n mov r2 1\n tload r0 r2 r1\n mload r0 [r9,-4]\n eq r1 r0 1015130275\n cjmp r1 .LBL12_1\n jmp .LBL12_2\n.LBL12_1:\n mload r0 [r9,-7]\n mload r1 [r0,+0]\n mload r0 [r9,-7]\n mload r2 [r0,+1]\n call bench_fib_non_recursive\n mov r2 r0\n mov r1 2\n.PROPHET12_2:\n mov r0 psp\n mload r0 [r0]\n mov r1 1\n mstore [r0,-1] r1\n mstore [r0,-2] r2\n add r0 r0 -2\n tstore r0 2\n jmp .LBL12_3\n.LBL12_2:\n jmp .LBL12_3\n.LBL12_3:\n mov r0 0\n add r9 r9 -10\n end\n",
"prophets": [
{
"label": ".PROPHET9_0",
"code": "%{\n entry() {\n cid.addr = malloc(cid.len);\n }\n%}",
"inputs": [
{
"name": "cid.len",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": [
{
"name": "cid.addr",
"length": 1,
"is_ref": false,
"is_input_output": false
}
]
},
{
"label": ".PROPHET10_0",
"code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}",
"inputs": [
{
"name": "cid.base",
"length": 1,
"is_ref": false,
"is_input_output": false
},
{
"name": "cid.flag",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": []
},
{
"label": ".PROPHET10_1",
"code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}",
"inputs": [
{
"name": "cid.base",
"length": 1,
"is_ref": false,
"is_input_output": false
},
{
"name": "cid.flag",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": []
},
{
"label": ".PROPHET11_0",
"code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}",
"inputs": [
{
"name": "cid.base",
"length": 1,
"is_ref": false,
"is_input_output": false
},
{
"name": "cid.flag",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": []
},
{
"label": ".PROPHET11_1",
"code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}",
"inputs": [
{
"name": "cid.base",
"length": 1,
"is_ref": false,
"is_input_output": false
},
{
"name": "cid.flag",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": []
},
{
"label": ".PROPHET11_2",
"code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}",
"inputs": [
{
"name": "cid.base",
"length": 1,
"is_ref": false,
"is_input_output": false
},
{
"name": "cid.flag",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": []
},
{
"label": ".PROPHET11_3",
"code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}",
"inputs": [
{
"name": "cid.base",
"length": 1,
"is_ref": false,
"is_input_output": false
},
{
"name": "cid.flag",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": []
},
{
"label": ".PROPHET12_0",
"code": "%{\n entry() {\n cid.addr = malloc(cid.len);\n }\n%}",
"inputs": [
{
"name": "cid.len",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": [
{
"name": "cid.addr",
"length": 1,
"is_ref": false,
"is_input_output": false
}
]
},
{
"label": ".PROPHET12_1",
"code": "%{\n entry() {\n cid.addr = malloc(cid.len);\n }\n%}",
"inputs": [
{
"name": "cid.len",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": [
{
"name": "cid.addr",
"length": 1,
"is_ref": false,
"is_input_output": false
}
]
},
{
"label": ".PROPHET12_2",
"code": "%{\n entry() {\n cid.addr = malloc(cid.len);\n }\n%}",
"inputs": [
{
"name": "cid.len",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": [
{
"name": "cid.addr",
"length": 1,
"is_ref": false,
"is_input_output": false
}
]
}
]
}
14 changes: 6 additions & 8 deletions circuits/benches/fibo_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use circuits::stark::verifier::verify_proof;
use core::merkle_tree::tree::AccountTree;
use core::program::Program;
use core::types::{Field, GoldilocksField};
use core::vm::transaction::init_tx_context;
use core::vm::transaction::init_tx_context_mock;
use core::vm::vm_state::Address;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use executor::load_tx::init_tape;
Expand Down Expand Up @@ -43,7 +43,7 @@ pub fn test_by_asm_json(path: String) {
let mut process = Process::new();
let now = Instant::now();

let calldata = [47u64, 1000u64, 2u64, 4185064725u64]
let calldata = [4185064725u64, 2u64, 47u64, 1000u64]
.iter()
.map(|v| GoldilocksField::from_canonical_u64(*v))
.collect_vec();
Expand All @@ -54,14 +54,12 @@ pub fn test_by_asm_json(path: String) {
Address::default(),
Address::default(),
Address::default(),
&init_tx_context(),
&init_tx_context_mock(),
);

let _ = process.execute(
&mut program,
&mut Some(prophets),
&mut AccountTree::new_test(),
);
program.prophets = prophets;

let _ = process.execute(&mut program, &mut AccountTree::new_test());
info!(
"exec time:{}, len:{}",
now.elapsed().as_millis(),
Expand Down
13 changes: 5 additions & 8 deletions circuits/benches/sqrt_prophet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use circuits::stark::verifier::verify_proof;
use core::merkle_tree::tree::AccountTree;
use core::program::Program;
use core::types::{Field, GoldilocksField};
use core::vm::transaction::init_tx_context;
use core::vm::transaction::init_tx_context_mock;
use core::vm::vm_state::Address;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use executor::load_tx::init_tape;
Expand Down Expand Up @@ -42,7 +42,7 @@ pub fn test_by_asm_json(path: String) {
let mut process = Process::new();
let now = Instant::now();

let calldata = [1073741824u64, 16000u64, 2u64, 3509365327u64]
let calldata = [3509365327u64, 2u64, 1073741824u64, 16000u64]
.iter()
.map(|v| GoldilocksField::from_canonical_u64(*v))
.collect_vec();
Expand All @@ -53,14 +53,11 @@ pub fn test_by_asm_json(path: String) {
Address::default(),
Address::default(),
Address::default(),
&init_tx_context(),
&init_tx_context_mock(),
);

let _ = process.execute(
&mut program,
&mut Some(prophets),
&mut AccountTree::new_test(),
);
program.prophets = prophets;
let _ = process.execute(&mut program, &mut AccountTree::new_test());
info!("exec time:{}", now.elapsed().as_millis());
let mut ola_stark = OlaStark::default();
let now = Instant::now();
Expand Down
7 changes: 2 additions & 5 deletions circuits/src/builtins/bitwise/bitwise_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,10 @@ mod tests {
program.instructions.push(inst.to_string());
}

program.prophets = prophets;
let mut process = Process::new();
process.addr_storage = Address::default();
let _ = process.execute(
&mut program,
&mut Some(prophets),
&mut AccountTree::new_test(),
);
let _ = process.execute(&mut program, &mut AccountTree::new_test());

let (rows, bitwise_beta) =
generate_bitwise_trace::<F>(&program.trace.builtin_bitwise_combined);
Expand Down
8 changes: 2 additions & 6 deletions circuits/src/builtins/rangecheck/rangecheck_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,14 @@ mod tests {
}

let mut program: Program = Program::default();

program.prophets = prophets;
for inst in instructions {
program.instructions.push(inst.to_string());
}

let mut process = Process::new();
process.addr_storage = Address::default();
let _ = process.execute(
&mut program,
&mut Some(prophets),
&mut AccountTree::new_test(),
);
let _ = process.execute(&mut program, &mut AccountTree::new_test());

let rows = generate_rc_trace::<F>(&program.trace.builtin_rangecheck);
let len = rows[0].len();
Expand Down
8 changes: 4 additions & 4 deletions circuits/src/cpu/cpu_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,19 +1035,19 @@ mod tests {
#[test]
fn test_ola_vote() {
let db_name = "vote_test".to_string();
let init_calldata = [3u64, 1u64, 2u64, 3u64, 4u64, 2817135588u64]
let init_calldata = [2817135588u64, 4u64, 3u64, 1u64, 2u64, 3u64]
.iter()
.map(|v| GoldilocksField::from_canonical_u64(*v))
.collect_vec();
let vote_calldata = [2u64, 1u64, 2791810083u64]
let vote_calldata = [2791810083u64, 1u64, 2u64]
.iter()
.map(|v| GoldilocksField::from_canonical_u64(*v))
.collect_vec();
let winning_proposal_calldata = [0u64, 3186728800u64]
let winning_proposal_calldata = [3186728800u64, 0u64]
.iter()
.map(|v| GoldilocksField::from_canonical_u64(*v))
.collect_vec();
let winning_name_calldata = [0u64, 363199787u64]
let winning_name_calldata = [363199787u64, 0u64]
.iter()
.map(|v| GoldilocksField::from_canonical_u64(*v))
.collect_vec();
Expand Down
7 changes: 4 additions & 3 deletions circuits/src/generation/ctl_test/debug_trace_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use core::{
crypto::{hash::Hasher, ZkHasher},
merkle_tree::log::{StorageLog, WitnessStorageLog},
types::merkle_tree::{encode_addr, tree_key_default},
vm::transaction::init_tx_context,
vm::transaction::init_tx_context_mock,
};
use executor::{
load_tx::init_tape,
Expand Down Expand Up @@ -221,7 +221,7 @@ pub fn get_exec_trace(
caller_addr,
callee,
callee_exe_addr,
&init_tx_context(),
&init_tx_context_mock(),
);
}

Expand All @@ -245,7 +245,8 @@ pub fn get_exec_trace(
previous_value: tree_key_default(),
});

let res = process.execute(&mut program, &mut Some(prophets), &mut db);
program.prophets = prophets;
let res = process.execute(&mut program, &mut db);
match res {
Ok(_) => {}
Err(e) => {
Expand Down
8 changes: 4 additions & 4 deletions circuits/src/generation/ctl_test/poseidon_chunk_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ fn print_poseidon_chunk_mem_ctl_info() {
let program_file_name: String = "vote.json".to_string();
let db_name = "vote_test".to_string();

let init_calldata = [3u64, 1u64, 2u64, 3u64, 4u64, 2817135588u64]
let init_calldata = [2817135588u64, 4u64, 3u64, 1u64, 2u64, 3u64]
.iter()
.map(|v| GoldilocksField::from_canonical_u64(*v))
.collect_vec();
let vote_calldata = [2u64, 1u64, 2791810083u64]
let vote_calldata = [2791810083u64, 1u64, 2u64]
.iter()
.map(|v| GoldilocksField::from_canonical_u64(*v))
.collect_vec();
let winning_proposal_calldata = [0u64, 3186728800u64]
let winning_proposal_calldata = [3186728800u64, 0u64]
.iter()
.map(|v| GoldilocksField::from_canonical_u64(*v))
.collect_vec();
let winning_name_calldata = [0u64, 363199787u64]
let winning_name_calldata = [363199787u64, 0u64]
.iter()
.map(|v| GoldilocksField::from_canonical_u64(*v))
.collect_vec();
Expand Down
Loading

0 comments on commit 21b70b1

Please sign in to comment.