-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe.go
92 lines (82 loc) · 1.74 KB
/
safe.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
88
89
90
91
92
package gnark_nimue
import (
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/math/uints"
"github.com/reilabs/gnark-nimue/hash"
_ "unsafe"
)
type Safe[U any, H hash.DuplexHash[U]] struct {
sponge H
ops OpQueue
}
//go:linkname keccakF1600 golang.org/x/crypto/sha3.keccakF1600
func keccakF1600(a *[25]uint64)
func keccakF(a *[200]byte) {
b := [25]uint64{}
for i := 0; i < 25; i++ {
for j := 0; j < 8; j++ {
b[i] |= uint64(a[i*8+j]) << uint(j*8)
}
}
keccakF1600(&b)
for i := 0; i < 25; i++ {
for j := 0; j < 8; j++ {
a[i*8+j] = byte(b[i] >> uint(j*8))
}
}
}
func generateTag(io []byte) [32]uints.U8 {
state := [200]byte{}
absorbPos := 0
R := 136
for len(io) > 0 {
if absorbPos == R {
keccakF(&state)
absorbPos = 0
} else {
chunkLen := min(len(io), R-absorbPos)
chunk, rest := io[:chunkLen], io[chunkLen:]
copy(state[absorbPos:], chunk)
absorbPos += chunkLen
io = rest
}
}
keccakF(&state)
tag := [32]uints.U8{}
for i := 0; i < 32; i++ {
tag[i] = uints.NewU8(state[i])
}
return tag
}
func NewSafe[U any, H hash.DuplexHash[U]](sponge H, ioStr []byte) (*Safe[U, H], error) {
tag := generateTag(ioStr)
sponge.Initialize(tag)
io := IOPattern{}
err := io.Parse(ioStr)
if err != nil {
return nil, err
}
return &Safe[U, H]{
ops: io.GetOpQueue(),
sponge: sponge,
}, nil
}
func (safe *Safe[U, H]) Squeeze(out []U) (err error) {
err = safe.ops.Squeeze(uint64(len(out)))
if err != nil {
return
}
safe.sponge.Squeeze(out)
return
}
func (safe *Safe[U, H]) Absorb(in []U) (err error) {
err = safe.ops.Absorb(uint64(len(in)))
if err != nil {
return
}
safe.sponge.Absorb(in)
return
}
func (safe *Safe[U, H]) PrintState(api frontend.API) {
safe.sponge.PrintState(api)
}