-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharthur.go
192 lines (172 loc) · 5.01 KB
/
arthur.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package gnark_nimue
import (
"fmt"
"math/big"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/frontend"
bits2 "github.com/consensys/gnark/std/math/bits"
"github.com/consensys/gnark/std/math/uints"
"github.com/reilabs/gnark-nimue/hash"
skyscraper "github.com/reilabs/gnark-skyscraper"
)
type Arthur interface {
FillNextBytes(uints []uints.U8) error
FillChallengeBytes(uints []uints.U8) error
FillNextScalars(scalars []frontend.Variable) error
FillChallengeScalars(scalars []frontend.Variable) error
PrintState(api frontend.API)
}
type byteArthur[H hash.DuplexHash[uints.U8]] struct {
api frontend.API
transcript []uints.U8
safe *Safe[uints.U8, H]
}
func NewByteArthur[S hash.DuplexHash[uints.U8]](api frontend.API, io []byte, transcript []uints.U8, hash S) (Arthur, error) {
safe, err := NewSafe[uints.U8, S](hash, io)
if err != nil {
return nil, err
}
return &byteArthur[S]{
api,
transcript,
safe,
}, nil
}
func NewKeccakArthur(api frontend.API, io []byte, transcript []uints.U8) (Arthur, error) {
sponge, err := hash.NewKeccak(api)
if err != nil {
return nil, err
}
return NewByteArthur[hash.Keccak](api, io, transcript, sponge)
}
func (arthur *byteArthur[H]) FillNextBytes(uints []uints.U8) error {
copy(uints, arthur.transcript)
arthur.transcript = arthur.transcript[len(uints):]
err := arthur.safe.Absorb(uints)
if err != nil {
return err
}
return nil
}
func (arthur *byteArthur[H]) FillChallengeBytes(uints []uints.U8) error {
return arthur.safe.Squeeze(uints)
}
func (arthur *byteArthur[H]) FillNextScalars(scalars []frontend.Variable) error {
bytesToRead := (arthur.api.Compiler().FieldBitLen() + 7) / 8
bytes := make([]uints.U8, bytesToRead)
for i := range scalars {
scalars[i] = frontend.Variable(0)
err := arthur.FillNextBytes(bytes)
if err != nil {
return err
}
curMul := big.NewInt(1)
for _, b := range bytes {
scalars[i] = arthur.api.Add(scalars[i], arthur.api.Mul(b.Val, curMul))
curMul.Mul(curMul, big.NewInt(256))
}
}
return nil
}
func (arthur *byteArthur[H]) FillChallengeScalars(scalars []frontend.Variable) error {
bytesToGenerate := (arthur.api.Compiler().FieldBitLen() + 128) / 8
bytes := make([]uints.U8, bytesToGenerate)
for i := range scalars {
err := arthur.FillChallengeBytes(bytes)
if err != nil {
return err
}
scalars[i] = frontend.Variable(0)
for _, b := range bytes {
scalars[i] = arthur.api.Add(b.Val, arthur.api.Mul(scalars[i], 256))
}
}
return nil
}
func (arthur *byteArthur[H]) PrintState(api frontend.API) {
msg := fmt.Sprintf("remaining transcript bytes: %d", len(arthur.transcript))
api.Println(msg)
arthur.safe.sponge.PrintState(api)
}
type nativeArthur[H hash.DuplexHash[frontend.Variable]] struct {
api frontend.API
transcript []uints.U8
safe *Safe[frontend.Variable, H]
}
func (arthur *nativeArthur[H]) FillNextBytes(uints []uints.U8) error {
copy(uints, arthur.transcript)
for _, i := range uints {
err := arthur.safe.Absorb([]frontend.Variable{i.Val})
if err != nil {
return err
}
}
arthur.transcript = arthur.transcript[len(uints):]
return nil
}
func randomBytesInModulus(api frontend.API) (int, error) {
if api.Compiler().Field().Cmp(ecc.BN254.ScalarField()) == 0 {
return 15, nil
}
return 0, fmt.Errorf("unsupported field")
}
func (arthur *nativeArthur[H]) FillChallengeBytes(out []uints.U8) error {
numBytes, err := randomBytesInModulus(arthur.api)
if err != nil {
return err
}
if len(out) == 0 {
return nil
}
lenGood := min(len(out), numBytes)
tmp := make([]frontend.Variable, 1)
for i := range (len(out) + lenGood - 1) / lenGood {
err = arthur.FillChallengeScalars(tmp)
if err != nil {
return err
}
bits := bits2.ToBinary(arthur.api, tmp[0])
for k := range lenGood {
o := i*lenGood + k
if o >= len(out) {
break
}
out[o] = uints.NewU8(0)
curMul := 1
for j := range 8 {
out[o].Val = arthur.api.Add(arthur.api.Mul(curMul, bits[8*k+j]), out[o].Val)
curMul *= 2
}
}
}
return nil
}
func (arthur *nativeArthur[H]) FillNextScalars(out []frontend.Variable) error {
wordSize := (arthur.api.Compiler().FieldBitLen() + 7) / 8
for i := range out {
bytes := arthur.transcript[:wordSize]
arthur.transcript = arthur.transcript[wordSize:]
out[i] = frontend.Variable(0)
curMul := big.NewInt(1)
for _, b := range bytes {
out[i] = arthur.api.Add(out[i], arthur.api.Mul(b.Val, curMul))
curMul.Mul(curMul, big.NewInt(256))
}
}
err := arthur.safe.Absorb(out)
return err
}
func (arthur *nativeArthur[H]) FillChallengeScalars(out []frontend.Variable) error {
return arthur.safe.Squeeze(out)
}
func (arthur *nativeArthur[H]) PrintState(api frontend.API) {
arthur.safe.sponge.PrintState(api)
}
func NewSkyscraperArthur(api frontend.API, sc *skyscraper.Skyscraper, io []byte, transcript []uints.U8) (Arthur, error) {
sponge, err := hash.NewSkyScraper(sc)
if err != nil {
return nil, err
}
safe, err := NewSafe[frontend.Variable, hash.Skyscraper](sponge, io)
return &nativeArthur[hash.Skyscraper]{api, transcript, safe}, nil
}