mirror of
https://github.com/arnaucube/go-iden3-crypto.git
synced 2026-02-07 11:36:41 +01:00
add travis, add go.mod go.sum, update babyjub hex encoders to avoid
importing go-iden3
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
// "golang.org/x/crypto/blake2b"
|
||||
"math/big"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user