forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
87 lines (71 loc) · 2.83 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Copyright © 2020 ConsenSys
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package hash provides an interface that hash functions (as gadget) should implement.
package hash
import (
"errors"
"sync"
"github.com/aakash4dev/gnark2/frontend"
"github.com/aakash4dev/gnark2/std/math/uints"
)
// FieldHasher hashes inputs into a short digest. This interface mocks
// [BinaryHasher], but is more suitable in-circuit by assuming the inputs are
// scalar field elements and outputs digest as a field element. Such hash
// functions are for examle Poseidon, MiMC etc.
type FieldHasher interface {
// Sum computes the hash of the internal state of the hash function.
Sum() frontend.Variable
// Write populate the internal state of the hash function with data. The inputs are native field elements.
Write(data ...frontend.Variable)
// Reset empty the internal state and put the intermediate state to zero.
Reset()
}
var (
builderRegistry = make(map[string]func(api frontend.API) (FieldHasher, error))
lock sync.RWMutex
)
func Register(name string, builder func(api frontend.API) (FieldHasher, error)) {
lock.Lock()
defer lock.Unlock()
builderRegistry[name] = builder
}
func GetFieldHasher(name string, api frontend.API) (FieldHasher, error) {
lock.RLock()
defer lock.RUnlock()
builder, ok := builderRegistry[name]
if !ok {
return nil, errors.New("hash function not found")
}
return builder(api)
}
// BinaryHasher hashes inputs into a short digest. It takes as inputs bytes and
// outputs byte array whose length depends on the underlying hash function. For
// SNARK-native hash functions use [FieldHasher].
type BinaryHasher interface {
// Sum finalises the current hash and returns the digest.
Sum() []uints.U8
// Write writes more bytes into the current hash state.
Write([]uints.U8)
// Size returns the number of bytes this hash function returns in a call to
// [BinaryHasher.Sum].
Size() int
}
// BinaryFixedLengthHasher is like [BinaryHasher], but assumes the length of the
// input is not full length as defined during compile time. This allows to
// compute digest of variable-length input, unlike [BinaryHasher] which assumes
// the length of the input is the total number of bytes written.
type BinaryFixedLengthHasher interface {
BinaryHasher
// FixedLengthSum returns digest of the first length bytes.
FixedLengthSum(length frontend.Variable) []uints.U8
}