snarkvm_console_program/data/record/to_commitment.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 super::*;
17
18impl<N: Network> Record<N, Plaintext<N>> {
19 /// Returns the record commitment.
20 pub fn to_commitment(
21 &self,
22 program_id: &ProgramID<N>,
23 record_name: &Identifier<N>,
24 record_view_key: &Field<N>,
25 ) -> Result<Field<N>> {
26 // Construct the input as `(program_id || record_name || record)`.
27 let input = to_bits_le![program_id, record_name, self];
28
29 // Version 0 - Construct the input as the *record bits* without the version bits.
30 let input_v0 = input[..input.len() - 8].to_vec();
31 // Version 0 - Compute the BHP hash of the program record.
32 let digest = N::hash_bhp1024(&input_v0)?;
33
34 // Version 1 - Construct the input as the *digest* with the version bits.
35 let mut input_v1 = digest.to_bits_le();
36 // Append the version bits.
37 input_v1.extend_from_slice(&input[input.len() - 8..]);
38
39 // If the record is non-hiding, then return the digest. Otherwise, return the commitment.
40 match !self.is_hiding() {
41 // Version 0 - Compute the BHP hash of the program record.
42 true => Ok(digest),
43 // Version 1 - Compute the BHP commitment of the program record.
44 false => {
45 // Construct the commitment nonce.
46 let cm_nonce = N::hash_to_scalar_psd2(&[N::commitment_domain(), *record_view_key])?;
47 // Compute the BHP commitment of the program record using the commitment nonce.
48 N::commit_bhp512(&input_v1, &cm_nonce)
49 }
50 }
51 }
52}
53
54impl<N: Network> Record<N, Ciphertext<N>> {
55 /// Returns the record commitment.
56 pub fn to_commitment(
57 &self,
58 _program_id: &ProgramID<N>,
59 _record_name: &Identifier<N>,
60 _record_view_key: &Field<N>,
61 ) -> Result<Field<N>> {
62 bail!("Illegal operation: Record::to_commitment() cannot be invoked on the `Ciphertext` variant.")
63 }
64}