forked from rynobey/ECC-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_handlers.go
107 lines (100 loc) · 3 KB
/
math_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
105
106
107
package main
import (
"net/http"
"encoding/json"
"math/big"
"github.com/rynobey/bn256"
"crypto/rand"
)
func CryptoRandBigInt(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
num, _ := rand.Int(rand.Reader, bn256.Order)
encoder.Encode(Response{Num: NewNumber(num)})
}
func BigIntAdd(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
var binaryOpParams BinaryOpParams
err := ReadContentsIntoStruct(r, &binaryOpParams)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
a, err := NewBigInt(binaryOpParams.A, err)
b, err := NewBigInt(binaryOpParams.B, err)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
ans := new(big.Int).Add(a, b)
encoder.Encode(Response{Num: NewNumber(ans)})
}
func BigIntSubMod(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
var ternaryOpParams TernaryOpParams
err := ReadContentsIntoStruct(r, &ternaryOpParams)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
a, err := NewBigInt(ternaryOpParams.A, err)
b, err := NewBigInt(ternaryOpParams.B, err)
c, err := NewBigInt(ternaryOpParams.C, err)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
ans := new(big.Int).Sub(a, b)
ans.Mod(ans, c)
encoder.Encode(Response{Num: NewNumber(ans)})
}
func BigIntInvMod(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
var binaryOpParams BinaryOpParams
err := ReadContentsIntoStruct(r, &binaryOpParams)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
a, err := NewBigInt(binaryOpParams.A, err)
b, err := NewBigInt(binaryOpParams.B, err)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
ans := new(big.Int).ModInverse(a, b)
encoder.Encode(Response{Num: NewNumber(ans)})
}
func BigIntMul(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
var binaryOpParams BinaryOpParams
err := ReadContentsIntoStruct(r, &binaryOpParams)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
a, err := NewBigInt(binaryOpParams.A, err)
b, err := NewBigInt(binaryOpParams.B, err)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
ans := new(big.Int).Mul(a, b)
encoder.Encode(Response{Num: NewNumber(ans)})
}
func BigIntMod(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
var binaryOpParams BinaryOpParams
err := ReadContentsIntoStruct(r, &binaryOpParams)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
a, err := NewBigInt(binaryOpParams.A, err)
b, err := NewBigInt(binaryOpParams.B, err)
if err != nil {
encoder.Encode(Response{Err: &Error{Msg: err.Error()}})
return
}
ans := new(big.Int).Mod(a, b)
encoder.Encode(Response{Num: NewNumber(ans)})
}