-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcript.go
75 lines (62 loc) · 1.74 KB
/
transcript.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
package transcript
import (
"encoding/binary"
"math/rand"
"github.com/jsign/merlin/strobe128"
)
var (
labelMerlinProtocol = []byte("Merlin v1.0")
labelRng = []byte("rng")
labelDomainSeparator = []byte("dom-sep")
)
type Transcript struct {
str strobe128.Strobe128
}
// New returns a new Transcript object.
func New(label []byte) *Transcript {
tr := &Transcript{
str: strobe128.New(labelMerlinProtocol),
}
tr.AppendMessage(labelDomainSeparator, label)
return tr
}
func (t *Transcript) AppendMessage(label []byte, message []byte) {
var dataLen [4]byte
binary.LittleEndian.PutUint32(dataLen[:], uint32(len(message)))
t.str.MetaAD(label, false)
t.str.MetaAD(dataLen[:], true)
t.str.AD(message, false)
}
func (t *Transcript) AppendU64(label []byte, x uint64) {
var xbytes [8]byte
binary.LittleEndian.PutUint64(xbytes[:], x)
t.AppendMessage(label, xbytes[:])
}
func (t *Transcript) ChallengeBytes(label []byte, dest []byte) {
var dataLen [4]byte
binary.LittleEndian.PutUint32(dataLen[:], uint32(len(dest)))
t.str.MetaAD(label, false)
t.str.MetaAD(dataLen[:], true)
t.str.PRF(dest, false)
}
type TranscriptRngBuilder struct {
str strobe128.Strobe128
}
func (trb *TranscriptRngBuilder) RekeyWithWitnessBytes(label []byte, witness []byte) {
var dataLen [4]byte
binary.LittleEndian.PutUint32(dataLen[:], uint32(len(witness)))
trb.str.MetaAD(label, false)
trb.str.MetaAD(dataLen[:], true)
trb.str.Key(witness, false)
}
func (trb *TranscriptRngBuilder) Finalize(rand rand.Rand) Transcript {
var randbytes [32]byte
rand.Read(randbytes[:])
trb.str.MetaAD(labelRng, false)
trb.str.Key(randbytes[:], false)
return Transcript{
// A copy of trb.str is enough since the internal sturct
// contains no pointers.
str: trb.str,
}
}