Skip to content

Commit c371704

Browse files
committed
trancript: add TranscriptRngBuilder
Signed-off-by: Ignacio Hagopian <[email protected]>
1 parent 09bced1 commit c371704

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

strobe128/strobe128.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (s *Strobe128) PRF(data []byte, more bool) {
5757
s.squeeze(data)
5858
}
5959

60-
func (s *Strobe128) key(data []byte, more bool) {
60+
func (s *Strobe128) Key(data []byte, more bool) {
6161
s.beginOp(flagA|flagC, more)
6262
s.overwrite(data)
6363
}

strobe128/strobe128_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestConformance(t *testing.T) {
2929
require.Equal(t, exp, got)
3030

3131
s1.MetaAD([]byte("key"), false)
32-
s1.key(prf1, false)
32+
s1.Key(prf1, false)
3333

3434
prf1 = make([]byte, 32)
3535
s1.MetaAD([]byte("prf"), false)

transcript.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package transcript
22

33
import (
44
"encoding/binary"
5+
"math/rand"
56

67
"github.com/jsign/merlin/strobe128"
78
)
89

910
var (
10-
merlinProtocolLabel = []byte("Merlin v1.0")
11+
labelMerlinProtocol = []byte("Merlin v1.0")
12+
labelRng = []byte("rng")
1113
labelDomainSeparator = []byte("dom-sep")
1214
)
1315

@@ -18,7 +20,7 @@ type Transcript struct {
1820
// New returns a new Transcript object.
1921
func New(label []byte) *Transcript {
2022
tr := &Transcript{
21-
str: strobe128.New(merlinProtocolLabel),
23+
str: strobe128.New(labelMerlinProtocol),
2224
}
2325
tr.AppendMessage(labelDomainSeparator, label)
2426

@@ -46,3 +48,28 @@ func (t *Transcript) ChallengeBytes(label []byte, dest []byte) {
4648
t.str.MetaAD(dataLen[:], true)
4749
t.str.PRF(dest, false)
4850
}
51+
52+
type TranscriptRngBuilder struct {
53+
str strobe128.Strobe128
54+
}
55+
56+
func (trb *TranscriptRngBuilder) RekeyWithWitnessBytes(label []byte, witness []byte) {
57+
var dataLen [4]byte
58+
binary.LittleEndian.PutUint32(dataLen[:], uint32(len(witness)))
59+
trb.str.MetaAD(label, false)
60+
trb.str.MetaAD(dataLen[:], true)
61+
trb.str.Key(witness, false)
62+
}
63+
64+
func (trb *TranscriptRngBuilder) Finalize(rand rand.Rand) Transcript {
65+
var randbytes [32]byte
66+
rand.Read(randbytes[:])
67+
trb.str.MetaAD(labelRng, false)
68+
trb.str.Key(randbytes[:], false)
69+
70+
return Transcript{
71+
// A copy of trb.str is enough since the internal sturct
72+
// contains no pointers.
73+
str: trb.str,
74+
}
75+
}

0 commit comments

Comments
 (0)