Skip to content

Commit

Permalink
feat: Caelum (#111)
Browse files Browse the repository at this point in the history
* bls signature for basic account

* benchmark for bls and ed25519

* added bls sig verify cost to genesis

* Revert "Merge branch 'fetchai:master' into bls"

This reverts commit a5dd8ea, reversing
changes made to 082e071.

* format using go tools

* nuisance golangci-lint errors

* Bls (#104)

* bls signature for basic account

* benchmark for bls and ed25519

* added bls sig verify cost to genesis

* Revert "Merge branch 'fetchai:master' into bls"

This reverts commit a5dd8ea, reversing
changes made to 082e071.

* format using go tools

* nuisance golangci-lint errors

* POP interfaces in accounts and authentication

* add bls multsig operations

* fixed golangci-lint error

* changes after comments

* initial commit from regen-ledger/x/group v1.0.0

* minor changes to bls12381 key generation

* initial commit from regen-ledger/proto/regen/group v1.0.0

* group module compatibility for fetchai cosomos-sdk

* add bls account restriction to group members

* fix bug in setting pop

* make msg uniqueness checking optional

* add bls basic/aggregate vote

* add checking on empty messages/public keys

* add gas caclulation/consumption for verifying aggregated votes

* minor change to gas calculation for voteagg

* initial commit for orm and types from regen-ledger v2.0.0-beta1

* upgrade testsuite to regen-ledger v2.0.0-beta1

* make bls requirement for group members optional

* add tests for bls related group operations

* client and server for poll and aggregated votes and integration tests

* fix bls related test errors

* fix proto-lint errors

* goimport format

* proto comments

* update blst to v0.3.5 and more tests for bls

* Update x/auth/ante/sigverify.go

Co-authored-by: daeMOn <[email protected]>

* Update x/group/client/util.go

Co-authored-by: daeMOn <[email protected]>

Co-authored-by: daeMOn <[email protected]>
  • Loading branch information
daeMOn63 committed Nov 12, 2021
1 parent 50ccad3 commit 46695a9
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 28 deletions.
9 changes: 7 additions & 2 deletions crypto/keys/bls12381/bls12381.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func genPrivKey(rand io.Reader) []byte {

sk := blst.KeyGen(ikm[:])
if sk == nil {
panic("Failed to generate secret key!")
panic("failed to generate secret key!")
}

skBytes := sk.Serialize()
Expand All @@ -136,7 +136,12 @@ func genPrivKey(rand io.Reader) []byte {
// if it's derived from user input.
func GenPrivKeyFromSecret(secret []byte) *PrivKey {
ikm := crypto.Sha256(secret) // Not Ripemd160 because we want 32 bytes.
skBytes := blst.KeyGen(ikm).Serialize()

sk := blst.KeyGen(ikm)
if sk == nil {
panic("failed to generate secret key from ikm")
}
skBytes := sk.Serialize()

return &PrivKey{Key: skBytes}
}
Expand Down
170 changes: 169 additions & 1 deletion crypto/keys/bls12381/bls12381_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package bls12381_test

import (
"encoding/base64"
"testing"

"github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/bls12381"
bench "github.com/cosmos/cosmos-sdk/crypto/keys/internal/benchmarking"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
"testing"
)

func TestSignAndValidateBls12381(t *testing.T) {
Expand All @@ -22,6 +30,166 @@ func TestSignAndValidateBls12381(t *testing.T) {

}

func TestKeyFromSecret(t *testing.T) {
insecureSeed := []byte("a random number for testing")
privKey := bls12381.GenPrivKeyFromSecret(insecureSeed)
pubKey := privKey.PubKey()

msg := []byte("hello")
sig, err := privKey.Sign(msg)
require.Nil(t, err)
assert.True(t, pubKey.VerifySignature(msg, sig))
}

func TestPubKeyEquals(t *testing.T) {
bls12381PubKey := bls12381.GenPrivKey().PubKey().(*bls12381.PubKey)

testCases := []struct {
msg string
pubKey cryptotypes.PubKey
other cryptotypes.PubKey
expectEq bool
}{
{
"different bytes",
bls12381PubKey,
bls12381.GenPrivKey().PubKey(),
false,
},
{
"equals",
bls12381PubKey,
&bls12381.PubKey{
Key: bls12381PubKey.Key,
},
true,
},
{
"different types",
bls12381PubKey,
secp256k1.GenPrivKey().PubKey(),
false,
},
}

for _, tc := range testCases {
t.Run(tc.msg, func(t *testing.T) {
eq := tc.pubKey.Equals(tc.other)
require.Equal(t, eq, tc.expectEq)
})
}
}

func TestPrivKeyEquals(t *testing.T) {
bls12381PrivKey := bls12381.GenPrivKey()

testCases := []struct {
msg string
privKey cryptotypes.PrivKey
other cryptotypes.PrivKey
expectEq bool
}{
{
"different bytes",
bls12381PrivKey,
bls12381.GenPrivKey(),
false,
},
{
"equals",
bls12381PrivKey,
&bls12381.PrivKey{
Key: bls12381PrivKey.Key,
},
true,
},
{
"different types",
bls12381PrivKey,
secp256k1.GenPrivKey(),
false,
},
}

for _, tc := range testCases {
t.Run(tc.msg, func(t *testing.T) {
eq := tc.privKey.Equals(tc.other)
require.Equal(t, eq, tc.expectEq)
})
}
}

func TestMarshalAmino(t *testing.T) {
aminoCdc := codec.NewLegacyAmino()
privKey := bls12381.GenPrivKey()
pubKey := privKey.PubKey().(*bls12381.PubKey)

testCases := []struct {
desc string
msg codec.AminoMarshaler
typ interface{}
expBinary []byte
expJSON string
}{
{
"bls12381 private key",
privKey,
&bls12381.PrivKey{},
append([]byte{32}, privKey.Bytes()...), // Length-prefixed.
"\"" + base64.StdEncoding.EncodeToString(privKey.Bytes()) + "\"",
},
{
"bls12381 public key",
pubKey,
&bls12381.PubKey{},
append([]byte{96}, pubKey.Bytes()...), // Length-prefixed.
"\"" + base64.StdEncoding.EncodeToString(pubKey.Bytes()) + "\"",
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
// Do a round trip of encoding/decoding binary.
bz, err := aminoCdc.Marshal(tc.msg)
require.NoError(t, err)
require.Equal(t, tc.expBinary, bz)

err = aminoCdc.Unmarshal(bz, tc.typ)
require.NoError(t, err)

require.Equal(t, tc.msg, tc.typ)

// Do a round trip of encoding/decoding JSON.
bz, err = aminoCdc.MarshalJSON(tc.msg)
require.NoError(t, err)
require.Equal(t, tc.expJSON, string(bz))

err = aminoCdc.UnmarshalJSON(bz, tc.typ)
require.NoError(t, err)

require.Equal(t, tc.msg, tc.typ)
})
}
}

func TestMarshalJSON(t *testing.T) {
require := require.New(t)
privKey := bls12381.GenPrivKey()
pk := privKey.PubKey()

registry := types.NewInterfaceRegistry()
cryptocodec.RegisterInterfaces(registry)
cdc := codec.NewProtoCodec(registry)

bz, err := cdc.MarshalInterfaceJSON(pk)
require.NoError(err)

var pk2 cryptotypes.PubKey
err = cdc.UnmarshalInterfaceJSON(bz, &pk2)
require.NoError(err)
require.True(pk2.Equals(pk))
}

func BenchmarkSignBls(b *testing.B) {
privKey := bls12381.GenPrivKey()

Expand Down
22 changes: 18 additions & 4 deletions crypto/keys/bls12381/multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func AggregateSignature(sigs [][]byte) ([]byte, error) {

// VerifyMultiSignature assumes public key is already validated
func VerifyMultiSignature(msg []byte, sig []byte, pks []*PubKey) error {
return VerifyAggregateSignature([][]byte{msg}, sig, [][]*PubKey{pks})
return VerifyAggregateSignature([][]byte{msg}, false, sig, [][]*PubKey{pks})
}

func Unique(msgs [][]byte) bool {
Expand All @@ -65,18 +65,32 @@ func Unique(msgs [][]byte) bool {
return true
}

func VerifyAggregateSignature(msgs [][]byte, sig []byte, pkss [][]*PubKey) error {
func VerifyAggregateSignature(msgs [][]byte, msgCheck bool, sig []byte, pkss [][]*PubKey) error {
n := len(msgs)
if n == 0 {
return fmt.Errorf("messages cannot be empty")
}

for i, msg := range msgs {
if len(msg) == 0 {
return fmt.Errorf("%d-th message is empty", i)
}
}

if len(pkss) != n {
return fmt.Errorf("the number of messages and public key sets must match")
}

if !Unique(msgs) {
return fmt.Errorf("messages must be pairwise distinct")
for i, pks := range pkss {
if len(pks) == 0 {
return fmt.Errorf("%d-th public key set is empty", i)
}
}

if msgCheck {
if !Unique(msgs) {
return fmt.Errorf("messages must be pairwise distinct")
}
}

apks := make([]*blst.P1Affine, len(pkss))
Expand Down
5 changes: 2 additions & 3 deletions crypto/keys/bls12381/multisig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func TestBlsAggSig(t *testing.T) {
aggSig, err := bls.AggregateSignature(sigs)
require.Nil(t, err)

assert.Nil(t, bls.VerifyAggregateSignature(msgs, aggSig, pks))

assert.Nil(t, bls.VerifyAggregateSignature(msgs, true, aggSig, pks))
}

func benchmarkBlsVerifyMulti(total int, b *testing.B) {
Expand Down Expand Up @@ -113,7 +112,7 @@ func benchmarkBlsVerifyAgg(total int, b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
bls.VerifyAggregateSignature(msgs, aggSig, pks)
bls.VerifyAggregateSignature(msgs, false, aggSig, pks)
}
}

Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/DataDog/zstd v1.4.5 // indirect
github.com/armon/go-metrics v0.3.8
github.com/bgentry/speakeasy v0.1.0
github.com/btcsuite/btcd v0.21.0-beta
github.com/btcsuite/btcutil v1.0.2
github.com/btcsuite/btcd v0.22.0-beta
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
github.com/confio/ics23/go v0.6.6
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/iavl v0.17.1
Expand All @@ -27,6 +27,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/golang-lru v0.5.4
github.com/lib/pq v1.10.2 // indirect
github.com/magiconair/properties v1.8.5
github.com/mattn/go-isatty v0.0.12
github.com/mitchellh/mapstructure v1.3.3 // indirect
Expand All @@ -45,14 +46,14 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
github.com/supranational/blst v0.3.4
github.com/supranational/blst v0.3.5
github.com/tendermint/btcd v0.1.1
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15
github.com/tendermint/go-amino v0.16.0
github.com/tendermint/tendermint v0.34.13
github.com/tendermint/tm-db v0.6.4
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
google.golang.org/genproto v0.0.0-20210114201628-6edceaf6022f
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c
google.golang.org/grpc v1.40.0
google.golang.org/protobuf v1.26.0
gopkg.in/ini.v1 v1.61.0 // indirect
Expand Down
Loading

0 comments on commit 46695a9

Please sign in to comment.