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.

125 lines
3.2 KiB

4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package utils
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "math/big"
  7. "strings"
  8. "github.com/iden3/go-iden3-crypto/constants"
  9. "github.com/iden3/go-iden3-crypto/ff"
  10. )
  11. // NewIntFromString creates a new big.Int from a decimal integer encoded as a
  12. // string. It will panic if the string is not a decimal integer.
  13. func NewIntFromString(s string) *big.Int {
  14. v, ok := new(big.Int).SetString(s, 10)
  15. if !ok {
  16. panic(fmt.Sprintf("Bad base 10 string %s", s))
  17. }
  18. return v
  19. }
  20. // SwapEndianness swaps the endianness of the value encoded in xs. If xs is
  21. // Big-Endian, the result will be Little-Endian and viceversa.
  22. func SwapEndianness(xs []byte) []byte {
  23. ys := make([]byte, len(xs))
  24. for i, b := range xs {
  25. ys[len(xs)-1-i] = b
  26. }
  27. return ys
  28. }
  29. // BigIntLEBytes encodes a big.Int into an array in Little-Endian.
  30. func BigIntLEBytes(v *big.Int) [32]byte {
  31. le := SwapEndianness(v.Bytes())
  32. res := [32]byte{}
  33. copy(res[:], le)
  34. return res
  35. }
  36. // SetBigIntFromLEBytes sets the value of a big.Int from a Little-Endian
  37. // encoded value.
  38. func SetBigIntFromLEBytes(v *big.Int, leBuf []byte) *big.Int {
  39. beBuf := SwapEndianness(leBuf)
  40. return v.SetBytes(beBuf)
  41. }
  42. // Hex is a byte slice type that can be marshalled and unmarshaled in hex
  43. type Hex []byte
  44. // MarshalText encodes buf as hex
  45. func (buf Hex) MarshalText() ([]byte, error) {
  46. return []byte(hex.EncodeToString(buf)), nil
  47. }
  48. // String encodes buf as hex
  49. func (buf Hex) String() string {
  50. return hex.EncodeToString(buf)
  51. }
  52. // HexEncode encodes an array of bytes into a string in hex.
  53. func HexEncode(bs []byte) string {
  54. return fmt.Sprintf("0x%s", hex.EncodeToString(bs))
  55. }
  56. // HexDecode decodes a hex string into an array of bytes.
  57. func HexDecode(h string) ([]byte, error) {
  58. h = strings.TrimPrefix(h, "0x")
  59. return hex.DecodeString(h)
  60. }
  61. // HexDecodeInto decodes a hex string into an array of bytes (dst), verifying
  62. // that the decoded array has the same length as dst.
  63. func HexDecodeInto(dst []byte, h []byte) error {
  64. if bytes.HasPrefix(h, []byte("0x")) {
  65. h = h[2:]
  66. }
  67. if len(h)/2 != len(dst) {
  68. return fmt.Errorf("expected %v bytes in hex string, got %v", len(dst), len(h)/2) //nolint:gomnd
  69. }
  70. n, err := hex.Decode(dst, h)
  71. if err != nil {
  72. return err
  73. } else if n != len(dst) {
  74. return fmt.Errorf("expected %v bytes when decoding hex string, got %v", len(dst), n)
  75. }
  76. return nil
  77. }
  78. // CheckBigIntInField checks if given *big.Int fits in a Field Q element
  79. func CheckBigIntInField(a *big.Int) bool {
  80. return a.Cmp(constants.Q) == -1
  81. }
  82. // CheckBigIntArrayInField checks if given *big.Int fits in a Field Q element
  83. func CheckBigIntArrayInField(arr []*big.Int) bool {
  84. for _, a := range arr {
  85. if !CheckBigIntInField(a) {
  86. return false
  87. }
  88. }
  89. return true
  90. }
  91. // BigIntArrayToElementArray converts an array of *big.Int into an array of *ff.Element
  92. func BigIntArrayToElementArray(bi []*big.Int) []*ff.Element {
  93. var o []*ff.Element
  94. for i := range bi {
  95. o = append(o, ff.NewElement().SetBigInt(bi[i]))
  96. }
  97. return o
  98. }
  99. // ElementArrayToBigIntArray converts an array of *ff.Element into an array of *big.Int
  100. func ElementArrayToBigIntArray(e []*ff.Element) []*big.Int {
  101. var o []*big.Int
  102. for i := range e {
  103. ei := e[i]
  104. bi := big.NewInt(0)
  105. ei.ToBigIntRegular(bi)
  106. o = append(o, bi)
  107. }
  108. return o
  109. }