Replies: 1 comment 1 reply
-
How do you intend to do that without loops and divs ? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
RLP Encoding vs Decoding Analysis for EOA Validate function
Context
For Kakarot EOA / AA, upon receiving the raw TX data, we need to:
(Here is the initial discussion.)
The process of decoding, extracting, and encoding can take place either on the RPC or within the EOA itself. For instance:
To determine which flow is most efficient, I have drafted this document to provide a clearer perspective on the decision.
In Cairo, certain mathematical operations are more cost-effective than others; for example, multiplication, addition, and subtraction are less expensive than division and modulo.
Thus, in this document, I present an example flow detailing each logical step required for both encoding and decoding. I also describe the mathematical primitives (add, mul, sub, div, mod) and computational primitives associated with each logical step."
RLP Encoding
Data to Encode: List containing two numbers [1, 100000]
1. Determine the Type of Data
[1, 100000]
2. Convert Each Number to Its Byte Representation
[0x01]
[0x01, 0x86, 0xA0]
[0x01], [0x01, 0x86, 0xA0]
3. Calculate the Length of Each Item
[1, 3]
4. Calculate the Total Length of the List
5. Prepare the Prefix for Each Item
0x83
.[None, 0x83]
[0x01, 0x83, 0x01, 0x86, 0xA0]
6. Prepare the Prefix for the List
0xc0 + 4 = 0xc4
.0xc4
.[0xc4, ..., ..., ..., ..., ...]
7. Append the Data to the List's Prefix
[0xc4, 0x01, 0x83, 0x01, 0x86, 0xA0]
[0xc4, 0x01, 0x83, 0x01, 0x86, 0xA0]
Final Encoded Data:
[0xc4, 0x01, 0x83, 0x01, 0x86, 0xA0]
RLP Decoding
Data to Decode:
[0xc4, 0x01, 0x83, 0x01, 0x86, 0xA0]
1. Read the First Byte to Get the Prefix
0xc4
.[0xc4, ..., ..., ..., ..., ...]
2. Identify the Type and Length from the Prefix
0xc0
from0xc4
to find the length.3. Slice the Payload from the Data
[0x01, 0x83, 0x01, 0x86, 0xA0]
.[0x01, 0x83, 0x01, 0x86, 0xA0]
4. Identify Items in the Payload
0x01
is a single byte item.0x83
indicates the next item is 3 bytes long.0x01
and0x83 0x01 0x86 0xA0
.[0x01], [0x83, 0x01, 0x86, 0xA0]
5. Decode Each Item
0x01
to 1.0x83 0x01 0x86 0xA0
to 100000.[1, 100000]
Final Decoded Data:
[1, 100000]
Ethereum Spec implementation
https://github.com/ethereum/execution-specs/blob/fade6e95726c9c0b47b779fd8873b5737fbea3b3/src/ethereum/rlp.py#L27
Simple Benchmark Simple Short List Encoding function vs Herodotus Short List Decoding
OBS: As I implemented the Simple Short List Encoding, there could definitely have some optimizations to be done, but either way would like to share the current state of the tests:
Overall Encoding Code:
Encoding Test
Decoding Test
Results
Conclusion
Given that Div and Mod is a lot more expensive in Cairo and there isn't bigger difference in computational primitives between encoding and decoding (decoding might have a few more checks) , my conclusion is that we should just send the encoded data as u8 to the EOA Contract, decode it as u8 and use the same encoded data to generate the hash.
Beta Was this translation helpful? Give feedback.
All reactions