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 new ZK opcodes #870

Merged
merged 31 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
56e31f5
Add boilerplate new opcodes
AurelienFT Nov 14, 2024
87dd242
Format
AurelienFT Nov 14, 2024
9705f6d
Update changelog
AurelienFT Nov 14, 2024
d53e060
Add more boilerplate
AurelienFT Nov 14, 2024
273ffda
Add code for ec add and ec mul
AurelienFT Nov 14, 2024
deac347
Update reserved_registers
AurelienFT Nov 14, 2024
59aae8e
Add source code pairing
AurelienFT Nov 15, 2024
d455d86
use alloc vec
AurelienFT Nov 15, 2024
cc22760
Add gas costs charge
AurelienFT Nov 15, 2024
bd193f2
Add tests for new zk opcodes
AurelienFT Nov 15, 2024
09f62c8
remove forget dbg
AurelienFT Nov 15, 2024
6615559
Add more test case for zk opcodes
AurelienFT Nov 15, 2024
0d41300
Remove old todo
AurelienFT Nov 15, 2024
c8b88ee
Fix clippy in tests
AurelienFT Nov 15, 2024
d59b18d
Change elliptic curve point error variant
AurelienFT Nov 18, 2024
c0a14ff
Change gas costs to v5 because v4 is already released
AurelienFT Nov 19, 2024
4cf15da
format
AurelienFT Nov 19, 2024
c8bc192
Update ec pairing to ouput directly in the register and fix the test
AurelienFT Nov 19, 2024
519e8f3
Update comments on opcodes
AurelienFT Nov 19, 2024
b25ef51
Update zk opcode to match the new spec
AurelienFT Nov 20, 2024
edc190f
Update EPAR prototype
AurelienFT Nov 22, 2024
d12b6fb
update gas costs functions
AurelienFT Nov 23, 2024
c83a7a1
Update default gas ecop based on benches
AurelienFT Nov 23, 2024
00ffd60
Add tests with instructions
AurelienFT Nov 25, 2024
04b8d30
Change gas epar
AurelienFT Nov 25, 2024
4493b33
Fomat
AurelienFT Nov 25, 2024
1741110
Update fuel-vm/src/interpreter/crypto.rs
AurelienFT Nov 28, 2024
ff0bbf7
cleanup for add zk opcodes PR (#874)
Dentosal Nov 28, 2024
498395c
Add some doc about the usage of operation symbols + and *
AurelienFT Nov 28, 2024
5f8b8e8
Add memory overflow tests
AurelienFT Nov 28, 2024
003ade7
fix clippy
AurelienFT Nov 28, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
- [870](https://github.com/FuelLabs/fuel-vm/pull/870): Add 3 new ZK-related opcodes: eadd (ecAdd on EVM), emul (ecMul on EVM), epar (ecPairing on EVM)

### Fixed
- [860](https://github.com/FuelLabs/fuel-vm/pull/860): Fixed missing fuzzing coverage report in CI.

Expand Down
11 changes: 8 additions & 3 deletions fuel-asm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ impl_instructions! {
0xba BSIZ bsiz [dst: RegId blob_id_ptr: RegId]
"Load blob as data"
0xbb BLDD bldd [dst_ptr: RegId blob_id_ptr: RegId offset: RegId len: RegId]
"Given some curve, performs addition of two points"
0xbc EADD eadd [dst: RegId curve_id: RegId point1_ptr: RegId point2_ptr: RegId]
"Given some curve, performs a scalar multiplication"
0xbd EMUL emul [dst: RegId curve_id: RegId point_ptr: RegId scalar_ptr: RegId]
"Given some curve, performs bilinear function on groups"
0xbe EPAR epar [success: RegId curve_id: RegId num_points: RegId points_ptr: RegId]
}

impl Instruction {
Expand Down Expand Up @@ -701,9 +707,8 @@ impl Opcode {
| K256 | S256 | NOOP | FLAG | ADDI | ANDI | DIVI | EXPI | MODI | MULI
| MLDV | ORI | SLLI | SRLI | SUBI | XORI | JNEI | LB | LW | SB | SW
| MCPI | MCLI | GM | MOVI | JNZI | JI | JMP | JNE | JMPF | JMPB | JNZF
| JNZB | JNEF | JNEB | CFEI | CFSI | CFE | CFS | GTF | LDC | BSIZ | BLDD => {
true
}
| JNZB | JNEF | JNEB | CFEI | CFSI | CFE | CFS | GTF | LDC | BSIZ | BLDD
| EADD | EMUL | EPAR => true,
_ => false,
}
}
Expand Down
5 changes: 5 additions & 0 deletions fuel-asm/src/panic_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ enum_from! {
BlobIdAlreadyUploaded = 0x37,
/// Active gas costs do not define the cost for this instruction.
GasCostNotDefined = 0x38,
/// The curve id is not supported.
UnsupportedCurveId = 0x39,
// TODO: Maybe add more different errors
/// Read alt_bn_128 curve point is invalid.
InvalidAltBn128Point = 0x3a,
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
36 changes: 36 additions & 0 deletions fuel-tx/src/transaction/consensus_parameters/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,24 @@ impl GasCostsValues {
}
}

pub fn eadd(&self) -> Result<Word, GasCostNotDefined> {
match self {
GasCostsValues::V1(_) => Err(GasCostNotDefined),
GasCostsValues::V2(_) => Err(GasCostNotDefined),
GasCostsValues::V3(_) => Err(GasCostNotDefined),
GasCostsValues::V4(v4) => Ok(v4.eadd),
}
}

pub fn emul(&self) -> Result<Word, GasCostNotDefined> {
match self {
GasCostsValues::V1(_) => Err(GasCostNotDefined),
GasCostsValues::V2(_) => Err(GasCostNotDefined),
GasCostsValues::V3(_) => Err(GasCostNotDefined),
GasCostsValues::V4(v4) => Ok(v4.emul),
}
}

pub fn aloc(&self) -> DependentCost {
match self {
GasCostsValues::V1(v1) => DependentCost::HeavyOperation {
Expand Down Expand Up @@ -1098,6 +1116,15 @@ impl GasCostsValues {
}
}

pub fn epar(&self) -> Result<DependentCost, GasCostNotDefined> {
match self {
GasCostsValues::V1(_v1) => Err(GasCostNotDefined),
GasCostsValues::V2(_v2) => Err(GasCostNotDefined),
GasCostsValues::V3(_v3) => Err(GasCostNotDefined),
GasCostsValues::V4(v4) => Ok(v4.epar),
}
}

pub fn contract_root(&self) -> DependentCost {
match self {
GasCostsValues::V1(v1) => v1.contract_root,
Expand Down Expand Up @@ -1616,6 +1643,8 @@ pub struct GasCostsValuesV4 {
pub wqmm: Word,
pub xor: Word,
pub xori: Word,
pub eadd: Word,
pub emul: Word,

// Dependent
pub aloc: DependentCost,
Expand Down Expand Up @@ -1643,6 +1672,7 @@ pub struct GasCostsValuesV4 {
pub smo: DependentCost,
pub srwq: DependentCost,
pub swwq: DependentCost,
pub epar: DependentCost,

// Non-opcode costs
pub contract_root: DependentCost,
Expand Down Expand Up @@ -2509,6 +2539,8 @@ impl GasCostsValuesV4 {
wqmm: 0,
xor: 0,
xori: 0,
eadd: 0,
emul: 0,
aloc: DependentCost::free(),
bsiz: DependentCost::free(),
bldd: DependentCost::free(),
Expand All @@ -2533,6 +2565,7 @@ impl GasCostsValuesV4 {
smo: DependentCost::free(),
srwq: DependentCost::free(),
swwq: DependentCost::free(),
epar: DependentCost::free(),

// Non-opcode costs
contract_root: DependentCost::free(),
Expand Down Expand Up @@ -2630,6 +2663,8 @@ impl GasCostsValuesV4 {
wqmm: 1,
xor: 1,
xori: 1,
eadd: 1,
emul: 1,
aloc: DependentCost::unit(),
bsiz: DependentCost::unit(),
bldd: DependentCost::unit(),
Expand All @@ -2654,6 +2689,7 @@ impl GasCostsValuesV4 {
smo: DependentCost::unit(),
srwq: DependentCost::unit(),
swwq: DependentCost::unit(),
epar: DependentCost::unit(),

// Non-opcode costs
contract_root: DependentCost::unit(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ pub fn default_gas_costs() -> GasCostsValues {
wqmm: 3,
xor: 1,
xori: 1,
// TODO: Change
eadd: 1,
emul: 1,
epar: DependentCost::LightOperation {
base: 1,
units_per_gas: 1,
},
aloc: DependentCost::LightOperation {
base: 2,
units_per_gas: 214,
Expand Down
2 changes: 2 additions & 0 deletions fuel-vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ anyhow = { version = "1.0", optional = true }
async-trait = "0.1"
backtrace = { version = "0.3", optional = true } # requires debug symbols to work
bitflags = { workspace = true }
bn = { package = "substrate-bn", version = "0.6", default-features = false }
derivative = "2.2"
derive_more = { version = "0.99", default-features = false, features = [
"display",
Expand Down Expand Up @@ -62,6 +63,7 @@ fuel-vm = { path = ".", default-features = false, features = [
"random",
] }
futures = "0.3.28"
hex = "0.4.3"
ntest = "0.9.2"
num-integer = "0.1.45"
p256 = "0.13"
Expand Down
Loading
Loading