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

Allow multiple hints for the same pc #193

Merged
merged 5 commits into from
Feb 1, 2024
Merged

Allow multiple hints for the same pc #193

merged 5 commits into from
Feb 1, 2024

Conversation

cicr99
Copy link
Contributor

@cicr99 cicr99 commented Jan 29, 2024

Fixes #189

@cicr99
Copy link
Contributor Author

cicr99 commented Jan 29, 2024

An example of multiple hints for the same pc (Simplified code extracted from https://github.com/starkware-libs/cairo-lang/blob/master/src/demo/amm_demo/amm.cairo):

%builtins output pedersen range_check

from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.dict import dict_new, dict_read, dict_squash, dict_update, dict_write
from starkware.cairo.common.dict_access import DictAccess
from starkware.cairo.common.hash import hash2
from starkware.cairo.common.math import assert_nn_le, unsigned_div_rem
from starkware.cairo.common.registers import get_fp_and_pc
from starkware.cairo.common.small_merkle_tree import small_merkle_tree_update

struct AmmState {
    // A dictionary that tracks the accounts' state.
    account_dict_start: DictAccess*,
    account_dict_end: DictAccess*,
    // The amount of the tokens currently in the AMM.
    // Must be in the range [0, MAX_BALANCE].
    token_a_balance: felt,
    token_b_balance: felt,
}

// Computes the Merkle roots before and after the batch.
// Hint argument: initial_account_dict should be a dictionary
// from account_id to an address in memory of the Account struct.
func compute_merkle_roots{pedersen_ptr: HashBuiltin*, range_check_ptr}(state: AmmState) -> (
    root_before: felt, root_after: felt
) {
    alloc_locals;

    // Squash the account dictionary.
    let (squashed_dict_start, squashed_dict_end) = dict_squash(
        dict_accesses_start=state.account_dict_start, dict_accesses_end=state.account_dict_end
    );

    let (local hash_dict_start: DictAccess*) = dict_new();
    let (hash_dict_end) = dict_new();

    // Compute the two Merkle roots.
    let (root_before, root_after) = small_merkle_tree_update{hash_ptr=pedersen_ptr}(
        squashed_dict_start=hash_dict_start, squashed_dict_end=hash_dict_end, height=10
    );

    return (root_before=root_before, root_after=root_after);
}

func main{output_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
    alloc_locals;

    // Create the initial state.
    local state: AmmState;
    %{
        # Initialize the balances using a hint.
        # Later we will output them to the output struct,
        # which will allow the verifier to check that they
        # are indeed valid.
        ids.state.token_a_balance = \
            program_input['token_a_balance']
        ids.state.token_b_balance = \
            program_input['token_b_balance']
    %}

    // Write the Merkle roots to the output.
    let (root_before, root_after) = compute_merkle_roots(state=state);

    return ();
}

Run cairo-compile example.cairo --proof_mode --output output.json to get the compile output. Note that for pc = 319 you get two different hints

Copy link
Contributor

@quasilyte quasilyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cicr99 cicr99 merged commit 88587ae into main Feb 1, 2024
4 checks passed
@cicr99 cicr99 deleted the multihints branch February 1, 2024 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check multihints for the same pc
2 participants