snarkvm_synthesizer_program/logic/instruction/opcode/
mod.rs

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