arnaucube
arnaucube/go-iden3-cryptoPublic Self-hosted repository
go-iden3-crypto/utils/utils.go
utils/utils.goRaw
  1package utils
  2
  3import (
  4	"bytes"
  5	"encoding/hex"
  6	"fmt"
  7	"math/big"
  8	"strings"
  9)
 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.
 13func 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
 21// SwapEndianness swaps the endianness of the value encoded in xs.  If xs is
 22// Big-Endian, the result will be Little-Endian and viceversa.
 23func SwapEndianness(xs []byte) []byte {
 24	ys := make([]byte, len(xs))
 25	for i, b := range xs {
 26		ys[len(xs)-1-i] = b
 27	}
 28	return ys
 29}
 30
 31// BigIntLEBytes encodes a big.Int into an array in Little-Endian.
 32func BigIntLEBytes(v *big.Int) [32]byte {
 33	le := SwapEndianness(v.Bytes())
 34	res := [32]byte{}
 35	copy(res[:], le)
 36	return res
 37}
 38
 39// SetBigIntFromLEBytes sets the value of a big.Int from a Little-Endian
 40// encoded value.
 41func SetBigIntFromLEBytes(v *big.Int, leBuf []byte) *big.Int {
 42	beBuf := SwapEndianness(leBuf)
 43	return v.SetBytes(beBuf)
 44}
 45
 46// Hex is a byte slice type that can be marshalled and unmarshaled in hex
 47type Hex []byte
 48
 49// MarshalText encodes buf as hex
 50func (buf Hex) MarshalText() ([]byte, error) {
 51	return []byte(hex.EncodeToString(buf)), nil
 52}
 53
 54// String encodes buf as hex
 55func (buf Hex) String() string {
 56	return hex.EncodeToString(buf)
 57}
 58
 59// HexEncode encodes an array of bytes into a string in hex.
 60func HexEncode(bs []byte) string {
 61	return fmt.Sprintf("0x%s", hex.EncodeToString(bs))
 62}
 63
 64// HexDecode decodes a hex string into an array of bytes.
 65func HexDecode(h string) ([]byte, error) {
 66	if strings.HasPrefix(h, "0x") {
 67		h = h[2:]
 68	}
 69	return hex.DecodeString(h)
 70}
 71
 72// HexDecodeInto decodes a hex string into an array of bytes (dst), verifying
 73// that the decoded array has the same length as dst.
 74func HexDecodeInto(dst []byte, h []byte) error {
 75	if bytes.HasPrefix(h, []byte("0x")) {
 76		h = h[2:]
 77	}
 78	if len(h)/2 != len(dst) {
 79		return fmt.Errorf("expected %v bytes in hex string, got %v", len(dst), len(h)/2)
 80	}
 81	n, err := hex.Decode(dst, h)
 82	if err != nil {
 83		return err
 84	} else if n != len(dst) {
 85		return fmt.Errorf("expected %v bytes when decoding hex string, got %v", len(dst), n)
 86	}
 87	return nil
 88}
 89
 90// CheckBigIntInField checks if given big.Int fits in a Field Q element
 91func CheckBigIntInField(a *big.Int, q *big.Int) bool {
 92	if a.Cmp(q) != -1 {
 93		return false
 94	}
 95	return true
 96}
 97
 98// CheckBigIntArrayInField checks if given big.Int fits in a Field Q element
 99func CheckBigIntArrayInField(arr []*big.Int, q *big.Int) bool {
100	for _, a := range arr {
101		if !CheckBigIntInField(a, q) {
102			return false
103		}
104	}
105	return true
106}