Skip to content

Commit c1dc050

Browse files
authored
Merge pull request #178 from HerodotusDev/taskHash
taskHash mod
2 parents 83341bb + 70e866d commit c1dc050

File tree

14 files changed

+189
-188
lines changed

14 files changed

+189
-188
lines changed

crates/dry_hint_processor/src/input.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use cairo_vm::{
88
Felt252,
99
};
1010
use hints::vars;
11-
use types::param::Param;
11+
use types::param;
1212

1313
use super::CustomHintProcessor;
1414

@@ -23,8 +23,29 @@ impl CustomHintProcessor {
2323
_hint_data: &HintProcessorData,
2424
_constants: &HashMap<String, Felt252>,
2525
) -> Result<(), HintError> {
26-
exec_scopes.insert_value::<Vec<Param>>(vars::scopes::PARAMS, self.private_inputs.params.to_owned());
27-
exec_scopes.insert_value::<CasmContractClass>(vars::scopes::COMPILED_CLASS, self.private_inputs.compiled_class.to_owned());
26+
exec_scopes.insert_value::<Vec<Felt252>>(
27+
vars::scopes::PUBLIC_INPUTS,
28+
self.inputs
29+
.params
30+
.iter()
31+
.filter_map(|f| match f.visibility {
32+
param::Visibility::Public => Some(f.value),
33+
param::Visibility::Private => None,
34+
})
35+
.collect(),
36+
);
37+
exec_scopes.insert_value::<Vec<Felt252>>(
38+
vars::scopes::PRIVATE_INPUTS,
39+
self.inputs
40+
.params
41+
.iter()
42+
.filter_map(|f| match f.visibility {
43+
param::Visibility::Private => Some(f.value),
44+
param::Visibility::Public => None,
45+
})
46+
.collect(),
47+
);
48+
exec_scopes.insert_value::<CasmContractClass>(vars::scopes::COMPILED_CLASS, self.inputs.compiled_class.to_owned());
2849
Ok(())
2950
}
3051
}

crates/dry_hint_processor/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ use tokio::{runtime::Handle, task};
3131
use types::HDPDryRunInput;
3232

3333
pub struct CustomHintProcessor {
34-
private_inputs: HDPDryRunInput,
34+
inputs: HDPDryRunInput,
3535
builtin_hint_proc: BuiltinHintProcessor,
3636
cairo1_builtin_hint_proc: Cairo1HintProcessor,
3737
hints: HashMap<String, HintImpl>,
3838
extensive_hints: HashMap<String, ExtensiveHintImpl>,
3939
}
4040

4141
impl CustomHintProcessor {
42-
pub fn new(private_inputs: HDPDryRunInput) -> Self {
42+
pub fn new(inputs: HDPDryRunInput) -> Self {
4343
Self {
44-
private_inputs,
44+
inputs,
4545
builtin_hint_proc: BuiltinHintProcessor::new_empty(),
4646
cairo1_builtin_hint_proc: Cairo1HintProcessor::new(Default::default(), Default::default(), true),
4747
hints: Self::hints(),

crates/hints/src/contract_bootloader/params.rs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,62 @@ use std::collections::HashMap;
33
use cairo_vm::{
44
hint_processor::builtin_hint_processor::{
55
builtin_hint_processor_definition::HintProcessorData,
6-
hint_utils::{get_ptr_from_var_name, insert_value_from_var_name},
6+
hint_utils::{get_ptr_from_var_name, insert_value_into_ap},
77
},
8-
types::{exec_scope::ExecutionScopes, relocatable::Relocatable},
8+
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable},
99
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
1010
Felt252,
1111
};
12-
use types::param::Param;
1312

1413
use crate::vars;
1514

16-
pub const LOAD_PARMAS: &str = "ids.params_len = len(params)\nsegments.write_arg(ids.params, [param.value for param in params])";
15+
pub const LOAD_PUBLIC_INPUTS: &str = "segments.write_arg(ids.public_inputs, public_inputs)";
16+
pub const LOAD_PUBLIC_INPUTS_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(public_inputs))";
17+
pub const LOAD_PRIVATE_INPUTS: &str = "segments.write_arg(ids.private_inputs, private_inputs)";
18+
pub const LOAD_PRIVATE_INPUTS_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(private_inputs))";
1719

18-
pub fn load_parmas(
20+
pub fn load_public_inputs(
1921
vm: &mut VirtualMachine,
2022
exec_scopes: &mut ExecutionScopes,
2123
hint_data: &HintProcessorData,
2224
_constants: &HashMap<String, Felt252>,
2325
) -> Result<(), HintError> {
24-
let params = exec_scopes.get::<Vec<Param>>(vars::scopes::PARAMS)?;
25-
insert_value_from_var_name(vars::ids::PARAMS_LEN, params.len(), vm, &hint_data.ids_data, &hint_data.ap_tracking)?;
26-
27-
let params_base = get_ptr_from_var_name(vars::ids::PARAMS, vm, &hint_data.ids_data, &hint_data.ap_tracking)?;
28-
29-
write_params(vm, params_base, params)?;
26+
let inputs = exec_scopes.get::<Vec<Felt252>>(vars::scopes::PUBLIC_INPUTS)?;
27+
let inputs_base = get_ptr_from_var_name(vars::ids::PUBLIC_INPUTS, vm, &hint_data.ids_data, &hint_data.ap_tracking)?;
28+
vm.load_data(inputs_base, &inputs.iter().map(MaybeRelocatable::from).collect::<Vec<_>>())?;
29+
Ok(())
30+
}
3031

32+
pub fn load_public_inputs_len(
33+
vm: &mut VirtualMachine,
34+
exec_scopes: &mut ExecutionScopes,
35+
_hint_data: &HintProcessorData,
36+
_constants: &HashMap<String, Felt252>,
37+
) -> Result<(), HintError> {
38+
let inputs = exec_scopes.get::<Vec<Felt252>>(vars::scopes::PUBLIC_INPUTS)?;
39+
insert_value_into_ap(vm, inputs.len())?;
3140
Ok(())
3241
}
3342

34-
pub fn write_params(vm: &mut VirtualMachine, ptr: Relocatable, params: Vec<Param>) -> Result<(), HintError> {
35-
for (idx, param) in params.into_iter().enumerate() {
36-
vm.insert_value((ptr + idx)?, param.value)?;
37-
}
43+
pub fn load_private_inputs(
44+
vm: &mut VirtualMachine,
45+
exec_scopes: &mut ExecutionScopes,
46+
hint_data: &HintProcessorData,
47+
_constants: &HashMap<String, Felt252>,
48+
) -> Result<(), HintError> {
49+
let inputs = exec_scopes.get::<Vec<Felt252>>(vars::scopes::PRIVATE_INPUTS)?;
50+
let inputs_base = get_ptr_from_var_name(vars::ids::PRIVATE_INPUTS, vm, &hint_data.ids_data, &hint_data.ap_tracking)?;
51+
vm.load_data(inputs_base, &inputs.iter().map(MaybeRelocatable::from).collect::<Vec<_>>())?;
52+
Ok(())
53+
}
3854

55+
pub fn load_private_inputs_len(
56+
vm: &mut VirtualMachine,
57+
exec_scopes: &mut ExecutionScopes,
58+
_hint_data: &HintProcessorData,
59+
_constants: &HashMap<String, Felt252>,
60+
) -> Result<(), HintError> {
61+
let inputs = exec_scopes.get::<Vec<Felt252>>(vars::scopes::PRIVATE_INPUTS)?;
62+
insert_value_into_ap(vm, inputs.len())?;
3963
Ok(())
4064
}

crates/hints/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ pub fn hints() -> HashMap<String, HintImpl> {
3535
hints.insert(contract_bootloader::builtins::UPDATE_BUILTIN_PTRS.into(), contract_bootloader::builtins::update_builtin_ptrs);
3636
hints.insert(contract_bootloader::contract_class::LOAD_CONTRACT_CLASS.into(), contract_bootloader::contract_class::load_contract_class);
3737
hints.insert(contract_bootloader::dict_manager::DICT_MANAGER_CREATE.into(), contract_bootloader::dict_manager::dict_manager_create);
38-
hints.insert(contract_bootloader::params::LOAD_PARMAS.into(), contract_bootloader::params::load_parmas);
38+
hints.insert(contract_bootloader::params::LOAD_PRIVATE_INPUTS.into(), contract_bootloader::params::load_private_inputs);
39+
hints.insert(contract_bootloader::params::LOAD_PRIVATE_INPUTS_LEN.into(), contract_bootloader::params::load_private_inputs_len);
40+
hints.insert(contract_bootloader::params::LOAD_PUBLIC_INPUTS.into(), contract_bootloader::params::load_public_inputs);
41+
hints.insert(contract_bootloader::params::LOAD_PUBLIC_INPUTS_LEN.into(), contract_bootloader::params::load_public_inputs_len);
3942
hints.insert(decoder::evm::has_type_prefix::HINT_HAS_TYPE_PREFIX.into(), decoder::evm::has_type_prefix::hint_has_type_prefix);
4043
hints.insert(decoder::evm::is_byzantium::HINT_IS_BYZANTIUM.into(), decoder::evm::is_byzantium::hint_is_byzantium);
4144
hints.insert(decoder::evm::v_is_encoded::HINT_V_IS_ENCODED.into(), decoder::evm::v_is_encoded::hint_v_is_encoded);
@@ -44,7 +47,7 @@ pub fn hints() -> HashMap<String, HintImpl> {
4447
hints.insert(merkle::HINT_IS_LEFT_SMALLER.into(), merkle::hint_is_left_smaller);
4548
hints.insert(merkle::HINT_TARGET_TASK_HASH.into(), merkle::hint_target_task_hash);
4649
hints.insert(print::HINT_PRINT_TASK_RESULT.into(), print::hint_print_task_result);
47-
hints.insert(print::PROGRAM_HASH.into(), print::program_hash);
50+
hints.insert(print::MODULE_HASH.into(), print::module_hash);
4851
hints.insert(rlp::divmod::HINT_DIVMOD_RLP.into(), rlp::divmod::hint_divmod_rlp);
4952
hints.insert(rlp::divmod::HINT_DIVMOD_VALUE.into(), rlp::divmod::hint_divmod_value);
5053
hints.insert(rlp::item_type::HINT_IS_LONG.into(), rlp::item_type::hint_is_long);

crates/hints/src/print.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ use tracing::debug;
1414

1515
use crate::vars;
1616

17-
pub const PROGRAM_HASH: &str = "print(\"program_hash\", hex(ids.program_hash))";
17+
pub const MODULE_HASH: &str = "print(\"module_hash\", hex(ids.module_hash))";
1818

19-
pub fn program_hash(
19+
pub fn module_hash(
2020
vm: &mut VirtualMachine,
2121
_exec_scopes: &mut ExecutionScopes,
2222
hint_data: &HintProcessorData,
2323
_constants: &HashMap<String, Felt252>,
2424
) -> Result<(), HintError> {
25-
let program_hash = get_integer_from_var_name(vars::ids::PROGRAM_HASH, vm, &hint_data.ids_data, &hint_data.ap_tracking)?;
25+
let module_hash = get_integer_from_var_name(vars::ids::MODULE_HASH, vm, &hint_data.ids_data, &hint_data.ap_tracking)?;
2626

27-
debug!("program_hash: {}", program_hash);
27+
debug!("module_hash: {}", module_hash);
2828
Ok(())
2929
}
3030

crates/hints/src/vars.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ pub mod scopes {
77
pub const HEADER_EVM: &str = "header_evm";
88
pub const HEADER_STARKNET: &str = "header_starknet";
99
pub const N_SELECTED_BUILTINS: &str = "n_selected_builtins";
10-
pub const PARAMS: &str = "params";
10+
pub const PUBLIC_INPUTS: &str = "public_inputs";
11+
pub const PRIVATE_INPUTS: &str = "private_inputs";
1112
pub const PROOF: &str = "proof";
1213
pub const PROOFS: &str = "proofs";
1314
pub const CHAIN_PROOFS: &str = "chain_proofs";
@@ -21,6 +22,8 @@ pub mod scopes {
2122
}
2223

2324
pub mod ids {
25+
pub const PUBLIC_INPUTS: &str = "public_inputs";
26+
pub const PRIVATE_INPUTS: &str = "private_inputs";
2427
pub const NODES: &str = "nodes";
2528
pub const NODE: &str = "node";
2629
pub const N_NODES: &str = "n_nodes";
@@ -59,7 +62,7 @@ pub mod ids {
5962
pub const N_SELECTED_BUILTINS: &str = "n_selected_builtins";
6063
pub const PARAMS_LEN: &str = "params_len";
6164
pub const PARAMS: &str = "params";
62-
pub const PROGRAM_HASH: &str = "program_hash";
65+
pub const MODULE_HASH: &str = "module_hash";
6366
pub const Q: &str = "q";
6467
pub const R: &str = "r";
6568
pub const RESULT: &str = "result";

crates/sound_hint_processor/src/input.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use cairo_vm::{
88
Felt252,
99
};
1010
use hints::vars;
11-
use types::{param::Param, ChainProofs};
11+
use types::{param, ChainProofs};
1212

1313
use super::CustomHintProcessor;
1414

@@ -22,9 +22,30 @@ impl CustomHintProcessor {
2222
_hint_data: &HintProcessorData,
2323
_constants: &HashMap<String, Felt252>,
2424
) -> Result<(), HintError> {
25-
exec_scopes.insert_value::<Vec<ChainProofs>>(vars::scopes::CHAIN_PROOFS, self.private_inputs.chain_proofs.to_owned());
26-
exec_scopes.insert_value::<Vec<Param>>(vars::scopes::PARAMS, self.private_inputs.params.to_owned());
27-
exec_scopes.insert_value::<CasmContractClass>(vars::scopes::COMPILED_CLASS, self.private_inputs.compiled_class.to_owned());
25+
exec_scopes.insert_value::<Vec<ChainProofs>>(vars::scopes::CHAIN_PROOFS, self.inputs.chain_proofs.to_owned());
26+
exec_scopes.insert_value::<Vec<Felt252>>(
27+
vars::scopes::PUBLIC_INPUTS,
28+
self.inputs
29+
.params
30+
.iter()
31+
.filter_map(|f| match f.visibility {
32+
param::Visibility::Public => Some(f.value),
33+
param::Visibility::Private => None,
34+
})
35+
.collect(),
36+
);
37+
exec_scopes.insert_value::<Vec<Felt252>>(
38+
vars::scopes::PRIVATE_INPUTS,
39+
self.inputs
40+
.params
41+
.iter()
42+
.filter_map(|f| match f.visibility {
43+
param::Visibility::Private => Some(f.value),
44+
param::Visibility::Public => None,
45+
})
46+
.collect(),
47+
);
48+
exec_scopes.insert_value::<CasmContractClass>(vars::scopes::COMPILED_CLASS, self.inputs.compiled_class.to_owned());
2849
Ok(())
2950
}
3051
}

crates/sound_hint_processor/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ use tokio::{runtime::Handle, task};
3131
use types::HDPInput;
3232

3333
pub struct CustomHintProcessor {
34-
private_inputs: HDPInput,
34+
inputs: HDPInput,
3535
builtin_hint_proc: BuiltinHintProcessor,
3636
cairo1_builtin_hint_proc: Cairo1HintProcessor,
3737
hints: HashMap<String, HintImpl>,
3838
extensive_hints: HashMap<String, ExtensiveHintImpl>,
3939
}
4040

4141
impl CustomHintProcessor {
42-
pub fn new(private_inputs: HDPInput) -> Self {
42+
pub fn new(inputs: HDPInput) -> Self {
4343
Self {
44-
private_inputs,
44+
inputs,
4545
builtin_hint_proc: BuiltinHintProcessor::new_empty(),
4646
cairo1_builtin_hint_proc: Cairo1HintProcessor::new(Default::default(), Default::default(), true),
4747
hints: Self::hints(),

crates/types/src/lib.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,23 @@ impl<'de> Deserialize<'de> for ChainIds {
132132

133133
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
134134
pub struct HDPDryRunOutput {
135-
pub program_hash: Felt252,
135+
pub task_hash_low: Felt252,
136+
pub task_hash_high: Felt252,
136137
pub output_tree_root_low: Felt252,
137138
pub output_tree_root_high: Felt252,
138139
}
139140

140141
impl FromIterator<Felt252> for HDPDryRunOutput {
141142
fn from_iter<T: IntoIterator<Item = Felt252>>(iter: T) -> Self {
142143
let mut i = iter.into_iter();
143-
let program_hash = i.next().unwrap();
144+
let task_hash_low = i.next().unwrap();
145+
let task_hash_high = i.next().unwrap();
144146
let output_tree_root_low = i.next().unwrap();
145147
let output_tree_root_high = i.next().unwrap();
146148

147149
Self {
148-
program_hash,
150+
task_hash_low,
151+
task_hash_high,
149152
output_tree_root_low,
150153
output_tree_root_high,
151154
}
@@ -162,7 +165,8 @@ pub struct MmrMetaOutput {
162165

163166
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
164167
pub struct HDPOutput {
165-
pub program_hash: Felt252,
168+
pub task_hash_low: Felt252,
169+
pub task_hash_high: Felt252,
166170
pub output_tree_root_low: Felt252,
167171
pub output_tree_root_high: Felt252,
168172
pub mmr_metas: Vec<MmrMetaOutput>,
@@ -172,7 +176,8 @@ impl FromIterator<Felt252> for HDPOutput {
172176
fn from_iter<T: IntoIterator<Item = Felt252>>(iter: T) -> Self {
173177
let mut i = iter.into_iter();
174178

175-
let program_hash = i.next().unwrap();
179+
let task_hash_low = i.next().unwrap();
180+
let task_hash_high = i.next().unwrap();
176181
let output_tree_root_low = i.next().unwrap();
177182
let output_tree_root_high = i.next().unwrap();
178183

@@ -183,7 +188,8 @@ impl FromIterator<Felt252> for HDPOutput {
183188
}
184189

185190
Self {
186-
program_hash,
191+
task_hash_low,
192+
task_hash_high,
187193
output_tree_root_low,
188194
output_tree_root_high,
189195
mmr_metas,
@@ -193,8 +199,12 @@ impl FromIterator<Felt252> for HDPOutput {
193199

194200
impl HDPOutput {
195201
pub fn to_felt_vec(&self) -> Vec<Felt252> {
196-
let mut felt_vec = vec![self.program_hash];
197-
felt_vec.extend([self.output_tree_root_low, self.output_tree_root_high]);
202+
let mut felt_vec = vec![
203+
self.task_hash_low,
204+
self.task_hash_high,
205+
self.output_tree_root_low,
206+
self.output_tree_root_high,
207+
];
198208
self.mmr_metas.iter().for_each(|mmr_meta| {
199209
felt_vec.extend([mmr_meta.id, mmr_meta.size, mmr_meta.chain_id, mmr_meta.root]);
200210
});

src/contract_bootloader/contract.cairo

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,12 @@ func compute_contract{
3636
starknet_memorizer: DictAccess*,
3737
starknet_decoder_ptr: felt***,
3838
starknet_key_hasher_ptr: felt**,
39-
}() -> (program_hash: felt, retdata: felt*, retdata_size: felt) {
39+
}(module_inputs: felt*, module_inputs_len: felt) -> (module_hash: felt, retdata: felt*, retdata_size: felt) {
4040
alloc_locals;
4141

42-
local params_len: felt;
43-
let (params) = alloc();
4442
local compiled_class: CompiledClass*;
45-
4643
%{ ids.compiled_class = segments.gen_arg(get_compiled_class_struct(compiled_class=compiled_class)) %}
4744

48-
%{
49-
ids.params_len = len(params)
50-
segments.write_arg(ids.params, [param.value for param in params])
51-
%}
52-
5345
let (builtin_costs: felt*) = alloc();
5446
assert builtin_costs[0] = 0;
5547
assert builtin_costs[1] = 0;
@@ -62,9 +54,9 @@ func compute_contract{
6254
builtin_costs, felt
6355
);
6456

65-
let (local program_hash) = compiled_class_hash(compiled_class=compiled_class);
57+
let (local module_hash) = compiled_class_hash(compiled_class=compiled_class);
6658

67-
%{ print("program_hash", hex(ids.program_hash)) %}
59+
%{ print("module_hash", hex(ids.module_hash)) %}
6860

6961
%{
7062
vm_load_program(
@@ -80,14 +72,14 @@ func compute_contract{
8072
assert calldata[2] = nondet %{ ids.starknet_memorizer.address_.segment_index %};
8173
assert calldata[3] = nondet %{ ids.starknet_memorizer.address_.offset %};
8274

83-
memcpy(dst=calldata + 4, src=params, len=params_len);
84-
let calldata_size = 4 + params_len;
75+
memcpy(dst=calldata + 4, src=module_inputs, len=module_inputs_len);
76+
let calldata_size = 4 + module_inputs_len;
8577

8678
with evm_memorizer, starknet_memorizer, pow2_array {
8779
let (retdata_size, retdata) = run_contract_bootloader(
8880
compiled_class=compiled_class, calldata_size=calldata_size, calldata=calldata, dry_run=0
8981
);
9082
}
9183

92-
return (program_hash=program_hash, retdata=retdata, retdata_size=retdata_size);
84+
return (module_hash=module_hash, retdata=retdata, retdata_size=retdata_size);
9385
}

0 commit comments

Comments
 (0)