Skip to content

Commit

Permalink
Merge branch 'master' into feature-19
Browse files Browse the repository at this point in the history
  • Loading branch information
autholykos committed May 26, 2020
2 parents c6f3f00 + 6e4a110 commit e0d6c50
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
25 changes: 25 additions & 0 deletions bls/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ func NewApk(pk *PublicKey) *Apk {
}
}

// Copy the APK by marshalling and unmarshalling the internals. It is somewhat
// wasteful but does the job
func (apk *Apk) Copy() *Apk {
g2 := new(bn256.G2)
b := apk.gx.Marshal()
// no need to check errors. We deal with well formed APKs
_, _ = g2.Unmarshal(b)

cpy := &Apk{
PublicKey: &PublicKey{g2},
}
return cpy
}

// UnmarshalApk unmarshals a byte array into an aggregated PublicKey
func UnmarshalApk(b []byte) (*Apk, error) {
apk := &Apk{
Expand Down Expand Up @@ -253,6 +267,17 @@ func UnmarshalSignature(sig []byte) (*Signature, error) {
return sigma, nil
}

// Copy (inefficiently) the Signature by unmarshaling and marshaling the
// embedded G1
func (sigma *Signature) Copy() *Signature {
b := sigma.e.Marshal()
s := &Signature{
e: new(bn256.G1),
}
_, _ = s.e.Unmarshal(b)
return s
}

// Add creates an aggregated signature from a normal BLS Signature and related public key
func (sigma *Signature) Add(pk *PublicKey, sig *UnsafeSignature) error {
other, err := apkSigWrap(pk, sig)
Expand Down
23 changes: 23 additions & 0 deletions bls/bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"math/big"
"reflect"
"testing"

"github.com/dusk-network/bn256"
Expand All @@ -19,6 +20,28 @@ func randomMessage() []byte {
return msg
}

func TestCopyApk(t *testing.T) {
require := require.New(t)
pub, _, err := GenKeyPair(rand.Reader)
require.NoError(err)
apk := NewApk(pub)

cpy := apk.Copy()
require.True(reflect.DeepEqual(apk, cpy))
}

func TestCopySig(t *testing.T) {
require := require.New(t)
pub, priv, err := GenKeyPair(rand.Reader)
require.NoError(err)

sigma, err := Sign(priv, pub, []byte("ciao!!"))
require.NoError(err)

cpy := sigma.Copy()
require.True(reflect.DeepEqual(sigma, cpy))
}

// TestSignVerify
func TestSignVerify(t *testing.T) {
msg := randomMessage()
Expand Down

0 comments on commit e0d6c50

Please sign in to comment.