utils.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bls12381
  17. import (
  18. "errors"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. )
  22. func bigFromHex(hex string) *big.Int {
  23. return new(big.Int).SetBytes(common.FromHex(hex))
  24. }
  25. // decodeFieldElement expects 64 byte input with zero top 16 bytes,
  26. // returns lower 48 bytes.
  27. func decodeFieldElement(in []byte) ([]byte, error) {
  28. if len(in) != 64 {
  29. return nil, errors.New("invalid field element length")
  30. }
  31. // check top bytes
  32. for i := 0; i < 16; i++ {
  33. if in[i] != byte(0x00) {
  34. return nil, errors.New("invalid field element top bytes")
  35. }
  36. }
  37. out := make([]byte, 48)
  38. copy(out[:], in[16:])
  39. return out, nil
  40. }