-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoding.go
59 lines (45 loc) · 1.33 KB
/
encoding.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
package musig2
import (
"fmt"
"github.com/gtank/ristretto255"
)
func marshalNonces(nonces [NoncesLen]*ristretto255.Element) []byte {
out := make([]byte, 0, noncesBytesLen)
for _, v := range nonces {
out = v.Encode(out)
}
return out
}
func marshalSig(R *ristretto255.Element, s *ristretto255.Scalar) []byte {
var out [64]byte
R.Encode(out[0:0:32])
s.Encode(out[32:32:64])
return out[:]
}
func unmarshalNonces(data []byte) ([NoncesLen]*ristretto255.Element, error) {
var out, zeros [NoncesLen]*ristretto255.Element
if len(data) != noncesBytesLen {
return zeros, fmt.Errorf("length(%d)!=%d: %w", len(data), noncesBytesLen, ErrBadNonces)
}
for i := range out {
out[i] = ristretto255.NewElement()
if err := out[i].Decode(data[i*32 : (i+1)*32]); err != nil {
return zeros, fmt.Errorf("%d-th nonce is invalid: %w", i, ErrBadNonces)
}
}
return out, nil
}
func unmarshalSig(sig []byte) (*ristretto255.Element, *ristretto255.Scalar, error) {
if len(sig) != 64 {
return nil, nil, fmt.Errorf("expect length 64, got %d: %w", len(sig), ErrBadSig)
}
R := ristretto255.NewElement()
if err := R.Decode(sig[:32]); err != nil {
return nil, nil, fmt.Errorf("invalid R: %w", err)
}
s := ristretto255.NewScalar()
if err := s.Decode(sig[32:]); err != nil {
return nil, nil, fmt.Errorf("invalid s: %w", err)
}
return R, s, nil
}