-
Notifications
You must be signed in to change notification settings - Fork 0
/
serde.go
160 lines (128 loc) · 3.77 KB
/
serde.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package dleq
import (
"bytes"
"errors"
"github.com/athanorlabs/go-dleq/types"
)
var errInputBytesTooShort = errors.New("input bytes too short")
// Serialize encodes the proof.
func (p *Proof) Serialize() []byte {
b := append(p.CommitmentA.Encode(), p.CommitmentB.Encode()...)
// WARN: this assumes the bitlen of the witness is less than 256.
b = append(b, byte(len(p.proofs)))
for _, bp := range p.proofs {
b = append(b, bp.encode()...)
}
// WARN: this assumes the signature length is less than 256.
b = append(b, byte(len(p.signatureA.inner)))
b = append(b, p.signatureA.inner...)
b = append(b, byte(len(p.signatureB.inner)))
b = append(b, p.signatureB.inner...)
return b
}
func (p *bitProof) encode() []byte {
b := append(p.commitmentA.commitment.Encode(), p.commitmentB.commitment.Encode()...)
b = append(b, p.ringSig.eCurveA.Encode()...)
b = append(b, p.ringSig.eCurveB.Encode()...)
b = append(b, p.ringSig.a0.Encode()...)
b = append(b, p.ringSig.a1.Encode()...)
b = append(b, p.ringSig.b0.Encode()...)
b = append(b, p.ringSig.b1.Encode()...)
return b
}
// Deserialize decodes the proof for the given curves.
// The curves must match those passed into `NewProof`.
func (p *Proof) Deserialize(curveA, curveB types.Curve, in []byte) error {
reader := bytes.NewBuffer(in)
pointLenA := curveA.CompressedPointSize()
pointLenB := curveB.CompressedPointSize()
if len(in) < pointLenA+pointLenB {
return errInputBytesTooShort
}
// WARN: this assumes the groups have an encoded scalar length of 32!
const scalarLen = 32
var err error
p.CommitmentA, err = curveA.DecodeToPoint(reader.Next(pointLenA))
if err != nil {
return err
}
p.CommitmentB, err = curveB.DecodeToPoint(reader.Next(pointLenB))
if err != nil {
return err
}
if reader.Len() < 1 {
return errInputBytesTooShort
}
bitProofsLen := reader.Next(1)
// TODO put bitProofsLen + sigLens first so we know the total expected length?
minLenRemaining := (int(bitProofsLen[0]) * (pointLenA + pointLenB + scalarLen*6))
if reader.Len() < minLenRemaining {
return errInputBytesTooShort
}
p.proofs = make([]bitProof, bitProofsLen[0])
for i := 0; i < int(bitProofsLen[0]); i++ {
bp := new(bitProof)
err = bp.decode(reader, curveA, curveB, scalarLen)
if err != nil {
return err
}
p.proofs[i] = *bp
}
if reader.Len() < 1 {
return errInputBytesTooShort
}
sigLen := reader.Next(1)
if reader.Len() < int(sigLen[0]) {
return errInputBytesTooShort
}
p.signatureA.inner = make([]byte, sigLen[0])
copy(p.signatureA.inner, reader.Next(int(sigLen[0])))
if reader.Len() < 1 {
return errInputBytesTooShort
}
sigLen = reader.Next(1)
if reader.Len() < int(sigLen[0]) {
return errInputBytesTooShort
}
p.signatureB.inner = make([]byte, sigLen[0])
copy(p.signatureB.inner, reader.Next(int(sigLen[0])))
return nil
}
func (p *bitProof) decode(r *bytes.Buffer, curveA, curveB types.Curve, scalarLen int) error {
pointLenA := curveA.CompressedPointSize()
pointLenB := curveB.CompressedPointSize()
var err error
p.commitmentA.commitment, err = curveA.DecodeToPoint(r.Next(pointLenA))
if err != nil {
return err
}
p.commitmentB.commitment, err = curveB.DecodeToPoint(r.Next(pointLenB))
if err != nil {
return err
}
p.ringSig.eCurveA, err = curveA.DecodeToScalar(r.Next(scalarLen))
if err != nil {
return err
}
p.ringSig.eCurveB, err = curveB.DecodeToScalar(r.Next(scalarLen))
if err != nil {
return err
}
p.ringSig.a0, err = curveA.DecodeToScalar(r.Next(scalarLen))
if err != nil {
return err
}
p.ringSig.a1, err = curveA.DecodeToScalar(r.Next(scalarLen))
if err != nil {
return err
}
p.ringSig.b0, err = curveB.DecodeToScalar(r.Next(scalarLen))
if err != nil {
return err
}
p.ringSig.b1, err = curveB.DecodeToScalar(r.Next(scalarLen))
if err != nil {
return err
}
return nil
}