starknet.js icon indicating copy to clipboard operation
starknet.js copied to clipboard

Add test and support for negative Signed integers smaller than 252 bits

Open tabaktoni opened this issue 1 year ago • 13 comments

Is your feature request related to a problem? Please describe. https://docs.starknet.io/documentation/architecture_and_concepts/Smart_Contracts/serialization_of_Cairo_types/#data_types_of_252_bits_or_less

Describe the solution you'd like Test current behavior and support required field el calc i8, i16, i32, i64, and i128

Negativ value should be 2^{251} + 172^{192} + 1 + (negative value) ex -5 should serialise to 2^{251} + 172^{192} + 1 - 5

tabaktoni avatar Mar 14 '24 08:03 tabaktoni

@tabaktoni Can I work on this one after completing #1018 ? :)

AryanGodara avatar Mar 20 '24 10:03 AryanGodara

Yes ofc, maybe the task will need to be expanded to multiple sub tasks we will need to check how to implement all Cairo int types in the manner of CairoInt. But the initial step (Test current behavior ) can be immediately tackled.

tabaktoni avatar Mar 20 '24 13:03 tabaktoni

Yes ofc, maybe the task will need to be expanded to multiple sub tasks we will need to check how to implement all Cairo int types in the manner of CairoInt. But the initial step (Test current behavior ) can be immediately tackled.

Got it! I'll start looking into the tests first! Then see how to break this down into subtasks :)

AryanGodara avatar Mar 20 '24 14:03 AryanGodara

Task will be offered again on the ODHack event https://onlydust.notion.site/ODHack-Common-Guidelines-b9c6b6a4ac4146d087185568aca38a3f

ivpavici avatar Apr 19 '24 13:04 ivpavici

@ivpavici I'm still working on the other issue. So please don't unassign there šŸ˜… I had left a doubt regarding testing earlier. And I'd like to complete it during this week

AryanGodara avatar Apr 20 '24 18:04 AryanGodara

ok when you are done there we can assign you here if no one else wants to pick it up

ivpavici avatar Apr 22 '24 08:04 ivpavici

Hey! I am from Argentina. I am at the mu. I want to contribute and make this my first issue. Regards!

lucilapastore avatar Apr 24 '24 21:04 lucilapastore

@lucilapastore good luck!

ivpavici avatar Apr 25 '24 09:04 ivpavici

Could I work on this? @ivpavici

fmmesen avatar May 22 '24 00:05 fmmesen

good luck!

ivpavici avatar May 22 '24 07:05 ivpavici

Hey @ivpavici , sorry for the delay! I just have a doubt

Doing the serialization manually:

2^{251} + 172^{192} + 1 - 5

I'll receive:

3618502788666131113263695016908177884250476444008934042335404944711319814140 result that will be returned as a felt252

So the idea is to create a unit test that will expect(result).ToBe(361850278866613111......) for i8, i16, i32, i64 and i128?

fmmesen avatar Jun 11 '24 09:06 fmmesen

Implementation needs to be the similar to CairoUint256

In addition to this you need to define and store:

  1. LImits: For each signed variant can store numbers from: $āˆ’(2^{nāˆ’1})$ to $2^{nāˆ’1}āˆ’1$ inclusive, where n is the number of bits that variant uses. Example: So an i8 can store numbers from $āˆ’(2^7)$ to $2^7āˆ’1$, which equals -128 to 127

  2. felt252 representation in field math notation (one you mentioning)

  3. JSC Representation in ''standard'' math example you provided will return -5

The class will contain all these properties + existing ones as in the uint256 example. Class needs to encode for request -5 to 3618502788... , and decode on response to -5 This needs to be a public class method. From an end-user perspective, it would be used as a normal number -X ... +X, and lib will based on ABI compile it to field math representation.

So to finally answer the question, yes for unit testing you need to test all Class methods as you described, but in e2e test you will expect -5.

tabaktoni avatar Jun 18 '24 13:06 tabaktoni

Understood, I'll update you soon!

fmmesen avatar Jun 26 '24 03:06 fmmesen

:tada: This issue has been resolved in version 8.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Aug 15 '25 15:08 github-actions[bot]