Skip to content

Commit

Permalink
use new write_io n syntax in all test & example programs
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Nov 7, 2023
1 parent c247d7a commit 6276aa9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
4 changes: 2 additions & 2 deletions triton-vm/src/example_programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn fibonacci_sequence() -> Program {

// pop zero, write result
pop 1
write_io
write_io 1
halt

// before: ⊥ 0 1 i
Expand Down Expand Up @@ -74,7 +74,7 @@ fn greatest_common_divisor() -> Program {

terminate:
// _ d n where d == 0
write_io // _ d
write_io 1 // _ d
halt
)
}
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ mod tests {

#[test]
fn stringify_some_instructions() {
let instructions = triton_asm!(push 3 invert push 2 mul push 1 add write_io halt);
let instructions = triton_asm!(push 3 invert push 2 mul push 1 add write_io 1 halt);
let code = stringify_instructions(&instructions);
println!("{code}");
}
Expand Down
15 changes: 8 additions & 7 deletions triton-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! read_io 1 // n
//! push 1 // n 1
//! call factorial // 0 n!
//! write_io // 0
//! write_io 1 // 0
//! halt
//!
//! factorial: // n acc
Expand Down Expand Up @@ -170,15 +170,15 @@ pub mod vm;
/// let program = triton_program!(
/// read_io 1 push 5 mul
/// call check_eq_15
/// push 1 write_io
/// push 17 write_io 1
/// halt
/// // assert that the top of the stack is 15
/// check_eq_15:
/// push 15 eq assert
/// return
/// );
/// let output = program.run(vec![3].into(), [].into()).unwrap();
/// assert_eq!(1, output[0].value());
/// assert_eq!(17, output[0].value());
/// ```
///
/// Any type with an appropriate [`Display`](std::fmt::Display) implementation can be
Expand Down Expand Up @@ -613,7 +613,7 @@ mod tests {
push 51 read_mem
push 42 read_mem
swap 1 swap 2 mul
write_io halt
write_io 1 halt
);

let initial_ram = [(42, 17), (51, 13)].into();
Expand Down Expand Up @@ -747,15 +747,16 @@ mod tests {

#[test]
fn nested_triton_asm_interpolation() {
let triple_write = triton_asm![write_io; 3];
let double_write = triton_asm![write_io 1; 2];
let quadruple_write = triton_asm!({&double_write} write_io 2);
let snippet_0 = triton_asm!(push 7 nop call my_label);
let snippet_1 = triton_asm!(pop 2 halt my_label: push 8 {&triple_write});
let snippet_1 = triton_asm!(pop 2 halt my_label: push 8 push 9 {&quadruple_write});
let source_code = triton_asm!(push 6 {&snippet_0} {&snippet_1} halt);

let program = triton_program!({ &source_code });
let public_output = program.run([].into(), [].into()).unwrap();

let expected_output = [8, 7, 6].map(BFieldElement::new).to_vec();
let expected_output = [9, 8, 7, 6].map(BFieldElement::new).to_vec();
assert_eq!(expected_output, public_output);
}

Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ pub(crate) mod tests {
#[test]
fn check_io_terminals() {
let read_nop_program = triton_program!(
read_io 3 nop nop write_io push 17 write_io halt
read_io 3 nop nop write_io 2 push 17 write_io 1 halt
);
let mut program_and_input = ProgramAndInput::without_input(read_nop_program);
program_and_input.public_input = vec![3, 5, 7];
Expand Down
59 changes: 30 additions & 29 deletions triton-vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1762,7 +1762,7 @@ pub(crate) mod tests {

pub(crate) fn test_program_for_read_io_write_io() -> ProgramAndInput {
let program = triton_program!(
read_io 1 assert read_io 2 dup 1 dup 1 add write_io mul write_io halt
read_io 1 assert read_io 2 dup 1 dup 1 add write_io 1 mul push 5 write_io 2 halt
);
ProgramAndInput {
program,
Expand All @@ -1784,7 +1784,7 @@ pub(crate) mod tests {
push {left_operand.coefficients[1]}
push {left_operand.coefficients[0]}
xxadd
write_io write_io write_io
write_io 3
halt
);
let program = ProgramAndInput {
Expand Down Expand Up @@ -1812,7 +1812,7 @@ pub(crate) mod tests {
push {left_operand.coefficients[1]}
push {left_operand.coefficients[0]}
xxmul
write_io write_io write_io
write_io 3
halt
);
let program = ProgramAndInput {
Expand All @@ -1838,7 +1838,7 @@ pub(crate) mod tests {
push {operand.coefficients[1]}
push {operand.coefficients[0]}
xinvert
write_io write_io write_io
write_io 3
halt
);
let program = ProgramAndInput {
Expand All @@ -1861,7 +1861,7 @@ pub(crate) mod tests {
push {operand.coefficients[0]}
push {scalar}
xbmul
write_io write_io write_io
write_io 3
halt
);
let program = ProgramAndInput {
Expand All @@ -1882,7 +1882,7 @@ pub(crate) mod tests {
#[strategy(arb())] subtrahend: BFieldElement,
) {
let program = triton_program!(
push {subtrahend} push {minuend} call sub write_io halt
push {subtrahend} push {minuend} call sub write_io 1 halt
sub:
swap 1 push -1 mul add return
);
Expand All @@ -1903,22 +1903,23 @@ pub(crate) mod tests {
const _COMPILE_TIME_ASSERTION: () = op_stack_is_big_enough();

#[test]
fn run_tvm_hello_world_1() {
fn run_tvm_hello_world() {
let program = triton_program!(
push 10 write_io
push 33 write_io
push 100 write_io
push 108 write_io
push 114 write_io
push 111 write_io
push 87 write_io
push 32 write_io
push 44 write_io
push 111 write_io
push 108 write_io
push 108 write_io
push 101 write_io
push 72 write_io
push 10 // \n
push 33 // !
push 100 // d
push 108 // l
push 114 // r
push 111 // o
push 87 // W
push 32 //
push 44 // ,
push 111 // o
push 108 // l
push 108 // l
push 101 // e
push 72 // H
write_io 5 write_io 5 write_io 4
halt
);
let terminal_state = program
Expand All @@ -1929,7 +1930,7 @@ pub(crate) mod tests {

#[test]
fn run_tvm_halt_then_do_stuff() {
let program = triton_program!(halt push 1 push 2 add invert write_io);
let program = triton_program!(halt push 1 push 2 add invert write_io 5);
let (aet, _) = program.trace_execution([].into(), [].into()).unwrap();

let last_processor_row = aet.processor_trace.rows().into_iter().last().unwrap();
Expand Down Expand Up @@ -2043,7 +2044,7 @@ pub(crate) mod tests {
dup 3 dup 3 push -1 mul add // dx = p0_x - p1_x
invert mul add // compute result
swap 3 pop 3 // leave a clean stack
write_io halt
write_io 1 halt
);

let public_input = vec![p2_x, p1.1, p1.0, p0.1, p0.0].into();
Expand All @@ -2059,13 +2060,13 @@ pub(crate) mod tests {

loop:
dup 0
write_io
write_io 1
push -1
add
dup 0
skiz
recurse
write_io
write_io 1
halt
);

Expand All @@ -2083,7 +2084,7 @@ pub(crate) mod tests {

#[test]
fn run_tvm_swap() {
let program = triton_program!(push 1 push 2 swap 1 assert write_io halt);
let program = triton_program!(push 1 push 2 swap 1 assert write_io 1 halt);
let standard_out = program.run([].into(), [].into()).unwrap();
assert_eq!(BFieldElement::new(2), standard_out[0]);
}
Expand All @@ -2097,7 +2098,7 @@ pub(crate) mod tests {

#[test]
fn read_non_deterministically_initialized_ram_at_address_0() {
let program = triton_program!(read_mem write_io halt);
let program = triton_program!(read_mem write_io 1 halt);

let mut initial_ram = HashMap::new();
initial_ram.insert(0_u64.into(), 42_u64.into());
Expand All @@ -2117,8 +2118,8 @@ pub(crate) mod tests {
fn read_non_deterministically_initialized_ram_at_random_address() {
let random_address = thread_rng().gen_range(1..2_u64.pow(16));
let program = triton_program!(
read_mem write_io
push {random_address} read_mem write_io
read_mem write_io 1
push {random_address} read_mem write_io 1
halt
);

Expand Down

0 comments on commit 6276aa9

Please sign in to comment.