From 7b219e15ecda52fd1aa5517ba24e5c18562cb554 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Tue, 25 Jun 2019 15:13:40 +0200 Subject: [PATCH] add travis, add go.mod go.sum, update babyjub hex encoders to avoid importing go-iden3 --- .travis.yml | 8 +++++++ README.md | 2 +- babyjub/babyjub_test.go | 4 ++-- babyjub/eddsa.go | 26 ++++++++++----------- babyjub/eddsa_test.go | 1 - babyjub/helpers.go | 51 ++++++++++++++++++++++++++++++++++++++++- go.mod | 10 ++++++++ go.sum | 18 +++++++++++++++ mimc7/mimc7.go | 2 +- mimc7/mimc7_test.go | 2 +- 10 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 .travis.yml create mode 100644 go.mod create mode 100644 go.sum diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1e86435 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +dist: xenial +language: go + +go: + - "1.12" + +env: + - GO111MODULE=on diff --git a/README.md b/README.md index 26abbba..87c58e9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# go-iden3-crypto [![Go Report Card](https://goreportcard.com/badge/github.com/iden3/go-iden3-crypto)](https://goreportcard.com/report/github.com/iden3/go-iden3-crypto) [![GoDoc](https://godoc.org/github.com/iden3/go-iden3-crypto?status.svg)](https://godoc.org/github.com/iden3/go-iden3-crypto) +# go-iden3-crypto [![Go Report Card](https://goreportcard.com/badge/github.com/iden3/go-iden3-crypto)](https://goreportcard.com/report/github.com/iden3/go-iden3-crypto) [![Build Status](https://travis-ci.org/iden3/go-iden3-crypto.svg?branch=master)](https://travis-ci.org/iden3/go-iden3-crypto) [![GoDoc](https://godoc.org/github.com/iden3/go-iden3-crypto?status.svg)](https://godoc.org/github.com/iden3/go-iden3-crypto) Go implementation of some cryptographic primitives used in iden3 diff --git a/babyjub/babyjub_test.go b/babyjub/babyjub_test.go index 0a820a8..0328412 100644 --- a/babyjub/babyjub_test.go +++ b/babyjub/babyjub_test.go @@ -1,11 +1,11 @@ package babyjub import ( - // "fmt" "encoding/hex" - "github.com/stretchr/testify/assert" "math/big" "testing" + + "github.com/stretchr/testify/assert" ) func TestAdd1(t *testing.T) { diff --git a/babyjub/eddsa.go b/babyjub/eddsa.go index 5a189d2..e9e7cd7 100644 --- a/babyjub/eddsa.go +++ b/babyjub/eddsa.go @@ -2,11 +2,9 @@ package babyjub import ( "crypto/rand" - // "encoding/hex" - // "fmt" - common3 "github.com/iden3/go-iden3/common" - "github.com/iden3/go-iden3/crypto/mimc7" - // "golang.org/x/crypto/blake2b" + + "github.com/iden3/go-iden3-crypto/mimc7" + "math/big" ) @@ -78,17 +76,17 @@ type PublicKey Point func (pk PublicKey) MarshalText() ([]byte, error) { pkc := pk.Compress() - return common3.Hex(pkc[:]).MarshalText() + return Hex(pkc[:]).MarshalText() } func (pk PublicKey) String() string { pkc := pk.Compress() - return common3.Hex(pkc[:]).String() + return Hex(pkc[:]).String() } func (pk *PublicKey) UnmarshalText(h []byte) error { var pkc PublicKeyComp - if err := common3.HexDecodeInto(pkc[:], h); err != nil { + if err := HexDecodeInto(pkc[:], h); err != nil { return err } pkd, err := pkc.Decompress() @@ -108,9 +106,9 @@ func (p *PublicKey) Point() *Point { // point. type PublicKeyComp [32]byte -func (buf PublicKeyComp) MarshalText() ([]byte, error) { return common3.Hex(buf[:]).MarshalText() } -func (buf PublicKeyComp) String() string { return common3.Hex(buf[:]).String() } -func (buf *PublicKeyComp) UnmarshalText(h []byte) error { return common3.HexDecodeInto(buf[:], h) } +func (buf PublicKeyComp) MarshalText() ([]byte, error) { return Hex(buf[:]).MarshalText() } +func (buf PublicKeyComp) String() string { return Hex(buf[:]).String() } +func (buf *PublicKeyComp) UnmarshalText(h []byte) error { return HexDecodeInto(buf[:], h) } func (p *PublicKey) Compress() PublicKeyComp { return PublicKeyComp((*Point)(p).Compress()) @@ -134,9 +132,9 @@ type Signature struct { // SignatureComp represents a compressed EdDSA signature. type SignatureComp [64]byte -func (buf SignatureComp) MarshalText() ([]byte, error) { return common3.Hex(buf[:]).MarshalText() } -func (buf SignatureComp) String() string { return common3.Hex(buf[:]).String() } -func (buf *SignatureComp) UnmarshalText(h []byte) error { return common3.HexDecodeInto(buf[:], h) } +func (buf SignatureComp) MarshalText() ([]byte, error) { return Hex(buf[:]).MarshalText() } +func (buf SignatureComp) String() string { return Hex(buf[:]).String() } +func (buf *SignatureComp) UnmarshalText(h []byte) error { return HexDecodeInto(buf[:], h) } // Compress an EdDSA signature by concatenating the compression of // the point R8 and the Little-Endian encoding of S. diff --git a/babyjub/eddsa_test.go b/babyjub/eddsa_test.go index fbf56dd..c9c4153 100644 --- a/babyjub/eddsa_test.go +++ b/babyjub/eddsa_test.go @@ -7,7 +7,6 @@ import ( "github.com/stretchr/testify/assert" - // "golang.org/x/crypto/blake2b" "math/big" "testing" ) diff --git a/babyjub/helpers.go b/babyjub/helpers.go index c983979..1372a2f 100644 --- a/babyjub/helpers.go +++ b/babyjub/helpers.go @@ -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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bba6f24 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/iden3/go-iden3-crypto + +go 1.12 + +require ( + github.com/dchest/blake512 v1.0.0 + github.com/ethereum/go-ethereum v1.8.27 + github.com/stretchr/testify v1.3.0 + golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..63114f7 --- /dev/null +++ b/go.sum @@ -0,0 +1,18 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dchest/blake512 v1.0.0 h1:oDFEQFIqFSeuA34xLtXZ/rWxCXdSjirjzPhey5EUvmA= +github.com/dchest/blake512 v1.0.0/go.mod h1:FV1x7xPPLWukZlpDpWQ88rF/SFwZ5qbskrzhLMB92JI= +github.com/ethereum/go-ethereum v1.8.27 h1:d+gkiLaBDk5fn3Pe/xNVaMrB/ozI+AUB2IlVBp29IrY= +github.com/ethereum/go-ethereum v1.8.27/go.mod h1:PwpWDrCLZrV+tfrhqqF6kPknbISMHaJv9Ln3kPCZLwY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 h1:ydJNl0ENAG67pFbB+9tfhiL2pYqLhfoaZFw/cjLhY4A= +golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/mimc7/mimc7.go b/mimc7/mimc7.go index a519617..18afdf2 100644 --- a/mimc7/mimc7.go +++ b/mimc7/mimc7.go @@ -6,7 +6,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/crypto" - "github.com/iden3/go-iden3/crypto/field" + "github.com/iden3/go-iden3-crypto/field" ) const SEED = "mimc" diff --git a/mimc7/mimc7_test.go b/mimc7/mimc7_test.go index 14d2e52..f589388 100644 --- a/mimc7/mimc7_test.go +++ b/mimc7/mimc7_test.go @@ -5,7 +5,7 @@ import ( "math/big" "testing" - "github.com/iden3/go-iden3/crypto/field" + "github.com/iden3/go-iden3-crypto/field" "github.com/stretchr/testify/assert" )