Skip to content

Commit

Permalink
Remove kriptology dependency and update IBFT signer package (1st part)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Oct 13, 2023
1 parent b203665 commit 3910247
Show file tree
Hide file tree
Showing 26 changed files with 166 additions and 334 deletions.
3 changes: 2 additions & 1 deletion bls/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"errors"
"math/big"

"github.com/0xPolygon/polygon-edge/helper/common"
bn256 "github.com/umbracle/go-eth-bn256"

"github.com/0xPolygon/polygon-edge/helper/common"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion bls/private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestPrivate_Marshal(t *testing.T) {
t.Parallel()

blsKey, err := GenerateBlsKey() // structure which holds private/public key pair
blsKey, err := GeneratePrivateKey() // structure which holds private/public key pair
require.NoError(t, err)

// marshal public key
Expand Down
3 changes: 2 additions & 1 deletion bls/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"fmt"
"math/big"

"github.com/0xPolygon/polygon-edge/helper/common"
bn256 "github.com/umbracle/go-eth-bn256"

"github.com/0xPolygon/polygon-edge/helper/common"
)

var errInfinityPoint = errors.New("infinity point")
Expand Down
6 changes: 3 additions & 3 deletions bls/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestPublic_Marshal(t *testing.T) {
t.Parallel()

blsKey, err := GenerateBlsKey() // structure which holds private/public key pair
blsKey, err := GeneratePrivateKey() // structure which holds private/public key pair
require.NoError(t, err)

pubKey := blsKey.PublicKey() // structure which holds public key
Expand All @@ -29,7 +29,7 @@ func TestPublic_Marshal(t *testing.T) {
func TestPublic_UnmarshalPublicKeyFromBigInt(t *testing.T) {
t.Parallel()

key, _ := GenerateBlsKey()
key, _ := GeneratePrivateKey()
pub := key.PublicKey()

pub2, err := UnmarshalPublicKeyFromBigInt(pub.ToBigInt())
Expand All @@ -41,7 +41,7 @@ func TestPublic_UnmarshalPublicKeyFromBigInt(t *testing.T) {
func TestPublic_MarshalUnmarshalText(t *testing.T) {
t.Parallel()

key, err := GenerateBlsKey()
key, err := GeneratePrivateKey()
require.NoError(t, err)

pubKey := key.PublicKey()
Expand Down
14 changes: 7 additions & 7 deletions bls/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_VerifySignature(t *testing.T) {

validTestMsg, invalidTestMsg := testGenRandomBytes(t, messageSize), testGenRandomBytes(t, messageSize)

blsKey, _ := GenerateBlsKey()
blsKey, _ := GeneratePrivateKey()
signature, err := blsKey.Sign(validTestMsg, expectedDomain)
require.NoError(t, err)

Expand All @@ -45,7 +45,7 @@ func Test_VerifySignature_NegativeCases(t *testing.T) {

validTestMsg := testGenRandomBytes(t, messageSize)

blsKey, err := GenerateBlsKey()
blsKey, err := GeneratePrivateKey()
require.NoError(t, err)

signature, err := blsKey.Sign(validTestMsg, expectedDomain)
Expand Down Expand Up @@ -121,9 +121,9 @@ func Test_AggregatedSignatureSimple(t *testing.T) {

validTestMsg, invalidTestMsg := testGenRandomBytes(t, messageSize), testGenRandomBytes(t, messageSize)

bls1, _ := GenerateBlsKey()
bls2, _ := GenerateBlsKey()
bls3, _ := GenerateBlsKey()
bls1, _ := GeneratePrivateKey()
bls2, _ := GeneratePrivateKey()
bls3, _ := GeneratePrivateKey()

sig1, err := bls1.Sign(validTestMsg, expectedDomain)
require.NoError(t, err)
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestSignature_BigInt(t *testing.T) {

validTestMsg := testGenRandomBytes(t, messageSize)

bls1, err := GenerateBlsKey()
bls1, err := GeneratePrivateKey()
require.NoError(t, err)

sig1, err := bls1.Sign(validTestMsg, unexpectedDomain)
Expand All @@ -196,7 +196,7 @@ func TestSignature_Unmarshal(t *testing.T) {

validTestMsg := testGenRandomBytes(t, messageSize)

bls1, err := GenerateBlsKey()
bls1, err := GeneratePrivateKey()
require.NoError(t, err)

sig, err := bls1.Sign(validTestMsg, unexpectedDomain)
Expand Down
22 changes: 19 additions & 3 deletions bls/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var (
g2Point = mustG2Point("198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c21800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa") //nolint
)

// GenerateBlsKey creates a random private and its corresponding public keys
func GenerateBlsKey() (*PrivateKey, error) {
// GeneratePrivateKey creates a random private key (and its corresponding public key)
func GeneratePrivateKey() (*PrivateKey, error) {
s, err := randomK(rand.Reader)
if err != nil {
return nil, err
Expand All @@ -28,12 +28,28 @@ func GenerateBlsKey() (*PrivateKey, error) {
return &PrivateKey{s: s}, nil
}

// GenerateAndEncodePrivateKey creates a random private key and marshals it
// into byte array
func GenerateAndEncodePrivateKey() (*PrivateKey, []byte, error) {
privKey, err := GeneratePrivateKey()
if err != nil {
return nil, nil, err
}

privKeyEncoded, err := privKey.Marshal()
if err != nil {
return nil, nil, err
}

return privKey, privKeyEncoded, nil
}

// CreateRandomBlsKeys creates an array of random private and their corresponding public keys
func CreateRandomBlsKeys(total int) ([]*PrivateKey, error) {
blsKeys := make([]*PrivateKey, total)

for i := 0; i < total; i++ {
blsKey, err := GenerateBlsKey()
blsKey, err := GeneratePrivateKey()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion bls/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Test_SingleSign(t *testing.T) {

validTestMsg, invalidTestMsg := testGenRandomBytes(t, messageSize), testGenRandomBytes(t, messageSize)

blsKey, err := GenerateBlsKey() // structure which holds private/public key pair
blsKey, err := GeneratePrivateKey() // structure which holds private/public key pair
require.NoError(t, err)

// Sign valid message
Expand Down
15 changes: 3 additions & 12 deletions command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/0xPolygon/polygon-edge/bls"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/secrets"
Expand Down Expand Up @@ -145,20 +146,10 @@ func getBLSPublicKeyBytesFromSecretManager(manager secrets.SecretsManager) ([]by
return nil, err
}

secretKey, err := crypto.BytesToBLSSecretKey(keyBytes)
secretKey, err := bls.UnmarshalPrivateKey(keyBytes)
if err != nil {
return nil, err
}

pubKey, err := secretKey.GetPublicKey()
if err != nil {
return nil, err
}

pubKeyBytes, err := pubKey.MarshalBinary()
if err != nil {
return nil, err
}

return pubKeyBytes, nil
return secretKey.PublicKey().Marshal(), nil
}
4 changes: 2 additions & 2 deletions command/ibft/propose/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"fmt"
"strings"

"github.com/0xPolygon/polygon-edge/bls"
"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/helper"
ibftOp "github.com/0xPolygon/polygon-edge/consensus/ibft/proto"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/types"
)

Expand Down Expand Up @@ -89,7 +89,7 @@ func (p *proposeParams) initBLSPublicKey() error {
return fmt.Errorf("failed to parse BLS Public Key: %w", err)
}

if _, err := crypto.UnmarshalBLSPublicKey(blsPubkeyBytes); err != nil {
if _, err := bls.UnmarshalPublicKey(blsPubkeyBytes); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion consensus/ibft/fork/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path"
"testing"

"github.com/0xPolygon/polygon-edge/bls"
"github.com/0xPolygon/polygon-edge/consensus/ibft/hook"
"github.com/0xPolygon/polygon-edge/consensus/ibft/signer"
"github.com/0xPolygon/polygon-edge/crypto"
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestNewForkManager(t *testing.T) {
_, ecdsaKeyBytes, err := crypto.GenerateAndEncodeECDSAPrivateKey()
assert.NoError(t, err)

_, blsKeyBytes, err := crypto.GenerateAndEncodeBLSSecretKey()
_, blsKeyBytes, err := bls.GenerateAndEncodePrivateKey()
assert.NoError(t, err)

logger := hclog.NewNullLogger()
Expand Down
4 changes: 2 additions & 2 deletions consensus/ibft/operator_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"errors"
"fmt"

"github.com/0xPolygon/polygon-edge/bls"
"github.com/0xPolygon/polygon-edge/consensus/ibft/proto"
"github.com/0xPolygon/polygon-edge/consensus/ibft/signer"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/types"
"github.com/0xPolygon/polygon-edge/validators"
"github.com/0xPolygon/polygon-edge/validators/store"
Expand Down Expand Up @@ -141,7 +141,7 @@ func (o *operator) parseCandidate(req *proto.Candidate) (validators.Validator, e
return nil, errors.New("BLS public key required")
}

if _, err := crypto.UnmarshalBLSPublicKey(req.BlsPubkey); err != nil {
if _, err := bls.UnmarshalPublicKey(req.BlsPubkey); err != nil {
return nil, err
}
}
Expand Down
Loading

0 comments on commit 3910247

Please sign in to comment.