arnaucube
arnaucube/blindsig-client-server-examplePublic Self-hosted repository
blindsig-client-server-example/main.go
main.goRaw
 1package main
 2
 3import (
 4	"fmt"
 5	"math/big"
 6	"net/http"
 7
 8	blindsecp256k1 "github.com/arnaucube/go-blindsecp256k1"
 9	"github.com/gin-gonic/gin"
10)
11
12var sk *blindsecp256k1.PrivateKey
13var secretRs = make(map[string]*big.Int)
14
15func getNewRequest(c *gin.Context) {
16	k, signerR := blindsecp256k1.NewRequestParameters()
17	key := signerR.X.String() + signerR.Y.String()
18	secretRs[key] = k
19	c.JSON(http.StatusOK, gin.H{"signerR": signerR, "signerQ": sk.Public().Point()})
20}
21
22type msgPostBlindSign struct {
23	M string                `json:"m"`
24	R *blindsecp256k1.Point `json:"r"`
25}
26
27func postBlindSign(c *gin.Context) {
28	var msg msgPostBlindSign
29	c.BindJSON(&msg)
30
31	m, ok := new(big.Int).SetString(msg.M, 10)
32	if !ok {
33		c.String(http.StatusBadRequest, "can not parse m")
34		return
35	}
36
37	key := msg.R.X.String() + msg.R.Y.String()
38	if _, ok := secretRs[key]; !ok {
39		c.String(http.StatusBadRequest, "unknown R")
40		return
41	}
42	k := secretRs[key]
43	sBlind, err := sk.BlindSign(m, k)
44	if err != nil {
45		c.String(http.StatusBadRequest, "err on BlindSign: "+err.Error())
46		return
47	}
48	delete(secretRs, key)
49	c.JSON(http.StatusOK, gin.H{"sBlind": sBlind.String()})
50}
51
52type msgPostVerify struct {
53	M   string                    `json:"m"`
54	Sig *blindsecp256k1.Signature `json:"sig"`
55	Q   *blindsecp256k1.PublicKey `json:"q"`
56}
57
58func postVerify(c *gin.Context) {
59	var msg msgPostVerify
60	c.BindJSON(&msg)
61
62	m, ok := new(big.Int).SetString(msg.M, 10)
63	if !ok {
64		c.String(http.StatusBadRequest, "can not parse m")
65		return
66	}
67	fmt.Println(msg.Sig.S, msg.Sig.F)
68	v := blindsecp256k1.Verify(m, msg.Sig, sk.Public())
69	fmt.Println("v", v)
70	if !v {
71		fmt.Println("m", m)
72		fmt.Println("sig.s", msg.Sig.S)
73		fmt.Println("sig.f", msg.Sig.F)
74		fmt.Println("pubk", sk.Public())
75		fmt.Println("q", msg.Q)
76		c.JSON(http.StatusNotAcceptable, gin.H{"verification": false})
77		return
78	}
79	c.JSON(http.StatusOK, gin.H{"verification": v})
80}
81
82func main() {
83	secretRs = make(map[string]*big.Int)
84	sk = blindsecp256k1.NewPrivateKey()
85
86	r := gin.Default()
87
88	r.GET("/request", getNewRequest)
89	r.POST("/blindsign", postBlindSign)
90	r.POST("/verify", postVerify)
91	r.Static("/web", "./client")
92
93	r.Run("127.0.0.1:3000")
94}