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

Decoder Documentation #31

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,32 @@ TODO: Short explanation of Felts and the Cairo/Stark field we use through Lambda

Go through the main parts of a compiled program `Json` file. `data` field with instructions, identifiers, program entrypoint, etc.


### Program decoding

Once the VM has been fed with a compiled cairo program it has to be decoded. The decoder takes as input the hexadecimal number in little endian format that represents the instruction an generates an Instruction struct with it.
Instruction is the representation of the first word of each Cairo instruction. Some instructions spread over two words when they use an immediate value, so representing the first one with this struct is enougth.
mmsc2 marked this conversation as resolved.
Show resolved Hide resolved

Structure of the 63-bit that form the first word of each instruction.
See Cairo whitepaper, page 32 - https://eprint.iacr.org/2021/1063.pdf.
┌─────────────────────────────────────────────────────────────────────────┐
│ off_dst (biased representation) │
├─────────────────────────────────────────────────────────────────────────┤
│ off_op0 (biased representation) │
├─────────────────────────────────────────────────────────────────────────┤
│ off_op1 (biased representation) │
├─────┬─────┬───────┬───────┬───────────┬────────┬───────────────────┬────┤
│ dst │ op0 │ op1 │ res │ pc │ ap │ opcode │ 0 │
│ reg │ reg │ src │ logic │ update │ update │ │ │
├─────┼─────┼───┬───┼───┬───┼───┬───┬───┼───┬────┼────┬────┬────┬────┼────┤
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │ 12 │ 13 │ 14 │ 15 │
└─────┴─────┴───┴───┴───┴───┴───┴───┴───┴───┴────┴────┴────┴────┴────┴────┘
mmsc2 marked this conversation as resolved.
Show resolved Hide resolved

The decoder perform bitwise operations with masks to extract the fields from the hexadecimal values and store them in the struct representation, each value of the masks and padding can be deduced from the table above.
mmsc2 marked this conversation as resolved.
Show resolved Hide resolved
for example to extract the dst_reg flag we can define a mask 'DST_REG_MASK' with value 0x0001 and and a padding 'DST_REG_OFF' with value 0 because it is the less significant bit, then we can perform the operation
'(((encoded_instr) >> 48) & DST_REG_MASK) >> DST_REG_OFF' to obtain the dst_reg flag. We shifted 48 bits first because there is where the flags start.
Copy link
Member

@jrchatruc jrchatruc Aug 2, 2023

Choose a reason for hiding this comment

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

This code is not rendering well because you are using ' to enclose it instead of `. Consider changing it or just moving to a code block with using ```

The same goes for a bunch of code and instructions mentioned shown above



### Code walkthrough/Write your own Cairo VM

TODO
Expand Down
19 changes: 0 additions & 19 deletions src/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,6 @@
#include <stdbool.h>
#include <stdint.h>

// Structure of the 63-bit that form the first word of each instruction.
// See Cairo whitepaper, page 32 - https://eprint.iacr.org/2021/1063.pdf.
// ┌─────────────────────────────────────────────────────────────────────────┐
// │ off_dst (biased representation) │
// ├─────────────────────────────────────────────────────────────────────────┤
// │ off_op0 (biased representation) │
// ├─────────────────────────────────────────────────────────────────────────┤
// │ off_op1 (biased representation) │
// ├─────┬─────┬───────┬───────┬───────────┬────────┬───────────────────┬────┤
// │ dst │ op0 │ op1 │ res │ pc │ ap │ opcode │ 0 │
// │ reg │ reg │ src │ logic │ update │ update │ │ │
// ├─────┼─────┼───┬───┼───┬───┼───┬───┬───┼───┬────┼────┬────┬────┬────┼────┤
// │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │ 12 │ 13 │ 14 │ 15 │
// └─────┴─────┴───┴───┴───┴───┴───┴───┴───┴───┴────┴────┴────┴────┴────┴────┘

// Instruction is the representation of the first word of each Cairo instruction.
// Some instructions spread over two words when they use an immediate value, so
// representing the first one with this struct is enougth.
mmsc2 marked this conversation as resolved.
Show resolved Hide resolved

// ---------------------
// structures
// ---------------------
Expand Down