Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
yorik committed Sep 14, 2023
1 parent cf821aa commit 8462cff
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 64 deletions.
8 changes: 4 additions & 4 deletions src/glue/ram_permutation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ pub fn ram_permutation_entry_point<
)?;

let num_nondeterministic_writes = UInt32::conditionally_select(
cs,
&structured_input.start_flag,
&UInt32::zero(),
cs,
&structured_input.start_flag,
&UInt32::zero(),
&structured_input
.hidden_fsm_input
.num_nondeterministic_writes
.num_nondeterministic_writes,
)?;

// walk over queues, accumulate grand products, and ensure sorting
Expand Down
55 changes: 37 additions & 18 deletions src/glue/sort_decommittment_requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ pub fn sort_and_deduplicate_code_decommittments_entry_point<
];

let [initial_log_queue_state_from_fsm, sorted_queue_state_from_fsm, final_queue_state_from_fsm] =
queue_states_from_fsm.map(|el| {
DecommitQueue::from_raw_parts(cs, el.head, el.tail, el.length)
});
queue_states_from_fsm
.map(|el| DecommitQueue::from_raw_parts(cs, el.head, el.tail, el.length));

let initial_queue_from_passthrough = DecommitQueue::from_raw_parts(
cs,
Expand Down Expand Up @@ -147,20 +146,25 @@ pub fn sort_and_deduplicate_code_decommittments_entry_point<

taken += 1;
}
assert_eq!(fs_challenges.len(), DecommitHash::<E>::CIRCUIT_ENCODING_LEN + 1);
assert_eq!(
fs_challenges.len(),
DecommitHash::<E>::CIRCUIT_ENCODING_LEN + 1
);

let additive_part = fs_challenges.pop().unwrap();

let mut grand_products = <[Num<E>; 2]>::conditionally_select(
cs,
&structured_input.start_flag,
&[Num::one(); 2],
&[Num::one(); 2],
&structured_input.hidden_fsm_input.grand_products,
)?;

let mut previous_packed_key = structured_input.hidden_fsm_input.previous_packed_key;
let mut previous_item_is_trivial = structured_input.hidden_fsm_input.previous_item_is_trivial;
let mut first_encountered_timestamp = structured_input.hidden_fsm_input.first_encountered_timestamp;
let mut first_encountered_timestamp = structured_input
.hidden_fsm_input
.first_encountered_timestamp;
let previous_record_encoding = structured_input.hidden_fsm_input.previous_record_encoding;
let mut previous_record = DecommitQuery::allocate_and_prove_encoding(
cs,
Expand Down Expand Up @@ -195,15 +199,18 @@ pub fn sort_and_deduplicate_code_decommittments_entry_point<
structured_input.hidden_fsm_output.grand_products = grand_products;
structured_input.hidden_fsm_output.previous_packed_key = previous_packed_key;
structured_input.hidden_fsm_output.previous_item_is_trivial = previous_item_is_trivial;
structured_input.hidden_fsm_output.first_encountered_timestamp = first_encountered_timestamp;
structured_input
.hidden_fsm_output
.first_encountered_timestamp = first_encountered_timestamp;
structured_input.hidden_fsm_output.previous_record_encoding = previous_record.encode(cs)?;

structured_input.observable_output.final_queue_state = FullSpongeLikeQueueState::conditionally_select(
cs,
&completed,
&structured_input.hidden_fsm_output.final_queue_state,
&structured_input.observable_output.final_queue_state
)?;
structured_input.observable_output.final_queue_state =
FullSpongeLikeQueueState::conditionally_select(
cs,
&completed,
&structured_input.hidden_fsm_output.final_queue_state,
&structured_input.observable_output.final_queue_state,
)?;

structured_input.completion_flag = completed;

Expand Down Expand Up @@ -297,8 +304,10 @@ pub fn sort_and_deduplicate_code_decommittments_inner<
let lhs_candidate = grand_products[0].mul(cs, &lhs_contribution)?;
let rhs_candidate = grand_products[1].mul(cs, &rhs_contribution)?;

grand_products[0] = Num::conditionally_select(cs, &should_pop, &lhs_candidate, &grand_products[0])?;
grand_products[1] = Num::conditionally_select(cs, &should_pop, &rhs_candidate, &grand_products[1])?;
grand_products[0] =
Num::conditionally_select(cs, &should_pop, &lhs_candidate, &grand_products[0])?;
grand_products[1] =
Num::conditionally_select(cs, &should_pop, &rhs_candidate, &grand_products[1])?;
}

// if this circuit is the last one the queues must be empty and grand products must be equal
Expand Down Expand Up @@ -355,7 +364,10 @@ pub fn sort_and_deduplicate_code_decommittments_inner<

// otherwise it should have the same memory page
let enforce_same_memory_page = if first_iter {
smart_and(cs, &[same_hash, previous_item_is_trivial.not(), start_flag.not()])?
smart_and(
cs,
&[same_hash, previous_item_is_trivial.not(), start_flag.not()],
)?
} else {
smart_and(cs, &[same_hash, previous_item_is_trivial.not()])?
};
Expand All @@ -368,7 +380,14 @@ pub fn sort_and_deduplicate_code_decommittments_inner<

// decide if we should add the PREVIOUS into the queue
let add_to_the_queue = if first_iter {
smart_and(cs, &[previous_item_is_trivial.not(), same_hash.not(), start_flag.not()])?
smart_and(
cs,
&[
previous_item_is_trivial.not(),
same_hash.not(),
start_flag.not(),
],
)?
} else {
smart_and(cs, &[previous_item_is_trivial.not(), same_hash.not()])?
};
Expand All @@ -391,7 +410,7 @@ pub fn sort_and_deduplicate_code_decommittments_inner<

first_iter = false;
}

// finalization step - push the last one if necessary
{
let add_to_the_queue = Boolean::and(cs, &previous_item_is_trivial.not(), &completed)?;
Expand Down
15 changes: 10 additions & 5 deletions src/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,15 @@ pub fn scheduler_function<
round_function,
)?;

let decommits_sorter_intermediate_queue_state = project_ref!(witness, decommits_sorter_intermediate_queue_state).cloned();
let decommits_sorter_intermediate_queue_state =
project_ref!(witness, decommits_sorter_intermediate_queue_state).cloned();
let decommits_sorter_intermediate_queue_state =
SpongeLikeQueueState::alloc_from_witness(cs, decommits_sorter_intermediate_queue_state)?;
let mut decommits_sorter_circuit_sorted_queue_state = FullSpongeLikeQueueState::empty();
decommits_sorter_circuit_sorted_queue_state.tail = decommits_sorter_intermediate_queue_state.sponge_state;
decommits_sorter_circuit_sorted_queue_state.length = decommits_sorter_intermediate_queue_state.length;

decommits_sorter_circuit_sorted_queue_state.tail =
decommits_sorter_intermediate_queue_state.sponge_state;
decommits_sorter_circuit_sorted_queue_state.length =
decommits_sorter_intermediate_queue_state.length;

let decommittments_sorter_circuit_input = CodeDecommittmentsDeduplicatorInputData::<E> {
initial_log_queue_state: vm_end_of_execution_observable_output
Expand Down Expand Up @@ -986,7 +988,10 @@ pub fn scheduler_function<
minus_one.negate();

if let Some(inputs) = &full_inputs_witnesses {
assert!(limit as usize >= inputs.len(), "The scheduler inputs limit is too small");
assert!(
limit as usize >= inputs.len(),
"The scheduler inputs limit is too small"
);
}

for _idx in 0..limit {
Expand Down
88 changes: 51 additions & 37 deletions src/vm/vm_cycle/opcode_execution/special_call_ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,8 +834,10 @@ fn callstack_candidate_for_far_call<
// we need a completely fresh one
let mut new_callstack_entry = ExecutionContextRecord::uninitialized();
// apply memory stipends right away
new_callstack_entry.common_part.heap_upper_bound = UInt32::from_uint(zkevm_opcode_defs::system_params::NEW_FRAME_MEMORY_STIPEND);
new_callstack_entry.common_part.aux_heap_upper_bound = UInt32::from_uint(zkevm_opcode_defs::system_params::NEW_FRAME_MEMORY_STIPEND);
new_callstack_entry.common_part.heap_upper_bound =
UInt32::from_uint(zkevm_opcode_defs::system_params::NEW_FRAME_MEMORY_STIPEND);
new_callstack_entry.common_part.aux_heap_upper_bound =
UInt32::from_uint(zkevm_opcode_defs::system_params::NEW_FRAME_MEMORY_STIPEND);

// now also create target for mimic
let implicit_mimic_call_reg = current_state.registers
Expand Down Expand Up @@ -915,7 +917,10 @@ fn callstack_candidate_for_far_call<

if crate::VERBOSE_CIRCUITS && execute.get_value().unwrap_or(false) {
println!("Calling shard {}", destination_shard.get_value().unwrap());
println!("Calling address {}", destination_address.get_value().unwrap());
println!(
"Calling address {}",
destination_address.get_value().unwrap()
);
}

let target_is_kernel = {
Expand Down Expand Up @@ -1107,8 +1112,14 @@ fn callstack_candidate_for_far_call<
let masked_bytecode_hash_upper_decomposition =
masked_bytecode_hash.inner[3].decompose_into_uint8_in_place(cs)?;
let mut lc = LinearCombination::zero();
lc.add_assign_number_with_coeff(&masked_bytecode_hash_upper_decomposition[4].inner, shifts[0]);
lc.add_assign_number_with_coeff(&masked_bytecode_hash_upper_decomposition[5].inner, shifts[8]);
lc.add_assign_number_with_coeff(
&masked_bytecode_hash_upper_decomposition[4].inner,
shifts[0],
);
lc.add_assign_number_with_coeff(
&masked_bytecode_hash_upper_decomposition[5].inner,
shifts[8],
);
let mut code_hash_length_in_words = UInt16::from_num_unchecked(lc.into_num(cs)?);

// if we call now-in-construction system contract, then we formally mask into 0 (even though it's not needed),
Expand Down Expand Up @@ -1241,26 +1252,37 @@ fn callstack_candidate_for_far_call<
)?;

// now any extra cost
let callee_stipend = if zk_evm::opcodes::execution::far_call::FORCED_ERGS_FOR_MSG_VALUE_SIMULATOR == false {
UInt32::zero()
} else {
use crate::vm::primitives::u160;
let target_is_msg_value = UInt160::equals(cs, &destination_address, &UInt160::from_uint(u160::from_u64(zkevm_opcode_defs::ADDRESS_MSG_VALUE as u64)))?;
let is_system_abi = far_call_abi.system_call;
let require_extra = smart_and(cs, &[target_is_msg_value, is_system_abi])?;

let additive_cost = UInt32::from_uint(zkevm_opcode_defs::system_params::MSG_VALUE_SIMULATOR_ADDITIVE_COST);
let pubdata_cost = UInt32::from_uint(zkevm_opcode_defs::system_params::MSG_VALUE_SIMULATOR_PUBDATA_BYTES_TO_PREPAY).inner.mul(
cs, &current_state.ergs_per_pubdata_byte.inner
)?;
let pubdata_cost = UInt32 {inner: pubdata_cost};
let cost = pubdata_cost.add_unchecked(cs, &additive_cost)?;

cost.mask(cs, &require_extra)?
};
let callee_stipend =
if zk_evm::opcodes::execution::far_call::FORCED_ERGS_FOR_MSG_VALUE_SIMULATOR == false {
UInt32::zero()
} else {
use crate::vm::primitives::u160;
let target_is_msg_value = UInt160::equals(
cs,
&destination_address,
&UInt160::from_uint(u160::from_u64(zkevm_opcode_defs::ADDRESS_MSG_VALUE as u64)),
)?;
let is_system_abi = far_call_abi.system_call;
let require_extra = smart_and(cs, &[target_is_msg_value, is_system_abi])?;

let additive_cost = UInt32::from_uint(
zkevm_opcode_defs::system_params::MSG_VALUE_SIMULATOR_ADDITIVE_COST,
);
let pubdata_cost = UInt32::from_uint(
zkevm_opcode_defs::system_params::MSG_VALUE_SIMULATOR_PUBDATA_BYTES_TO_PREPAY,
)
.inner
.mul(cs, &current_state.ergs_per_pubdata_byte.inner)?;
let pubdata_cost = UInt32 {
inner: pubdata_cost,
};
let cost = pubdata_cost.add_unchecked(cs, &additive_cost)?;

cost.mask(cs, &require_extra)?
};

let (ergs_left_after_extra_costs, uf) = ergs_left_after_growth
.sub_using_delayed_bool_allocation(cs, &callee_stipend, optimizer)?;
let (ergs_left_after_extra_costs, uf) =
ergs_left_after_growth.sub_using_delayed_bool_allocation(cs, &callee_stipend, optimizer)?;
let ergs_left_after_extra_costs = ergs_left_after_extra_costs.mask(cs, &uf.not())?; // if not enough - set to 0
let callee_stipend = callee_stipend.mask(cs, &uf.not())?; // also set to 0 if we were not able to take it
exceptions.push(uf);
Expand Down Expand Up @@ -1342,7 +1364,9 @@ fn callstack_candidate_for_far_call<
let remaining_ergs_if_pass = remaining_for_this_context;
let passed_ergs_if_pass = ergs_to_pass;
let passed_ergs_if_pass = callee_stipend.inner.add(cs, &passed_ergs_if_pass.inner)?;
let passed_ergs_if_pass = UInt32 { inner: passed_ergs_if_pass };
let passed_ergs_if_pass = UInt32 {
inner: passed_ergs_if_pass,
};

if crate::VERBOSE_CIRCUITS && execute.get_value().unwrap_or(false) {
println!(
Expand Down Expand Up @@ -1606,12 +1630,7 @@ pub fn may_be_read_code_hash<

// - if we couldn't read porter
bytecode_hash =
UInt256::conditionally_select(
cs,
&needs_porter_mask,
&UInt256::zero(),
&bytecode_hash
)?;
UInt256::conditionally_select(cs, &needs_porter_mask, &UInt256::zero(), &bytecode_hash)?;

// bytecode is exactly 0 if we did NOT mask it into defualt AA
let t0 = smart_and(cs, &[bytecode_is_empty, mask_for_default_aa.not()])?;
Expand Down Expand Up @@ -1750,12 +1769,7 @@ pub fn add_to_decommittment_queue<
println!("WIll refund ergs for decommit");
}
let ergs_remaining_after_decommit =
UInt32::conditionally_select(
cs,
&refund,
&ergs_remaining,
&ergs_remaining_after_decommit
)?;
UInt32::conditionally_select(cs, &refund, &ergs_remaining, &ergs_remaining_after_decommit)?;

let mut all_sponge_requests = vec![];

Expand Down

0 comments on commit 8462cff

Please sign in to comment.