Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Flat instructions encoding #18

Open
s1na opened this issue Sep 6, 2019 · 2 comments
Open

Flat instructions encoding #18

s1na opened this issue Sep 6, 2019 · 2 comments

Comments

@s1na
Copy link
Collaborator

s1na commented Sep 6, 2019

Currently instructions are RLP-encoded like other data structures. That's not strictly necessary as after reading each opcode (1 byte) we'd know how many bytes of data to read for that opcode. So it could be a simple flat encoding of the opcodes and their operands without any metadata. This would eliminate the need for decoding them first on the verifier's side. This is also what @cdetrio is doing in turbo-mpas.

I could start working on a PR if you think this makes sense.

@s1na
Copy link
Collaborator Author

s1na commented Sep 11, 2019

Something that could complicate this flat encoding is the EXTENSION opcode. Its operand doesn't have a pre-determined length and might need an additional byte for the length of the extension key.

@s1na
Copy link
Collaborator Author

s1na commented Sep 24, 2019

Implemented this in the Typescript version: ethereumjs/merkle-patricia-tree@cb63d56

Each opcode takes one byte, values for Add and Branch each take one byte. For Extension we first encode the length of the nibbles (one byte is sufficient), and then each nibble as one byte. As an example, the instructions below are encoded to 0204050304030303030006:

const instructions = [                                                                                                                                                                             
  { kind: Opcode.Leaf },                                                                                                                                                                           
  { kind: Opcode.Add, value: 5 },                                                                                                                                                                  
  { kind: Opcode.Extension, value: [3, 3, 3, 3] },                                                                                                                                                 
  { kind: Opcode.Branch, value: 6 }                                                            
]                                                                                              

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant