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.

68 lines
1.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. }