Skip to content

Commit 6adcf17

Browse files
committed
utils: randomness: remove mock provider
The current architecture assumes that whoever needs randomness, will just accept it as an argument. It's the caller's responsibility to make sure the random value is cached for subsequent uses. In tests its enough to just define any byte array and reuse it. Signed-off-by: Wojciech Zmuda <[email protected]>
1 parent 31840a2 commit 6adcf17

File tree

6 files changed

+71
-96
lines changed

6 files changed

+71
-96
lines changed

offline/actions/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func Phase2Init(_ context.Context, cmd *cli.Command) error {
15-
rand, err := randomness.NewDrandProvider()
15+
rand, err := randomness.New()
1616
if err != nil {
1717
return err
1818
}

offline/test/offline_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/reilabs/trusted-setup/offline/phase2"
1616
"github.com/reilabs/trusted-setup/offline/r1cs"
1717
test_circuit "github.com/reilabs/trusted-setup/test"
18-
"github.com/reilabs/trusted-setup/utils/randomness"
1918
)
2019

2120
// testOfflineCeremony verifies the trusted setup ceremony in the offline mode.
@@ -44,7 +43,7 @@ var (
4443
srsCommonsFileName string
4544
pkFileName string
4645
vkFileName string
47-
rand randomness.MockProvider
46+
rand []byte
4847
)
4948

5049
func createTempFile(pattern string) *os.File {
@@ -80,8 +79,7 @@ func setup() {
8079
fVk := createTempFile("vk")
8180
vkFileName = fVk.Name()
8281

83-
mockBeacon := bytes.Repeat([]byte{0x42}, 32)
84-
rand = randomness.MockProvider{Beacon: mockBeacon}
82+
rand = bytes.Repeat([]byte{0x42}, 32)
8583
}
8684

8785
func teardown() {
@@ -130,7 +128,7 @@ func testPtau(t *testing.T) {
130128
}
131129

132130
func testInit(t *testing.T) {
133-
assert.NoError(t, phase2.Init(phase1FileName, r1csFileName, phase2FileName, srsCommonsFileName, rand.GetBeacon()))
131+
assert.NoError(t, phase2.Init(phase1FileName, r1csFileName, phase2FileName, srsCommonsFileName, rand))
134132

135133
p2, err := phase2.FromFile(phase2FileName)
136134
assert.NoError(t, err)
@@ -187,7 +185,7 @@ func testExtractKeys(t *testing.T) {
187185
assert.NoError(
188186
t,
189187
phase2.ExtractKeys(
190-
r1csFileName, srsCommonsFileName, phase2Contributions[1:], pkFileName, vkFileName, rand.GetBeacon(),
188+
r1csFileName, srsCommonsFileName, phase2Contributions[1:], pkFileName, vkFileName, rand,
191189
),
192190
)
193191
}

utils/randomness/drand_provider.go

Lines changed: 0 additions & 64 deletions
This file was deleted.

utils/randomness/mock_provider.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

utils/randomness/randomness.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,70 @@
22
// by the drand organization (https://github.com/drand/drand).
33
package randomness
44

5+
import (
6+
"context"
7+
"encoding/hex"
8+
"fmt"
9+
10+
"github.com/drand/go-clients/client"
11+
"github.com/drand/go-clients/client/http"
12+
"github.com/drand/go-clients/drand"
13+
)
14+
515
// BeaconProvider defines an interface to get 32 bytes of randomness (beacon).
616
type BeaconProvider interface {
717
GetBeacon() []byte
818
}
19+
20+
// drandProvider implements BeaconProvider by retrieving randomness from drand network.
21+
type drandProvider struct {
22+
client drand.Client
23+
}
24+
25+
// New initializes the randomness module. It connects to the drand network, so random value can be
26+
// obtained with GetBeacon.
27+
func New() (BeaconProvider, error) {
28+
// Default network chain hash as per the drand project documentation.
29+
const chainHash = "8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce"
30+
// API returning the randomness, as per the drand project documentation.
31+
const apiHost = "https://api.drand.sh/"
32+
33+
httpClient, err := http.NewSimpleClient(apiHost, chainHash)
34+
if err != nil {
35+
panic(err)
36+
}
37+
chb, err := hex.DecodeString(chainHash)
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
p := drandProvider{}
43+
p.client, err = client.New(
44+
client.From(httpClient),
45+
client.WithChainHash(chb),
46+
)
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
return &p, nil
52+
}
53+
54+
// GetBeacon returns the 32 bytes of randomness.
55+
func (d *drandProvider) GetBeacon() []byte {
56+
const mostRecentKnownRound = 0
57+
r, err := d.client.Get(context.Background(), mostRecentKnownRound)
58+
if err != nil {
59+
panic(err)
60+
}
61+
62+
beacon := r.GetRandomness()
63+
if len(beacon) != 32 {
64+
panic(fmt.Errorf("randomness: expected 32 bytes, got %d", len(beacon)))
65+
}
66+
if beacon == nil {
67+
panic(fmt.Errorf("randomness: drand did not return randomness"))
68+
}
69+
70+
return beacon
71+
}
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
package randomness_test
22

33
import (
4-
"bytes"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
87

98
"github.com/reilabs/trusted-setup/utils/randomness"
109
)
1110

12-
func TestGetBeaconWithMock(t *testing.T) {
13-
mockValue := bytes.Repeat([]byte{0x42}, 32)
14-
mock := &randomness.MockProvider{Beacon: mockValue}
15-
16-
got := mock.GetBeacon()
17-
if !bytes.Equal(got, mockValue) {
18-
t.Fatalf("GetBeacon() = %x; want %x", got, mockValue)
19-
}
20-
}
21-
22-
func TestGetBeaconWithDrand(t *testing.T) {
23-
drand, err := randomness.NewDrandProvider()
11+
func Test(t *testing.T) {
12+
r, err := randomness.New()
2413
assert.NoError(t, err)
2514

26-
got := drand.GetBeacon()
15+
got := r.GetBeacon()
2716
assert.NotEmpty(t, got)
2817
assert.Equal(t, 32, len(got))
2918
}

0 commit comments

Comments
 (0)