-
Notifications
You must be signed in to change notification settings - Fork 2
/
ec_handlers.go
104 lines (97 loc) · 2.96 KB
/
ec_handlers.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
package main
import (
"net/http"
"encoding/json"
"github.com/rynobey/bn256"
)
func ECOrder(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
encoder.Encode(Response{Num: NewNumber(bn256.Order)})
}
func ECAdd(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
var binaryEcOpParams BinaryEcOpParams
err := ReadContentsIntoStruct(r, &binaryEcOpParams)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
A, err := NewECPoint(binaryEcOpParams.A.X, binaryEcOpParams.A.Y, err)
B, err := NewECPoint(binaryEcOpParams.B.X, binaryEcOpParams.B.Y, err)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
ans := new(bn256.G1).Add(A, B)
curvePoint := NewCurvePoint(ans)
encoder.Encode(Response{P: curvePoint})
}
func ECSub(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
var binaryEcOpParams BinaryEcOpParams
err := ReadContentsIntoStruct(r, &binaryEcOpParams)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
A, err := NewECPoint(binaryEcOpParams.A.X, binaryEcOpParams.A.Y, err)
B, err := NewECPoint(binaryEcOpParams.B.X, binaryEcOpParams.B.Y, err)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
ans := new(bn256.G1).Add(A, B.Neg(B))
curvePoint := NewCurvePoint(ans)
encoder.Encode(Response{P: curvePoint})
}
func ECMul(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
var scalarEcOpParams ScalarEcOpParams
err := ReadContentsIntoStruct(r, &scalarEcOpParams)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
s, err := NewBigInt(scalarEcOpParams.S.V, err)
A, err := NewECPoint(scalarEcOpParams.A.X, scalarEcOpParams.A.Y, err)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
ans := new(bn256.G1).ScalarMult(A, s)
curvePoint := NewCurvePoint(ans)
encoder.Encode(Response{P: curvePoint})
}
func ECBaseMul(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
var number Number
err := ReadContentsIntoStruct(r, &number)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
s, err := NewBigInt(number.V, err)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
ans := new(bn256.G1).ScalarBaseMult(s)
curvePoint := NewCurvePoint(ans)
encoder.Encode(Response{P: curvePoint})
}
func ECHashToPoint(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
var text Text
err := ReadContentsIntoStruct(r, &text)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
A := new(bn256.G1).Hash(text.T)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
curvePoint := NewCurvePoint(A)
encoder.Encode(Response{P: curvePoint})
}