-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimitives.go
75 lines (62 loc) · 1.5 KB
/
primitives.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
// Copyright (c) 2015-2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package threshold
import (
"math/big"
)
// copyBytes copies a byte slice to a 32 byte array.
func copyBytes(aB []byte) *[32]byte {
if aB == nil {
return nil
}
s := new([32]byte)
// If we have a short byte string, expand
// it so that it's long enough.
aBLen := len(aB)
if aBLen < scalarSize {
diff := scalarSize - aBLen
for i := 0; i < diff; i++ {
aB = append([]byte{0x00}, aB...)
}
}
for i := 0; i < scalarSize; i++ {
s[i] = aB[i]
}
return s
}
// bigIntToEncodedBytes converts a big integer into its corresponding
// 32 byte little endian representation.
func bigIntToEncodedBytes(a *big.Int) *[32]byte {
s := new([32]byte)
if a == nil {
return s
}
// Caveat: a can be longer than 32 bytes.
aB := a.Bytes()
// If we have a short byte string, expand
// it so that it's long enough.
aBLen := len(aB)
if aBLen < scalarSize {
diff := scalarSize - aBLen
for i := 0; i < diff; i++ {
aB = append([]byte{0x00}, aB...)
}
}
for i := 0; i < scalarSize; i++ {
s[i] = aB[i]
}
return s
}
// encodedBytesToBigInt converts a 32 byte big endian representation of
// an integer into a big integer.
func encodedBytesToBigInt(s *[32]byte) *big.Int {
// Use a copy so we don't screw up our original
// memory.
sCopy := new([32]byte)
for i := 0; i < scalarSize; i++ {
sCopy[i] = s[i]
}
bi := new(big.Int).SetBytes(sCopy[:])
return bi
}