snarkvm_console_program/function_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
16use crate::{Identifier, ProgramID};
17use snarkvm_console_account::ToBits;
18use snarkvm_console_algorithms::Result;
19use snarkvm_console_network::Network;
20use snarkvm_console_types::{Field, U8, U16};
21
22/// Compute the function ID as `Hash(network_id, program_id.len(), program_id, function_name.len(), function_name)`.
23pub fn compute_function_id<N: Network>(
24    network_id: &U16<N>,
25    program_id: &ProgramID<N>,
26    function_name: &Identifier<N>,
27) -> Result<Field<N>> {
28    N::hash_bhp1024(
29        &(
30            *network_id,
31            U8::<N>::new(program_id.name().size_in_bits()),
32            program_id.name(),
33            U8::<N>::new(program_id.network().size_in_bits()),
34            program_id.network(),
35            U8::<N>::new(function_name.size_in_bits()),
36            function_name,
37        )
38            .to_bits_le(),
39    )
40}