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

develop #51

Merged
merged 32 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9f783f7
Merge pull request #48 from 0xPolygonHermez/main
zkronos73 Jan 23, 2023
350c424
fix error on undefined label
zkronos73 Jan 23, 2023
c48de07
remove code generation
zkronos73 Jan 23, 2023
5cc0a2f
Merge pull request #49 from 0xPolygonHermez/fix/undefined-label
zkronos73 Jan 23, 2023
125f311
Merge pull request #50 from 0xPolygonHermez/feature/remove-code-gener…
zkronos73 Jan 23, 2023
0c854c0
fix command line defined constants on metadata
zkronos73 Jan 31, 2023
9e086a8
Merge pull request #52 from 0xPolygonHermez/fix/update-json-defines-info
krlosMata Jan 31, 2023
a81b702
remove obsolete commands
zkronos73 Jan 31, 2023
31c9fbc
update zkasmcom parser build
zkronos73 Feb 2, 2023
f65bb45
Merge pull request #53 from 0xPolygonHermez/fix/remove-obsolete-commands
zkronos73 Feb 3, 2023
72e76d1
add -t option allowUndefinedLabels, allowOverwriteLabels, allowUndefi…
zkronos73 Feb 3, 2023
6c8ab27
update readme
zkronos73 Feb 3, 2023
9ca4461
Merge pull request #54 from 0xPolygonHermez/feature/develop-allow-opt…
zkronos73 Feb 6, 2023
8d80e67
add instructions md
zkronos73 Feb 14, 2023
6c258ab
update instructions
zkronos73 Feb 14, 2023
5110d51
update instructions
zkronos73 Feb 14, 2023
080fd49
update instructions md
zkronos73 Feb 14, 2023
1fb7e2e
update README.md
zkronos73 Feb 14, 2023
a7c0697
update link
zkronos73 Feb 14, 2023
d8ac631
Merge pull request #55 from 0xPolygonHermez/feature/doc-instructions
zkronos73 Feb 14, 2023
9aaf66b
update version from package.json
zkronos73 Feb 14, 2023
2e8a552
remove MAXMEM register
zkronos73 Feb 14, 2023
c82d3c4
remove MAXMEM register
zkronos73 Feb 14, 2023
3f8c63c
Merge pull request #56 from 0xPolygonHermez/fix/non-maxmem-reg
krlosMata Feb 20, 2023
bc4f2a7
fix config default value
zkronos73 Mar 1, 2023
65ba4e3
add config in compile recursive call
krlosMata Mar 6, 2023
5309af3
add control over constants/numeric values
zkronos73 Mar 6, 2023
bca5604
Merge pull request #57 from 0xPolygonHermez/feature/fix-config-compiler
zkronos73 Mar 15, 2023
da6a7ff
feature to control left assignments required
zkronos73 Mar 16, 2023
c79b046
fix typo
zkronos73 Mar 16, 2023
6d19c8b
Merge pull request #58 from 0xPolygonHermez/feature/control-constants…
zkronos73 Mar 16, 2023
231829b
Merge pull request #59 from 0xPolygonHermez/feature/check-left-assign…
zkronos73 Mar 16, 2023
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
32 changes: 11 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,23 @@ Generate json file from zkasm file:
```sh
$ node src/zkasm.js <input.zkasm> -o <output.json>
```
Generate C++ code from zkasm file (C++ files will be generated in the ./main_exec_generated folder):
For test purposes (partial inclusion of files):
- allowUndefinedLabels: Allows to leave labels undefined.
- allowOverwriteLabels: Allows to overwrite labels.
- allowUndefinedVariables: Allows to leave variables without declaration (undefined)

```sh
$ node src/zkasm.js <input.zkasm> -c
node src/zkasm.js <input.zkasm> -o <output.json> -t allowUndefinedLabels -t allowOverwriteLabels -t allowUndefinedVariables
```

## Instruction

### ROTL_C
Left rotate one register (4 bytes) of C, only valid for register C.
`````
ROTL_C => C
`````
ROTL_C is as readonly register, can be combined with other elements. ROTL_C is C 4 bytes left rotated.
`````
ROTL_C + 2 => A
`````
Example:
`````
0x101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2Fn => C
ROTL_C => A
0x1415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F10111213n: ASSERT
`````
## Instructions

In this [link](instructions.md) found more information about instructions.

## License

### Copyright
Polygon `zkasmcom` was developed by Polygon. While we plan to adopt an open source license, we haven’t selected one yet, so all rights are reserved for the time being. Please reach out to us if you have thoughts on licensing.
Polygon `zkasmcom` was developed by Polygon. While we plan to adopt an open source license, we haven’t selected one yet, so all rights are reserved for the time being. Please reach out to us if you have thoughts on licensing.

### Disclaimer
This code has not yet been audited, and should not be used in any production systems.
96 changes: 47 additions & 49 deletions build/command_parser.js

Large diffs are not rendered by default.

403 changes: 189 additions & 214 deletions build/zkasm_parser.js

Large diffs are not rendered by default.

223 changes: 223 additions & 0 deletions instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
# zkVM instructions

### MLOAD(addr)

op = mem(addr)

addr = SP | SP++ | SP-- | SP+offset | SP-offset | SYS:E+offset | SYS:E+offset | SYS:E-offset | SYS:E | MEM:E | MEM:E+offset | MEM:E-offset | STACK:E | STACK:E+offset | STACK:E-offset | variable | variable + E | variable + E

### MSTORE(addr)

mem(addr) = op

### SLOAD
key0 = [C0, C1, C2, C3, C4, C5, C6, C7]
key1 = [A0, A1, A2, A3, A4, A5, B0, B1]
key = HP(key1, HP(key0))
op = storage.get(SR, key)

where:
storage.get(root, key) -> value

### SSTORE

key0 = [C0, C1, C2, C3, C4, C5, C6, C7]
key1 = [A0, A1, A2, A3, A4, A5, B0, B1]
value = [D0, D1, D2, D3, D4, D5, D6, D7]
SR’ = storage.get(SR, key, value)

where:
storage.set(oldRoot, key, newValue) -> newRoot

### HASHK(hashId)

hashK[hashId][HASHPOS..HASHPOS+D-1] = op[0..D-1]
HASHPOS := HASHPOS + D
hashId = number | E

### HASHK1(hashId)

hashK1[hashId][HASHPOS] = op[0]
HASHPOS := HASHPOS + 1

### HASHKLEN(hashId)

hashK[hashId].len = op

### HASHKDIGEST(hashId)

hashK[hashId].digest = op

### HASHP(hashId)

hashP[hashId][HASHPOS..HASHPOS+D-1] = op[0..D-1]

### HASHP1(hashId)

hashP[hashId][HASHPOS] = op[0]

### HASHPLEN(hashId)

hashP[hashId].len = op

### HASHPDIGEST(hashId)

hashP[hashId].digest = op

### ARITH

A*B + C = D*2**256 + op

### ARITH_ECADD_DIFFERENT

Addition of two secp256k1 elliptic curve points (points are different)
(A, B) + (C, D) = (E, op)

### ARITH_ECADD_SAME

Addition of two secp256k1 elliptic curve points (points are equals)
(A, B) + (A, B) = (E, op)


### ASSERT

A = op

### ADD SUB LT SLT EQ AND OR XOR

op = A ~BinOp~ B

### MEM_ALIGN_RD

M0=A, M1=B, V=op, Offset=C

M0 = 256bit word read in position x of ZKEVM memory (32x EVM)
M1 = 256bit word read in position x+1 of ZKEVM memory (32x+1 EVM)
Offset = 0..31 bytes
V = value of 256 bits

### MEM_ALIGN_WR

M0=A, M1=B, V=op Offset=C, W0=D W1=E
W0 = 256bit word to write position x of ZKEVM memory (32x EVM)
W1 = 256bit word to write in position x+1 of ZKEVM memory (32x+1 EVM)


### MEM_ALIGN_WR8

M0=A, V=op, Offset=C, W0=D
W0 = 256bit word to write position x of ZKEVM memory (32x EVM)
V = value of 8 bits

### JMP (jmpaddr)

zkPC' = jmpaddr
jmpaddr = label | RR | E | reference + E | reference + RR
reference = @label

### JMPN/JMPC/JMPZ/JMPNC/JMPNZ (jmpaddr[,elseaddr])

JMPN: jump if op[0] was negative
JMPC: jump if carry bit, only use with binary operations
JMPZ: jump if op[0] was zero
JMPNC: jump if no carry bit, only use with binary operations
JMPNZ: jump if op[0] was different of zero

### CALL (calladdr)

calladdr = label | reference + RR | reference + E
RR' = zkPC + 1
JMP(calladdr)

### RETURN

JMP(RR)

### ROTL_C

ROTL_C' = C[6] C[5] C[4] C[3] C[2] C[1] C[0] C[7]

### REPEAT(RCX)

RCX != 0 => RCX' = RCX - 1
RCX != 0 => zkPC = zkPC
REPEAT was executed at least one time

### CNT_ARITH, CNT_BINARY, CNT_KECCAK_F, CNT_MEM_ALIGN, CNT_PADDING_PG, CNT_POSEIDON_G

ReadOnly counters

### CONST, CONSTL %constname = expression

define constants
const set lsr (op0) and reset the rest (op1,....,op7)
constl set 8 registers (op0, op1, op2, ..,op7)

## Registers
- Each element is a Goldilocks prime Field number

### A, B, C, D, E
- generic purpose registers
- Array of 8 elements `[V0, V1,..., V7]`

### SR
- Array of 8 elements `[V0, V1,..., V7]`
- State root

### CTX
- 1 element
- Context
- Used to move through zkEVM memory

### SP
- 1 element
- Stack Pointer
- Used to move through zkEVM memory

### PC
- 1 element
- Program Counter
- Used to move through zkEVM memory

### GAS
- 1 element
- Gas in a transaction

### RR
- 1 element
- Return register
- Saves origin `zkPC` in `RR` when a `CALL` instruction is performed
- `RETURN` will load `RR` into future `zkPC`

### zkPC
- 1 element
- zk pogram counter

### STEP
- 1 element
- number of instruction done

### MAXMEM
- 1 element
- maximum memory

### HASHPOS
- 1 element
- used to set/get bytes from poseidon/keccaks bytes

### ROTL_C
- Array of 8 elements `[V0, V1,..., V7]`. Each element is a Goldilocks prime Field number
- Rotate Left Register: `ROTL_C = [C[7], C[0], ..., C[6]]`

### RCX
- 1 element
- Used to repeat instructions

### zk-counters
- Keeps track of zk-counters
- `CNT_ARITH`
- `CNT_BINARY`
- `CNT_KECCAK_F`
- `CNT_MEM_ALIGN`
- `CNT_PADDING_PG`
- `CNT_POSEIDON_G`
Loading