Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crypto/ecies/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"hash"

ethcrypto "github.com/PlatONnetwork/PlatON-Go/crypto"
"github.com/PlatONnetwork/PlatON-Go/crypto/sm"
)

var (
Expand Down Expand Up @@ -72,6 +73,14 @@ type ECIESParams struct {
// * ECIES using AES256 and HMAC-SHA-512-64

var (
ECIES_SM2_SM3 = &ECIESParams{
Hash: sm.NewSM3,
hashAlgo: crypto.SHA256,
Cipher: aes.NewCipher,
BlockSize: aes.BlockSize,
KeyLen: 16,
}

ECIES_AES128_SHA256 = &ECIESParams{
Hash: sha256.New,
hashAlgo: crypto.SHA256,
Expand Down Expand Up @@ -114,6 +123,7 @@ var (
)

var paramsFromCurve = map[elliptic.Curve]*ECIESParams{
sm.S256(): ECIES_SM2_SM3,
ethcrypto.S256(): ECIES_AES128_SHA256,
elliptic.P256(): ECIES_AES128_SHA256,
elliptic.P384(): ECIES_AES192_SHA384,
Expand Down
2 changes: 1 addition & 1 deletion crypto/sm/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (BitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool {
return x3.Cmp(y2) == 0
}

//TODO: double check if the function is okay
// TODO: double check if the function is okay
// affineFromJacobian reverses the Jacobian transform. See the comment at the
// top of the file.
func (BitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
Expand Down
2 changes: 2 additions & 0 deletions crypto/vrf/vrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package vrf

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"io"
"testing"
)

func TestVrf(t *testing.T) {
curve := elliptic.P256()
for i := 0; i < 10; i++ {
sk, err := ecdsa.GenerateKey(curve, rand.Reader)
if nil != err {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ require (
)

require (
github.com/cespare/xxhash v1.1.0
github.com/golang/mock v1.6.0
github.com/herumi/bls-eth-go-binary v1.32.1
github.com/supranational/blst v0.3.11
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PlatONnetwork/wagon v0.6.1-0.20201026015350-67507c2a7b96 h1:BA5xEQQrv82VdaxUoAZeGi/G/UQ3z6z+eNe2rijhhpg=
github.com/PlatONnetwork/wagon v0.6.1-0.20201026015350-67507c2a7b96/go.mod h1:zPWloKR2Ep7uqrhyLyE483NCxlAlQnbPsQUJXWN6bVM=
Expand Down Expand Up @@ -251,6 +252,7 @@ github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 h1:Gb2Tyox57NRNuZ2d3rmvB3pcmbu7O1RS3m8WRx7ilrg=
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q=
Expand Down
10 changes: 8 additions & 2 deletions miner/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package miner

import (
"errors"
"github.com/PlatONnetwork/PlatON-Go/sdk"
"github.com/PlatONnetwork/PlatON-Go/sdk/simapp"
"math/big"
"math/rand"
"testing"
"time"

"github.com/PlatONnetwork/PlatON-Go/sdk"
"github.com/PlatONnetwork/PlatON-Go/sdk/simapp"

"github.com/PlatONnetwork/PlatON-Go/core/rawdb"

"github.com/PlatONnetwork/PlatON-Go/core/cbfttypes"
Expand Down Expand Up @@ -547,6 +548,11 @@ func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine co
type EmptyBackend struct {
}

func (e EmptyBackend) ChainConfig() *params.ChainConfig {
//TODO implement me
panic("implement me")
}

func (e EmptyBackend) ChainId() (*big.Int, error) {
//TODO implement me
panic("implement me")
Expand Down
8 changes: 7 additions & 1 deletion p2p/discover/v5wire/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"golang.org/x/crypto/hkdf"

"github.com/PlatONnetwork/PlatON-Go/common"
"github.com/PlatONnetwork/PlatON-Go/common/math"
"github.com/PlatONnetwork/PlatON-Go/crypto"
"github.com/PlatONnetwork/PlatON-Go/p2p/enode"
Expand Down Expand Up @@ -92,7 +93,12 @@ func makeIDSignature(hash hash.Hash, key *ecdsa.PrivateKey, challenge, ephkey []
// s256raw is an unparsed secp256k1 public key ENR entry.
type s256raw []byte

func (s256raw) ENRKey() string { return "secp256k1" }
func (s256raw) ENRKey() string {
if common.SignAlgo == common.SMSignAlgo {
return "sm2"
}
return "secp256k1"
}

// verifyIDSignature checks that signature over idnonce was made by the given node.
func verifyIDSignature(hash hash.Hash, sig []byte, n *enode.Node, challenge, ephkey []byte, destID enode.ID) error {
Expand Down
15 changes: 13 additions & 2 deletions p2p/enode/idscheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"

"github.com/PlatONnetwork/PlatON-Go/common"
"github.com/PlatONnetwork/PlatON-Go/common/math"
"github.com/PlatONnetwork/PlatON-Go/crypto"
"github.com/PlatONnetwork/PlatON-Go/p2p/enr"
Expand Down Expand Up @@ -100,7 +101,12 @@ func (V4ID) NodeAddr(r *enr.Record) []byte {
// Secp256k1 is the "secp256k1" key, which holds a public key.
type Secp256k1 ecdsa.PublicKey

func (v Secp256k1) ENRKey() string { return "secp256k1" }
func (v Secp256k1) ENRKey() string {
if common.GetSignAlgo() == common.SMSignAlgo {
return "sm2"
}
return "secp256k1"
}

// EncodeRLP implements rlp.Encoder.
func (v Secp256k1) EncodeRLP(w io.Writer) error {
Expand All @@ -124,7 +130,12 @@ func (v *Secp256k1) DecodeRLP(s *rlp.Stream) error {
// s256raw is an unparsed secp256k1 public key entry.
type s256raw []byte

func (s256raw) ENRKey() string { return "secp256k1" }
func (s256raw) ENRKey() string {
if common.GetSignAlgo() == common.SMSignAlgo {
return "sm2"
}
return "secp256k1"
}

// v4CompatID is a weaker and insecure version of the "v4" scheme which only checks for the
// presence of a secp256k1 public key, but doesn't verify the signature.
Expand Down
57 changes: 57 additions & 0 deletions p2p/enode/idscheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/PlatONnetwork/PlatON-Go/common"
"github.com/PlatONnetwork/PlatON-Go/crypto"
"github.com/PlatONnetwork/PlatON-Go/p2p/enr"
"github.com/PlatONnetwork/PlatON-Go/rlp"
Expand All @@ -34,9 +35,23 @@ import (
var (
privkey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
pubkey = &privkey.PublicKey
//smPrivkey, _ = crypto.HexToECDSA("b517045a9e4cf82daf5d6c21d9d32d12e11b1c850b2d0448670d5aee8a3bcbcc")
smPrivkey = func() func() *ecdsa.PrivateKey {
return func() *ecdsa.PrivateKey {
p, _ := crypto.HexToECDSA("b517045a9e4cf82daf5d6c21d9d32d12e11b1c850b2d0448670d5aee8a3bcbcc")
return p
}
}()
//smPubkey = &smPrivkey().PublicKey
smPubkey = func() func() *ecdsa.PublicKey {
return func() *ecdsa.PublicKey {
return &smPrivkey().PublicKey
}
}()
)

func TestEmptyNodeID(t *testing.T) {
common.SetSignAlgo(common.SMSignAlgo)
var r enr.Record
if addr := ValidSchemes.NodeAddr(&r); addr != nil {
t.Errorf("wrong address on empty record: got %v, want %v", addr, nil)
Expand Down Expand Up @@ -73,3 +88,45 @@ func TestGetSetSecp256k1(t *testing.T) {
require.NoError(t, r.Load(&pk))
assert.EqualValues(t, pubkey, &pk)
}

// for sm test
func TestEmptyNodeIDForSm(t *testing.T) {
common.SetSignAlgo(common.SMSignAlgo)
var r enr.Record
if addr := ValidSchemes.NodeAddr(&r); addr != nil {
t.Errorf("wrong address on empty record: got %v, want %v", addr, nil)
}

require.NoError(t, SignV4(&r, smPrivkey()))
expected := "a1e1c33bebe596bda273fd5eb1b1b932a8640ac8524bbd3d9f0863b106b8667d"
assert.Equal(t, expected, hex.EncodeToString(ValidSchemes.NodeAddr(&r)))
}

// Checks that failure to sign leaves the record unmodified.
func TestSignErrorForSm(t *testing.T) {
common.SetSignAlgo(common.SMSignAlgo)
invalidKey := &ecdsa.PrivateKey{D: new(big.Int), PublicKey: *(smPubkey())}

var r enr.Record
emptyEnc, _ := rlp.EncodeToBytes(&r)
if err := SignV4(&r, invalidKey); err == nil {
t.Fatal("expected error from SignV4")
}
newEnc, _ := rlp.EncodeToBytes(&r)
if !bytes.Equal(newEnc, emptyEnc) {
t.Fatal("record modified even though signing failed")
}
}

// TestGetSetSecp256k1 tests encoding/decoding and setting/getting of the Secp256k1 key.
func TestGetSetSecp256k1ForSm(t *testing.T) {
common.SetSignAlgo(common.SMSignAlgo)
var r enr.Record
if err := SignV4(&r, smPrivkey()); err != nil {
t.Fatal(err)
}

var pk Secp256k1
require.NoError(t, r.Load(&pk))
assert.EqualValues(t, smPubkey(), &pk)
}
7 changes: 6 additions & 1 deletion sdk/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ type SimApp struct {
validators *cbfttypes.Validators
}

func (app *SimApp) TxPoolParam(ctx sdk.Context, param sdk.TxPoolParam) uint64 {
func (app *SimApp) SystemParam(ctx sdk.SystemParamContext, param sdk.SystemParam) uint64 {
//TODO implement me
panic("implement me")
}

func (app *SimApp) TxPoolParam(ctx sdk.Context, param sdk.TxPoolApp) uint64 {
//TODO implement me
return 0
}
Expand Down
Loading