-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.go
67 lines (55 loc) · 1.7 KB
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright © 2024 Galactica Network
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package zkcertificate
import (
"math/big"
)
// Hash represents a cryptographic hash value obtained by Poseidon algorithm.
type Hash big.Int
// HashFromBigInt creates a Hash from a given big.Int value.
func HashFromBigInt(n *big.Int) Hash {
if n == nil {
n = big.NewInt(1)
}
return Hash(*n)
}
// BigInt converts a Hash value to a big.Int.
func (h Hash) BigInt() *big.Int {
n := big.Int(h)
return &n
}
// Bytes32 converts a Hash value to a 32-byte array.
func (h Hash) Bytes32() [32]byte {
var res [32]byte
h.BigInt().FillBytes(res[:])
return res
}
// String returns the string representation of the Hash value.
func (h Hash) String() string {
return h.BigInt().String()
}
// UnmarshalText implements [encoding.TextUnmarshaler].
func (h *Hash) UnmarshalText(text []byte) error {
var res big.Int
if err := res.UnmarshalText(text); err != nil {
return err
}
*h = Hash(res)
return nil
}
// MarshalText implements [encoding.TextMarshaler].
func (h Hash) MarshalText() (text []byte, err error) {
return h.BigInt().MarshalText()
}