snarkvm_synthesizer_program/logic/instruction/opcode/mod.rs
1// Copyright (c) 2019-2026 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use console::network::prelude::*;
17
18/// The `Opcode` enum stores the mnemonic for the instruction.
19#[derive(Copy, Clone, PartialEq, Eq, Hash)]
20pub enum Opcode {
21 /// The opcode is for an assert operation (i.e. `assert`).
22 Assert(&'static str),
23 /// The opcode is for an async call operation (i.e. `async`).
24 Async,
25 /// The opcode is for a call operation (i.e. `call`).
26 Call(&'static str),
27 /// The opcode is for a cast operation (i.e. `cast`).
28 Cast(&'static str),
29 /// The opcode is for a finalize command (i.e. `increment`).
30 Command(&'static str),
31 /// The opcode is for a commit operation (i.e. `commit.psd4`).
32 Commit(&'static str),
33 /// The opcdode is a for a deserialize operation (i.e. `deserialize.bytes.raw`).
34 Deserialize(&'static str),
35 /// The opcode is for ECDSA signature verification (i.e. `ecdsa.verify.keccak`).
36 ECDSA(&'static str),
37 /// The opcode is for a get.record.dynamic operation (i.e. `get.record.dynamic`).
38 GetRecordDynamic(&'static str),
39 /// The opcode is for a hash operation (i.e. `hash.psd4`).
40 Hash(&'static str),
41 /// The opcode is for an 'is' operation (i.e. `is.eq`).
42 Is(&'static str),
43 /// The opcode is for a literal operation (i.e. `add`).
44 Literal(&'static str),
45 /// The opcode is for a serialize operation. (i.e. `serialize.bytes.raw`).
46 Serialize(&'static str),
47 /// The opcode is for signature verification (i.e. `sign.verify`).
48 Sign(&'static str),
49 /// The opcode is for snark verification (i.e. `snark.verify`).
50 Snark(&'static str),
51}
52
53impl Deref for Opcode {
54 type Target = &'static str;
55
56 /// Returns the opcode as a string.
57 fn deref(&self) -> &Self::Target {
58 match self {
59 Opcode::Assert(opcode) => opcode,
60 Opcode::Async => &"async",
61 Opcode::Call(opcode) => opcode,
62 Opcode::Cast(opcode) => opcode,
63 Opcode::Command(opcode) => opcode,
64 Opcode::Commit(opcode) => opcode,
65 Opcode::Deserialize(opcode) => opcode,
66 Opcode::ECDSA(opcode) => opcode,
67 Opcode::GetRecordDynamic(opcode) => opcode,
68 Opcode::Hash(opcode) => opcode,
69 Opcode::Is(opcode) => opcode,
70 Opcode::Literal(opcode) => opcode,
71 Opcode::Serialize(opcode) => opcode,
72 Opcode::Sign(opcode) => opcode,
73 Opcode::Snark(opcode) => opcode,
74 }
75 }
76}
77
78impl Debug for Opcode {
79 /// Prints the opcode as a string.
80 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
81 Display::fmt(self, f)
82 }
83}
84
85impl Display for Opcode {
86 /// Prints the opcode as a string, i.e. `add`.
87 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
88 match self {
89 Self::Assert(opcode) => write!(f, "{opcode}"),
90 Self::Async => write!(f, "{}", self.deref()),
91 Self::Call(opcode) => write!(f, "{opcode}"),
92 Self::Cast(opcode) => write!(f, "{opcode}"),
93 Self::Command(opcode) => write!(f, "{opcode}"),
94 Self::Commit(opcode) => write!(f, "{opcode}"),
95 Self::Deserialize(opcode) => write!(f, "{opcode}"),
96 Self::ECDSA(opcode) => write!(f, "{opcode}"),
97 Self::GetRecordDynamic(opcode) => write!(f, "{opcode}"),
98 Self::Hash(opcode) => write!(f, "{opcode}"),
99 Self::Is(opcode) => write!(f, "{opcode}"),
100 Self::Literal(opcode) => write!(f, "{opcode}"),
101 Self::Serialize(opcode) => write!(f, "{opcode}"),
102 Self::Sign(opcode) => write!(f, "{opcode}"),
103 Self::Snark(opcode) => write!(f, "{opcode}"),
104 }
105 }
106}