-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from Fairblock/add-keyshare-module-unit-test
Add keyshare module unit test
- Loading branch information
Showing
36 changed files
with
2,927 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package random | ||
|
||
import ( | ||
"encoding/hex" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// RandHex returns a random hexadecimal string of length n. | ||
func RandHex(n int) string { | ||
src := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
b := make([]byte, (n+1)/2) | ||
|
||
if _, err := src.Read(b); err != nil { | ||
panic(err) | ||
} | ||
|
||
return hex.EncodeToString(b)[:n] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package random | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/hex" | ||
distIBE "github.com/FairBlock/DistributedIBE" | ||
"github.com/Fairblock/fairyring/testutil/sample" | ||
"github.com/Fairblock/fairyring/x/keyshare/types" | ||
dcrdSecp256k1 "github.com/decred/dcrd/dcrec/secp256k1" | ||
"github.com/drand/kyber" | ||
bls "github.com/drand/kyber-bls12381" | ||
"math" | ||
"math/big" | ||
) | ||
|
||
type GeneratedShare struct { | ||
Share string | ||
EncShare string | ||
Index kyber.Scalar | ||
PrivateKey *dcrdSecp256k1.PrivateKey | ||
PK *dcrdSecp256k1.PublicKey | ||
ValidatorAddress string | ||
} | ||
|
||
type GenerateResult struct { | ||
GeneratedShare []*GeneratedShare | ||
KeyShareEncryptedKeyShares []*types.EncryptedKeyShare | ||
Commitments []string | ||
MasterPublicKey string | ||
} | ||
|
||
func GeneratePubKeyAndShares(totalNumberOfValidator uint32) (*GenerateResult, error) { | ||
|
||
t := int(math.Ceil(float64(totalNumberOfValidator) * (2.0 / 3.0))) | ||
|
||
shares, mpk, _, err := distIBE.GenerateShares(totalNumberOfValidator, uint32(t)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
masterPublicKeyByte, err := mpk.MarshalBinary() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result GenerateResult | ||
result.MasterPublicKey = hex.EncodeToString(masterPublicKeyByte) | ||
|
||
suite := bls.NewBLS12381Suite() | ||
keyShareCommitments := make([]string, totalNumberOfValidator) | ||
sharesList := make([]*GeneratedShare, totalNumberOfValidator) | ||
|
||
for _, s := range shares { | ||
indexByte, _ := hex.DecodeString(s.Index.String()) | ||
indexInt := big.NewInt(0).SetBytes(indexByte).Uint64() | ||
|
||
commitmentPoints := suite.G1().Point().Mul(s.Value, suite.G1().Point().Base()) | ||
commitmentPointsBinary, _ := commitmentPoints.MarshalBinary() | ||
|
||
keyShareCommitments[indexInt-1] = hex.EncodeToString(commitmentPointsBinary) | ||
|
||
randomAddr := sample.AccAddress() | ||
sb, _ := s.Value.MarshalBinary() | ||
privKey, err := dcrdSecp256k1.GeneratePrivateKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, err := dcrdSecp256k1.Encrypt(privKey.PubKey(), sb) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
share := GeneratedShare{ | ||
hex.EncodeToString(sb), | ||
base64.StdEncoding.EncodeToString(res), | ||
s.Index, | ||
privKey, | ||
privKey.PubKey(), | ||
randomAddr, | ||
} | ||
|
||
sharesList[indexInt-1] = &share | ||
} | ||
|
||
n := len(sharesList) | ||
|
||
encShares := make([]*types.EncryptedKeyShare, n) | ||
|
||
for _, v := range sharesList { | ||
indexByte, _ := hex.DecodeString(v.Index.String()) | ||
indexInt := big.NewInt(0).SetBytes(indexByte).Uint64() | ||
encShares[indexInt-1] = &types.EncryptedKeyShare{ | ||
Data: v.EncShare, | ||
Validator: v.ValidatorAddress, | ||
} | ||
} | ||
|
||
result.GeneratedShare = sharesList | ||
result.KeyShareEncryptedKeyShares = encShares | ||
result.Commitments = keyShareCommitments | ||
|
||
return &result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package shares | ||
|
||
import ( | ||
"encoding/hex" | ||
distIBE "github.com/FairBlock/DistributedIBE" | ||
bls "github.com/drand/kyber-bls12381" | ||
) | ||
|
||
func DeriveShare(hexShare string, shareIndex uint32, id string) (string, error) { | ||
shareByte, err := hex.DecodeString(hexShare) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
share := bls.NewKyberScalar() | ||
err = share.UnmarshalBinary(shareByte) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
s := bls.NewBLS12381Suite() | ||
extractedKey := distIBE.Extract(s, share, shareIndex, []byte(id)) | ||
|
||
extractedBinary, err := extractedKey.SK.MarshalBinary() | ||
if err != nil { | ||
return "", err | ||
} | ||
extractedKeyHex := hex.EncodeToString(extractedBinary) | ||
|
||
// commitmentPoint := s.G1().Point().Mul(share, s.G1().Point().Base()) | ||
//commitmentBinary, err := commitmentPoint.MarshalBinary() | ||
//if err != nil { | ||
// return "", err | ||
//} | ||
// commitmentHex := hex.EncodeToString(commitmentBinary) | ||
|
||
return extractedKeyHex, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package shares | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/hex" | ||
"github.com/btcsuite/btcd/btcec" | ||
) | ||
|
||
// EncryptWithPublicKey encrypts data using an RSA public key. | ||
func EncryptWithPublicKey(data string, pubKeyBase64 string) (string, error) { | ||
// Decode the base64 public key | ||
pubKeyBytes, err := base64.StdEncoding.DecodeString(pubKeyBase64) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Load the secp256k1 public key | ||
pubKey, err := btcec.ParsePubKey(pubKeyBytes, btcec.S256()) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
ciphertext, err := btcec.Encrypt(pubKey, []byte(data)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Encode ciphertext as hex for easy handling | ||
return hex.EncodeToString(ciphertext), nil | ||
} |
Oops, something went wrong.