snarkvm_circuit_program/id/
mod.rs

1// Copyright 2024 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
16#[cfg(test)]
17use snarkvm_circuit_types::environment::assert_scope;
18
19mod to_address;
20mod to_bits;
21mod to_fields;
22
23use crate::Identifier;
24use snarkvm_circuit_network::Aleo;
25use snarkvm_circuit_types::{Address, Boolean, Field, environment::prelude::*};
26
27/// A program ID is of the form `{name}.{network}`.
28/// If no `network`-level domain is specified, the default network is used.
29#[derive(Clone)]
30pub struct ProgramID<A: Aleo> {
31    /// The program name.
32    name: Identifier<A>,
33    /// The network-level domain (NLD).
34    network: Identifier<A>,
35}
36
37#[cfg(feature = "console")]
38impl<A: Aleo> Inject for ProgramID<A> {
39    type Primitive = console::ProgramID<A::Network>;
40
41    /// Injects a program ID with the given primitive.
42    fn new(_: Mode, id: Self::Primitive) -> Self {
43        Self {
44            name: Identifier::new(Mode::Constant, *id.name()),
45            network: Identifier::new(Mode::Constant, *id.network()),
46        }
47    }
48}
49
50impl<A: Aleo> ProgramID<A> {
51    /// Returns the program name.
52    #[inline]
53    pub const fn name(&self) -> &Identifier<A> {
54        &self.name
55    }
56
57    /// Returns the network-level domain (NLD).
58    #[inline]
59    pub const fn network(&self) -> &Identifier<A> {
60        &self.network
61    }
62}
63
64#[cfg(feature = "console")]
65impl<A: Aleo> Eject for ProgramID<A> {
66    type Primitive = console::ProgramID<A::Network>;
67
68    /// Ejects the mode of the program ID.
69    fn eject_mode(&self) -> Mode {
70        Mode::Constant
71    }
72
73    /// Ejects a program ID into a primitive.
74    fn eject_value(&self) -> Self::Primitive {
75        match console::ProgramID::try_from((self.name.eject_value(), self.network.eject_value())) {
76            Ok(id) => id,
77            Err(error) => A::halt(format!("Failed to eject program ID: {error}")),
78        }
79    }
80}
81
82impl<A: Aleo> Equal<Self> for ProgramID<A> {
83    type Output = Boolean<A>;
84
85    /// Returns `true` if `self` and `other` are equal.
86    fn is_equal(&self, other: &Self) -> Self::Output {
87        self.name.is_equal(&other.name) & (self.network.is_equal(&other.network))
88    }
89
90    /// Returns `true` if `self` and `other` are **not** equal.
91    fn is_not_equal(&self, other: &Self) -> Self::Output {
92        self.name.is_not_equal(&other.name) | (self.network.is_not_equal(&other.network))
93    }
94}