Skip to main content

snarkvm_circuit_program/id/
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
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> ProgramID<A> {
38    /// Returns a constant program ID.
39    pub fn constant(id: console::ProgramID<A::Network>) -> Self {
40        Self { name: Identifier::constant(*id.name()), network: Identifier::constant(*id.network()) }
41    }
42
43    /// Returns a public program ID.
44    /// Note: This method should be used cautiously since program IDs typically should be constant.
45    pub fn public(id: console::ProgramID<A::Network>) -> Self {
46        Self { name: Identifier::public(*id.name()), network: Identifier::public(*id.network()) }
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
64impl<A: Aleo> Eject for ProgramID<A> {
65    type Primitive = console::ProgramID<A::Network>;
66
67    /// Ejects the mode of the program ID.
68    fn eject_mode(&self) -> Mode {
69        Mode::Constant
70    }
71
72    /// Ejects a program ID into a primitive.
73    fn eject_value(&self) -> Self::Primitive {
74        match console::ProgramID::try_from((self.name.eject_value(), self.network.eject_value())) {
75            Ok(id) => id,
76            Err(error) => A::halt(format!("Failed to eject program ID: {error}")),
77        }
78    }
79}
80
81impl<A: Aleo> Equal<Self> for ProgramID<A> {
82    type Output = Boolean<A>;
83
84    /// Returns `true` if `self` and `other` are equal.
85    fn is_equal(&self, other: &Self) -> Self::Output {
86        self.name.is_equal(&other.name) & (self.network.is_equal(&other.network))
87    }
88
89    /// Returns `true` if `self` and `other` are **not** equal.
90    fn is_not_equal(&self, other: &Self) -> Self::Output {
91        self.name.is_not_equal(&other.name) | (self.network.is_not_equal(&other.network))
92    }
93}