• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vocdoni / vocdoni-node / 11856589598

15 Nov 2024 12:49PM UTC coverage: 62.207% (-0.08%) from 62.286%
11856589598

push

github

altergui
Merge branch 'origin/main' into release-lts-1

29 of 56 new or added lines in 10 files covered. (51.79%)

6 existing lines in 3 files now uncovered.

16837 of 27066 relevant lines covered (62.21%)

38382.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.33
/tree/arbo/hash.go
1
package arbo
2

3
import (
4
        "crypto/sha256"
5
        "math/big"
6

7
        "github.com/iden3/go-iden3-crypto/poseidon"
8
        "golang.org/x/crypto/blake2b"
9
        "lukechampine.com/blake3"
10
)
11

12
var (
13
        // TypeHashSha256 represents the label for the HashFunction of Sha256
14
        TypeHashSha256 = []byte("sha256")
15
        // TypeHashPoseidon represents the label for the HashFunction of
16
        // Poseidon
17
        TypeHashPoseidon = []byte("poseidon")
18
        // TypeHashBlake2b represents the label for the HashFunction of Blake2b
19
        TypeHashBlake2b = []byte("blake2b")
20
        // TypeHashBlake3 represents the label for the HashFunction of Blake3
21
        TypeHashBlake3 = []byte("blake3")
22

23
        // HashFunctionSha256 contains the HashSha256 struct which implements
24
        // the HashFunction interface
25
        HashFunctionSha256 HashSha256
26
        // HashFunctionPoseidon contains the HashPoseidon struct which implements
27
        // the HashFunction interface
28
        HashFunctionPoseidon HashPoseidon
29
        // HashFunctionBlake2b contains the HashBlake2b struct which implements
30
        // the HashFunction interface
31
        HashFunctionBlake2b HashBlake2b
32
        // HashFunctionBlake3 contains the HashBlake3 struct which implements
33
        // the HashFunction interface
34
        HashFunctionBlake3 HashBlake3
35
)
36

37
// Once Generics are at Go, this will be updated (August 2021
38
// https://blog.golang.org/generics-next-step)
39

40
// HashFunction defines the interface that is expected for a hash function to be
41
// used in a generic way in the Tree.
42
type HashFunction interface {
43
        Type() []byte
44
        Len() int
45
        Hash(...[]byte) ([]byte, error)
46
        // CheckInput checks if the input is valid without computing the hash
47
        // CheckInput(...[]byte) error
48
}
49

50
// HashSha256 implements the HashFunction interface for the Sha256 hash
51
type HashSha256 struct{}
52

53
// Type returns the type of HashFunction for the HashSha256
54
func (HashSha256) Type() []byte {
×
55
        return TypeHashSha256
×
56
}
×
57

58
// Len returns the length of the Hash output
59
func (HashSha256) Len() int {
6,760,261✔
60
        return 32
6,760,261✔
61
}
6,760,261✔
62

63
// Hash implements the hash method for the HashFunction HashSha256
64
func (HashSha256) Hash(b ...[]byte) ([]byte, error) {
1,453,211✔
65
        var toHash []byte
1,453,211✔
66
        for i := 0; i < len(b); i++ {
4,463,633✔
67
                toHash = append(toHash, b[i]...)
3,010,422✔
68
        }
3,010,422✔
69
        h := sha256.Sum256(toHash)
1,453,211✔
70
        return h[:], nil
1,453,211✔
71
}
72

73
// HashPoseidon implements the HashFunction interface for the Poseidon hash
74
type HashPoseidon struct{}
75

76
// Type returns the type of HashFunction for the HashPoseidon
77
func (HashPoseidon) Type() []byte {
×
78
        return TypeHashPoseidon
×
79
}
×
80

81
// Len returns the length of the Hash output
82
func (HashPoseidon) Len() int {
3,219,953✔
83
        return 32
3,219,953✔
84
}
3,219,953✔
85

86
// Hash implements the hash method for the HashFunction HashPoseidon. It
87
// expects the byte arrays to be little-endian representations of big.Int
88
// values.
89
func (f HashPoseidon) Hash(b ...[]byte) ([]byte, error) {
409,713✔
90
        var toHash []*big.Int
409,713✔
91
        for i := 0; i < len(b); i++ {
1,281,676✔
92
                bi := BytesLEToBigInt(b[i])
871,963✔
93
                toHash = append(toHash, bi)
871,963✔
94
        }
871,963✔
95
        h, err := poseidon.Hash(toHash)
409,713✔
96
        if err != nil {
409,713✔
97
                return nil, err
×
98
        }
×
99
        hB := BigIntToBytesLE(f.Len(), h)
409,713✔
100
        return hB, nil
409,713✔
101
}
102

103
// HashBlake2b implements the HashFunction interface for the Blake2b hash
104
type HashBlake2b struct{}
105

106
// Type returns the type of HashFunction for the HashBlake2b
107
func (HashBlake2b) Type() []byte {
×
108
        return TypeHashBlake2b
×
109
}
×
110

111
// Len returns the length of the Hash output
112
func (HashBlake2b) Len() int {
1,644,223✔
113
        return 32
1,644,223✔
114
}
1,644,223✔
115

116
// Hash implements the hash method for the HashFunction HashBlake2b
117
func (HashBlake2b) Hash(b ...[]byte) ([]byte, error) {
489,587✔
118
        hasher, err := blake2b.New256(nil)
489,587✔
119
        if err != nil {
489,587✔
120
                return nil, err
×
121
        }
×
122
        for i := 0; i < len(b); i++ {
1,526,952✔
123
                if _, err = hasher.Write(b[i]); err != nil {
1,037,365✔
124
                        return nil, err
×
125
                }
×
126
        }
127
        return hasher.Sum(nil), nil
489,587✔
128
}
129

130
// HashBlake3 implements the HashFunction interface for the Blake3 hash
131
type HashBlake3 struct{}
132

133
// Type returns the type of HashFunction for the HashBlake3
NEW
134
func (HashBlake3) Type() []byte {
×
NEW
135
        return TypeHashBlake3
×
NEW
136
}
×
137

138
// Len returns the length of the Hash output
NEW
139
func (HashBlake3) Len() int {
×
NEW
140
        return 32
×
NEW
141
}
×
142

143
// Hash implements the hash method for the HashFunction HashBlake3
NEW
144
func (HashBlake3) Hash(b ...[]byte) ([]byte, error) {
×
NEW
145
        hasher := blake3.New(32, nil)
×
NEW
146
        for i := 0; i < len(b); i++ {
×
NEW
147
                if _, err := hasher.Write(b[i]); err != nil {
×
NEW
148
                        return nil, err
×
NEW
149
                }
×
150
        }
NEW
151
        return hasher.Sum(nil), nil
×
152
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc