forked from rynobey/ECC-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
143 lines (133 loc) · 3.94 KB
/
util.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"errors"
"crypto/rand"
"net/http"
"encoding/json"
"io/ioutil"
"math/big"
"github.com/rynobey/bn256"
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/crypto/sha3"
)
func Hex32ByteChunksToStr(hexChunks []string) (string) {
hexStr := ""
lenArr := len(hexChunks)
for i := lenArr-1; i > 0; i-- {
hexStr = fmt.Sprintf("%s%s", hexChunks[i][2:], hexStr)
}
// remove leading zeros while keeping length even
lastChunk := hexChunks[0]
curIndex := 2
curDigit := lastChunk[curIndex]
for i := 2; curDigit == '0'; i++ {
curIndex = i
curDigit = lastChunk[curIndex]
}
if curIndex % 2 == 1 { curIndex -= 1 }
hexStr = fmt.Sprintf("0x%s%s", hexChunks[0][curIndex:], hexStr)
return hexStr
}
func HexStrTo32ByteChunks(hexStr string) ([]string) { // 32 bytes in hex = 64 digits
if (hexStr[:2] == "0x") { hexStr = hexStr[2:] }
length := len(hexStr)
lenArr := length/64
if (length % 64 != 0) { lenArr += 1 }
hexStrArr := make([]string, lenArr)
start := length - 64
end := length
for i := lenArr-1; i > 0; i-- {
hexStrArr[i] = fmt.Sprintf("0x%064s", hexStr[start:end])
start -= 64
end -= 64
}
hexStrArr[0] = fmt.Sprintf("0x%064s", hexStr[:end])
return hexStrArr
}
func IsZero(num *big.Int) (bool) {
return (num.Cmp(new(big.Int).SetInt64(0)) == 0)
}
func ReadContentsIntoStruct(r *http.Request, obj interface{}) (error) {
contents, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil { return err }
err = json.Unmarshal(contents, &obj)
if err != nil { return err }
return nil
}
func AddPrefixIfMissing(num string) (string) {
var newNum string
if len(num) < 3 || num[0:2] != "0x" {
newNum = fmt.Sprintf("0x%s", num)
return newNum
}
return num
}
func NewBigInt(num string, err error) (*big.Int, error) {
if err != nil {
return nil, err
} else {
num = AddPrefixIfMissing(num)
if len(num) < 3 {
return nil, errors.New("Unable to initialize big.Int from string: too short")
}
bn, ok := new(big.Int).SetString(num[2:], 16)
if !ok {
return nil, errors.New("Failed to initialize big.Int from string")
}
return bn, nil
}
}
func NewECPoint(xCoord string, yCoord string, err error) (*bn256.G1, error) {
if err != nil {
return nil, err
} else {
P := new(bn256.G1)
xCoord = AddPrefixIfMissing(xCoord)
yCoord = AddPrefixIfMissing(yCoord)
marshalledPoint := fmt.Sprintf("%064s%064s", xCoord[2:], yCoord[2:])
marshalledBytes, err := hex.DecodeString(marshalledPoint)
if err != nil {
return nil, err
}
_, err = P.Unmarshal(marshalledBytes)
if err != nil {
return nil, err
}
return P, nil
}
}
func GenerateSchnorrSignature(M string, X *big.Int, err error) (*bn256.G1, *bn256.G1, string, *big.Int, *big.Int, error) {
if err != nil {
return nil, nil, "", nil, nil, err
} else {
P := new(bn256.G1).ScalarBaseMult(X)
P_point := NewCurvePoint(P)
k, _ := rand.Int(rand.Reader, bn256.Order)
kG := new(bn256.G1).ScalarBaseMult(k)
kG_point := NewCurvePoint(kG)
h := sha3.NewKeccak256()
h.Reset()
h.Write([]byte(fmt.Sprintf("%s%s%s%s%s", M, P_point.X, P_point.Y, kG_point.X, kG_point.Y)))
e, _ := new(big.Int).SetString(fmt.Sprintf("%x", h.Sum(nil)), 16)
s := new(big.Int).Mod(new(big.Int).Add(k, new(big.Int).Mul(e, X)), bn256.Order)
return P, kG, M, e, s, nil
}
}
func VerifySchnorrSignature(P *bn256.G1, M string, E, S *big.Int, err error) (bool, error) {
if err != nil {
return false, err
} else {
P_point := NewCurvePoint(P)
sG := new(bn256.G1).ScalarBaseMult(S)
eP := new(bn256.G1).ScalarMult(P, E)
kG := new(bn256.G1).Add(sG, eP.Neg(eP))
kG_point := NewCurvePoint(kG)
h := sha3.NewKeccak256()
h.Reset()
h.Write([]byte(fmt.Sprintf("%s%s%s%s%s", M, P_point.X, P_point.Y, kG_point.X, kG_point.Y)))
e, _ := new(big.Int).SetString(fmt.Sprintf("%x", h.Sum(nil)), 16)
return (e.Cmp(E) == 0), nil
}
}