Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enum encoding is different from the SNIP-12 spec #1278

Closed
thiagodeev opened this issue Dec 2, 2024 · 3 comments · Fixed by #1281
Closed

Enum encoding is different from the SNIP-12 spec #1278

thiagodeev opened this issue Dec 2, 2024 · 3 comments · Fixed by #1281
Assignees

Comments

@thiagodeev
Copy link

Hi. I want to talk about this line of code:

revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes]),

The code only hashes the enum index + the enum parameters but the SNIP-12 says the enum hash is:
Enc[enum] = hash_array(type_hash(enum), variant_index, Enc[chosen_variant_parameter1],..., Enc[chosen_variant_parameterN])
(ref: https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-12.md#encoding-4).

AFAIU, it should also hashes the 'type hash' with the index and parameters.

Look at these prints taken when debugging while starknet.js encodes the enum_example:

Here we are encoding EnumA, and the data is [2, [0, 1]]. The code encodes the data to a new array containing the encoded data and passes it with the variantIndex variable (value 1).
image

Inside the hash function, we can see that only these three values will be hashed, with no mention of the type hash value quoted by the spec. The returned value is '0x583136381661e3eb69d103f2d6f40e6aa190005c3d5c8f0351b080c69602dcb' to the EnumA type.
image

This is the result of EnumB (value: '0x1fb7169b936dd880cb7ebc50e932a495a60e0084cdab94a681040cb4006e1a0'):
image




At the end, this will be the result of calling the getStructHash function to calculate the hash of the Example type.

  • '0x8eb4aeac64b707f3e843284c4258df6df1f0f7fd38dcffdd8a153a495cd351' is the type hash of Example
  • '0x583136381661e3eb69d103f2d6f40e6aa190005c3d5c8f0351b080c69602dcb' is the hash of EnumA
  • '0x1fb7169b936dd880cb7ebc50e932a495a60e0084cdab94a681040cb4006e1a0' is the hash of EnumB.

This is correct for the Example type, since the spec says that this is the default for object types (Enc[x] = hash_array(type_hash(MyObject), Enc[param1], Enc[param2], ..., Enc[paramN])), but the values of the EnumA and EnumB encoding are theoretically wrong since they were calculated without the type hash value. So, it's a wrong result after all.
image

What do you think guys? Please let me know if I misunderstood something.

@penovicp penovicp self-assigned this Dec 2, 2024
@penovicp
Copy link
Collaborator

penovicp commented Dec 3, 2024

@thiagodeev Good catch, thank you for reporting it!

I opened PR #1281 that should address the issue, I would appreciate it if you could take a look.

@thiagodeev
Copy link
Author

Thanks!
LGTM. I compared your test result with the one made on starknet.go and they are the same

Copy link

github-actions bot commented Jan 8, 2025

🎉 This issue has been resolved in version 6.21.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants