Skip to content

Commit

Permalink
lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronit committed Aug 28, 2023
1 parent f08f062 commit 6a613a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 2 additions & 0 deletions pkg/node/threshold_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ func (pv *ThresholdValidator) SignBlock(chainID string, block *Block) ([]byte, t
// Used to track how close we are to threshold

// Here the actual signing process starts from a cryptological perspective
// TODO: This process should be factored out. It is not the responsibility of the validator to know
// how to arrange signature of a block. It should be a separate component that is injected into the validator.
nonces := make(map[ICosigner][]pcosigner.CosignerNonce)
thresholdPeersMutex := sync.Mutex{}

Expand Down
3 changes: 2 additions & 1 deletion pkg/pcosigner/cosigner_key_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func CreateCosignerEd25519ShardsFromFile(priv string, threshold, shards uint8) (
// by splitting the secret using Shamir secret sharing.
func CreateCosignerEd25519Shards(pv privval.FilePVKey, threshold, shards uint8) []CosignerEd25519Key {
// tsed25519.DealShares splits the secret using Shamir Secret Sharing (Note its: no verifiable secret sharing)
privShards := tsed25519.DealShares(tsed25519.ExpandSecret(pv.PrivKey.Bytes()[:32]), threshold, shards) // privshards is shamir shares
// privshards is shamir shares
privShards := tsed25519.DealShares(tsed25519.ExpandSecret(pv.PrivKey.Bytes()[:32]), threshold, shards)
out := make([]CosignerEd25519Key, shards)
for i, shard := range privShards {
out[i] = CosignerEd25519Key{
Expand Down
35 changes: 20 additions & 15 deletions pkg/pcosigner/cosigner_signer_soft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func TestSignthreshold25519(test *testing.T) {
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
require.NoError(test, err)

// persistent_shares is the privateKey split into 3 shamir parts
persistent_shares := tsed25519.DealShares(tsed25519.ExpandSecret(privateKey.Seed()), 2, 3)
// persistentshares is the privateKey split into 3 shamir parts
persistentshares := tsed25519.DealShares(tsed25519.ExpandSecret(privateKey.Seed()), 2, 3)

// each player generates secret Ri
r1 := make([]byte, 32)
Expand Down Expand Up @@ -92,12 +92,13 @@ func TestSignthreshold25519(test *testing.T) {
ephPublicKey := tsed25519.AddElements([]tsed25519.Element{pub1, pub2, pub3})

// Double check Pubkey
persistent_shares_pub1 := tsed25519.ScalarMultiplyBase(persistent_shares[0])
persistent_shares_pub2 := tsed25519.ScalarMultiplyBase(persistent_shares[1])
persistent_shares_pub3 := tsed25519.ScalarMultiplyBase(persistent_shares[2])
persistentSharesPub1 := tsed25519.ScalarMultiplyBase(persistentshares[0])
persistentSharesPub2 := tsed25519.ScalarMultiplyBase(persistentshares[1])
persistentSharesPub3 := tsed25519.ScalarMultiplyBase(persistentshares[2])

// A=A1+A2+...An = A=s1⋅B+s2⋅B+...sn⋅B
publicKey_2 := tsed25519.AddElements([]tsed25519.Element{persistent_shares_pub1, persistent_shares_pub2, persistent_shares_pub3})
publicKey2 := tsed25519.AddElements(
[]tsed25519.Element{persistentSharesPub1, persistentSharesPub2, persistentSharesPub3})
// require.Equal(test, publicKey, publicKey_2)

// each player sends s(i)_{j} to corresponding other player j (i.e. s(1)_{2} to player 2)
Expand All @@ -107,31 +108,35 @@ func TestSignthreshold25519(test *testing.T) {
s3 := tsed25519.AddScalars([]tsed25519.Scalar{shares1[2], shares2[2], shares3[2]})

_, _ = fmt.Printf("public keys: %x\n", publicKey)
_, _ = fmt.Printf("public keys: %x\n", publicKey_2)
_, _ = fmt.Printf("public keys: %x\n", publicKey2)
_, err = fmt.Printf("eph pub: %x\n", ephPublicKey)
if err != nil {
panic(err)
}
// fmt.Printf("eph secret: %x\n", ephemeralPublic)

shareSig1 := tsed25519.SignWithShare(message, persistent_shares[0], s1, publicKey, ephPublicKey)
shareSig2 := tsed25519.SignWithShare(message, persistent_shares[1], s2, publicKey, ephPublicKey)
shareSig3 := tsed25519.SignWithShare(message, persistent_shares[2], s3, publicKey, ephPublicKey)
shareSig1 := tsed25519.SignWithShare(message, persistentshares[0], s1, publicKey, ephPublicKey)
shareSig2 := tsed25519.SignWithShare(message, persistentshares[1], s2, publicKey, ephPublicKey)
shareSig3 := tsed25519.SignWithShare(message, persistentshares[2], s3, publicKey, ephPublicKey)

{
combinedSig := tsed25519.CombineShares(3, []int{1, 2, 3}, [][]byte{shareSig1, shareSig2, shareSig3})
signature := append(ephPublicKey, combinedSig...)
var signature []byte
signature = append(signature, ephPublicKey...)
signature = append(signature, combinedSig...)
fmt.Println(hex.EncodeToString(signature))
fmt.Println(ed25519.Verify(publicKey, message, signature[:]))
fmt.Println(ed25519.Verify(publicKey, message, signature))

if !ed25519.Verify(publicKey, message, signature[:]) {
if !ed25519.Verify(publicKey, message, signature) {
test.Error("Invalid Signature for signer [1,2,3]")
}
}
{
combinedSig := tsed25519.CombineShares(3, []int{1, 2}, [][]byte{shareSig1, shareSig2})
signature := append(ephPublicKey, combinedSig...)
if !ed25519.Verify(publicKey, message, signature[:]) {
var signature []byte
signature = append(signature, ephPublicKey...)
signature = append(signature, combinedSig...)
if !ed25519.Verify(publicKey, message, signature) {
test.Error("Invalid Signature for signer [1,2]")
}
}
Expand Down

0 comments on commit 6a613a5

Please sign in to comment.