Merge pull request #943 from rlan35/rj_fork

Remove bls address util and sha256 hash
pull/948/head
Rongjian Lan 6 years ago committed by GitHub
commit 7a84a503eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      accounts/abi/bind/backend.go
  2. 2
      accounts/abi/bind/backends/simulated.go
  3. 2
      accounts/abi/bind/base.go
  4. 2
      accounts/abi/bind/base_test.go
  5. 5
      consensus/consensus_leader_test.go
  6. 7
      consensus/consensus_service.go
  7. 2
      core/blockchain.go
  8. 26
      crypto/hash/hash.go
  9. 30
      crypto/pki/utils.go
  10. 44
      crypto/pki/utils_test.go
  11. 8
      crypto/sha256.go
  12. 7
      drand/drand.go
  13. 3
      node/node.go
  14. 3
      node/node_handler.go
  15. 27
      test/chain/main.go

@ -21,7 +21,7 @@ import (
"errors" "errors"
"math/big" "math/big"
"github.com/ethereum/go-ethereum" ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
) )

@ -24,7 +24,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/ethereum/go-ethereum" ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"

@ -22,7 +22,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"github.com/ethereum/go-ethereum" ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"

@ -23,7 +23,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/ethereum/go-ethereum" ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"

@ -1,11 +1,12 @@
package consensus package consensus
import ( import (
"crypto/sha256"
"fmt" "fmt"
"testing" "testing"
"time" "time"
"github.com/harmony-one/harmony/crypto/hash"
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
@ -24,7 +25,7 @@ import (
var ( var (
ip = "127.0.0.1" ip = "127.0.0.1"
blockHash = sha256.Sum256([]byte("test")) blockHash = hash.Keccak256Hash([]byte("test"))
) )
func TestProcessMessageLeaderPrepare(test *testing.T) { func TestProcessMessageLeaderPrepare(test *testing.T) {

@ -2,12 +2,13 @@ package consensus
import ( import (
"bytes" "bytes"
"crypto/sha256"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"reflect" "reflect"
"github.com/harmony-one/harmony/crypto/hash"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
protobuf "github.com/golang/protobuf/proto" protobuf "github.com/golang/protobuf/proto"
@ -261,7 +262,7 @@ func (consensus *Consensus) Author(header *types.Header) (common.Address, error)
// Sign on the hash of the message // Sign on the hash of the message
func (consensus *Consensus) signMessage(message []byte) []byte { func (consensus *Consensus) signMessage(message []byte) []byte {
hash := sha256.Sum256(message) hash := hash.Keccak256(message)
signature := consensus.priKey.SignHash(hash[:]) signature := consensus.priKey.SignHash(hash[:])
return signature.Serialize() return signature.Serialize()
} }
@ -477,7 +478,7 @@ func verifyMessageSig(signerPubKey *bls.PublicKey, message *msg_pb.Message) erro
if err != nil { if err != nil {
return err return err
} }
msgHash := sha256.Sum256(messageBytes) msgHash := hash.Keccak256(messageBytes)
if !msgSig.VerifyHash(signerPubKey, msgHash[:]) { if !msgSig.VerifyHash(signerPubKey, msgHash[:]) {
return errors.New("failed to verify the signature") return errors.New("failed to verify the signature")
} }

@ -211,7 +211,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
} }
// ValidateNewBlock validates new block. // ValidateNewBlock validates new block.
func (bc *BlockChain) ValidateNewBlock(block *types.Block, address common.Address) error { func (bc *BlockChain) ValidateNewBlock(block *types.Block) error {
state, err := state.New(bc.CurrentBlock().Root(), bc.stateCache) state, err := state.New(bc.CurrentBlock().Root(), bc.stateCache)
if err != nil { if err != nil {

@ -0,0 +1,26 @@
package hash
import (
"github.com/ethereum/go-ethereum/common"
"golang.org/x/crypto/sha3"
)
// Keccak256 calculates and returns the Keccak256 hash of the input data.
func Keccak256(data ...[]byte) []byte {
d := sha3.NewLegacyKeccak256()
for _, b := range data {
d.Write(b)
}
return d.Sum(nil)
}
// 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) {
d := sha3.NewLegacyKeccak256()
for _, b := range data {
d.Write(b)
}
d.Sum(h[:0])
return h
}

@ -1,7 +1,6 @@
package pki package pki
import ( import (
"crypto/sha256"
"encoding/binary" "encoding/binary"
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
@ -11,35 +10,6 @@ func init() {
bls.Init(bls.BLS12_381) bls.Init(bls.BLS12_381)
} }
// GetAddressFromPublicKey returns address given a public key.
func GetAddressFromPublicKey(pubKey *bls.PublicKey) [20]byte {
bytes := pubKey.Serialize()
address := [20]byte{}
hash := sha256.Sum256(bytes)
copy(address[:], hash[12:])
return address
}
// GetAddressFromPrivateKey returns address given a private key.
func GetAddressFromPrivateKey(priKey *bls.SecretKey) [20]byte {
return GetAddressFromPublicKey(priKey.GetPublicKey())
}
// GetAddressFromPrivateKeyBytes returns address from private key in bytes.
func GetAddressFromPrivateKeyBytes(priKey [32]byte) [20]byte {
var privateKey bls.SecretKey
privateKey.SetLittleEndian(priKey[:])
return GetAddressFromPublicKey(privateKey.GetPublicKey())
}
// GetAddressFromInt is the temporary helper function for benchmark use
func GetAddressFromInt(value int) [20]byte {
priKey := [32]byte{}
binary.LittleEndian.PutUint32(priKey[:], uint32(value))
return GetAddressFromPrivateKeyBytes(priKey)
}
// GetBLSPrivateKeyFromInt returns bls private key // GetBLSPrivateKeyFromInt returns bls private key
func GetBLSPrivateKeyFromInt(value int) *bls.SecretKey { func GetBLSPrivateKeyFromInt(value int) *bls.SecretKey {
priKey := [32]byte{} priKey := [32]byte{}

@ -2,52 +2,12 @@ package pki
import ( import (
"encoding/binary" "encoding/binary"
"reflect"
"testing" "testing"
"time" "time"
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
) )
func TestGetAddressFromPublicKey(test *testing.T) {
t := time.Now().UnixNano()
priKey := [32]byte{}
binary.LittleEndian.PutUint32(priKey[:], uint32(t))
var privateKey bls.SecretKey
privateKey.SetLittleEndian(priKey[:])
addr1 := GetAddressFromPublicKey(privateKey.GetPublicKey())
addr2 := GetAddressFromPrivateKey(&privateKey)
if !reflect.DeepEqual(addr1, addr2) {
test.Error("two public address should be equal")
}
}
func TestGetAddressFromPrivateKeyBytes(test *testing.T) {
t := time.Now().UnixNano()
priKey := [32]byte{}
binary.LittleEndian.PutUint32(priKey[:], uint32(t))
address1 := GetAddressFromPrivateKeyBytes(priKey)
var privateKey bls.SecretKey
privateKey.SetLittleEndian(priKey[:])
address2 := GetAddressFromPublicKey(privateKey.GetPublicKey())
if !reflect.DeepEqual(address1, address2) {
test.Error("two public address should be equal")
}
}
func TestGetAddressFromInt(test *testing.T) {
t := time.Now().UnixNano()
address1 := GetAddressFromInt(int(t))
priKey := [32]byte{}
binary.LittleEndian.PutUint32(priKey[:], uint32(t))
var privateKey bls.SecretKey
privateKey.SetLittleEndian(priKey[:])
address2 := GetAddressFromPublicKey(privateKey.GetPublicKey())
if !reflect.DeepEqual(address1, address2) {
test.Error("two public address should be equal")
}
}
func TestGetBLSPrivateKeyFromInt(test *testing.T) { func TestGetBLSPrivateKeyFromInt(test *testing.T) {
t := time.Now().UnixNano() t := time.Now().UnixNano()
privateKey1 := GetBLSPrivateKeyFromInt(int(t)) privateKey1 := GetBLSPrivateKeyFromInt(int(t))
@ -55,9 +15,7 @@ func TestGetBLSPrivateKeyFromInt(test *testing.T) {
binary.LittleEndian.PutUint32(priKey[:], uint32(t)) binary.LittleEndian.PutUint32(priKey[:], uint32(t))
var privateKey2 bls.SecretKey var privateKey2 bls.SecretKey
privateKey2.SetLittleEndian(priKey[:]) privateKey2.SetLittleEndian(priKey[:])
address1 := GetAddressFromPrivateKey(privateKey1) if !privateKey1.IsEqual(&privateKey2) {
address2 := GetAddressFromPrivateKey(&privateKey2)
if !reflect.DeepEqual(address1, address2) {
test.Error("two public address should be equal") test.Error("two public address should be equal")
} }
} }

@ -1,8 +0,0 @@
package crypto
import "crypto/sha256"
// HashSha256 returns result of Sha256.
func HashSha256(message string) [32]byte {
return sha256.Sum256([]byte(message))
}

@ -1,10 +1,11 @@
package drand package drand
import ( import (
"crypto/sha256"
"errors" "errors"
"sync" "sync"
"github.com/harmony-one/harmony/crypto/hash"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
protobuf "github.com/golang/protobuf/proto" protobuf "github.com/golang/protobuf/proto"
@ -158,7 +159,7 @@ func (dRand *DRand) signDRandMessage(message *msg_pb.Message) error {
return err return err
} }
// 64 byte of signature on previous data // 64 byte of signature on previous data
hash := sha256.Sum256(marshaledMessage) hash := hash.Keccak256(marshaledMessage)
signature := dRand.priKey.SignHash(hash[:]) signature := dRand.priKey.SignHash(hash[:])
message.Signature = signature.Serialize() message.Signature = signature.Serialize()
@ -213,7 +214,7 @@ func verifyMessageSig(signerPubKey *bls.PublicKey, message *msg_pb.Message) erro
if err != nil { if err != nil {
return err return err
} }
msgHash := sha256.Sum256(messageBytes) msgHash := hash.Keccak256(messageBytes)
if !msgSig.VerifyHash(signerPubKey, msgHash[:]) { if !msgSig.VerifyHash(signerPubKey, msgHash[:]) {
return errors.New("failed to verify the signature") return errors.New("failed to verify the signature")
} }

@ -25,7 +25,6 @@ import (
"github.com/harmony-one/harmony/contracts/structs" "github.com/harmony-one/harmony/contracts/structs"
"github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/crypto/pki"
"github.com/harmony-one/harmony/drand" "github.com/harmony-one/harmony/drand"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node" nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/ctxerror" "github.com/harmony-one/harmony/internal/ctxerror"
@ -312,7 +311,7 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, chainDBFactory shardc
node.ConfirmedBlockChannel = make(chan *types.Block) node.ConfirmedBlockChannel = make(chan *types.Block)
node.BeaconBlockChannel = make(chan *types.Block) node.BeaconBlockChannel = make(chan *types.Block)
node.TxPool = core.NewTxPool(core.DefaultTxPoolConfig, params.TestChainConfig, chain) node.TxPool = core.NewTxPool(core.DefaultTxPoolConfig, params.TestChainConfig, chain)
node.Worker = worker.New(params.TestChainConfig, chain, node.Consensus, pki.GetAddressFromPublicKey(node.SelfPeer.ConsensusPubKey), node.Consensus.ShardID) node.Worker = worker.New(params.TestChainConfig, chain, node.Consensus, node.Consensus.SelfAddress, node.Consensus.ShardID)
node.Consensus.VerifiedNewBlock = make(chan *types.Block) node.Consensus.VerifiedNewBlock = make(chan *types.Block)
// the sequence number is the next block number to be added in consensus protocol, which is always one more than current chain header block // the sequence number is the next block number to be added in consensus protocol, which is always one more than current chain header block

@ -29,7 +29,6 @@ import (
"github.com/harmony-one/harmony/contracts/structs" "github.com/harmony-one/harmony/contracts/structs"
"github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/crypto/pki"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node" nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/ctxerror" "github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
@ -254,7 +253,7 @@ func (node *Node) BroadcastNewBlock(newBlock *types.Block) {
func (node *Node) VerifyNewBlock(newBlock *types.Block) error { func (node *Node) VerifyNewBlock(newBlock *types.Block) error {
// TODO ek – where do we verify parent-child invariants, // TODO ek – where do we verify parent-child invariants,
// e.g. "child.Number == child.IsGenesis() ? 0 : parent.Number+1"? // e.g. "child.Number == child.IsGenesis() ? 0 : parent.Number+1"?
err := node.Blockchain().ValidateNewBlock(newBlock, pki.GetAddressFromPublicKey(node.SelfPeer.ConsensusPubKey)) err := node.Blockchain().ValidateNewBlock(newBlock)
if err != nil { if err != nil {
return ctxerror.New("cannot ValidateNewBlock", return ctxerror.New("cannot ValidateNewBlock",
"blockHash", newBlock.Hash(), "blockHash", newBlock.Hash(),

@ -6,10 +6,11 @@ import (
"log" "log"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
//"github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/crypto/hash"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/consensus"
@ -18,7 +19,6 @@ import (
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm" "github.com/harmony-one/harmony/core/vm"
pkgworker "github.com/harmony-one/harmony/node/worker" pkgworker "github.com/harmony-one/harmony/node/worker"
"golang.org/x/crypto/sha3"
) )
const ( const (
@ -154,10 +154,9 @@ func callFaucetContractToFundAnAddress(chain *core.BlockChain) {
paddedAddress := common.LeftPadBytes(allRandomUserAddress[0].Bytes(), 32) paddedAddress := common.LeftPadBytes(allRandomUserAddress[0].Bytes(), 32)
transferFnSignature := []byte("request(address)") transferFnSignature := []byte("request(address)")
//hash := sha3.NewKeccak256()
hash := sha3.NewLegacyKeccak256() fnHash := hash.Keccak256(transferFnSignature)
hash.Write(transferFnSignature) callFuncHex := fnHash[:4]
callFuncHex := hash.Sum(nil)[:4]
var callEnc []byte var callEnc []byte
callEnc = append(callEnc, callFuncHex...) callEnc = append(callEnc, callFuncHex...)
@ -225,10 +224,9 @@ func playSetupStakingContract(chain *core.BlockChain) {
func playStaking(chain *core.BlockChain) { func playStaking(chain *core.BlockChain) {
depositFnSignature := []byte("deposit()") depositFnSignature := []byte("deposit()")
//hash = sha3.NewKeccak256()
hash := sha3.NewLegacyKeccak256() fnHash := hash.Keccak256(depositFnSignature)
hash.Write(depositFnSignature) methodID := fnHash[:4]
methodID := hash.Sum(nil)[:4]
var callEncl []byte var callEncl []byte
callEncl = append(callEncl, methodID...) callEncl = append(callEncl, methodID...)
@ -276,10 +274,9 @@ func playWithdrawStaking(chain *core.BlockChain) {
fmt.Println("--------- Now Setting up Withdrawing Stakes ---------") fmt.Println("--------- Now Setting up Withdrawing Stakes ---------")
withdrawFnSignature := []byte("withdraw(uint256)") withdrawFnSignature := []byte("withdraw(uint256)")
//hash = sha3.NewKeccak256()
hash := sha3.NewLegacyKeccak256() fnHash := hash.Keccak256(withdrawFnSignature)
hash.Write(withdrawFnSignature) methodID := fnHash[:4]
methodID := hash.Sum(nil)[:4]
withdraw := "5000" withdraw := "5000"
withdrawstake := new(big.Int) withdrawstake := new(big.Int)

Loading…
Cancel
Save