snarkvm_console_network_environment/traits/algorithms.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 anyhow::Result;
17
18/// A trait for a commitment scheme.
19pub trait Commit {
20 type Input;
21 type Output;
22 type Randomizer;
23
24 /// Returns the commitment to the given input and randomizer.
25 fn commit(&self, input: &[Self::Input], randomizer: &Self::Randomizer) -> Result<Self::Output>;
26}
27
28/// A trait for a commitment scheme.
29pub trait CommitUncompressed {
30 type Input;
31 type Output;
32 type Randomizer;
33
34 /// Returns the commitment to the given input and randomizer.
35 fn commit_uncompressed(&self, input: &[Self::Input], randomizer: &Self::Randomizer) -> Result<Self::Output>;
36}
37
38/// A trait for a hash function.
39pub trait Hash {
40 type Input;
41 type Output;
42
43 /// Returns the hash of the given input.
44 fn hash(&self, input: &[Self::Input]) -> Result<Self::Output>;
45}
46
47/// A trait for a hash function that produces multiple outputs.
48pub trait HashMany {
49 type Input;
50 type Output;
51
52 /// Returns the hash of the given input.
53 fn hash_many(&self, input: &[Self::Input], num_outputs: u16) -> Vec<Self::Output>;
54}
55
56/// A trait for a hash function that projects the value to an affine group element.
57pub trait HashToGroup {
58 type Input;
59 type Output;
60
61 /// Returns the hash of the given input.
62 fn hash_to_group(&self, input: &[Self::Input]) -> Result<Self::Output>;
63}
64
65/// A trait for a hash function that projects the value to a scalar.
66pub trait HashToScalar {
67 type Input;
68 type Output;
69
70 /// Returns the hash of the given input.
71 fn hash_to_scalar(&self, input: &[Self::Input]) -> Result<Self::Output>;
72}
73
74/// A trait for a hash function of an uncompressed variant.
75pub trait HashUncompressed {
76 type Input;
77 type Output;
78
79 /// Returns the hash of the given input.
80 fn hash_uncompressed(&self, input: &[Self::Input]) -> Result<Self::Output>;
81}
82
83/// A trait for a pseudorandom function.
84pub trait PRF {
85 type Seed;
86 type Input;
87 type Output;
88
89 /// Returns the output for the given seed and input.
90 fn prf(&self, seed: &Self::Seed, input: &[Self::Input]) -> Result<Self::Output>;
91}