Skip to content

Commit

Permalink
ZK opcodes in the Sway assembly (#6876)
Browse files Browse the repository at this point in the history
Depends on #6851.

---------

Co-authored-by: green <[email protected]>
Co-authored-by: IGI-111 <[email protected]>
  • Loading branch information
3 people authored Feb 3, 2025
1 parent 95a86ec commit 8c5afa3
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 1 deletion.
1 change: 0 additions & 1 deletion forc-plugins/forc-client/src/util/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub enum ForcClientAccount {
KmsSigner(AwsSigner),
}

#[async_trait]
impl Account for ForcClientAccount {
fn add_witnesses<Tb: TransactionBuilder>(&self, tb: &mut Tb) -> Result<()> {
tb.add_signer(self.clone())?;
Expand Down
2 changes: 2 additions & 0 deletions sway-ast/src/expr/op_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ define_op_codes!(
(Ed19, Ed19Opcode, "ed19", (addr: reg, sig: reg, hash: reg, len: reg)),
(K256, K256Opcode, "k256", (addr: reg, data: reg, size: reg)),
(S256, S256Opcode, "s256", (addr: reg, data: reg, size: reg)),
(ECOP, ECOPOpcode, "ecop", (dst_addr: reg, curve: reg, operation: reg, src_addr: reg)),
(EPAR, EPAROpcode, "epar", (ret: reg, curve: reg, groups_of_points: reg, addr: reg)),
/* Other Instructions */
(Flag, FlagOpcode, "flag", (value: reg)),
(Gm, GmOpcode, "gm", (ret: reg, op: imm)),
Expand Down
22 changes: 22 additions & 0 deletions sway-core/src/asm_lang/allocated_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ pub(crate) enum AllocatedOpcode {
),
K256(AllocatedRegister, AllocatedRegister, AllocatedRegister),
S256(AllocatedRegister, AllocatedRegister, AllocatedRegister),
ECOP(
AllocatedRegister,
AllocatedRegister,
AllocatedRegister,
AllocatedRegister,
),
EPAR(
AllocatedRegister,
AllocatedRegister,
AllocatedRegister,
AllocatedRegister,
),

/* Other Instructions */
FLAG(AllocatedRegister),
Expand Down Expand Up @@ -392,6 +404,8 @@ impl AllocatedOpcode {
ED19(_r1, _r2, _r3, _r4) => vec![],
K256(_r1, _r2, _r3) => vec![],
S256(_r1, _r2, _r3) => vec![],
ECOP(_r1, _r2, _r3, _r4) => vec![],
EPAR(r1, _r2, _r3, _r4) => vec![r1],

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

/* Other Instructions */
FLAG(a) => write!(fmtr, "flag {a}"),
Expand Down Expand Up @@ -751,6 +767,12 @@ impl AllocatedOp {
}
K256(a, b, c) => op::K256::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(),
S256(a, b, c) => op::S256::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(),
ECOP(a, b, c, d) => {
op::ECOP::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into()
}
EPAR(a, b, c, d) => {
op::EPAR::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into()
}

/* Other Instructions */
FLAG(a) => op::FLAG::new(a.to_reg_id()).into(),
Expand Down
10 changes: 10 additions & 0 deletions sway-core/src/asm_lang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,14 @@ impl Op {
let (r1, r2, r3) = three_regs(handler, args, immediate, whole_op_span)?;
VirtualOp::S256(r1, r2, r3)
}
"ecop" => {
let (r1, r2, r3, r4) = four_regs(handler, args, immediate, whole_op_span)?;
VirtualOp::ECOP(r1, r2, r3, r4)
}
"epar" => {
let (r1, r2, r3, r4) = four_regs(handler, args, immediate, whole_op_span)?;
VirtualOp::EPAR(r1, r2, r3, r4)
}

/* Other Instructions */
"flag" => {
Expand Down Expand Up @@ -1228,6 +1236,8 @@ impl fmt::Display for VirtualOp {
ED19(a, b, c, d) => write!(fmtr, "ed19 {a} {b} {c} {d}"),
K256(a, b, c) => write!(fmtr, "k256 {a} {b} {c}"),
S256(a, b, c) => write!(fmtr, "s256 {a} {b} {c}"),
ECOP(a, b, c, d) => write!(fmtr, "ecop {a} {b} {c} {d}"),
EPAR(a, b, c, d) => write!(fmtr, "epar {a} {b} {c} {d}"),

/* Other Instructions */
FLAG(a) => write!(fmtr, "flag {a}"),
Expand Down
46 changes: 46 additions & 0 deletions sway-core/src/asm_lang/virtual_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ pub(crate) enum VirtualOp {
),
K256(VirtualRegister, VirtualRegister, VirtualRegister),
S256(VirtualRegister, VirtualRegister, VirtualRegister),
ECOP(
VirtualRegister,
VirtualRegister,
VirtualRegister,
VirtualRegister,
),
EPAR(
VirtualRegister,
VirtualRegister,
VirtualRegister,
VirtualRegister,
),

/* Other Instructions */
FLAG(VirtualRegister),
Expand Down Expand Up @@ -339,6 +351,8 @@ impl VirtualOp {
ED19(r1, r2, r3, r4) => vec![r1, r2, r3, r4],
K256(r1, r2, r3) => vec![r1, r2, r3],
S256(r1, r2, r3) => vec![r1, r2, r3],
ECOP(r1, r2, r3, r4) => vec![r1, r2, r3, r4],
EPAR(r1, r2, r3, r4) => vec![r1, r2, r3, r4],

/* Other Instructions */
FLAG(r1) => vec![r1],
Expand Down Expand Up @@ -407,6 +421,7 @@ impl VirtualOp {
| TIME(_, _)
| GM(_, _)
| GTF(_, _, _)
| EPAR(_, _, _, _)
// Virtual OPs
| LoadDataId(_, _)
| AddrDataId(_, _)
Expand Down Expand Up @@ -463,6 +478,7 @@ impl VirtualOp {
| ED19(_, _, _, _)
| K256(_, _, _)
| S256(_, _, _)
| ECOP(_, _, _, _)
| FLAG(_)
// Virtual OPs
| BLOB(_)
Expand Down Expand Up @@ -570,6 +586,8 @@ impl VirtualOp {
| TRO(_, _, _, _)
| K256(_, _, _)
| S256(_, _, _)
| ECOP(_, _, _, _)
| EPAR(_, _, _, _)
| GM(_, _)
| GTF(_, _, _)
| BLOB(_)
Expand Down Expand Up @@ -687,6 +705,8 @@ impl VirtualOp {
ED19(r1, r2, r3, r4) => vec![r1, r2, r3, r4],
K256(r1, r2, r3) => vec![r1, r2, r3],
S256(r1, r2, r3) => vec![r1, r2, r3],
ECOP(r1, r2, r3, r4) => vec![r1, r2, r3, r4],
EPAR(_r1, r2, r3, r4) => vec![r2, r3, r4],

/* Other Instructions */
FLAG(r1) => vec![r1],
Expand Down Expand Up @@ -809,6 +829,8 @@ impl VirtualOp {
ED19(_r1, _r2, _r3, _r4) => vec![],
K256(_r1, _r2, _r3) => vec![],
S256(_r1, _r2, _r3) => vec![],
ECOP(_r1, _r2, _r3, _r4) => vec![],
EPAR(r1, _r2, _r3, _r4) => vec![r1],

/* Other Instructions */
FLAG(_r1) => vec![],
Expand Down Expand Up @@ -1256,6 +1278,18 @@ impl VirtualOp {
update_reg(reg_to_reg_map, r2),
update_reg(reg_to_reg_map, r3),
),
ECOP(r1, r2, r3, r4) => Self::ECOP(
update_reg(reg_to_reg_map, r1),
update_reg(reg_to_reg_map, r2),
update_reg(reg_to_reg_map, r3),
update_reg(reg_to_reg_map, r4),
),
EPAR(r1, r2, r3, r4) => Self::EPAR(
update_reg(reg_to_reg_map, r1),
update_reg(reg_to_reg_map, r2),
update_reg(reg_to_reg_map, r3),
update_reg(reg_to_reg_map, r4),
),

/* Other Instructions */
FLAG(r1) => Self::FLAG(update_reg(reg_to_reg_map, r1)),
Expand Down Expand Up @@ -1737,6 +1771,18 @@ impl VirtualOp {
map_reg(&mapping, reg2),
map_reg(&mapping, reg3),
),
ECOP(reg1, reg2, reg3, reg4) => AllocatedOpcode::ECOP(
map_reg(&mapping, reg1),
map_reg(&mapping, reg2),
map_reg(&mapping, reg3),
map_reg(&mapping, reg4),
),
EPAR(reg1, reg2, reg3, reg4) => AllocatedOpcode::EPAR(
map_reg(&mapping, reg1),
map_reg(&mapping, reg2),
map_reg(&mapping, reg3),
map_reg(&mapping, reg4),
),

/* Other Instructions */
FLAG(reg) => AllocatedOpcode::FLAG(map_reg(&mapping, reg)),
Expand Down
12 changes: 12 additions & 0 deletions sway-parse/src/expr/op_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ define_op_codes!(
(Ed19, Ed19Opcode, "ed19", (addr, sig, hash, len)),
(K256, K256Opcode, "k256", (addr, data, size)),
(S256, S256Opcode, "s256", (addr, data, size)),
(
ECOP,
ECOPOpcode,
"ecop",
(dst_addr, curve, operation, src_addr)
),
(
EPAR,
EPAROpcode,
"epar",
(ret, curve, groups_of_points, addr)
),
/* Other Instructions */
(Flag, FlagOpcode, "flag", (value)),
(Gm, GmOpcode, "gm", (ret, op)),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "core"
source = "path+from-root-11C3CFAE4942D9B7"

[[package]]
name = "std"
source = "path+from-root-11C3CFAE4942D9B7"
dependencies = ["core"]

[[package]]
name = "zk_opcodes"
source = "member"
dependencies = ["std"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "zk_opcodes"

[dependencies]
std = { path = "../../../../reduced_std_libs/sway-lib-std-assert" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
script;

fn main() -> bool {
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];
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];
asm(dst_addr: dst_addr, curve: 0, op: 0, src_addr: src_addr) {
ecop dst_addr curve op src_addr;
}

asm(res, curve: 0, group_of_points: 1, addr: dst_addr) {
epar res curve group_of_points addr;
res: bool
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
category = "compile"

0 comments on commit 8c5afa3

Please sign in to comment.