Skip to content

Commit

Permalink
Feature: WRITE_ZKG_COMMITMENT
Browse files Browse the repository at this point in the history
This implementation uses a default (panicking) implementation of the KZG
commitment computation as we do not know how these must be computed.
  • Loading branch information
odesenfans committed Apr 17, 2024
1 parent 7ab51e4 commit 1d93fe6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
21 changes: 18 additions & 3 deletions src/execution/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use starknet_api::state::StorageKey;

use crate::config::STORED_BLOCK_HASH_BUFFER;
use crate::crypto::pedersen::PedersenHash;
use crate::execution::kzg::{KzgCommitmentComputer, PlaceholderKzgCommitmentComputer};
use crate::starknet::starknet_storage::{CommitmentInfo, CommitmentInfoError, OsSingleStarknetStorage};
use crate::storage::dict_storage::DictStorage;
use crate::storage::storage::StorageError;
Expand All @@ -29,7 +30,10 @@ type StorageByAddress = HashMap<Felt252, OsSingleStarknetStorage<DictStorage, Pe

/// Maintains the info for executing txns in the OS
#[derive(Debug)]
pub struct ExecutionHelper {
pub struct ExecutionHelper<KZG>
where
KZG: KzgCommitmentComputer,
{
pub _prev_block_context: Option<BlockContext>,
// Pointer tx execution info
pub tx_execution_info_iter: IntoIter<TransactionExecutionInfo>,
Expand Down Expand Up @@ -61,13 +65,15 @@ pub struct ExecutionHelper {
// Stores the state diff computed by the OS, in case that KZG commitment is used (since
// it won't be part of the OS output).
da_segment: Option<Vec<Felt252>>,
// Callback that computes the KZG commitment of a polynomial in coefficient representation.
kzg_computer: KZG,
}

/// ExecutionHelper is wrapped in Rc<RefCell<_>> in order
/// to clone the refrence when entering and exiting vm scopes
#[derive(Clone, Debug)]
pub struct ExecutionHelperWrapper {
pub execution_helper: Rc<RefCell<ExecutionHelper>>,
pub execution_helper: Rc<RefCell<ExecutionHelper<PlaceholderKzgCommitmentComputer>>>,
}

impl ExecutionHelperWrapper {
Expand Down Expand Up @@ -98,6 +104,7 @@ impl ExecutionHelperWrapper {
storage_by_address: StorageByAddress::default(),
state,
da_segment: None,
kzg_computer: PlaceholderKzgCommitmentComputer {},
})),
}
}
Expand Down Expand Up @@ -258,9 +265,17 @@ impl ExecutionHelperWrapper {

Ok(())
}

pub fn compute_kzg_commitment(&self, coefficients: &[Felt252]) -> (Felt252, Felt252) {
let execution_helper = &mut self.execution_helper.as_ref().borrow();
execution_helper.kzg_computer.compute_kzg_commitment_from_coefficients(coefficients)
}
}

fn assert_iterators_exhausted(eh_ref: &RefMut<'_, ExecutionHelper>) {
fn assert_iterators_exhausted<KZG>(eh_ref: &RefMut<'_, ExecutionHelper<KZG>>)
where
KZG: KzgCommitmentComputer,
{
assert!(eh_ref.deployed_contracts_iter.clone().peekable().peek().is_none());
assert!(eh_ref.result_iter.clone().peekable().peek().is_none());
assert!(eh_ref.execute_code_read_iter.clone().peekable().peek().is_none());
Expand Down
18 changes: 18 additions & 0 deletions src/execution/kzg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use cairo_vm::Felt252;

pub trait KzgCommitmentComputer {
fn compute_kzg_commitment_from_coefficients(&self, coefficients: &[Felt252]) -> (Felt252, Felt252);
}

/// A struct with a default implementation of the KZG commitment computation.
///
/// We do not know yet how to compute the KZG commitment, this structure is a default
/// implementation that will panic until we figure it out properly.
#[derive(Debug)]
pub struct PlaceholderKzgCommitmentComputer;

impl KzgCommitmentComputer for PlaceholderKzgCommitmentComputer {
fn compute_kzg_commitment_from_coefficients(&self, _coefficients: &[Felt252]) -> (Felt252, Felt252) {
todo!("Define correct method to compute KZG commitment");
}
}
1 change: 1 addition & 0 deletions src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod constants;
pub mod deprecated_syscall_handler;
pub mod execute_syscalls;
pub mod helper;
mod kzg;
pub mod syscall_handler;
pub mod syscall_utils;
mod syscalls;
18 changes: 8 additions & 10 deletions src/hints/commitment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, get_ptr_from_var_name, get_relocatable_from_var_name, insert_value_into_ap,
get_integer_from_var_name, get_ptr_from_var_name, get_relocatable_from_var_name,
};
use cairo_vm::hint_processor::hint_processor_definition::HintReference;
use cairo_vm::hint_processor::hint_processor_utils::felt_to_usize;
Expand All @@ -15,7 +15,7 @@ use indoc::indoc;
use crate::execution::helper::ExecutionHelperWrapper;
use crate::hints::vars;

pub const WRITE_ZKG_COMMITMENT_ADDRESS: &str = indoc! {r#"
pub const WRITE_ZKG_COMMITMENT: &str = indoc! {r#"
execution_helper.store_da_segment(
da_segment=memory.get_range_as_ints(addr=ids.state_updates_start, size=ids.da_size)
)
Expand All @@ -27,7 +27,7 @@ pub const WRITE_ZKG_COMMITMENT_ADDRESS: &str = indoc! {r#"
)"#
};

pub fn write_zkg_commitment_address(
pub fn write_zkg_commitment(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
Expand All @@ -43,15 +43,13 @@ pub fn write_zkg_commitment_address(
let da_segment: Vec<_> =
vm.get_integer_range(state_updates_start, da_size)?.into_iter().map(|x| x.into_owned()).collect();

execution_helper.store_da_segment(da_segment)?;
execution_helper.store_da_segment(da_segment.clone())?;

// segments.write_arg(
// ids.kzg_commitment.address_,
// execution_helper.polynomial_coefficients_to_kzg_commitment_callback(
// execution_helper.da_segment
// )
// )"#
// Compute and store KZG commitment
let kzg_commitment_address = get_relocatable_from_var_name(vars::ids::KZG_COMMITMENT, vm, ids_data, ap_tracking)?;
let kzg_commitment = execution_helper.compute_kzg_commitment(&da_segment);

vm.write_arg(kzg_commitment_address, &vec![kzg_commitment.0, kzg_commitment.1])?;

Ok(())
}
3 changes: 2 additions & 1 deletion src/hints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub type HintImpl = fn(
) -> Result<(), HintError>;

#[rustfmt::skip]
static HINTS: [(&str, HintImpl); 164] = [
static HINTS: [(&str, HintImpl); 165] = [
(BREAKPOINT, breakpoint),
(INITIALIZE_CLASS_HASHES, initialize_class_hashes),
(INITIALIZE_STATE_CHANGES, initialize_state_changes),
Expand Down Expand Up @@ -82,6 +82,7 @@ static HINTS: [(&str, HintImpl); 164] = [
(builtins::SELECTED_BUILTINS, builtins::selected_builtins),
(builtins::SELECT_BUILTIN, builtins::select_builtin),
(builtins::UPDATE_BUILTIN_PTRS, builtins::update_builtin_ptrs),
(commitment::WRITE_ZKG_COMMITMENT, commitment::write_zkg_commitment),
(compiled_class::ASSIGN_BYTECODE_SEGMENTS, compiled_class::assign_bytecode_segments),
(execute_syscalls::IS_BLOCK_NUMBER_IN_BLOCK_HASH_BUFFER, execute_syscalls::is_block_number_in_block_hash_buffer),
(execution::ADD_RELOCATION_RULE, execution::add_relocation_rule),
Expand Down

0 comments on commit 1d93fe6

Please sign in to comment.