-
Notifications
You must be signed in to change notification settings - Fork 0
/
EllipticCurvePointAddition.go
83 lines (72 loc) · 1.7 KB
/
EllipticCurvePointAddition.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
package routers
import (
"encoding/json"
"math"
"net/http"
"strconv"
"github.com/JavDomGom/gcat/models"
"github.com/JavDomGom/gcat/resources"
)
// EllipticCurvePointAddition for elliptic curve: y² = x³ + ax + b mod p
func EllipticCurvePointAddition(w http.ResponseWriter, r *http.Request) {
// Parameters.
a, err := strconv.ParseInt(r.URL.Query().Get("a"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
b, err := strconv.ParseInt(r.URL.Query().Get("b"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
p, err := strconv.ParseInt(r.URL.Query().Get("p"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
x_1, err := strconv.ParseInt(r.URL.Query().Get("x_1"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
y_1, err := strconv.ParseInt(r.URL.Query().Get("y_1"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
x_2, err := strconv.ParseInt(r.URL.Query().Get("x_2"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
y_2, err := strconv.ParseInt(r.URL.Query().Get("y_2"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
op_x := x_1 - x_2
op_y := y_1 - y_2
inv_op_x := resources.ModInv(op_x, p)
lambda := resources.Mod(op_y*inv_op_x, p)
x := resources.Mod(int64(math.Pow(float64(lambda), 2))-x_1-x_2, p)
y := resources.Mod(lambda*(x_1-x)-y_1, p)
var ecpa = models.EllipticCurvePointAddition{
A: a,
B: b,
P: p,
X_1: x_1,
Y_1: y_1,
X_2: x_2,
Y_2: y_2,
Op_x: op_x,
Op_y: op_y,
Inv_op_x: inv_op_x,
Lambda: lambda,
X: x,
Y: y,
}
w.Header().Set("context-type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(ecpa)
}