Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Host and AdviceProvider #1572

Merged
merged 12 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Changes
- [BREAKING] `Process` no longer takes ownership of the `Host` (#1571)
- [BREAKING] `ProcessState` was converted from a trait to a struct (#1571)
- [BREAKING] `Host` and `AdviceProvider` traits simplified (#1572)

#### Enhancements
- Added `miden_core::mast::MastForest::advice_map` to load it into the advice provider before the `MastForest` execution (#1574).
Expand Down
6 changes: 0 additions & 6 deletions assembly/src/ast/instruction/advice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use crate::{ast::ImmU8, Felt, ZERO};
pub enum AdviceInjectorNode {
PushU64Div,
PushExt2intt,
PushSmtGet,
PushSmtSet,
PushSmtPeek,
PushMapVal,
PushMapValImm { offset: ImmU8 },
Expand All @@ -37,8 +35,6 @@ impl From<&AdviceInjectorNode> for AdviceInjector {
match value {
PushU64Div => Self::U64Div,
PushExt2intt => Self::Ext2Intt,
PushSmtGet => Self::SmtGet,
PushSmtSet => Self::SmtSet,
PushSmtPeek => Self::SmtPeek,
PushMapVal => Self::MapValueToStack { include_len: false, key_offset: 0 },
PushMapValImm { offset: ImmU8::Value(offset) } => Self::MapValueToStack {
Expand Down Expand Up @@ -76,8 +72,6 @@ impl fmt::Display for AdviceInjectorNode {
match self {
Self::PushU64Div => write!(f, "push_u64div"),
Self::PushExt2intt => write!(f, "push_ext2intt"),
Self::PushSmtGet => write!(f, "push_smtget"),
Self::PushSmtSet => write!(f, "push_smtset"),
Self::PushSmtPeek => write!(f, "push_smtpeek"),
Self::PushMapVal => write!(f, "push_mapval"),
Self::PushMapValImm { offset } => write!(f, "push_mapval.{offset}"),
Expand Down
6 changes: 1 addition & 5 deletions assembly/src/ast/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,10 @@ fn test_ast_parsing_adv_injection() -> Result<(), Report> {
use super::AdviceInjectorNode::*;

let context = TestContext::new();
let source = source_file!(
&context,
"begin adv.push_u64div adv.push_mapval adv.push_smtget adv.insert_mem end"
);
let source = source_file!(&context, "begin adv.push_u64div adv.push_mapval adv.insert_mem end");
let forms = module!(begin!(
inst!(AdvInject(PushU64Div)),
inst!(AdvInject(PushMapVal)),
inst!(AdvInject(PushSmtGet)),
inst!(AdvInject(InsertMem))
));
assert_eq!(context.parse_forms(source)?, forms);
Expand Down
4 changes: 0 additions & 4 deletions assembly/src/ast/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,6 @@ where
},
AdviceInjectorNode::PushU64Div
| AdviceInjectorNode::PushExt2intt
| AdviceInjectorNode::PushSmtGet
| AdviceInjectorNode::PushSmtSet
| AdviceInjectorNode::PushSmtPeek
| AdviceInjectorNode::PushMapVal
| AdviceInjectorNode::PushMapValN
Expand Down Expand Up @@ -813,8 +811,6 @@ where
},
AdviceInjectorNode::PushU64Div
| AdviceInjectorNode::PushExt2intt
| AdviceInjectorNode::PushSmtGet
| AdviceInjectorNode::PushSmtSet
| AdviceInjectorNode::PushSmtPeek
| AdviceInjectorNode::PushMapVal
| AdviceInjectorNode::PushMapValN
Expand Down
6 changes: 0 additions & 6 deletions assembly/src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ extern {
"push_mtnode" => Token::PushMtnode,
"push_sig" => Token::PushSig,
"push_smtpeek" => Token::PushSmtpeek,
"push_smtget" => Token::PushSmtget,
"push_smtset" => Token::PushSmtset,
"push_u64div" => Token::PushU64Div,
"and" => Token::And,
"assert" => Token::Assert,
Expand Down Expand Up @@ -685,8 +683,6 @@ AdviceInjector: Instruction = {
"adv" "." "push_mtnode" => Instruction::AdvInject(AdviceInjectorNode::PushMtNode),
"adv" "." "push_sig" "." <kind:SignatureKind> => Instruction::AdvInject(AdviceInjectorNode::PushSignature { kind }),
"adv" "." "push_smtpeek" => Instruction::AdvInject(AdviceInjectorNode::PushSmtPeek),
"adv" "." "push_smtget" => Instruction::AdvInject(AdviceInjectorNode::PushSmtGet),
"adv" "." "push_smtset" => Instruction::AdvInject(AdviceInjectorNode::PushSmtSet),
"adv" "." "push_u64div" => Instruction::AdvInject(AdviceInjectorNode::PushU64Div),
}

Expand Down Expand Up @@ -1681,9 +1677,7 @@ Opcode: &'static str = {
"movdnw" => "movdnw",
"movup" => "movup",
"movupw" => "movupw",
"mtree_get" => "mtree_get",
"mtree_merge" => "mtree_merge",
"mtree_set" => "mtree_set",
"mtree_verify" => "mtree_verify",
"mul" => "mul",
"neg" => "neg",
Expand Down
12 changes: 0 additions & 12 deletions core/src/mast/serialization/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ impl DecoratorInfo {
EncodedDecoratorVariant::AdviceInjectorExt2Intt => {
Ok(Decorator::Advice(AdviceInjector::Ext2Intt))
},
EncodedDecoratorVariant::AdviceInjectorSmtGet => {
Ok(Decorator::Advice(AdviceInjector::SmtGet))
},
EncodedDecoratorVariant::AdviceInjectorSmtSet => {
Ok(Decorator::Advice(AdviceInjector::SmtSet))
},
EncodedDecoratorVariant::AdviceInjectorSmtPeek => {
Ok(Decorator::Advice(AdviceInjector::SmtPeek))
},
Expand Down Expand Up @@ -222,8 +216,6 @@ pub enum EncodedDecoratorVariant {
AdviceInjectorU64Div,
AdviceInjectorExt2Inv,
AdviceInjectorExt2Intt,
AdviceInjectorSmtGet,
AdviceInjectorSmtSet,
AdviceInjectorSmtPeek,
AdviceInjectorU32Clz,
AdviceInjectorU32Ctz,
Expand Down Expand Up @@ -271,8 +263,6 @@ impl From<&Decorator> for EncodedDecoratorVariant {
AdviceInjector::U64Div => Self::AdviceInjectorU64Div,
AdviceInjector::Ext2Inv => Self::AdviceInjectorExt2Inv,
AdviceInjector::Ext2Intt => Self::AdviceInjectorExt2Intt,
AdviceInjector::SmtGet => Self::AdviceInjectorSmtGet,
AdviceInjector::SmtSet => Self::AdviceInjectorSmtSet,
AdviceInjector::SmtPeek => Self::AdviceInjectorSmtPeek,
AdviceInjector::U32Clz => Self::AdviceInjectorU32Clz,
AdviceInjector::U32Ctz => Self::AdviceInjectorU32Ctz,
Expand Down Expand Up @@ -366,8 +356,6 @@ impl DecoratorDataBuilder {
| AdviceInjector::U64Div
| AdviceInjector::Ext2Inv
| AdviceInjector::Ext2Intt
| AdviceInjector::SmtGet
| AdviceInjector::SmtSet
| AdviceInjector::SmtPeek
| AdviceInjector::U32Clz
| AdviceInjector::U32Ctz
Expand Down
4 changes: 0 additions & 4 deletions core/src/mast/serialization/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ fn confirm_operation_and_decorator_structure() {
AdviceInjector::U64Div => (),
AdviceInjector::Ext2Inv => (),
AdviceInjector::Ext2Intt => (),
AdviceInjector::SmtGet => (),
AdviceInjector::SmtSet => (),
AdviceInjector::SmtPeek => (),
AdviceInjector::U32Clz => (),
AdviceInjector::U32Ctz => (),
Expand Down Expand Up @@ -256,8 +254,6 @@ fn serialize_deserialize_all_nodes() {
(1, Decorator::Advice(AdviceInjector::U64Div)),
(3, Decorator::Advice(AdviceInjector::Ext2Inv)),
(5, Decorator::Advice(AdviceInjector::Ext2Intt)),
(5, Decorator::Advice(AdviceInjector::SmtGet)),
(5, Decorator::Advice(AdviceInjector::SmtSet)),
(5, Decorator::Advice(AdviceInjector::SmtPeek)),
(5, Decorator::Advice(AdviceInjector::U32Clz)),
(10, Decorator::Advice(AdviceInjector::U32Ctz)),
Expand Down
8 changes: 0 additions & 8 deletions core/src/operations/decorators/advice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,6 @@ pub enum AdviceInjector {
/// degree coefficients are located at the top of the advice stack.
Ext2Intt,

/// Currently unimplemented
SmtGet,

/// Currently unimplemented
SmtSet,

/// Pushes onto the advice stack the value associated with the specified key in a Sparse
/// Merkle Tree defined by the specified root.
///
Expand Down Expand Up @@ -303,8 +297,6 @@ impl fmt::Display for AdviceInjector {
Self::U64Div => write!(f, "div_u64"),
Self::Ext2Inv => write!(f, "ext2_inv"),
Self::Ext2Intt => write!(f, "ext2_intt"),
Self::SmtGet => write!(f, "smt_get"),
Self::SmtSet => write!(f, "smt_set"),
Self::SmtPeek => write!(f, "smt_peek"),
Self::U32Clz => write!(f, "u32clz"),
Self::U32Ctz => write!(f, "u32ctz"),
Expand Down
10 changes: 5 additions & 5 deletions miden/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ let exec_options = ExecutionOptions::default();
let trace = execute(&program, stack_inputs.clone(), &mut host, exec_options).unwrap();

// now, execute the same program in debug mode and iterate over VM states
for vm_state in execute_iter(&program, stack_inputs, host) {
for vm_state in execute_iter(&program, stack_inputs, &mut host) {
match vm_state {
Ok(vm_state) => println!("{:?}", vm_state),
Err(_) => println!("something went terribly wrong!"),
Expand Down Expand Up @@ -111,13 +111,13 @@ let program = assembler.assemble_program("begin push.3 push.5 add end").unwrap()
let (outputs, proof) = prove(
&program,
StackInputs::default(), // we won't provide any inputs
DefaultHost::default(), // we'll be using a default host
&mut DefaultHost::default(), // we'll be using a default host
ProvingOptions::default(), // we'll be using default options
)
.unwrap();

// the output should be 8
assert_eq!(8, outputs.stack().first().unwrap().as_int());
assert_eq!(8, outputs.first().unwrap().as_int());
```

### Verifying program execution
Expand Down Expand Up @@ -196,7 +196,7 @@ let mut assembler = Assembler::default();
let program = assembler.assemble_program(&source).unwrap();

// initialize a default host (with an empty advice provider)
let host = DefaultHost::default();
let mut host = DefaultHost::default();

// initialize the stack with values 0 and 1
let stack_inputs = StackInputs::try_from_ints([0, 1]).unwrap();
Expand All @@ -205,7 +205,7 @@ let stack_inputs = StackInputs::try_from_ints([0, 1]).unwrap();
let (outputs, proof) = miden_vm::prove(
&program,
stack_inputs,
host,
&mut host,
ProvingOptions::default(), // use default proving options
)
.unwrap();
Expand Down
41 changes: 8 additions & 33 deletions miden/tests/integration/operations/decorators/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::sync::Arc;

use processor::{
AdviceExtractor, AdviceProvider, ExecutionError, Host, HostResponse, MastForest,
MemAdviceProvider, ProcessState,
AdviceProvider, ExecutionError, Host, MastForest, MemAdviceProvider, ProcessState,
};
use vm_core::{AdviceInjector, DebugOptions};
use vm_core::DebugOptions;

mod advice;
mod asmop;
Expand Down Expand Up @@ -41,47 +40,23 @@ impl<A: AdviceProvider> Host for TestHost<A> {
&mut self.adv_provider
}

fn get_advice(
&mut self,
process: ProcessState,
extractor: AdviceExtractor,
) -> Result<HostResponse, ExecutionError> {
self.adv_provider.get_advice(process, &extractor)
}

fn set_advice(
&mut self,
process: ProcessState,
injector: AdviceInjector,
) -> Result<HostResponse, ExecutionError> {
self.adv_provider.set_advice(process, &injector)
}

fn on_event(
&mut self,
_process: ProcessState,
event_id: u32,
) -> Result<HostResponse, ExecutionError> {
fn on_event(&mut self, _process: ProcessState, event_id: u32) -> Result<(), ExecutionError> {
self.event_handler.push(event_id);
Ok(HostResponse::None)
Ok(())
}

fn on_trace(
&mut self,
_process: ProcessState,
trace_id: u32,
) -> Result<HostResponse, ExecutionError> {
fn on_trace(&mut self, _process: ProcessState, trace_id: u32) -> Result<(), ExecutionError> {
self.trace_handler.push(trace_id);
Ok(HostResponse::None)
Ok(())
}

fn on_debug(
&mut self,
_process: ProcessState,
_options: &DebugOptions,
) -> Result<HostResponse, ExecutionError> {
) -> Result<(), ExecutionError> {
self.debug_handler.push(_options.to_string());
Ok(HostResponse::None)
Ok(())
}

fn get_mast_forest(&self, _node_digest: &prover::Digest) -> Option<Arc<MastForest>> {
Expand Down
2 changes: 1 addition & 1 deletion processor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let exec_options = ExecutionOptions::default();
let trace = execute(&program, stack_inputs.clone(), &mut host, exec_options).unwrap();

// now, execute the same program in debug mode and iterate over VM states
for vm_state in execute_iter(&program, stack_inputs, host, exec_options) {
for vm_state in execute_iter(&program, stack_inputs, &mut host, exec_options) {
match vm_state {
Ok(vm_state) => println!("{:?}", vm_state),
Err(_) => println!("something went terribly wrong!"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloc::vec::Vec;

use super::super::{ExecutionError, Felt, Word};
use super::{ExecutionError, Felt, Word};

/// Gets as input a vector containing a secret key, and a word representing a message and outputs a
/// vector of values to be pushed onto the advice stack.
Expand Down
Loading
Loading