Skip to content

Commit

Permalink
Merge pull request #141 from TimeleapLabs/feature-bls-test
Browse files Browse the repository at this point in the history
✨ feat(bls-test): add unittest for bls and move bls sign to identity …
  • Loading branch information
logicalangel authored Jun 22, 2024
2 parents 026801e + fadecbf commit 91d0177
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 18 deletions.
12 changes: 0 additions & 12 deletions internal/crypto/bls/bls.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package bls

import (
"math/big"

bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381"
)

Expand All @@ -11,16 +9,6 @@ func Hash(message []byte) (bls12381.G1Affine, error) {
return bls12381.HashToG1(message, dst)
}

func Sign(secretKey big.Int, message []byte) (bls12381.G1Affine, bls12381.G1Affine) {
hashedMessage, err := Hash(message)
if err != nil {
panic(err)
}
signature := new(bls12381.G1Affine).ScalarMultiplication(&hashedMessage, &secretKey)

return *signature, hashedMessage
}

func RecoverSignature(bytes [48]byte) (bls12381.G1Affine, error) {
signature := new(bls12381.G1Affine)
_, err := signature.SetBytes(bytes[:])
Expand Down
11 changes: 11 additions & 0 deletions internal/crypto/bls/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ func NewIdentity() *Signer {
return s
}

// Sign signs a message with the identities secret key.
func (s *Signer) Sign(message []byte) (bls12381.G1Affine, bls12381.G1Affine) {
hashedMessage, err := Hash(message)
if err != nil {
panic(err)
}
signature := new(bls12381.G1Affine).ScalarMultiplication(&hashedMessage, s.SecretKey)

return *signature, hashedMessage
}

// Verify verifies the signature of a message belongs to the public key.
func (s *Signer) Verify(
signature bls12381.G1Affine, hashedMessage bls12381.G1Affine, publicKey bls12381.G2Affine,
Expand Down
48 changes: 48 additions & 0 deletions internal/crypto/bls/identity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package bls

import (
"testing"

"github.com/TimeleapLabs/unchained/internal/config"
"github.com/stretchr/testify/suite"
)

const SamplePrivateKey = "3b885a8a8f043724abfa865eccd38f536887d9ea1c08a742720e810f38a86872"

var (
TestMessage = []byte("test message")
SecondMessage = []byte("second message")
)

type TestIdentitySuit struct {
suite.Suite
signer *Signer
}

func (s *TestIdentitySuit) SetupTest() {
config.App.System.AllowGenerateSecrets = true
config.App.Secret.EvmPrivateKey = SamplePrivateKey

s.signer = NewIdentity()
}

func (s *TestIdentitySuit) TestVerifyIdentity() {
signature, messageHash := s.signer.Sign(TestMessage)
isValid, err := s.signer.Verify(signature, messageHash, *s.signer.PublicKey)
s.NoError(err)
s.True(isValid)

secondSignature, secondMessageHash := s.signer.Sign(SecondMessage)

isValid, err = s.signer.Verify(signature, secondMessageHash, *s.signer.PublicKey)
s.NoError(err)
s.False(isValid)

isValid, err = s.signer.Verify(secondSignature, messageHash, *s.signer.PublicKey)
s.NoError(err)
s.False(isValid)
}

func TestIdentitySuite(t *testing.T) {
suite.Run(t, new(TestIdentitySuit))
}
3 changes: 1 addition & 2 deletions internal/service/evmlog/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/TimeleapLabs/unchained/internal/config"
"github.com/TimeleapLabs/unchained/internal/crypto"
"github.com/TimeleapLabs/unchained/internal/crypto/bls"
"github.com/dgraph-io/badger/v4"
goEthereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -164,7 +163,7 @@ func (s *service) ProcessBlocks(ctx context.Context, chain string) error {
}

toHash := event.Sia().Bytes()
signature, hash := bls.Sign(*crypto.Identity.Bls.SecretKey, toHash)
signature, hash := crypto.Identity.Bls.Sign(toHash)

if conf.Send {
s.SendPriceReport(signature, event)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/uniswap/uniswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (s *service) syncBlock(ctx context.Context, token model.Token, caser cases.
},
}

signature, hash := bls.Sign(*crypto.Identity.Bls.SecretKey, priceInfo.Sia().Bytes())
signature, hash := crypto.Identity.Bls.Sign(priceInfo.Sia().Bytes())

if token.Send && !conn.IsClosed {
compressedSignature := signature.Bytes()
Expand Down
5 changes: 2 additions & 3 deletions internal/transport/client/handler/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package handler

import (
"github.com/TimeleapLabs/unchained/internal/crypto"
"github.com/TimeleapLabs/unchained/internal/crypto/bls"
"github.com/TimeleapLabs/unchained/internal/model"
)

func (h *consumer) Challenge(message []byte) []byte {
challenge := new(model.ChallengePacket).FromBytes(message)

signature, _ := bls.Sign(*crypto.Identity.Bls.SecretKey, challenge.Random[:])
signature, _ := crypto.Identity.Bls.Sign(challenge.Random[:])
challenge.Signature = signature.Bytes()

return challenge.Sia().Bytes()
Expand All @@ -18,7 +17,7 @@ func (h *consumer) Challenge(message []byte) []byte {
func (w worker) Challenge(message []byte) []byte {
challenge := new(model.ChallengePacket).FromBytes(message)

signature, _ := bls.Sign(*crypto.Identity.Bls.SecretKey, challenge.Random[:])
signature, _ := crypto.Identity.Bls.Sign(challenge.Random[:])
challenge.Signature = signature.Bytes()

return challenge.Sia().Bytes()
Expand Down

0 comments on commit 91d0177

Please sign in to comment.