snarkvm_circuit_program/id/
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
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
37impl<A: Aleo> Inject for ProgramID<A> {
38    type Primitive = console::ProgramID<A::Network>;
39
40    /// Injects a program ID with the given primitive.
41    fn new(_: Mode, id: Self::Primitive) -> Self {
42        Self {
43            name: Identifier::new(Mode::Constant, *id.name()),
44            network: Identifier::new(Mode::Constant, *id.network()),
45        }
46    }
47}
48
49impl<A: Aleo> ProgramID<A> {
50    /// Returns the program name.
51    #[inline]
52    pub const fn name(&self) -> &Identifier<A> {
53        &self.name
54    }
55
56    /// Returns the network-level domain (NLD).
57    #[inline]
58    pub const fn network(&self) -> &Identifier<A> {
59        &self.network
60    }
61}
62
63impl<A: Aleo> Eject for ProgramID<A> {
64    type Primitive = console::ProgramID<A::Network>;
65
66    /// Ejects the mode of the program ID.
67    fn eject_mode(&self) -> Mode {
68        Mode::Constant
69    }
70
71    /// Ejects a program ID into a primitive.
72    fn eject_value(&self) -> Self::Primitive {
73        match console::ProgramID::try_from((self.name.eject_value(), self.network.eject_value())) {
74            Ok(id) => id,
75            Err(error) => A::halt(format!("Failed to eject program ID: {error}")),
76        }
77    }
78}
79
80impl<A: Aleo> Equal<Self> for ProgramID<A> {
81    type Output = Boolean<A>;
82
83    /// Returns `true` if `self` and `other` are equal.
84    fn is_equal(&self, other: &Self) -> Self::Output {
85        self.name.is_equal(&other.name) & (self.network.is_equal(&other.network))
86    }
87
88    /// Returns `true` if `self` and `other` are **not** equal.
89    fn is_not_equal(&self, other: &Self) -> Self::Output {
90        self.name.is_not_equal(&other.name) | (self.network.is_not_equal(&other.network))
91    }
92}