You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.1 KiB

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