snarkvm_circuit_algorithms/
traits.rs

1// Copyright 2024 Aleo Network Foundation
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 snarkvm_circuit_types::environment::{Eject, GroupTrait, Inject, ScalarTrait, ToBits};
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) -> 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) -> Self::Output;
36}
37
38/// A trait for a hash function.
39pub trait Hash {
40    type Input: Inject + Eject + Clone;
41    type Output: Inject + Eject + ToBits + Clone;
42
43    /// Returns the hash of the given input.
44    fn hash(&self, input: &[Self::Input]) -> 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 Group: GroupTrait<Self::Scalar>;
60    type Scalar: ScalarTrait;
61
62    /// Returns the hash of the given input.
63    fn hash_to_group(&self, input: &[Self::Input]) -> Self::Group;
64}
65
66/// A trait for a hash function that projects the value to a scalar.
67pub trait HashToScalar {
68    type Input;
69    type Scalar: ScalarTrait;
70
71    /// Returns the hash of the given input.
72    fn hash_to_scalar(&self, input: &[Self::Input]) -> Self::Scalar;
73}
74
75/// A trait for a hash function of an uncompressed variant.
76pub trait HashUncompressed {
77    type Input;
78    type Output;
79
80    /// Returns the hash of the given input.
81    fn hash_uncompressed(&self, input: &[Self::Input]) -> Self::Output;
82}
83
84/// A trait for a pseudorandom function.
85pub trait PRF {
86    type Seed;
87    type Input;
88    type Output;
89
90    /// Returns the output for the given seed and input.
91    fn prf(&self, seed: &Self::Seed, input: &[Self::Input]) -> Self::Output;
92}