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.

38 lines
1.0 KiB

  1. package common
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. ethCommon "github.com/ethereum/go-ethereum/common"
  6. "github.com/iden3/go-iden3-crypto/babyjub"
  7. )
  8. // SwapEndianness swaps the order of the bytes in the slice.
  9. func SwapEndianness(b []byte) []byte {
  10. o := make([]byte, len(b))
  11. for i := range b {
  12. o[len(b)-1-i] = b[i]
  13. }
  14. return o
  15. }
  16. // EthAddrToBigInt returns a *big.Int from a given ethereum common.Address.
  17. func EthAddrToBigInt(a ethCommon.Address) *big.Int {
  18. return new(big.Int).SetBytes(a.Bytes())
  19. }
  20. // BJJFromStringWithChecksum parses a hex string in Hermez format (which has
  21. // the Hermez checksum at the last byte, and is encoded in BigEndian) and
  22. // returns the corresponding *babyjub.PublicKey. This method is not part of the
  23. // spec, is used for importing javascript test vectors data.
  24. func BJJFromStringWithChecksum(s string) (*babyjub.PublicKey, error) {
  25. b, err := hex.DecodeString(s)
  26. if err != nil {
  27. return nil, err
  28. }
  29. pkBytes := SwapEndianness(b)
  30. var pkComp babyjub.PublicKeyComp
  31. copy(pkComp[:], pkBytes[:])
  32. return pkComp.Decompress()
  33. }