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

Add class hash check for declarev2 #819

Merged
merged 9 commits into from
Jul 17, 2023

Conversation

SantiagoPittella
Copy link
Collaborator

@SantiagoPittella SantiagoPittella commented Jul 13, 2023

Description

When compiling the casm class in the DeclareV2 we now check if the received casm_class_hash was correct.

Made on top of #814 (DO NOT MERGE UNTIL GET THAT MERGED)
closes #791

Checklist

  • Linked to Github Issue
  • Unit tests added
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.

@SantiagoPittella SantiagoPittella mentioned this pull request Jul 13, 2023
5 tasks
@codecov-commenter
Copy link

codecov-commenter commented Jul 13, 2023

Codecov Report

Merging #819 (a0cbb66) into main (71e98dc) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #819      +/-   ##
==========================================
+ Coverage   91.72%   91.74%   +0.01%     
==========================================
  Files          54       54              
  Lines       12503    12530      +27     
==========================================
+ Hits        11469    11496      +27     
  Misses       1034     1034              
Impacted Files Coverage Δ
src/testing/mod.rs 100.00% <ø> (ø)
src/transaction/error.rs 100.00% <ø> (ø)
src/lib.rs 98.71% <100.00%> (+0.01%) ⬆️
src/transaction/declare_v2.rs 78.36% <100.00%> (+0.75%) ⬆️

Comment on lines +323 to +324
let casm_class_hash = compute_casm_class_hash(casm_class)?;
if casm_class_hash != self.compiled_class_hash {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to always have this check? Maybe we could have it as an optional feature, or add it only for debugging.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, it's not necessary to always check this. We want to check this whenever a casm contract class and casm class hash is sent from the outside, because otherwise we couldn't guarantee that the compiled contract is really the corresponded to that sierra contract class. It wouldn't be a problem if the contract was already compiled on our side. I'll work on that next, but preferred to have this implemented first.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm in favor of making it a debug assertion. Either we computed it and they are the same by construction, or it was passed via one of the unchecked functions and the responsibility lies in the caller. The point of that was not having to compute the hash in the first place.
Unless I'm misunderstanding it, of course.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Another option may be to don't compile and check at all, and take the casm contract class and class hash as valid. But sound pretty insecure. We should think a better way of guarantee that.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is insecure, but that's the compromise made for performance and that's why we have to be extra explicit and opt-in. If you hold it wrong, it breaks.
The only way to guarantee correctness is to compute stuff ourselves, but then we only moved the costs and increased complexity for no gain for the user. If we're gonna check, just compute it on construction and never again.

Copy link
Contributor

Choose a reason for hiding this comment

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

Think of it like using unsafe in Rust. The keyword doesn't mean you can do whatever you like, but that you go to battle without the shield, it's your job to dodge the blows now. When the programmer opts for passing the hash themselves, it's their job to make sure it's the correct one, and if they get hurt they'll deal with it.

src/lib.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@MegaRedHand MegaRedHand left a comment

Choose a reason for hiding this comment

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

Aside from the check, LGTM, but we can leave the decision for later.

Copy link
Contributor

@xqft xqft left a comment

Choose a reason for hiding this comment

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

I agree with the leave-it-to-the-user side of the discussion and to leave it for another issue for now.

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
SantiagoPittella and others added 4 commits July 17, 2023 11:04
Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy>
Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy>
@SantiagoPittella SantiagoPittella added this pull request to the merge queue Jul 17, 2023
Merged via the queue into main with commit ae06254 Jul 17, 2023
@juanbono juanbono deleted the add-class-hash-check-for-declarev2 branch July 31, 2023 23:36
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.

Compiling Sierra to Casm in Declare V2 and checking the hash
5 participants