leo_core/algorithms/
pedersen.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::{IntegerType, Type};
19
20pub struct Pedersen64Hash;
21
22impl CoreFunction for Pedersen64Hash {
23    const NUM_ARGS: usize = 1;
24
25    fn first_arg_is_allowed_type(type_: &Type) -> bool {
26        matches!(
27            type_,
28            Type::Boolean
29                | Type::Integer(IntegerType::I8)
30                | Type::Integer(IntegerType::I16)
31                | Type::Integer(IntegerType::I32)
32                | Type::Integer(IntegerType::I64)
33                | Type::Integer(IntegerType::U8)
34                | Type::Integer(IntegerType::U16)
35                | Type::Integer(IntegerType::U32)
36                | Type::Integer(IntegerType::U64)
37                | Type::String
38        )
39    }
40
41    fn return_type() -> Type {
42        Type::Field
43    }
44}
45
46pub struct Pedersen64Commit;
47
48impl CoreFunction for Pedersen64Commit {
49    const NUM_ARGS: usize = 2;
50
51    fn first_arg_is_allowed_type(type_: &Type) -> bool {
52        matches!(
53            type_,
54            Type::Boolean
55                | Type::Integer(IntegerType::I8)
56                | Type::Integer(IntegerType::I16)
57                | Type::Integer(IntegerType::I32)
58                | Type::Integer(IntegerType::I64)
59                | Type::Integer(IntegerType::U8)
60                | Type::Integer(IntegerType::U16)
61                | Type::Integer(IntegerType::U32)
62                | Type::Integer(IntegerType::U64)
63                | Type::String
64        )
65    }
66
67    fn second_arg_is_allowed_type(type_: &Type) -> bool {
68        matches!(type_, Type::Scalar)
69    }
70
71    fn return_type() -> Type {
72        Type::Group
73    }
74}
75
76pub struct Pedersen128Hash;
77
78impl CoreFunction for Pedersen128Hash {
79    const NUM_ARGS: usize = 1;
80
81    fn first_arg_is_allowed_type(type_: &Type) -> bool {
82        matches!(type_, Type::Boolean | Type::Integer(_) | Type::String)
83    }
84
85    fn return_type() -> Type {
86        Type::Field
87    }
88}
89
90pub struct Pedersen128Commit;
91
92impl CoreFunction for Pedersen128Commit {
93    const NUM_ARGS: usize = 2;
94
95    fn first_arg_is_allowed_type(type_: &Type) -> bool {
96        matches!(type_, Type::Boolean | Type::Integer(_) | Type::String)
97    }
98
99    fn second_arg_is_allowed_type(type_: &Type) -> bool {
100        matches!(type_, Type::Scalar)
101    }
102
103    fn return_type() -> Type {
104        Type::Group
105    }
106}