-
Notifications
You must be signed in to change notification settings - Fork 11
/
serde.go
75 lines (60 loc) · 1.63 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
package ring
import (
"bytes"
"encoding/binary"
"errors"
"github.com/athanorlabs/go-dleq/types"
)
// Serialize converts the signature to a byte array.
func (r *RingSig) Serialize() ([]byte, error) {
sig := []byte{}
size := len(r.ring.pubkeys)
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(size))
sig = append(sig, b[:]...)
sig = append(sig, r.c.Encode()...)
sig = append(sig, r.image.Encode()...)
for i := 0; i < size; i++ {
sig = append(sig, r.s[i].Encode()...)
sig = append(sig, r.ring.pubkeys[i].Encode()...)
}
return sig, nil
}
// Deserialize converts the byteified signature into a *RingSig.
func (sig *RingSig) Deserialize(curve Curve, in []byte) error {
reader := bytes.NewBuffer(in)
pointLen := curve.CompressedPointSize()
size := binary.BigEndian.Uint32(reader.Next(4))
if len(in) < int(size)*pointLen {
return errors.New("input too short")
}
// WARN: this assumes the groups have an encoded scalar length of 32!
// which is fine for ed25519 and secp256k1, but may need to be changed
// if other curves are added.
const scalarLen = 32
var err error
sig.c, err = curve.DecodeToScalar(reader.Next(scalarLen))
if err != nil {
return err
}
sig.image, err = curve.DecodeToPoint(reader.Next(pointLen))
if err != nil {
return err
}
sig.ring = &Ring{
pubkeys: make([]types.Point, size),
curve: curve,
}
sig.s = make([]types.Scalar, size)
for i := 0; i < int(size); i++ {
sig.s[i], err = curve.DecodeToScalar(reader.Next(scalarLen))
if err != nil {
return err
}
sig.ring.pubkeys[i], err = curve.DecodeToPoint(reader.Next(pointLen))
if err != nil {
return err
}
}
return nil
}