add travis, add go.mod go.sum, update babyjub hex encoders to avoid

importing go-iden3
This commit is contained in:
arnaucube
2019-06-25 15:13:40 +02:00
parent e92a15d3b0
commit 7b219e15ec
10 changed files with 103 additions and 21 deletions

View File

@@ -1,8 +1,13 @@
package babyjub
import (
"github.com/dchest/blake512" // I have personally reviewed that this module doesn't do anything suspicious
"bytes"
"encoding/hex"
"fmt"
"math/big"
"strings"
"github.com/dchest/blake512" // I have personally reviewed that this module doesn't do anything suspicious
)
// SwapEndianness swaps the endianness of the value encoded in xs. If xs is
@@ -37,3 +42,47 @@ func Blake512(m []byte) []byte {
h.Write(m[:])
return h.Sum(nil)
}
// Hex is a byte slice type that can be marshalled and unmarshaled in hex
type Hex []byte
// MarshalText encodes buf as hex
func (buf Hex) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(buf)), nil
}
// String encodes buf as hex
func (buf Hex) String() string {
return hex.EncodeToString(buf)
}
// HexEncode encodes an array of bytes into a string in hex.
func HexEncode(bs []byte) string {
return fmt.Sprintf("0x%s", hex.EncodeToString(bs))
}
// HexDecode decodes a hex string into an array of bytes.
func HexDecode(h string) ([]byte, error) {
if strings.HasPrefix(h, "0x") {
h = h[2:]
}
return hex.DecodeString(h)
}
// HexDecodeInto decodes a hex string into an array of bytes (dst), verifying
// that the decoded array has the same length as dst.
func HexDecodeInto(dst []byte, h []byte) error {
if bytes.HasPrefix(h, []byte("0x")) {
h = h[2:]
}
if len(h)/2 != len(dst) {
return fmt.Errorf("expected %v bytes in hex string, got %v", len(dst), len(h)/2)
}
n, err := hex.Decode(dst, h)
if err != nil {
return err
} else if n != len(dst) {
return fmt.Errorf("expected %v bytes when decoding hex string, got %v", len(dst), n)
}
return nil
}