Skip to content

Commit

Permalink
use gnark crypto lib
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Dec 21, 2023
1 parent bd1f4bc commit 778c5f9
Show file tree
Hide file tree
Showing 14 changed files with 602 additions and 252 deletions.
57 changes: 57 additions & 0 deletions cmd/horcrux/cmd/shards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"testing"

"github.com/cometbft/cometbft/crypto/ed25519"
cometjson "github.com/cometbft/cometbft/libs/json"
"github.com/cometbft/cometbft/privval"
"github.com/strangelove-ventures/horcrux/cmd/horcrux/cmd/testdata"
"github.com/strangelove-ventures/horcrux/signer"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -139,3 +142,57 @@ func TestRSAShards(t *testing.T) {
})
}
}

func TestPrivValidatorBn254(t *testing.T) {
bz := testdata.PrivValidatorKeyBn254
var privvalKey privval.FilePVKey
err := cometjson.Unmarshal(bz, &privvalKey)
require.NoError(t, err)

msg := []byte("hello")

sig, err := privvalKey.PrivKey.Sign(msg)
require.NoError(t, err)

valid := privvalKey.PrivKey.PubKey().VerifySignature(msg, sig)
require.True(t, valid)

valid = privvalKey.PubKey.VerifySignature(msg, sig)
require.True(t, valid)

shards, err := signer.CreateCosignerShards(&privvalKey, 2, 3)
require.NoError(t, err)

var signers = make([]*signer.ThresholdSignerSoftBn254, len(shards))
var sigs = make([][]byte, len(shards))

for i, shard := range shards {
signers[i], err = signer.NewThresholdSignerSoftBn254(&shard, 2, 3)
require.NoError(t, err)

sig, err = signers[i].Sign(nil, msg)
require.NoError(t, err)

sigs[i] = sig
}

var partialSigs = make([]signer.PartialSignature, 0, 2)
for i, sig := range sigs {
if i == 0 {
continue
}
partialSigs = append(partialSigs, signer.PartialSignature{
ID: i + 1,
Signature: sig,
})
}

combinedSig, err := signers[0].CombineSignatures(partialSigs)
require.NoError(t, err)

valid = privvalKey.PrivKey.PubKey().VerifySignature(msg, combinedSig)
require.True(t, valid)

valid = privvalKey.PubKey.VerifySignature(msg, combinedSig)
require.True(t, valid)
}
11 changes: 11 additions & 0 deletions cmd/horcrux/cmd/testdata/priv_validator_key-bn254.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"address": "092F3E64DF1B43207DCB187747C7E35A0A8C277B",
"pub_key": {
"type": "tendermint/PubKeyBn254",
"value": "jX4uANsjStZrWdWurqaFKXB8AQjoUhd/GiZjeEAB01Y="
},
"priv_key": {
"type": "tendermint/PrivKeyBn254",
"value": "0sZKL1ORco5/VXVtoLSaF2QS4TmzZlBgctr1aUfja1gCXu6OqvPob9f2q7XnzSQRCL1gmY1SsYcoImN4iR/HSg=="
}
}
3 changes: 3 additions & 0 deletions cmd/horcrux/cmd/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ var CosignerRSAKeyMigrated string

//go:embed cosigner-key-v2.json
var CosignerKeyV2 []byte

//go:embed priv_validator_key-bn254.json
var PrivValidatorKeyBn254 []byte
Loading

0 comments on commit 778c5f9

Please sign in to comment.