aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-08-08 11:28:57 +0800
committerGitHub <noreply@github.com>2018-08-08 11:28:57 +0800
commita418ea95c0f5afb50cbb78aedecc68373353d06e (patch)
tree79bee8ef152f58c0b3bbcbe38d4dd537050b72aa /crypto
parent3a929b656b6bd5846849fd98dc29ff761db97ed3 (diff)
downloaddexon-consensus-a418ea95c0f5afb50cbb78aedecc68373353d06e.tar
dexon-consensus-a418ea95c0f5afb50cbb78aedecc68373353d06e.tar.gz
dexon-consensus-a418ea95c0f5afb50cbb78aedecc68373353d06e.tar.bz2
dexon-consensus-a418ea95c0f5afb50cbb78aedecc68373353d06e.tar.lz
dexon-consensus-a418ea95c0f5afb50cbb78aedecc68373353d06e.tar.xz
dexon-consensus-a418ea95c0f5afb50cbb78aedecc68373353d06e.tar.zst
dexon-consensus-a418ea95c0f5afb50cbb78aedecc68373353d06e.zip
crypto: Add crypto module. (#34)
Diffstat (limited to 'crypto')
-rw-r--r--crypto/eth/eth.go99
-rw-r--r--crypto/eth/eth_test.go79
-rw-r--r--crypto/interfaces.go42
-rw-r--r--crypto/utils.go30
-rw-r--r--crypto/utils_test.go52
5 files changed, 302 insertions, 0 deletions
diff --git a/crypto/eth/eth.go b/crypto/eth/eth.go
new file mode 100644
index 0000000..cb20d7d
--- /dev/null
+++ b/crypto/eth/eth.go
@@ -0,0 +1,99 @@
+// Copyright 2018 The dexon-consensus-core Authors
+// This file is part of the dexon-consensus-core library.
+//
+// The dexon-consensus-core library is free software: you can redistribute it
+// and/or modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus-core library is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+// General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus-core library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+package eth
+
+import (
+ "crypto/ecdsa"
+
+ ethcrypto "github.com/ethereum/go-ethereum/crypto"
+
+ "github.com/dexon-foundation/dexon-consensus-core/common"
+ "github.com/dexon-foundation/dexon-consensus-core/crypto"
+)
+
+// PrivateKey represents a private key structure used in geth and implments
+// Crypto.PrivateKey interface.
+type PrivateKey struct {
+ privateKey ecdsa.PrivateKey
+ publicKey PublicKey
+}
+
+// PublicKey represents a public key structure used in geth and implements
+// Crypto.PublicKey interface.
+type PublicKey struct {
+ publicKey []byte
+}
+
+// NewPrivateKey creates a new PrivateKey structure.
+func NewPrivateKey() (*PrivateKey, error) {
+ key, err := ethcrypto.GenerateKey()
+ if err != nil {
+ return nil, err
+ }
+ return &PrivateKey{
+ privateKey: *key,
+ publicKey: *newPublicKey(key),
+ }, nil
+}
+
+// newPublicKey creates a new PublicKey structure.
+func newPublicKey(prvKey *ecdsa.PrivateKey) *PublicKey {
+ return &PublicKey{
+ publicKey: ethcrypto.CompressPubkey(&prvKey.PublicKey),
+ }
+}
+
+// DecompressPubkey parses a public key in the 33-byte compressed format.
+func DecompressPubkey(pubkey []byte) (PublicKey, error) {
+ _, err := ethcrypto.DecompressPubkey(pubkey)
+ return PublicKey{
+ publicKey: pubkey,
+ }, err
+}
+
+// PublicKey returns the public key associate this private key.
+func (prv *PrivateKey) PublicKey() crypto.PublicKey {
+ return prv.publicKey
+}
+
+// Sign calculates an ECDSA signature.
+//
+// This function is susceptible to chosen plaintext attacks that can leak
+// information about the private key that is used for signing. Callers must
+// be aware that the given hash cannot be chosen by an adversery. Common
+// solution is to hash any input before calculating the signature.
+//
+// The produced signature is in the [R || S || V] format where V is 0 or 1.
+func (prv *PrivateKey) Sign(hash common.Hash) (sig crypto.Signature, err error) {
+ s, err := ethcrypto.Sign(hash[:], &prv.privateKey)
+ // Magic!
+ sig = crypto.Signature(s[:64])
+ return
+}
+
+// VerifySignature checks that the given public key created signature over hash.
+// The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format.
+// The signature should have the 64 byte [R || S] format.
+func (pub PublicKey) VerifySignature(hash common.Hash, signature crypto.Signature) bool {
+ return ethcrypto.VerifySignature(pub.publicKey, hash[:], signature)
+}
+
+// Compress encodes a public key to the 33-byte compressed format.
+func (pub PublicKey) Compress() []byte {
+ return pub.publicKey
+}
diff --git a/crypto/eth/eth_test.go b/crypto/eth/eth_test.go
new file mode 100644
index 0000000..b09297e
--- /dev/null
+++ b/crypto/eth/eth_test.go
@@ -0,0 +1,79 @@
+// Copyright 2018 The dexon-consensus-core Authors
+// This file is part of the dexon-consensus-core library.
+//
+// The dexon-consensus-core library is free software: you can redistribute it
+// and/or modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus-core library is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+// General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus-core library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+package eth
+
+import (
+ "testing"
+
+ "github.com/dexon-foundation/dexon-consensus-core/common"
+ "github.com/stretchr/testify/suite"
+)
+
+type ETHCryptoTestSuite struct {
+ suite.Suite
+}
+
+func (s *ETHCryptoTestSuite) TestSignature() {
+ prv1, err := NewPrivateKey()
+ s.Require().Nil(err)
+ hash1 := common.NewRandomHash()
+ hash2 := common.NewRandomHash()
+
+ // Test that same private key should produce same signature.
+ sig11, err := prv1.Sign(hash1)
+ s.Require().Nil(err)
+ sig112, err := prv1.Sign(hash1)
+ s.Require().Nil(err)
+ s.Equal(sig11, sig112)
+
+ // Test that different private key should produce different signature.
+ prv2, err := NewPrivateKey()
+ s.Require().Nil(err)
+ sig21, err := prv2.Sign(hash1)
+ s.Require().Nil(err)
+ s.NotEqual(sig11, sig21)
+
+ // Test that different hash should produce different signature.
+ sig12, err := prv1.Sign(hash2)
+ s.Require().Nil(err)
+ s.NotEqual(sig11, sig12)
+
+ // Test VerifySignature with correct public key.
+ pub1, ok := prv1.PublicKey().(PublicKey)
+ s.Require().True(ok)
+ s.True(pub1.VerifySignature(hash1, sig11))
+
+ // Test VerifySignature with wrong hash.
+ s.False(pub1.VerifySignature(hash2, sig11))
+ // Test VerifySignature with wrong signature.
+ s.False(pub1.VerifySignature(hash1, sig21))
+ // Test VerifySignature with wrong public key.
+ pub2 := prv2.PublicKey()
+ s.False(pub2.VerifySignature(hash1, sig11))
+
+ // Test compress and decompress of public key.
+ compressPub1 := pub1.Compress()
+ decompressPub1, err := DecompressPubkey(compressPub1)
+ s.Require().Nil(err)
+ s.Equal(pub1, decompressPub1)
+ s.True(decompressPub1.VerifySignature(hash1, sig11))
+}
+
+func TestCrypto(t *testing.T) {
+ suite.Run(t, new(ETHCryptoTestSuite))
+}
diff --git a/crypto/interfaces.go b/crypto/interfaces.go
new file mode 100644
index 0000000..015ff40
--- /dev/null
+++ b/crypto/interfaces.go
@@ -0,0 +1,42 @@
+// Copyright 2018 The dexon-consensus-core Authors
+// This file is part of the dexon-consensus-core library.
+//
+// The dexon-consensus-core library is free software: you can redistribute it
+// and/or modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus-core library is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+// General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus-core library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+package crypto
+
+import (
+ "github.com/dexon-foundation/dexon-consensus-core/common"
+)
+
+// Signature is the basic signature type in DEXON.
+type Signature []byte
+
+// PrivateKey describes the asymmetric cryptography interface that interacts
+// with the private key.
+type PrivateKey interface {
+ // PublicKey returns the public key associate this private key.
+ PublicKey() PublicKey
+
+ // Sign calculates a signature.
+ Sign(hash common.Hash) (sig Signature, err error)
+}
+
+// PublicKey describes the asymmetric cryptography interface that interacts
+// with the public key.
+type PublicKey interface {
+ // VerifySignature checks that the given public key created signature over hash.
+ VerifySignature(hash common.Hash, signature Signature) bool
+}
diff --git a/crypto/utils.go b/crypto/utils.go
new file mode 100644
index 0000000..5534ece
--- /dev/null
+++ b/crypto/utils.go
@@ -0,0 +1,30 @@
+// Copyright 2018 The dexon-consensus-core Authors
+// This file is part of the dexon-consensus-core library.
+//
+// The dexon-consensus-core library is free software: you can redistribute it
+// and/or modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus-core library is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+// General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus-core library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+package crypto
+
+import (
+ "github.com/ethereum/go-ethereum/crypto"
+
+ "github.com/dexon-foundation/dexon-consensus-core/common"
+)
+
+// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
+// converting it to an internal Hash data structure.
+func Keccak256Hash(data ...[]byte) (h common.Hash) {
+ return common.Hash(crypto.Keccak256Hash(data...))
+}
diff --git a/crypto/utils_test.go b/crypto/utils_test.go
new file mode 100644
index 0000000..977027a
--- /dev/null
+++ b/crypto/utils_test.go
@@ -0,0 +1,52 @@
+// Copyright 2018 The dexon-consensus-core Authors
+// This file is part of the dexon-consensus-core library.
+//
+// The dexon-consensus-core library is free software: you can redistribute it
+// and/or modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus-core library is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+// General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus-core library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+package crypto
+
+import (
+ "encoding/hex"
+ "testing"
+
+ "github.com/stretchr/testify/suite"
+)
+
+type CryptoTestSuite struct {
+ suite.Suite
+}
+
+func (s *CryptoTestSuite) TestHash() {
+ cases := []struct {
+ input string
+ output string
+ }{
+ {"DEXON ROCKS!",
+ "1a3f3a424aaa464e51b693585bba3a0c439d5f1ad3b5868e46d9f830225983bd"},
+ {"Dexon Foundation",
+ "25ed4237aa978bfe706cc11c7a46a95de1a46302faea7ff6e900b03fa2b7b480"},
+ {"INFINITELY SCALABLE AND LOW-LATENCY",
+ "ed3384c58a434fbc0bc887a85659eddf997e7da978ab66565ac865f995b77cf1"},
+ }
+ for _, testcase := range cases {
+ hash := Keccak256Hash([]byte(testcase.input))
+ output := hex.EncodeToString(hash[:])
+ s.Equal(testcase.output, output)
+ }
+}
+
+func TestCrypto(t *testing.T) {
+ suite.Run(t, new(CryptoTestSuite))
+}