Skip to content

Commit

Permalink
drop VecStream in favor of Vec<BFieldElement>
Browse files Browse the repository at this point in the history
fix #109
  • Loading branch information
jan-ferdinand committed Nov 11, 2022
1 parent 43ef887 commit 9668fbe
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 412 deletions.
2 changes: 1 addition & 1 deletion triton-vm/benches/prove_halt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn prove_halt(_criterion: &mut Criterion) {
let stark = Stark::new(claim, parameters);

// witness
let (aet, err, _) = program.simulate_no_input();
let (aet, _, err) = program.simulate_no_input();
if let Some(error) = err {
panic!("The VM encountered the following problem: {}", error);
}
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/benches/verify_halt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn verify_halt(criterion: &mut Criterion) {
Err(e) => panic!("Could not load proof from disk: {:?}", e),
}
} else {
let (aet, err, _) = program.simulate_no_input();
let (aet, _, err) = program.simulate_no_input();
if let Some(error) = err {
panic!("The VM encountered the following problem: {}", error);
}
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/cross_table_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ mod permutation_argument_tests {
let input = code_with_input.input;
let secret_input = code_with_input.secret_input;
let (output, _, _, ext_codeword_tables, all_challenges, _) =
parse_simulate_pad_extend(&code, &input, &secret_input);
parse_simulate_pad_extend(&code, input.clone(), secret_input);

let input_terminal = EvalArg::compute_terminal(
&input,
Expand All @@ -609,7 +609,7 @@ mod permutation_argument_tests {
);

let output_terminal = EvalArg::compute_terminal(
&output.to_bword_vec(),
&output,
EvalArg::default_initial(),
all_challenges
.processor_table_challenges
Expand Down
1 change: 0 additions & 1 deletion triton-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ pub mod proof_stream;
pub mod shared_tests;
pub mod stark;
pub mod state;
pub mod stdio;
pub mod table;
pub mod vm;
55 changes: 24 additions & 31 deletions triton-vm/src/shared_tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::{
error::Error,
fs::{create_dir_all, File},
io::{Read, Write},
path::Path,
};
use std::error::Error;
use std::fs::{create_dir_all, File};
use std::io::{Read, Write};
use std::path::Path;

use twenty_first::shared_math::b_field_element::BFieldElement;

Expand All @@ -12,50 +10,45 @@ use triton_profiler::{prof_start, prof_stop};

use crate::proof::{Claim, Proof};
use crate::stark::{Stark, StarkParameters};
use crate::stdio::VecStream;
use crate::table::base_matrix::AlgebraicExecutionTrace;
use crate::table::base_matrix::BaseMatrices;
use crate::table::table_collection::BaseTableCollection;
use crate::vm::Program;

pub fn parse_setup_simulate(
code: &str,
input_symbols: &[BFieldElement],
secret_input_symbols: &[BFieldElement],
input_symbols: Vec<BFieldElement>,
secret_input_symbols: Vec<BFieldElement>,
maybe_profiler: &mut Option<TritonProfiler>,
) -> (AlgebraicExecutionTrace, VecStream, Program) {
) -> (AlgebraicExecutionTrace, Vec<BFieldElement>, Program) {
let program = Program::from_code(code);

assert!(program.is_ok(), "program parses correctly");
let program = program.unwrap();

let mut stdin = VecStream::new(input_symbols);
let mut secret_in = VecStream::new(secret_input_symbols);
let mut stdout = VecStream::new(&[]);

if let Some(profiler) = maybe_profiler.as_mut() {
profiler.start("simulate")
}
let (aet, err) = program.simulate(&mut stdin, &mut secret_in, &mut stdout);
prof_start!(maybe_profiler, "simulate");
let (aet, stdout, err) = program.simulate(input_symbols, secret_input_symbols);
if let Some(error) = err {
panic!("The VM encountered the following problem: {}", error);
}
prof_stop!(maybe_profiler, "simulate");

if let Some(profiler) = maybe_profiler.as_mut() {
profiler.stop("simulate")
}
(aet, stdout, program)
}

pub fn parse_simulate_prove(
code: &str,
input_symbols: &[BFieldElement],
secret_input_symbols: &[BFieldElement],
output_symbols: &[BFieldElement],
input_symbols: Vec<BFieldElement>,
secret_input_symbols: Vec<BFieldElement>,
output_symbols: Vec<BFieldElement>,
maybe_profiler: &mut Option<TritonProfiler>,
) -> (Stark, Proof) {
let (aet, _, program) =
parse_setup_simulate(code, input_symbols, secret_input_symbols, maybe_profiler);
let (aet, _, program) = parse_setup_simulate(
code,
input_symbols.clone(),
secret_input_symbols,
maybe_profiler,
);
let base_matrices = BaseMatrices::new(aet.clone(), &program.to_bwords());

prof_start!(maybe_profiler, "padding");
Expand All @@ -75,9 +68,9 @@ pub fn parse_simulate_prove(
),
};
let claim = Claim {
input: input_symbols.to_vec(),
input: input_symbols,
program,
output: output_symbols.to_vec(),
output: output_symbols,
padded_height,
};
let stark = Stark::new(claim, parameters);
Expand Down Expand Up @@ -106,7 +99,7 @@ impl SourceCodeAndInput {

pub fn run(&self) -> Vec<BFieldElement> {
let program = Program::from_code(&self.source_code).expect("Could not load source code");
let (_, output, err) = program.run_with_input(&self.input, &self.secret_input);
let (_, output, err) = program.run(self.input.clone(), self.secret_input.clone());
if let Some(e) = err {
panic!("Running the program failed: {}", e)
}
Expand All @@ -117,11 +110,11 @@ impl SourceCodeAndInput {
&self,
) -> (
AlgebraicExecutionTrace,
Option<Box<dyn Error>>,
Vec<BFieldElement>,
Option<Box<dyn Error>>,
) {
let program = Program::from_code(&self.source_code).expect("Could not load source code.");
program.simulate_with_input(&self.input, &self.secret_input)
program.simulate(self.input.clone(), self.secret_input.clone())
}
}

Expand Down
80 changes: 40 additions & 40 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,6 @@ pub(crate) mod triton_stark_tests {
use crate::cross_table_arguments::EvalArg;
use crate::instruction::sample_programs;
use crate::shared_tests::*;
use crate::stdio::VecStream;
use crate::table::base_matrix::AlgebraicExecutionTrace;
use crate::table::base_table::InheritsFromTable;
use crate::table::table_collection::TableId::ProcessorTable;
Expand All @@ -1125,8 +1124,8 @@ pub(crate) mod triton_stark_tests {
#[test]
fn all_tables_pad_to_same_height_test() {
let code = "read_io read_io push -1 mul add split push 0 eq swap1 pop "; // simulates LTE
let input_symbols = [5_u64.into(), 7_u64.into()];
let (aet, _, program) = parse_setup_simulate(code, &input_symbols, &[]);
let input_symbols = vec![5_u64.into(), 7_u64.into()];
let (aet, _, program) = parse_setup_simulate(code, input_symbols, vec![]);
let base_matrices = BaseMatrices::new(aet, &program.to_bwords());
let mut base_tables = BaseTableCollection::from_base_matrices(&base_matrices);
base_tables.pad();
Expand All @@ -1142,19 +1141,15 @@ pub(crate) mod triton_stark_tests {

pub fn parse_setup_simulate(
code: &str,
input_symbols: &[BFieldElement],
secret_input_symbols: &[BFieldElement],
) -> (AlgebraicExecutionTrace, VecStream, Program) {
input_symbols: Vec<BFieldElement>,
secret_input_symbols: Vec<BFieldElement>,
) -> (AlgebraicExecutionTrace, Vec<BFieldElement>, Program) {
let program = Program::from_code(code);

assert!(program.is_ok(), "program parses correctly");
let program = program.unwrap();

let mut stdin = VecStream::new(input_symbols);
let mut secret_in = VecStream::new(secret_input_symbols);
let mut stdout = VecStream::new(&[]);

let (aet, err) = program.simulate(&mut stdin, &mut secret_in, &mut stdout);
let (aet, stdout, err) = program.simulate(input_symbols, secret_input_symbols);
if let Some(error) = err {
panic!("The VM encountered the following problem: {}", error);
}
Expand All @@ -1163,9 +1158,14 @@ pub(crate) mod triton_stark_tests {

pub fn parse_simulate_pad(
code: &str,
stdin: &[BFieldElement],
secret_in: &[BFieldElement],
) -> (BaseTableCollection, BaseTableCollection, usize, VecStream) {
stdin: Vec<BFieldElement>,
secret_in: Vec<BFieldElement>,
) -> (
BaseTableCollection,
BaseTableCollection,
usize,
Vec<BFieldElement>,
) {
let (aet, stdout, program) = parse_setup_simulate(code, stdin, secret_in);
let base_matrices = BaseMatrices::new(aet, &program.to_bwords());

Expand All @@ -1186,10 +1186,10 @@ pub(crate) mod triton_stark_tests {

pub fn parse_simulate_pad_extend(
code: &str,
stdin: &[BFieldElement],
secret_in: &[BFieldElement],
stdin: Vec<BFieldElement>,
secret_in: Vec<BFieldElement>,
) -> (
VecStream,
Vec<BFieldElement>,
BaseTableCollection,
BaseTableCollection,
ExtTableCollection,
Expand Down Expand Up @@ -1220,7 +1220,7 @@ pub(crate) mod triton_stark_tests {
#[test]
pub fn print_all_constraint_degrees() {
let (_, _, _, ext_tables, _, num_trace_randomizers) =
parse_simulate_pad_extend("halt", &[], &[]);
parse_simulate_pad_extend("halt", vec![], vec![]);
let padded_height = ext_tables.padded_height;
let all_degrees = ext_tables
.into_iter()
Expand Down Expand Up @@ -1339,7 +1339,7 @@ pub(crate) mod triton_stark_tests {
BFieldElement::new(7),
];
let (stdout, _, _, ext_table_collection, all_challenges, _) =
parse_simulate_pad_extend(read_nop_code, &input_symbols, &[]);
parse_simulate_pad_extend(read_nop_code, input_symbols.clone(), vec![]);

let ptie = ext_table_collection.data(ProcessorTable).last().unwrap()
[usize::from(InputTableEvalArg)];
Expand All @@ -1354,7 +1354,7 @@ pub(crate) mod triton_stark_tests {
[usize::from(OutputTableEvalArg)];

let oute = EvalArg::compute_terminal(
&stdout.to_bword_vec(),
&stdout,
EvalArg::default_initial(),
all_challenges
.output_challenges
Expand All @@ -1372,9 +1372,9 @@ pub(crate) mod triton_stark_tests {
for (code_idx, code_with_input) in code_collection.into_iter().enumerate() {
let code = code_with_input.source_code;
let input = code_with_input.input;
let secret_input = code_with_input.secret_input;
let secret_input = code_with_input.secret_input.clone();
let (output, _, _, ext_table_collection, all_challenges, _) =
parse_simulate_pad_extend(&code, &input, &secret_input);
parse_simulate_pad_extend(&code, input.clone(), secret_input);

let input_terminal = EvalArg::compute_terminal(
&input,
Expand All @@ -1385,7 +1385,7 @@ pub(crate) mod triton_stark_tests {
);

let output_terminal = EvalArg::compute_terminal(
&output.to_bword_vec(),
&output,
EvalArg::default_initial(),
all_challenges
.processor_table_challenges
Expand Down Expand Up @@ -1437,7 +1437,8 @@ pub(crate) mod triton_stark_tests {

#[test]
fn constraint_polynomials_use_right_variable_count_test() {
let (_, _, _, ext_tables, challenges, _) = parse_simulate_pad_extend("halt", &[], &[]);
let (_, _, _, ext_tables, challenges, _) =
parse_simulate_pad_extend("halt", vec![], vec![]);

for table in ext_tables.into_iter() {
let dummy_row = vec![0.into(); table.full_width()];
Expand All @@ -1454,7 +1455,7 @@ pub(crate) mod triton_stark_tests {
#[test]
fn extend_does_not_change_base_table() {
let (base_tables, _, num_trace_randomizers, _) =
parse_simulate_pad(sample_programs::FIBONACCI_LT, &[], &[]);
parse_simulate_pad(sample_programs::FIBONACCI_LT, vec![], vec![]);

let dummy_challenges = AllChallenges::placeholder();
let ext_tables = ExtTableCollection::extend_tables(
Expand All @@ -1478,7 +1479,7 @@ pub(crate) mod triton_stark_tests {
#[test]
fn print_number_of_all_constraints_per_table() {
let (_, _, _, ext_tables, challenges, _) =
parse_simulate_pad_extend(sample_programs::COUNTDOWN_FROM_10, &[], &[]);
parse_simulate_pad_extend(sample_programs::COUNTDOWN_FROM_10, vec![], vec![]);

println!("| Table | Init | Cons | Trans | Term | Sum |");
println!("|:---------------------|------:|------:|------:|------:|------:|");
Expand Down Expand Up @@ -1539,7 +1540,7 @@ pub(crate) mod triton_stark_tests {
#[test]
fn number_of_quotient_degree_bound_matches_number_of_constraints_test() {
let (_, _, _, ext_tables, challenges, num_trace_randomizers) =
parse_simulate_pad_extend(sample_programs::FIBONACCI_LT, &[], &[]);
parse_simulate_pad_extend(sample_programs::FIBONACCI_LT, vec![], vec![]);
let padded_height = ext_tables.padded_height;

for table in ext_tables.into_iter() {
Expand Down Expand Up @@ -1603,7 +1604,7 @@ pub(crate) mod triton_stark_tests {
let zero = XFieldElement::zero();
let source_code_and_input = test_halt();
let (_, _, _, ext_tables, challenges, _) =
parse_simulate_pad_extend(&source_code_and_input.source_code, &[], &[]);
parse_simulate_pad_extend(&source_code_and_input.source_code, vec![], vec![]);

for table in (&ext_tables).into_iter() {
if let Some(row) = table.data().get(0) {
Expand Down Expand Up @@ -1675,7 +1676,7 @@ pub(crate) mod triton_stark_tests {
fn triton_table_constraints_evaluate_to_zero_test_on_simple_program() {
let zero = XFieldElement::zero();
let (_, _, _, ext_tables, _, _) =
parse_simulate_pad_extend(sample_programs::FIBONACCI_LT, &[], &[]);
parse_simulate_pad_extend(sample_programs::FIBONACCI_LT, vec![], vec![]);

let challenges = AllChallenges::placeholder();
for table in (&ext_tables).into_iter() {
Expand Down Expand Up @@ -1746,9 +1747,9 @@ pub(crate) mod triton_stark_tests {
let code_with_input = test_hash_nop_nop_lt();
let (stark, proof) = parse_simulate_prove(
&code_with_input.source_code,
&code_with_input.input,
&code_with_input.secret_input,
&[],
code_with_input.input.clone(),
code_with_input.secret_input.clone(),
vec![],
&mut None,
);

Expand All @@ -1766,9 +1767,9 @@ pub(crate) mod triton_stark_tests {
let code_with_input = test_halt();
let (stark, proof) = parse_simulate_prove(
&code_with_input.source_code,
&code_with_input.input,
&code_with_input.secret_input,
&[],
code_with_input.input.clone(),
code_with_input.secret_input.clone(),
vec![],
&mut None,
);

Expand All @@ -1787,12 +1788,11 @@ pub(crate) mod triton_stark_tests {
let source_code = sample_programs::FIBONACCI_VIT;
let program = Program::from_code(source_code).unwrap();

let stdin = [100_u64.into()];
let secret_in = [];
let (_, stdout, _) = program.run_with_input(&stdin, &secret_in);
let stdin = vec![100_u64.into()];
let secret_in = vec![];

let (stark, proof) =
parse_simulate_prove(source_code, &stdin, &secret_in, &stdout, &mut None);
let (_, stdout, _) = program.run(stdin.clone(), secret_in.clone());
let (stark, proof) = parse_simulate_prove(source_code, stdin, secret_in, stdout, &mut None);

println!("between prove and verify");

Expand Down
Loading

0 comments on commit 9668fbe

Please sign in to comment.