-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfs_test.go
82 lines (59 loc) · 1.92 KB
/
fs_test.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
package zkstarks
import (
"bytes"
"encoding/binary"
"math"
"math/big"
"math/rand"
"testing"
"github.com/actuallyachraf/algebra/nt"
)
func TestFiatShamirChannel(t *testing.T) {
t.Run("TestFiatShamirReproducible", func(t *testing.T) {
c := NewChannel()
c.Send([]byte("Yes"))
r1 := c.RandInt(nt.FromInt64(0), nt.FromInt64(math.MaxUint32))
d := NewChannel()
d.Send([]byte("Yes"))
r2 := d.RandInt(nt.FromInt64(0), nt.FromInt64(math.MaxUint32))
if r1.Cmp(r2) != 0 {
t.Fatal("error FiatShamir channel should be reproducible")
}
})
t.Run("TestFiatShamirUniformity", func(t *testing.T) {
var rangeSize int64 = 10
var upperBound int64 = 1048576
var numTries int64 = 1024
c := NewChannel()
n := rand.Int63n(upperBound)
var intBuf = new(bytes.Buffer)
err := binary.Write(intBuf, binary.LittleEndian, n)
if err != nil {
t.Fatal("failed to write integer to buffer")
}
c.Send(intBuf.Bytes())
dist := make([]*big.Int, 0, numTries)
distRand := make([]*big.Int, 0, numTries)
var i int64 = 0
for i = 0; i < numTries; i++ {
dist = append(dist, c.RandInt(nt.FromInt64(0), nt.FromInt64(rangeSize-1)))
distRand = append(distRand, big.NewInt(rand.Int63n(rangeSize-1)))
}
mean := numTries / rangeSize
var yMeanChannel = big.NewInt(0)
var yMeanRand = big.NewInt(0)
for _, y := range dist {
yNorm := nt.ModExp(nt.Sub(y, nt.FromInt64(mean)), nt.FromInt64(2), nil)
yMeanChannel = nt.Add(yMeanChannel, yNorm)
}
for _, y := range distRand {
yNorm := nt.ModExp(nt.Sub(y, nt.FromInt64(mean)), nt.FromInt64(2), nil)
yMeanRand = nt.Add(yMeanRand, yNorm)
}
normalizedStdDevChannel := nt.Div(new(big.Int).Sqrt(yMeanChannel), nt.FromInt64(rangeSize))
normalizedStdDevRand := nt.Div(new(big.Int).Sqrt(yMeanRand), nt.FromInt64(rangeSize))
if (nt.Sub(normalizedStdDevChannel, normalizedStdDevRand)).Cmp(nt.FromInt64(4)) > 0 {
t.Error("fiat shamir channel is not uniform")
}
})
}