Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

feat: added sha256 syscall #1754

Merged
merged 1 commit into from
Jun 16, 2024
Merged

feat: added sha256 syscall #1754

merged 1 commit into from
Jun 16, 2024

Conversation

TomerStarkware
Copy link
Contributor

@TomerStarkware TomerStarkware commented Apr 3, 2024

This change is Reviewable

@codecov-commenter
Copy link

codecov-commenter commented Apr 3, 2024

Codecov Report

Attention: Patch coverage is 85.96491% with 8 lines in your changes missing coverage. Please review.

Project coverage is 78.42%. Comparing base (3ff794c) to head (e87c3e5).

Files Patch % Lines
crates/blockifier/src/execution/syscalls/mod.rs 84.00% 0 Missing and 8 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1754      +/-   ##
==========================================
+ Coverage   78.37%   78.42%   +0.04%     
==========================================
  Files          62       62              
  Lines        8856     8913      +57     
  Branches     8856     8913      +57     
==========================================
+ Hits         6941     6990      +49     
  Misses       1476     1476              
- Partials      439      447       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@TomerStarkware TomerStarkware changed the title added sha256 syscall feat: added sha256 syscall Apr 3, 2024
@TomerStarkware TomerStarkware force-pushed the tomer/sha256_syscall branch 5 times, most recently from 23ca409 to 7fc9922 Compare April 4, 2024 07:44
@ilyalesokhin-starkware
Copy link
Contributor

crates/blockifier/resources/versioned_constants_13_0.json line 140 at r2 (raw file):

        },
        "keccak_round_cost_gas_cost": 180000,
        "sha256_process_block_gas_cost": {

@noaov1 do we need to add it here?
The syscall wasn't supported in that version.

Code quote:

 "sha256_process_block_gas_cost": {

@ilyalesokhin-starkware
Copy link
Contributor

crates/blockifier/src/versioned_constants_test.rs line 201 at r2 (raw file):

    for file in files.map(Result::unwrap) {
        serde_json::from_reader::<_, VersionedConstants>(&std::fs::File::open(&file).unwrap())
            .unwrap_or_else(|_| panic!("Versioned constants JSON file {file:#?} is malformed"));

revert

Code quote:

  .unwrap_or_else(|_| panic!("Versioned constants JSON file {file:#?} is malformed"));

@ilyalesokhin-starkware
Copy link
Contributor

crates/blockifier/src/execution/entry_point_test.rs line 720 at r2 (raw file):

#[case(CairoVersion::Cairo0, "invoke_call_chain", "Couldn't compute operand op0. Unknown value for memory cell 1:37", (1081_u16, 1127_u16))]
#[case(CairoVersion::Cairo0, "fail", "An ASSERT_EQ instruction failed: 1 != 0.", (1184_u16, 1135_u16))]
#[case(CairoVersion::Cairo1, "invoke_call_chain", "0x4469766973696f6e2062792030 ('Division by 0')", (0_u16, 0_u16))]

this should probably be done in a preliminary PR.

Code quote:

x4469766973696f6e2062792030 ('Division by 0')

@ilyalesokhin-starkware
Copy link
Contributor

crates/blockifier/src/execution/syscalls/mod.rs line 759 at r2 (raw file):

#[derive(Debug, Eq, PartialEq)]
pub struct SHA256ProcessBlockResponse {
    pub state: Relocatable,

Suggestion:

state_ptr

@ilyalesokhin-starkware
Copy link
Contributor

crates/blockifier/src/execution/syscalls/mod.rs line 794 at r2 (raw file):

        return Err(SyscallExecutionError::SyscallError { error_data: vec![out_of_gas_error] });
    }
    *remaining_gas -= gas_cost;

this was already done here:

Code quote:

    let gas_cost = syscall_handler.context.gas_costs().sha256_process_block_gas_cost;
    if gas_cost > *remaining_gas {
        let out_of_gas_error =
            StarkFelt::try_from(OUT_OF_GAS_ERROR).map_err(SyscallExecutionError::from)?;

        return Err(SyscallExecutionError::SyscallError { error_data: vec![out_of_gas_error] });
    }
    *remaining_gas -= gas_cost;

@ilyalesokhin-starkware
Copy link
Contributor

crates/blockifier/src/execution/syscalls/mod.rs line 807 at r2 (raw file):

        .iter()
        .map(|felt| felt.to_bigint().to_u32().unwrap())
        .collect_vec()

is there a ways to do it without building a vector?

Code quote:

 .collect_vec()

Copy link
Contributor

@ilyalesokhin-starkware ilyalesokhin-starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 5 of 15 files at r1, 2 of 8 files at r2, all commit messages.
Reviewable status: 7 of 17 files reviewed, 6 unresolved discussions (waiting on @noaov1, @orizi, and @TomerStarkware)

@TomerStarkware TomerStarkware changed the base branch from main to tomer/recompile_test_contract_cairo_2.6.3 April 4, 2024 10:35
@TomerStarkware TomerStarkware force-pushed the tomer/recompile_test_contract_cairo_2.6.3 branch from 0b73de5 to e3be6bf Compare April 4, 2024 10:38
Copy link

@orizi orizi left a comment

Choose a reason for hiding this comment

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

Reviewable status: 7 of 17 files reviewed, 7 unresolved discussions (waiting on @noaov1 and @TomerStarkware)


crates/blockifier/src/execution/syscalls/mod.rs line 809 at r2 (raw file):

        .collect_vec()
        .try_into()
        .unwrap();

.

Suggestion:

    let mut state_as_words: [u32; SHA256_STATE_SIZE] = 
    core::array::from_fn(|i| prev_state[i].to_bigint().to_u32().unwrap());

@TomerStarkware TomerStarkware force-pushed the tomer/sha256_syscall branch 2 times, most recently from 7293bc4 to 66e91fa Compare April 4, 2024 11:21
Copy link
Contributor Author

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 6 of 17 files reviewed, 7 unresolved discussions (waiting on @ilyalesokhin-starkware, @noaov1, and @orizi)


crates/blockifier/src/execution/entry_point_test.rs line 720 at r2 (raw file):

Previously, ilyalesokhin-starkware wrote…

this should probably be done in a preliminary PR.

Done.


crates/blockifier/src/execution/syscalls/mod.rs line 759 at r2 (raw file):

#[derive(Debug, Eq, PartialEq)]
pub struct SHA256ProcessBlockResponse {
    pub state: Relocatable,

Done.


crates/blockifier/src/execution/syscalls/mod.rs line 794 at r2 (raw file):

Previously, ilyalesokhin-starkware wrote…

this was already done here:

Done.


crates/blockifier/src/execution/syscalls/mod.rs line 807 at r2 (raw file):

Previously, ilyalesokhin-starkware wrote…

is there a ways to do it without building a vector?

used Ori's solution


crates/blockifier/src/execution/syscalls/mod.rs line 809 at r2 (raw file):

Previously, orizi wrote…

.

Done.


crates/blockifier/src/versioned_constants_test.rs line 201 at r2 (raw file):

Previously, ilyalesokhin-starkware wrote…

revert

Done.

Copy link
Collaborator

@noaov1 noaov1 left a comment

Choose a reason for hiding this comment

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

Reviewable status: 5 of 17 files reviewed, 6 unresolved discussions (waiting on @ilyalesokhin-starkware and @orizi)


crates/blockifier/resources/versioned_constants_13_0.json line 140 at r2 (raw file):

Previously, ilyalesokhin-starkware wrote…

@noaov1 do we need to add it here?
The syscall wasn't supported in that version.

Yes, for backward compatibility. Please set all the values to 0 in the files 13.0, 13.1 and 13.1.1

@TomerStarkware TomerStarkware force-pushed the tomer/sha256_syscall branch 2 times, most recently from 3bbb911 to 2dcacf6 Compare May 16, 2024 10:19
orizi
orizi previously approved these changes May 21, 2024
Copy link

@orizi orizi left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 6 files at r17, 2 of 2 files at r18, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @TomerStarkware)

Copy link
Collaborator

@noaov1 noaov1 left a comment

Choose a reason for hiding this comment

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

Reviewed 10 of 10 files at r19, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @TomerStarkware)


crates/blockifier/resources/versioned_constants.json line 96 at r19 (raw file):

            "step_gas_cost": 11822,
            "range_check_gas_cost": 448
        },

What is it used for?

Code quote:

        "sha256_batch_gas_cost": {
            "step_gas_cost": 11822,
            "range_check_gas_cost": 448
        },

@TomerStarkware TomerStarkware force-pushed the tomer/sha256_syscall branch 2 times, most recently from 83094e6 to eda3a2e Compare May 29, 2024 17:47
Copy link
Contributor Author

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 18 of 20 files reviewed, 2 unresolved discussions (waiting on @ilyalesokhin-starkware and @noaov1)


crates/blockifier/resources/versioned_constants.json line 89 at r17 (raw file):

Previously, ilyalesokhin-starkware wrote…

how come there is no
bitwise_builtin?

Done.


crates/blockifier/resources/versioned_constants.json line 96 at r19 (raw file):

Previously, noaov1 (Noa Oved) wrote…

What is it used for?

we split the hint validation of the syscall into two parts, the first part copies the input of an array in the os,
the second which does the actual output validation is executed after all the transactions and validates all calls in batches of 7 calls at a time.
This is the price of validating all 7 syscalls,
the price of sha256_process_block_gas_cost is the combined price of the parts

Copy link
Contributor

@ilyalesokhin-starkware ilyalesokhin-starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 2 files at r18, 6 of 10 files at r19, 2 of 2 files at r20.
Reviewable status: all files reviewed (commit messages unreviewed), 1 unresolved discussion (waiting on @noaov1)

@TomerStarkware TomerStarkware force-pushed the tomer/sha256_syscall branch 2 times, most recently from 2ad4108 to 64b4458 Compare June 2, 2024 11:36
Copy link
Contributor

@ilyalesokhin-starkware ilyalesokhin-starkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 10 files at r19, 3 of 3 files at r21, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @noaov1)

Copy link
Collaborator

@noaov1 noaov1 left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 6 files at r17, 1 of 2 files at r18, 1 of 2 files at r20, 3 of 3 files at r21, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @TomerStarkware)


crates/blockifier/resources/versioned_constants_13_0.json line 148 at r21 (raw file):

            "step_gas_cost": 0,
            "range_check_gas_cost": 0
        },

Remove?

Code quote:

        "sha256_batch_gas_cost": {
            "step_gas_cost": 0,
            "range_check_gas_cost": 0
        },

crates/blockifier/resources/versioned_constants_13_1_1.json line 169 at r21 (raw file):

            "step_gas_cost": 0,
            "range_check_gas_cost": 0
        },

Remove?

Code quote:

        "sha256_batch_gas_cost": {
            "step_gas_cost": 0,
            "range_check_gas_cost": 0
        },

crates/blockifier/src/versioned_constants.rs line 438 at r21 (raw file):

    pub keccak_gas_cost: u64,
    pub keccak_round_cost_gas_cost: u64,
    pub sha256_process_block_gas_cost: u64,

Is it needed?

Code quote:

    pub sha256_process_block_gas_cost: u64,

Copy link
Contributor Author

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 18 of 20 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @noaov1)


crates/blockifier/resources/versioned_constants_13_0.json line 148 at r21 (raw file):

Previously, noaov1 (Noa Oved) wrote…

Remove?

Done.


crates/blockifier/resources/versioned_constants_13_1_1.json line 169 at r21 (raw file):

Previously, noaov1 (Noa Oved) wrote…

Remove?

Done.


crates/blockifier/src/versioned_constants.rs line 438 at r21 (raw file):

Previously, noaov1 (Noa Oved) wrote…

Is it needed?

this one is the full cost, which still exists in version_constants.json

Copy link
Collaborator

@noaov1 noaov1 left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r22, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)

Copy link
Collaborator

@noaov1 noaov1 left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 3 files at r23, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)

@TomerStarkware TomerStarkware merged commit b22fb07 into main Jun 16, 2024
9 checks passed
gswirski pushed a commit to reilabs/blockifier that referenced this pull request Jun 26, 2024
…-libs#1754)

* refactor: use model hash as ids for grpc

* fmt

* remove println

* refactor: get rid of wait for relay

* relay runner

* remove async
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants