/
utils.go
128 lines (102 loc) · 2.71 KB
/
utils.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
// Copyright © 2019 Binance
//
// This file is part of Binance. The full Binance copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
package signing
import (
"crypto/elliptic"
"math/big"
"github.com/agl/ed25519/edwards25519"
"github.com/holynull/tss-wasm-lib/common"
)
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 < 32; i++ {
sCopy[i] = s[i]
}
reverse(sCopy)
bi := new(big.Int).SetBytes(sCopy[:])
return bi
}
func bigIntToEncodedBytes(a *big.Int) *[32]byte {
s := new([32]byte)
if a == nil {
return s
}
// Caveat: a can be longer than 32 bytes.
s = copyBytes(a.Bytes())
// Reverse the byte string --> little endian after
// encoding.
reverse(s)
return s
}
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 < 32 {
diff := 32 - aBLen
for i := 0; i < diff; i++ {
aB = append([]byte{0x00}, aB...)
}
}
for i := 0; i < 32; i++ {
s[i] = aB[i]
}
return s
}
func ecPointToEncodedBytes(x *big.Int, y *big.Int) *[32]byte {
s := bigIntToEncodedBytes(y)
xB := bigIntToEncodedBytes(x)
xFE := new(edwards25519.FieldElement)
edwards25519.FeFromBytes(xFE, xB)
isNegative := edwards25519.FeIsNegative(xFE) == 1
if isNegative {
s[31] |= (1 << 7)
} else {
s[31] &^= (1 << 7)
}
return s
}
func reverse(s *[32]byte) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func addExtendedElements(p, q edwards25519.ExtendedGroupElement) edwards25519.ExtendedGroupElement {
var r edwards25519.CompletedGroupElement
var qCached edwards25519.CachedGroupElement
q.ToCached(&qCached)
edwards25519.GeAdd(&r, &p, &qCached)
var result edwards25519.ExtendedGroupElement
r.ToExtended(&result)
return result
}
func ecPointToExtendedElement(ec elliptic.Curve, x *big.Int, y *big.Int) edwards25519.ExtendedGroupElement {
encodedXBytes := bigIntToEncodedBytes(x)
encodedYBytes := bigIntToEncodedBytes(y)
z := common.GetRandomPositiveInt(ec.Params().N)
encodedZBytes := bigIntToEncodedBytes(z)
var fx, fy, fxy edwards25519.FieldElement
edwards25519.FeFromBytes(&fx, encodedXBytes)
edwards25519.FeFromBytes(&fy, encodedYBytes)
var X, Y, Z, T edwards25519.FieldElement
edwards25519.FeFromBytes(&Z, encodedZBytes)
edwards25519.FeMul(&X, &fx, &Z)
edwards25519.FeMul(&Y, &fy, &Z)
edwards25519.FeMul(&fxy, &fx, &fy)
edwards25519.FeMul(&T, &fxy, &Z)
return edwards25519.ExtendedGroupElement{
X: X,
Y: Y,
Z: Z,
T: T,
}
}