Skip to content

Commit 8c5afa3

Browse files
vaivaswathaxgreenxIGI-111
authored
ZK opcodes in the Sway assembly (#6876)
Depends on #6851. --------- Co-authored-by: green <xgreenx9999@gmail.com> Co-authored-by: IGI-111 <igi-111@protonmail.com>
1 parent 95a86ec commit 8c5afa3

File tree

10 files changed

+128
-1
lines changed

10 files changed

+128
-1
lines changed

forc-plugins/forc-client/src/util/account.rs

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub enum ForcClientAccount {
2222
KmsSigner(AwsSigner),
2323
}
2424

25-
#[async_trait]
2625
impl Account for ForcClientAccount {
2726
fn add_witnesses<Tb: TransactionBuilder>(&self, tb: &mut Tb) -> Result<()> {
2827
tb.add_signer(self.clone())?;

sway-ast/src/expr/op_code.rs

+2
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ define_op_codes!(
337337
(Ed19, Ed19Opcode, "ed19", (addr: reg, sig: reg, hash: reg, len: reg)),
338338
(K256, K256Opcode, "k256", (addr: reg, data: reg, size: reg)),
339339
(S256, S256Opcode, "s256", (addr: reg, data: reg, size: reg)),
340+
(ECOP, ECOPOpcode, "ecop", (dst_addr: reg, curve: reg, operation: reg, src_addr: reg)),
341+
(EPAR, EPAROpcode, "epar", (ret: reg, curve: reg, groups_of_points: reg, addr: reg)),
340342
/* Other Instructions */
341343
(Flag, FlagOpcode, "flag", (value: reg)),
342344
(Gm, GmOpcode, "gm", (ret: reg, op: imm)),

sway-core/src/asm_lang/allocated_ops.rs

+22
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,18 @@ pub(crate) enum AllocatedOpcode {
267267
),
268268
K256(AllocatedRegister, AllocatedRegister, AllocatedRegister),
269269
S256(AllocatedRegister, AllocatedRegister, AllocatedRegister),
270+
ECOP(
271+
AllocatedRegister,
272+
AllocatedRegister,
273+
AllocatedRegister,
274+
AllocatedRegister,
275+
),
276+
EPAR(
277+
AllocatedRegister,
278+
AllocatedRegister,
279+
AllocatedRegister,
280+
AllocatedRegister,
281+
),
270282

271283
/* Other Instructions */
272284
FLAG(AllocatedRegister),
@@ -392,6 +404,8 @@ impl AllocatedOpcode {
392404
ED19(_r1, _r2, _r3, _r4) => vec![],
393405
K256(_r1, _r2, _r3) => vec![],
394406
S256(_r1, _r2, _r3) => vec![],
407+
ECOP(_r1, _r2, _r3, _r4) => vec![],
408+
EPAR(r1, _r2, _r3, _r4) => vec![r1],
395409

396410
/* Other Instructions */
397411
FLAG(_r1) => vec![],
@@ -521,6 +535,8 @@ impl fmt::Display for AllocatedOpcode {
521535
ED19(a, b, c, d) => write!(fmtr, "ed19 {a} {b} {c} {d}"),
522536
K256(a, b, c) => write!(fmtr, "k256 {a} {b} {c}"),
523537
S256(a, b, c) => write!(fmtr, "s256 {a} {b} {c}"),
538+
ECOP(a, b, c, d) => write!(fmtr, "ecop {a} {b} {c} {d}"),
539+
EPAR(a, b, c, d) => write!(fmtr, "epar {a} {b} {c} {d}"),
524540

525541
/* Other Instructions */
526542
FLAG(a) => write!(fmtr, "flag {a}"),
@@ -751,6 +767,12 @@ impl AllocatedOp {
751767
}
752768
K256(a, b, c) => op::K256::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(),
753769
S256(a, b, c) => op::S256::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(),
770+
ECOP(a, b, c, d) => {
771+
op::ECOP::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into()
772+
}
773+
EPAR(a, b, c, d) => {
774+
op::EPAR::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into()
775+
}
754776

755777
/* Other Instructions */
756778
FLAG(a) => op::FLAG::new(a.to_reg_id()).into(),

sway-core/src/asm_lang/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,14 @@ impl Op {
674674
let (r1, r2, r3) = three_regs(handler, args, immediate, whole_op_span)?;
675675
VirtualOp::S256(r1, r2, r3)
676676
}
677+
"ecop" => {
678+
let (r1, r2, r3, r4) = four_regs(handler, args, immediate, whole_op_span)?;
679+
VirtualOp::ECOP(r1, r2, r3, r4)
680+
}
681+
"epar" => {
682+
let (r1, r2, r3, r4) = four_regs(handler, args, immediate, whole_op_span)?;
683+
VirtualOp::EPAR(r1, r2, r3, r4)
684+
}
677685

678686
/* Other Instructions */
679687
"flag" => {
@@ -1228,6 +1236,8 @@ impl fmt::Display for VirtualOp {
12281236
ED19(a, b, c, d) => write!(fmtr, "ed19 {a} {b} {c} {d}"),
12291237
K256(a, b, c) => write!(fmtr, "k256 {a} {b} {c}"),
12301238
S256(a, b, c) => write!(fmtr, "s256 {a} {b} {c}"),
1239+
ECOP(a, b, c, d) => write!(fmtr, "ecop {a} {b} {c} {d}"),
1240+
EPAR(a, b, c, d) => write!(fmtr, "epar {a} {b} {c} {d}"),
12311241

12321242
/* Other Instructions */
12331243
FLAG(a) => write!(fmtr, "flag {a}"),

sway-core/src/asm_lang/virtual_ops.rs

+46
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ pub(crate) enum VirtualOp {
218218
),
219219
K256(VirtualRegister, VirtualRegister, VirtualRegister),
220220
S256(VirtualRegister, VirtualRegister, VirtualRegister),
221+
ECOP(
222+
VirtualRegister,
223+
VirtualRegister,
224+
VirtualRegister,
225+
VirtualRegister,
226+
),
227+
EPAR(
228+
VirtualRegister,
229+
VirtualRegister,
230+
VirtualRegister,
231+
VirtualRegister,
232+
),
221233

222234
/* Other Instructions */
223235
FLAG(VirtualRegister),
@@ -339,6 +351,8 @@ impl VirtualOp {
339351
ED19(r1, r2, r3, r4) => vec![r1, r2, r3, r4],
340352
K256(r1, r2, r3) => vec![r1, r2, r3],
341353
S256(r1, r2, r3) => vec![r1, r2, r3],
354+
ECOP(r1, r2, r3, r4) => vec![r1, r2, r3, r4],
355+
EPAR(r1, r2, r3, r4) => vec![r1, r2, r3, r4],
342356

343357
/* Other Instructions */
344358
FLAG(r1) => vec![r1],
@@ -407,6 +421,7 @@ impl VirtualOp {
407421
| TIME(_, _)
408422
| GM(_, _)
409423
| GTF(_, _, _)
424+
| EPAR(_, _, _, _)
410425
// Virtual OPs
411426
| LoadDataId(_, _)
412427
| AddrDataId(_, _)
@@ -463,6 +478,7 @@ impl VirtualOp {
463478
| ED19(_, _, _, _)
464479
| K256(_, _, _)
465480
| S256(_, _, _)
481+
| ECOP(_, _, _, _)
466482
| FLAG(_)
467483
// Virtual OPs
468484
| BLOB(_)
@@ -570,6 +586,8 @@ impl VirtualOp {
570586
| TRO(_, _, _, _)
571587
| K256(_, _, _)
572588
| S256(_, _, _)
589+
| ECOP(_, _, _, _)
590+
| EPAR(_, _, _, _)
573591
| GM(_, _)
574592
| GTF(_, _, _)
575593
| BLOB(_)
@@ -687,6 +705,8 @@ impl VirtualOp {
687705
ED19(r1, r2, r3, r4) => vec![r1, r2, r3, r4],
688706
K256(r1, r2, r3) => vec![r1, r2, r3],
689707
S256(r1, r2, r3) => vec![r1, r2, r3],
708+
ECOP(r1, r2, r3, r4) => vec![r1, r2, r3, r4],
709+
EPAR(_r1, r2, r3, r4) => vec![r2, r3, r4],
690710

691711
/* Other Instructions */
692712
FLAG(r1) => vec![r1],
@@ -809,6 +829,8 @@ impl VirtualOp {
809829
ED19(_r1, _r2, _r3, _r4) => vec![],
810830
K256(_r1, _r2, _r3) => vec![],
811831
S256(_r1, _r2, _r3) => vec![],
832+
ECOP(_r1, _r2, _r3, _r4) => vec![],
833+
EPAR(r1, _r2, _r3, _r4) => vec![r1],
812834

813835
/* Other Instructions */
814836
FLAG(_r1) => vec![],
@@ -1256,6 +1278,18 @@ impl VirtualOp {
12561278
update_reg(reg_to_reg_map, r2),
12571279
update_reg(reg_to_reg_map, r3),
12581280
),
1281+
ECOP(r1, r2, r3, r4) => Self::ECOP(
1282+
update_reg(reg_to_reg_map, r1),
1283+
update_reg(reg_to_reg_map, r2),
1284+
update_reg(reg_to_reg_map, r3),
1285+
update_reg(reg_to_reg_map, r4),
1286+
),
1287+
EPAR(r1, r2, r3, r4) => Self::EPAR(
1288+
update_reg(reg_to_reg_map, r1),
1289+
update_reg(reg_to_reg_map, r2),
1290+
update_reg(reg_to_reg_map, r3),
1291+
update_reg(reg_to_reg_map, r4),
1292+
),
12591293

12601294
/* Other Instructions */
12611295
FLAG(r1) => Self::FLAG(update_reg(reg_to_reg_map, r1)),
@@ -1737,6 +1771,18 @@ impl VirtualOp {
17371771
map_reg(&mapping, reg2),
17381772
map_reg(&mapping, reg3),
17391773
),
1774+
ECOP(reg1, reg2, reg3, reg4) => AllocatedOpcode::ECOP(
1775+
map_reg(&mapping, reg1),
1776+
map_reg(&mapping, reg2),
1777+
map_reg(&mapping, reg3),
1778+
map_reg(&mapping, reg4),
1779+
),
1780+
EPAR(reg1, reg2, reg3, reg4) => AllocatedOpcode::EPAR(
1781+
map_reg(&mapping, reg1),
1782+
map_reg(&mapping, reg2),
1783+
map_reg(&mapping, reg3),
1784+
map_reg(&mapping, reg4),
1785+
),
17401786

17411787
/* Other Instructions */
17421788
FLAG(reg) => AllocatedOpcode::FLAG(map_reg(&mapping, reg)),

sway-parse/src/expr/op_code.rs

+12
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ define_op_codes!(
124124
(Ed19, Ed19Opcode, "ed19", (addr, sig, hash, len)),
125125
(K256, K256Opcode, "k256", (addr, data, size)),
126126
(S256, S256Opcode, "s256", (addr, data, size)),
127+
(
128+
ECOP,
129+
ECOPOpcode,
130+
"ecop",
131+
(dst_addr, curve, operation, src_addr)
132+
),
133+
(
134+
EPAR,
135+
EPAROpcode,
136+
"epar",
137+
(ret, curve, groups_of_points, addr)
138+
),
127139
/* Other Instructions */
128140
(Flag, FlagOpcode, "flag", (value)),
129141
(Gm, GmOpcode, "gm", (ret, op)),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[package]]
2+
name = "core"
3+
source = "path+from-root-11C3CFAE4942D9B7"
4+
5+
[[package]]
6+
name = "std"
7+
source = "path+from-root-11C3CFAE4942D9B7"
8+
dependencies = ["core"]
9+
10+
[[package]]
11+
name = "zk_opcodes"
12+
source = "member"
13+
dependencies = ["std"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[project]
2+
authors = ["Fuel Labs <contact@fuel.sh>"]
3+
entry = "main.sw"
4+
license = "Apache-2.0"
5+
name = "zk_opcodes"
6+
7+
[dependencies]
8+
std = { path = "../../../../reduced_std_libs/sway-lib-std-assert" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
script;
2+
3+
fn main() -> bool {
4+
let src_addr: [u64; 32] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
5+
let dst_addr: [u64; 32] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
6+
asm(dst_addr: dst_addr, curve: 0, op: 0, src_addr: src_addr) {
7+
ecop dst_addr curve op src_addr;
8+
}
9+
10+
asm(res, curve: 0, group_of_points: 1, addr: dst_addr) {
11+
epar res curve group_of_points addr;
12+
res: bool
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
category = "compile"

0 commit comments

Comments
 (0)