forked from renproject/mpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarshal.go
59 lines (53 loc) · 1.64 KB
/
marshal.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 brng
import (
"fmt"
"math/rand"
"reflect"
"github.com/renproject/secp256k1"
"github.com/renproject/surge"
)
// Generate implements the quick.Generator interface.
func (brnger BRNGer) Generate(_ *rand.Rand, _ int) reflect.Value {
batchSize := rand.Uint32()
index := secp256k1.RandomFn()
h := secp256k1.RandomPoint()
return reflect.ValueOf(BRNGer{batchSize, index, h})
}
// SizeHint implements the surge.SizeHinter interface.
func (brnger BRNGer) SizeHint() int {
return surge.SizeHint(brnger.batchSize) +
brnger.index.SizeHint() +
brnger.h.SizeHint()
}
// Marshal implements the surge.Marshaler interface.
func (brnger BRNGer) Marshal(buf []byte, rem int) ([]byte, int, error) {
buf, rem, err := surge.MarshalU32(uint32(brnger.batchSize), buf, rem)
if err != nil {
return buf, rem, fmt.Errorf("marshaling batchSize: %v", err)
}
buf, rem, err = brnger.index.Marshal(buf, rem)
if err != nil {
return buf, rem, fmt.Errorf("marshaling h: %v", err)
}
buf, rem, err = brnger.h.Marshal(buf, rem)
if err != nil {
return buf, rem, fmt.Errorf("marshaling h: %v", err)
}
return buf, rem, nil
}
// Unmarshal implements the surge.Unmarshaler interface.
func (brnger *BRNGer) Unmarshal(buf []byte, rem int) ([]byte, int, error) {
buf, rem, err := surge.UnmarshalU32(&brnger.batchSize, buf, rem)
if err != nil {
return buf, rem, fmt.Errorf("unmarshaling batchSize: %v", err)
}
buf, rem, err = brnger.index.Unmarshal(buf, rem)
if err != nil {
return buf, rem, fmt.Errorf("unmarshaling h: %v", err)
}
buf, rem, err = brnger.h.Unmarshal(buf, rem)
if err != nil {
return buf, rem, fmt.Errorf("unmarshaling h: %v", err)
}
return buf, rem, nil
}