From 4ec1effef75a25620fab9bdfaea7f9775aa58ee0 Mon Sep 17 00:00:00 2001 From: Hasekawa-Takumi Date: Thu, 18 Apr 2024 23:55:24 -0400 Subject: [PATCH 1/7] add fixed from file advice --- machine/src/advice.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/machine/src/advice.rs b/machine/src/advice.rs index 05ae7b46..0e439bd7 100644 --- a/machine/src/advice.rs +++ b/machine/src/advice.rs @@ -1,6 +1,7 @@ use core::slice; use std::io; use std::io::Read; +use std::fs::File; pub trait AdviceProvider { /// Get the next byte from the advice tape, if any. @@ -12,6 +13,20 @@ pub struct FixedAdviceProvider { index: usize, } +enum AdviceProviderType { + Stdin(StdinAdviceProvider), + Fixed(FixedAdviceProvider), +} + +impl AdviceProviderType { + fn get_advice(&mut self) -> Option { + match self { + AdviceProviderType::Stdin(provider) => provider.get_advice(), + AdviceProviderType::Fixed(provider) => provider.get_advice(), + } + } +} + impl FixedAdviceProvider { pub fn empty() -> Self { Self::new(vec![]) @@ -20,6 +35,12 @@ impl FixedAdviceProvider { pub fn new(advice: Vec) -> Self { Self { advice, index: 0 } } + pub fn from_file(file : &mut File) -> Self { + // read the entire file into self::advice: + let mut advice = Vec::new(); + file.read_to_end(&mut advice).unwrap(); + Self { advice, index: 0 } + } } impl AdviceProvider for FixedAdviceProvider { From 196d8aec191e77e2917b97615187ab605dc9ae7b Mon Sep 17 00:00:00 2001 From: Hasekawa-Takumi Date: Fri, 19 Apr 2024 00:13:21 -0400 Subject: [PATCH 2/7] use dynamic box --- basic/src/bin/valida.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/basic/src/bin/valida.rs b/basic/src/bin/valida.rs index c59b45e1..f130bd59 100644 --- a/basic/src/bin/valida.rs +++ b/basic/src/bin/valida.rs @@ -9,7 +9,7 @@ use p3_baby_bear::BabyBear; use p3_fri::{FriConfig, TwoAdicFriPcs, TwoAdicFriPcsConfig}; use valida_cpu::MachineWithCpuChip; -use valida_machine::{Machine, MachineProof, ProgramROM, StdinAdviceProvider, StoppingFlag}; +use valida_machine::{AdviceProvider, FixedAdviceProvider, Machine, MachineProof, ProgramROM, StdinAdviceProvider, StoppingFlag}; use valida_memory::MachineWithMemoryChip; use valida_elf::{load_executable_file, Program}; @@ -61,6 +61,7 @@ struct Context { last_fp_: u32, recorded_current_fp_: u32, last_fp_size_: u32, + advice : Box, } impl Context { @@ -73,6 +74,7 @@ impl Context { last_fp_: args.stack_height, recorded_current_fp_: args.stack_height, last_fp_size_: 0, + advice: Box::new(StdinAdviceProvider), }; let Program { code, data } = load_executable_file( From 0286700ce62f631dbb9f4cb588c5bde6b8a8e4fc Mon Sep 17 00:00:00 2001 From: Hasekawa-Takumi Date: Fri, 19 Apr 2024 00:15:13 -0400 Subject: [PATCH 3/7] part 2 --- basic/src/bin/valida.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/basic/src/bin/valida.rs b/basic/src/bin/valida.rs index f130bd59..d3d808ae 100644 --- a/basic/src/bin/valida.rs +++ b/basic/src/bin/valida.rs @@ -51,6 +51,10 @@ struct Args { /// Stack height (which is also the initial frame pointer value) #[arg(long, default_value = "16777216")] stack_height: u32, + + /// Advice file + #[arg(name = "Advice file")] + advice: Option, } struct Context { @@ -74,7 +78,13 @@ impl Context { last_fp_: args.stack_height, recorded_current_fp_: args.stack_height, last_fp_size_: 0, - advice: Box::new(StdinAdviceProvider), + advice: match &args.advice { + Some(file) => { + let mut file = File::open(file).unwrap(); + Box::new(FixedAdviceProvider::from_file(&mut file)) + } + _ => Box::new(StdinAdviceProvider), + }, }; let Program { code, data } = load_executable_file( From ae96ab374c43a15222f0c6da1b07e69e7bc01364 Mon Sep 17 00:00:00 2001 From: Hasekawa-Takumi Date: Fri, 19 Apr 2024 09:04:55 -0400 Subject: [PATCH 4/7] Use a global provider --- machine/src/advice.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/machine/src/advice.rs b/machine/src/advice.rs index 0e439bd7..994ed5b0 100644 --- a/machine/src/advice.rs +++ b/machine/src/advice.rs @@ -68,3 +68,28 @@ impl AdviceProvider for StdinAdviceProvider { } } } + +struct GlobalAdviceProvider { + provider : AdviceProviderType, +} +impl GlobalAdviceProvider { + pub fn new(file_name : Option) -> Self { + match file_name { + Some(file_name) => { + let mut file = File::open(file_name).unwrap(); + let provider = AdviceProviderType::Fixed(FixedAdviceProvider::from_file(&mut file)); + Self { provider } + }, + None => { + let provider = AdviceProviderType::Stdin(StdinAdviceProvider); + Self { provider } + } + } + } +} + +impl AdviceProvider for GlobalAdviceProvider { + fn get_advice(&mut self) -> Option { + self.provider.get_advice() + } +} \ No newline at end of file From 1a01e3dee09e7be80621b135bb27865f97e75af4 Mon Sep 17 00:00:00 2001 From: Hasekawa-Takumi Date: Fri, 19 Apr 2024 09:16:08 -0400 Subject: [PATCH 5/7] use a global provider encapsulation --- basic/src/bin/valida.rs | 12 +++--------- machine/src/advice.rs | 4 ++-- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/basic/src/bin/valida.rs b/basic/src/bin/valida.rs index d3d808ae..604e9c03 100644 --- a/basic/src/bin/valida.rs +++ b/basic/src/bin/valida.rs @@ -9,7 +9,7 @@ use p3_baby_bear::BabyBear; use p3_fri::{FriConfig, TwoAdicFriPcs, TwoAdicFriPcsConfig}; use valida_cpu::MachineWithCpuChip; -use valida_machine::{AdviceProvider, FixedAdviceProvider, Machine, MachineProof, ProgramROM, StdinAdviceProvider, StoppingFlag}; +use valida_machine::{AdviceProvider, GlobalAdviceProvider, Machine, MachineProof, ProgramROM, StdinAdviceProvider, StoppingFlag}; use valida_memory::MachineWithMemoryChip; use valida_elf::{load_executable_file, Program}; @@ -65,7 +65,7 @@ struct Context { last_fp_: u32, recorded_current_fp_: u32, last_fp_size_: u32, - advice : Box, + advice : GlobalAdviceProvider, } impl Context { @@ -78,13 +78,7 @@ impl Context { last_fp_: args.stack_height, recorded_current_fp_: args.stack_height, last_fp_size_: 0, - advice: match &args.advice { - Some(file) => { - let mut file = File::open(file).unwrap(); - Box::new(FixedAdviceProvider::from_file(&mut file)) - } - _ => Box::new(StdinAdviceProvider), - }, + advice : GlobalAdviceProvider::new(&args.advice), }; let Program { code, data } = load_executable_file( diff --git a/machine/src/advice.rs b/machine/src/advice.rs index 994ed5b0..f2420c46 100644 --- a/machine/src/advice.rs +++ b/machine/src/advice.rs @@ -69,11 +69,11 @@ impl AdviceProvider for StdinAdviceProvider { } } -struct GlobalAdviceProvider { +pub struct GlobalAdviceProvider { provider : AdviceProviderType, } impl GlobalAdviceProvider { - pub fn new(file_name : Option) -> Self { + pub fn new(file_name : &Option) -> Self { match file_name { Some(file_name) => { let mut file = File::open(file_name).unwrap(); From cb05181343eb90558b2cdd6f5fdab0ae30c58735 Mon Sep 17 00:00:00 2001 From: Hasekawa-Takumi Date: Fri, 19 Apr 2024 09:25:52 -0400 Subject: [PATCH 6/7] update --- basic/src/bin/valida.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/basic/src/bin/valida.rs b/basic/src/bin/valida.rs index 604e9c03..183c4ca9 100644 --- a/basic/src/bin/valida.rs +++ b/basic/src/bin/valida.rs @@ -99,7 +99,7 @@ impl Context { if self.stopped_ == StoppingFlag::DidStop { return (StoppingFlag::DidStop, 0); } - let state = self.machine_.step(&mut StdinAdviceProvider); + let state = self.machine_.step(&mut self.advice); let pc = self.machine_.cpu().pc; let fp = self.machine_.cpu().fp; @@ -338,7 +338,7 @@ fn main() { machine.static_data_mut().load(data); // Run the program - machine.run(&code, &mut StdinAdviceProvider); + machine.run(&code, &mut GlobalAdviceProvider::new(&args.advice)); type Val = BabyBear; type Challenge = BinomialExtensionField; From 347fb3e07d8e873d1fbf98b424c9c980250b6caa Mon Sep 17 00:00:00 2001 From: Hasekawa-Takumi Date: Fri, 19 Apr 2024 09:26:08 -0400 Subject: [PATCH 7/7] fmt --- basic/src/bin/valida.rs | 9 ++++++--- machine/src/advice.rs | 14 +++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/basic/src/bin/valida.rs b/basic/src/bin/valida.rs index 183c4ca9..d6b9ceac 100644 --- a/basic/src/bin/valida.rs +++ b/basic/src/bin/valida.rs @@ -9,7 +9,10 @@ use p3_baby_bear::BabyBear; use p3_fri::{FriConfig, TwoAdicFriPcs, TwoAdicFriPcsConfig}; use valida_cpu::MachineWithCpuChip; -use valida_machine::{AdviceProvider, GlobalAdviceProvider, Machine, MachineProof, ProgramROM, StdinAdviceProvider, StoppingFlag}; +use valida_machine::{ + AdviceProvider, GlobalAdviceProvider, Machine, MachineProof, ProgramROM, StdinAdviceProvider, + StoppingFlag, +}; use valida_memory::MachineWithMemoryChip; use valida_elf::{load_executable_file, Program}; @@ -65,7 +68,7 @@ struct Context { last_fp_: u32, recorded_current_fp_: u32, last_fp_size_: u32, - advice : GlobalAdviceProvider, + advice: GlobalAdviceProvider, } impl Context { @@ -78,7 +81,7 @@ impl Context { last_fp_: args.stack_height, recorded_current_fp_: args.stack_height, last_fp_size_: 0, - advice : GlobalAdviceProvider::new(&args.advice), + advice: GlobalAdviceProvider::new(&args.advice), }; let Program { code, data } = load_executable_file( diff --git a/machine/src/advice.rs b/machine/src/advice.rs index f2420c46..7edfefa6 100644 --- a/machine/src/advice.rs +++ b/machine/src/advice.rs @@ -1,7 +1,7 @@ use core::slice; +use std::fs::File; use std::io; use std::io::Read; -use std::fs::File; pub trait AdviceProvider { /// Get the next byte from the advice tape, if any. @@ -35,7 +35,7 @@ impl FixedAdviceProvider { pub fn new(advice: Vec) -> Self { Self { advice, index: 0 } } - pub fn from_file(file : &mut File) -> Self { + pub fn from_file(file: &mut File) -> Self { // read the entire file into self::advice: let mut advice = Vec::new(); file.read_to_end(&mut advice).unwrap(); @@ -70,21 +70,21 @@ impl AdviceProvider for StdinAdviceProvider { } pub struct GlobalAdviceProvider { - provider : AdviceProviderType, + provider: AdviceProviderType, } impl GlobalAdviceProvider { - pub fn new(file_name : &Option) -> Self { + pub fn new(file_name: &Option) -> Self { match file_name { Some(file_name) => { let mut file = File::open(file_name).unwrap(); let provider = AdviceProviderType::Fixed(FixedAdviceProvider::from_file(&mut file)); Self { provider } - }, + } None => { let provider = AdviceProviderType::Stdin(StdinAdviceProvider); Self { provider } } - } + } } } @@ -92,4 +92,4 @@ impl AdviceProvider for GlobalAdviceProvider { fn get_advice(&mut self) -> Option { self.provider.get_advice() } -} \ No newline at end of file +}