snarkvm_synthesizer_program/logic/instruction/opcode/
mod.rs

1// Copyright (c) 2019-2025 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,
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 a hash operation (i.e. `hash.psd4`).
36    Hash(&'static str),
37    /// The opcode is for an 'is' operation (i.e. `is.eq`).
38    Is(&'static str),
39    /// The opcode is for a literal operation (i.e. `add`).
40    Literal(&'static str),
41    /// The opcode is for a serialize operation. (i.e. `serialize.bytes.raw`).
42    Serialize(&'static str),
43    /// The opcode is for signature verification (i.e. `sign.verify`).
44    Sign(&'static str),
45    /// The opcode is for ECDSA signature verification (i.e. `ecdsa.verify.keccak`).
46    ECDSA(&'static str),
47}
48
49impl Deref for Opcode {
50    type Target = &'static str;
51
52    /// Returns the opcode as a string.
53    fn deref(&self) -> &Self::Target {
54        match self {
55            Opcode::Assert(opcode) => opcode,
56            Opcode::Async => &"async",
57            Opcode::Call => &"call",
58            Opcode::Cast(opcode) => opcode,
59            Opcode::Command(opcode) => opcode,
60            Opcode::Commit(opcode) => opcode,
61            Opcode::Deserialize(opcode) => opcode,
62            Opcode::Hash(opcode) => opcode,
63            Opcode::Is(opcode) => opcode,
64            Opcode::Literal(opcode) => opcode,
65            Opcode::Serialize(opcode) => opcode,
66            Opcode::Sign(opcode) => opcode,
67            Opcode::ECDSA(opcode) => opcode,
68        }
69    }
70}
71
72impl Debug for Opcode {
73    /// Prints the opcode as a string.
74    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
75        Display::fmt(self, f)
76    }
77}
78
79impl Display for Opcode {
80    /// Prints the opcode as a string, i.e. `add`.
81    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
82        match self {
83            Self::Assert(opcode) => write!(f, "{opcode}"),
84            Self::Async => write!(f, "{}", self.deref()),
85            Self::Call => write!(f, "{}", self.deref()),
86            Self::Cast(opcode) => write!(f, "{opcode}"),
87            Self::Command(opcode) => write!(f, "{opcode}"),
88            Self::Commit(opcode) => write!(f, "{opcode}"),
89            Self::Deserialize(opcode) => write!(f, "{opcode}"),
90            Self::Hash(opcode) => write!(f, "{opcode}"),
91            Self::Is(opcode) => write!(f, "{opcode}"),
92            Self::Literal(opcode) => write!(f, "{opcode}"),
93            Self::Serialize(opcode) => write!(f, "{opcode}"),
94            Self::Sign(opcode) => write!(f, "{opcode}"),
95            Self::ECDSA(opcode) => write!(f, "{opcode}"),
96        }
97    }
98}