leo_core/algorithms/
poseidon.rs

1// Copyright (C) 2019-2023 Aleo Systems Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::algorithms::CoreFunction;
18use leo_ast::Type;
19
20pub struct Poseidon2Hash;
21
22impl CoreFunction for Poseidon2Hash {
23    const NUM_ARGS: usize = 1;
24
25    fn first_arg_is_allowed_type(type_: &Type) -> bool {
26        !matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
27    }
28
29    fn return_type() -> Type {
30        Type::Field
31    }
32}
33
34pub struct Poseidon4Hash;
35
36impl CoreFunction for Poseidon4Hash {
37    const NUM_ARGS: usize = 1;
38
39    fn first_arg_is_allowed_type(type_: &Type) -> bool {
40        !matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
41    }
42
43    fn return_type() -> Type {
44        Type::Field
45    }
46}
47
48pub struct Poseidon8Hash;
49
50impl CoreFunction for Poseidon8Hash {
51    const NUM_ARGS: usize = 1;
52
53    fn first_arg_is_allowed_type(type_: &Type) -> bool {
54        !matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
55    }
56
57    fn return_type() -> Type {
58        Type::Field
59    }
60}