Skip to main content

snarkvm_circuit_program/function_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
16use crate::{Identifier, ProgramID};
17use snarkvm_circuit_network::Aleo;
18use snarkvm_circuit_types::{Field, U16, environment::prelude::*};
19
20/// Compute the function ID as `Hash(network_id, program_id.len(), program_id, function_name.len(), function_name)`.
21pub fn compute_function_id<A: Aleo>(
22    network_id: &U16<A>,
23    program_id: &ProgramID<A>,
24    function_name: &Identifier<A>,
25) -> Field<A> {
26    A::hash_bhp1024(
27        &(
28            network_id,
29            program_id.name().size_in_bits(),
30            program_id.name(),
31            program_id.network().size_in_bits(),
32            program_id.network(),
33            function_name.size_in_bits(),
34            function_name,
35        )
36            .to_bits_le(),
37    )
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use crate::Circuit;
44    use snarkvm_circuit_types::environment::UpdatableCount;
45
46    use snarkvm_console::types::U16 as ConsoleU16;
47    use snarkvm_console_program::{Identifier as ConsoleIdentifier, ProgramID as ConsoleProgramID};
48
49    use anyhow::Result;
50
51    fn check(
52        mode: Mode,
53        network_id: u16,
54        program_id: &str,
55        function_name: &str,
56        expected_count: UpdatableCount,
57    ) -> Result<()> {
58        Circuit::initialize_global_constants();
59        Circuit::reset();
60
61        // Initialize the console values.
62        let console_network_id = ConsoleU16::new(network_id);
63        let console_program_id = ConsoleProgramID::from_str(program_id)?;
64        let console_function_name = ConsoleIdentifier::from_str(function_name)?;
65
66        // Compute the expected function ID.
67        let expected = snarkvm_console_program::compute_function_id(
68            &console_network_id,
69            &console_program_id,
70            &console_function_name,
71        )?;
72
73        // Initialize the network ID as a constant.
74        let network_id = U16::<Circuit>::constant(console_network_id);
75
76        // Initialize the program ID.
77        let program_id = match mode {
78            Mode::Constant => ProgramID::<Circuit>::constant(console_program_id),
79            Mode::Public => ProgramID::<Circuit>::public(console_program_id),
80            _ => panic!("Unsupported mode for ProgramID"),
81        };
82
83        // Initialize the function name.
84        let function_name = match mode {
85            Mode::Constant => Identifier::<Circuit>::constant(console_function_name),
86            Mode::Public => Identifier::<Circuit>::public(console_function_name),
87            _ => panic!("Unsupported mode for Identifier"),
88        };
89
90        Circuit::scope("compute_function_id", || {
91            let candidate = compute_function_id(&network_id, &program_id, &function_name);
92            assert_eq!(expected, candidate.eject_value());
93            expected_count.assert_matches(
94                Circuit::num_constants_in_scope(),
95                Circuit::num_public_in_scope(),
96                Circuit::num_private_in_scope(),
97                Circuit::num_constraints_in_scope(),
98            );
99        });
100
101        Circuit::reset();
102        Ok(())
103    }
104
105    #[test]
106    fn test_compute_function_id_constant() -> Result<()> {
107        check(Mode::Constant, 0, "credits.aleo", "transfer_public", count_is!(767, 0, 0, 0))?;
108        check(Mode::Constant, 0, "credits.aleo", "transfer_private", count_is!(779, 0, 0, 0))?;
109        check(Mode::Constant, 0, "credits.aleo", "transfer_public_to_private", count_is!(883, 0, 0, 0))?;
110        check(Mode::Constant, 0, "token_registry.aleo", "transfer_public_to_private", count_is!(959, 0, 0, 0))?;
111        check(Mode::Constant, 0, "my.aleo", "foo", count_is!(584, 0, 0, 0))?;
112
113        Ok(())
114    }
115
116    #[test]
117    fn test_compute_function_id_public() -> Result<()> {
118        check(Mode::Public, 0, "credits.aleo", "transfer_public", count_is!(465, 0, 1895, 1901))?;
119        check(Mode::Public, 0, "credits.aleo", "transfer_private", count_is!(465, 0, 1909, 1915))?;
120        check(Mode::Public, 0, "credits.aleo", "transfer_public_to_private", count_is!(465, 0, 2040, 2046))?;
121        check(Mode::Public, 0, "token_registry.aleo", "transfer_public_to_private", count_is!(465, 0, 2135, 2141))?;
122        check(Mode::Public, 0, "my.aleo", "foo", count_is!(463, 0, 1664, 1670))?;
123
124        Ok(())
125    }
126}