implement reader interface to transcri[pt rng

This commit is contained in:
vedhavyas 2020-02-11 14:05:08 +01:00
parent 6b336b58be
commit de74c97117
No known key found for this signature in database
GPG Key ID: 317BF0923E3EB7E5
3 changed files with 29 additions and 8 deletions

2
go.sum
View File

@ -7,5 +7,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -99,10 +99,12 @@ func (t *TranscriptRNG) Finalize(rng io.Reader) (*TranscriptRNG, error) {
return t, nil
}
// RandomBytes returns random n bytes from the transcript.
func (t *TranscriptRNG) RandomBytes(outLen int) []byte {
// Read reads random data and writes to buf
func (t *TranscriptRNG) Read(buf []byte) (int, error) {
l := len(buf)
sizeBuffer := make([]byte, 4)
binary.LittleEndian.PutUint32(sizeBuffer[0:], uint32(outLen))
binary.LittleEndian.PutUint32(sizeBuffer[0:], uint32(l))
t.s.AD(true, sizeBuffer)
return t.s.PRF(outLen)
res := t.s.PRF(l)
return copy(buf, res), nil
}

View File

@ -86,11 +86,28 @@ func TestTranscriptRNG(t *testing.T) {
r4, err := t4.BuildRNG().ReKeyWithWitnessBytes([]byte("witness"), witness2).Finalize(rand.New(rand.NewSource(0)))
assert.NoError(t, err)
var (
s1 = make([]byte, 32)
s2 = make([]byte, 32)
s3 = make([]byte, 32)
s4 = make([]byte, 32)
)
s1 := r1.RandomBytes(32)
s2 := r2.RandomBytes(32)
s3 := r3.RandomBytes(32)
s4 := r4.RandomBytes(32)
n, err := r1.Read(s1)
assert.NoError(t, err)
assert.Equal(t, n, 32)
n, err = r2.Read(s2)
assert.NoError(t, err)
assert.Equal(t, n, 32)
n, err = r3.Read(s3)
assert.NoError(t, err)
assert.Equal(t, n, 32)
n, err = r4.Read(s4)
assert.NoError(t, err)
assert.Equal(t, n, 32)
// s1 shouldn't match with any due to different commitment data
// s2 shouldn't match with any due to different witness data