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.

169 lines
2.9 KiB

  1. package gocircomprover
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "math/big"
  6. "strings"
  7. bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  8. )
  9. func addZPadding(b []byte) []byte {
  10. var z [32]byte
  11. var r []byte
  12. r = append(r, z[len(b):]...) // add padding on the left
  13. r = append(r, b...)
  14. return r[:32]
  15. }
  16. func stringToBytes(s string) ([]byte, error) {
  17. if s == "1" {
  18. s = "0"
  19. }
  20. bi, ok := new(big.Int).SetString(s, 10)
  21. if !ok {
  22. return nil, fmt.Errorf("error parsing bigint stringToBytes")
  23. }
  24. b := bi.Bytes()
  25. if len(b) != 32 {
  26. b = addZPadding(b)
  27. }
  28. return b, nil
  29. }
  30. func arrayStringToG1(h [][]string) ([]*bn256.G1, error) {
  31. var o []*bn256.G1
  32. for i := 0; i < len(h); i++ {
  33. hi, err := stringToG1(h[i])
  34. if err != nil {
  35. return o, err
  36. }
  37. o = append(o, hi)
  38. }
  39. return o, nil
  40. }
  41. func arrayStringToG2(h [][][]string) ([]*bn256.G2, error) {
  42. var o []*bn256.G2
  43. for i := 0; i < len(h); i++ {
  44. hi, err := stringToG2(h[i])
  45. if err != nil {
  46. return o, err
  47. }
  48. o = append(o, hi)
  49. }
  50. return o, nil
  51. }
  52. func stringToG1(h []string) (*bn256.G1, error) {
  53. if len(h) <= 2 {
  54. return nil, fmt.Errorf("not enought data for stringToG1")
  55. }
  56. h = h[:2]
  57. hexa := false
  58. if len(h[0]) > 1 {
  59. if "0x" == h[0][:2] {
  60. hexa = true
  61. }
  62. }
  63. in := ""
  64. var b []byte
  65. var err error
  66. if hexa {
  67. for i := range h {
  68. in += strings.TrimPrefix(h[i], "0x")
  69. }
  70. b, err = hex.DecodeString(in)
  71. if err != nil {
  72. return nil, err
  73. }
  74. } else {
  75. // TODO TMP
  76. // TODO use stringToBytes()
  77. if h[0] == "1" {
  78. h[0] = "0"
  79. }
  80. if h[1] == "1" {
  81. h[1] = "0"
  82. }
  83. bi0, ok := new(big.Int).SetString(h[0], 10)
  84. if !ok {
  85. return nil, fmt.Errorf("error parsing stringToG1")
  86. }
  87. bi1, ok := new(big.Int).SetString(h[1], 10)
  88. if !ok {
  89. return nil, fmt.Errorf("error parsing stringToG1")
  90. }
  91. b0 := bi0.Bytes()
  92. b1 := bi1.Bytes()
  93. if len(b0) != 32 {
  94. b0 = addZPadding(b0)
  95. }
  96. if len(b1) != 32 {
  97. b1 = addZPadding(b1)
  98. }
  99. b = append(b, b0...)
  100. b = append(b, b1...)
  101. }
  102. p := new(bn256.G1)
  103. _, err = p.Unmarshal(b)
  104. return p, err
  105. }
  106. func stringToG2(h [][]string) (*bn256.G2, error) {
  107. if len(h) <= 2 {
  108. return nil, fmt.Errorf("not enought data for stringToG2")
  109. }
  110. h = h[:2]
  111. hexa := false
  112. if len(h[0][0]) > 1 {
  113. if "0x" == h[0][0][:2] {
  114. hexa = true
  115. }
  116. }
  117. in := ""
  118. var b []byte
  119. var err error
  120. if hexa {
  121. for i := 0; i < len(h); i++ {
  122. for j := 0; j < len(h[i]); j++ {
  123. in += strings.TrimPrefix(h[i][j], "0x")
  124. }
  125. }
  126. b, err = hex.DecodeString(in)
  127. if err != nil {
  128. return nil, err
  129. }
  130. } else {
  131. // TODO TMP
  132. bH, err := stringToBytes(h[0][1])
  133. if err != nil {
  134. return nil, err
  135. }
  136. b = append(b, bH...)
  137. bH, err = stringToBytes(h[0][0])
  138. if err != nil {
  139. return nil, err
  140. }
  141. b = append(b, bH...)
  142. bH, err = stringToBytes(h[1][1])
  143. if err != nil {
  144. return nil, err
  145. }
  146. b = append(b, bH...)
  147. bH, err = stringToBytes(h[1][0])
  148. if err != nil {
  149. return nil, err
  150. }
  151. b = append(b, bH...)
  152. }
  153. p := new(bn256.G2)
  154. _, err = p.Unmarshal(b)
  155. return p, err
  156. }