Remove bls address util and sha256 hash

pull/943/head
Rongjian Lan 6 years ago
parent 3235fbae2a
commit 16279258d0
  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. 2
      core/blockchain.go
  6. 26
      crypto/hash/hash.go
  7. 30
      crypto/pki/utils.go
  8. 44
      crypto/pki/utils_test.go
  9. 8
      crypto/sha256.go
  10. 3
      node/node.go
  11. 3
      node/node_handler.go
  12. 27
      test/chain/main.go

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

@ -24,7 +24,7 @@ import (
"sync"
"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/common"
"github.com/ethereum/go-ethereum/common/math"

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

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

@ -211,7 +211,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
}
// 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)
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
import (
"crypto/sha256"
"encoding/binary"
"github.com/harmony-one/bls/ffi/go/bls"
@ -11,35 +10,6 @@ func init() {
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
func GetBLSPrivateKeyFromInt(value int) *bls.SecretKey {
priKey := [32]byte{}

@ -2,52 +2,12 @@ package pki
import (
"encoding/binary"
"reflect"
"testing"
"time"
"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) {
t := time.Now().UnixNano()
privateKey1 := GetBLSPrivateKeyFromInt(int(t))
@ -55,9 +15,7 @@ func TestGetBLSPrivateKeyFromInt(test *testing.T) {
binary.LittleEndian.PutUint32(priKey[:], uint32(t))
var privateKey2 bls.SecretKey
privateKey2.SetLittleEndian(priKey[:])
address1 := GetAddressFromPrivateKey(privateKey1)
address2 := GetAddressFromPrivateKey(&privateKey2)
if !reflect.DeepEqual(address1, address2) {
if !privateKey1.IsEqual(&privateKey2) {
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))
}

@ -25,7 +25,6 @@ import (
"github.com/harmony-one/harmony/contracts/structs"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/crypto/pki"
"github.com/harmony-one/harmony/drand"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"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.BeaconBlockChannel = make(chan *types.Block)
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)
// 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/core"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/crypto/pki"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/ctxerror"
"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 {
// TODO ek – where do we verify parent-child invariants,
// 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 {
return ctxerror.New("cannot ValidateNewBlock",
"blockHash", newBlock.Hash(),

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

Loading…
Cancel
Save