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

Add ZK opcodes definition #619

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion spell-check-custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,8 @@ OOB
unspendable
priori
padding
incentivize
incentivize
EIPS
eip
eips
ethereum
54 changes: 54 additions & 0 deletions src/fuel-vm/instruction-set.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
- [`ED19`: EdDSA curve25519 verification](#ed19-eddsa-curve25519-verification)
- [`K256`: keccak-256](#k256-keccak-256)
- [`S256`: SHA-2-256](#s256-sha-2-256)
- [`ECOP`: Elliptic curve operation](#ecop-elliptic-curve-point-operation)
- [`EPAR`: Elliptic curve point pairing check](#epar-elliptic-curve-point-pairing-check)
- [Other Instructions](#other-instructions)
- [`ECAL`: Call external function](#ecal-call-external-function)
- [`FLAG`: Set flags](#flag-set-flags)
Expand Down Expand Up @@ -2370,6 +2372,58 @@ Panic if:
- `$rB + $rC` overflows or `> VM_MAX_RAM`
- The memory range `MEM[$rA, 32]` does not pass [ownership check](./index.md#ownership)

### `ECOP`: Elliptic curve point operation

| | |
|-------------|-----------------------------------------------------|
| Description | This opcode regroups all arithmetic operations that can be perform on elliptic curve points. `$rB` defines the curve used. `$rC` defines the type of operation to perform. `$rD` points to the start of the bytes of the operation inputs in memory. `$rA` points to the start of the bytes of the result in memory. |
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved
| Operation | ```MEM[$rA, X] = ecop(MEM[$rD, Y]);``` |
| Syntax | `ecop $rA, $rB, $rC, $rD` |
| Encoding | `0x00 rA rB rC rD` |
| Notes | For now, only `$rB` = 0 is accepted |

#### Curve ID `$rB` possible values

- `0`: `alt_bn128` elliptic curve.

#### Operation type `$rC` supported

- `0`: two points addition
- `1`: one point and one scalar multiplication

#### Encoding of points and results by curve ID and operation type

- 1P = one point = (X, Y) = ([32 bytes], [32 bytes])
- 1S = one scalar = X = [32 bytes]

| `$rB` Curve ID | `$rC` Operation type | `$rA` format | `$rD` format |
|----------------|----------------------|----------------------|----------------------------|
| `0` | `0` | `MEM[$rA, 64]` `1P` | `MEM[$rC, 128]` `1P1P` |
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved
| `0` | `1` | `MEM[$rA, 64]` `1P` | `MEM[$rC, 96]` `1P1S` |

### `EPAR`: Elliptic curve point pairing check

| | |
|-------------|-----------------------------------------------------|
| Description | Perform a specific pairing type within a specific curve both identified by `$rB`. `$rC` defines the number of batch of groups. `$rD` define where the bytes of the groups of points start. `$rA` contains either `0` or `1` as the result of the pairing. |
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved
| Operation | ```$rA = epar(MEM[$rD, X * $rC]);``` |
| Syntax | `epar $rA, $rB, $rC, $rD` |
| Encoding | `0x00 rA rB rC rD` |
| Notes | For now, only `$rB` = 0 is accepted. |

<!-- markdownlint-disable-next-line no-duplicate-header -->
#### Curve/Pairing ID `$rB` possible values

- `0`: optimal ate pairing on `alt_bn128` elliptic curve.

#### Encoding of points by curve ID and check type

- 1P = one point = (X, Y) = ([32 bytes], [32 bytes])

| `$rB` Curve / Pairing ID | `$rD` format |
|---------------------------|----------------------------|
| `0` | `MEM[$rD, (64 + 64 + 64) * $rC]` Each element is `1P1P1P` (three points coordinates) (192 bytes) |

## Other Instructions

All these instructions advance the program counter `$pc` by `4` after performing their operation.
Expand Down
Loading