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.

121 lines
2.1 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 stringToG1(h []string) (*bn256.G1, error) {
  10. if len(h) <= 2 {
  11. return nil, fmt.Errorf("not enought data for stringToG1")
  12. }
  13. h = h[:2]
  14. hexa := false
  15. if len(h[0]) > 1 {
  16. if "0x" == h[0][:2] {
  17. hexa = true
  18. }
  19. }
  20. in := ""
  21. var b []byte
  22. var err error
  23. if hexa {
  24. for i := range h {
  25. in += strings.TrimPrefix(h[i], "0x")
  26. }
  27. b, err = hex.DecodeString(in)
  28. if err != nil {
  29. return nil, err
  30. }
  31. } else {
  32. // TODO TMP
  33. // TODO use stringToBytes()
  34. if h[0] == "1" {
  35. h[0] = "0"
  36. }
  37. if h[1] == "1" {
  38. h[1] = "0"
  39. }
  40. bi0, ok := new(big.Int).SetString(h[0], 10)
  41. if !ok {
  42. return nil, fmt.Errorf("error parsing stringToG1")
  43. }
  44. bi1, ok := new(big.Int).SetString(h[1], 10)
  45. if !ok {
  46. return nil, fmt.Errorf("error parsing stringToG1")
  47. }
  48. b0 := bi0.Bytes()
  49. b1 := bi1.Bytes()
  50. if len(b0) != 32 {
  51. b0 = addZPadding(b0)
  52. }
  53. if len(b1) != 32 {
  54. b1 = addZPadding(b1)
  55. }
  56. b = append(b, b0...)
  57. b = append(b, b1...)
  58. }
  59. p := new(bn256.G1)
  60. _, err = p.Unmarshal(b)
  61. return p, err
  62. }
  63. func stringToG2(h [][]string) (*bn256.G2, error) {
  64. if len(h) <= 2 {
  65. return nil, fmt.Errorf("not enought data for stringToG2")
  66. }
  67. h = h[:2]
  68. hexa := false
  69. if len(h[0][0]) > 1 {
  70. if "0x" == h[0][0][:2] {
  71. hexa = true
  72. }
  73. }
  74. in := ""
  75. var b []byte
  76. var err error
  77. if hexa {
  78. for i := 0; i < len(h); i++ {
  79. for j := 0; j < len(h[i]); j++ {
  80. in += strings.TrimPrefix(h[i][j], "0x")
  81. }
  82. }
  83. b, err = hex.DecodeString(in)
  84. if err != nil {
  85. return nil, err
  86. }
  87. } else {
  88. // TODO TMP
  89. bH, err := stringToBytes(h[0][1])
  90. if err != nil {
  91. return nil, err
  92. }
  93. b = append(b, bH...)
  94. bH, err = stringToBytes(h[0][0])
  95. if err != nil {
  96. return nil, err
  97. }
  98. b = append(b, bH...)
  99. bH, err = stringToBytes(h[1][1])
  100. if err != nil {
  101. return nil, err
  102. }
  103. b = append(b, bH...)
  104. bH, err = stringToBytes(h[1][0])
  105. if err != nil {
  106. return nil, err
  107. }
  108. b = append(b, bH...)
  109. }
  110. p := new(bn256.G2)
  111. _, err = p.Unmarshal(b)
  112. return p, err
  113. }