Skip to content

Commit

Permalink
Merge pull request #18 from dusk-network/feature-17
Browse files Browse the repository at this point in the history
add (inefficient) copy for APK and Signature
  • Loading branch information
autholykos committed May 19, 2020
2 parents b7d73ad + 1fd0050 commit 6e4a110
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 24 deletions.
25 changes: 25 additions & 0 deletions bls/bls.go
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
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
47 changes: 26 additions & 21 deletions rangeproof/innerproduct/innerproduct.go
Expand Up @@ -40,7 +40,7 @@ func Generate(GVec, HVec []ristretto.Point, aVec, bVec, HprimeFactors []ristrett
H := make([]ristretto.Point, len(HVec))
copy(H, HVec)

hs := fiatshamir.HashCacher{[]byte{}}
hs := fiatshamir.HashCacher{Cache: []byte{}}

lgN := bits.TrailingZeros(nextPow2(uint(n)))

Expand Down Expand Up @@ -390,33 +390,35 @@ func (proof *Proof) Verify(G, H, L, R []ristretto.Point, HprimeFactor []ristrett
return have.Equals(&P)
}

func (p *Proof) Encode(w io.Writer) error {
// Encode a proof
func (proof *Proof) Encode(w io.Writer) error {

err := binary.Write(w, binary.BigEndian, p.A.Bytes())
err := binary.Write(w, binary.BigEndian, proof.A.Bytes())
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, p.B.Bytes())
err = binary.Write(w, binary.BigEndian, proof.B.Bytes())
if err != nil {
return err
}
lenL := uint32(len(p.L))
lenL := uint32(len(proof.L))

for i := uint32(0); i < lenL; i++ {
err = binary.Write(w, binary.BigEndian, p.L[i].Bytes())
err = binary.Write(w, binary.BigEndian, proof.L[i].Bytes())
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, p.R[i].Bytes())
err = binary.Write(w, binary.BigEndian, proof.R[i].Bytes())
if err != nil {
return err
}
}
return nil
}

func (p *Proof) Decode(r io.Reader) error {
if p == nil {
// Decode a proof
func (proof *Proof) Decode(r io.Reader) error {
if proof == nil {
return errors.New("struct is nil")
}

Expand All @@ -429,8 +431,8 @@ func (p *Proof) Decode(r io.Reader) error {
if err != nil {
return err
}
p.A.SetBytes(&ABytes)
p.B.SetBytes(&BBytes)
proof.A.SetBytes(&ABytes)
proof.B.SetBytes(&BBytes)

buf := &bytes.Buffer{}
_, err = buf.ReadFrom(r)
Expand All @@ -443,8 +445,8 @@ func (p *Proof) Decode(r io.Reader) error {
}
lenL := uint32(numBytes / 64)

p.L = make([]ristretto.Point, lenL)
p.R = make([]ristretto.Point, lenL)
proof.L = make([]ristretto.Point, lenL)
proof.R = make([]ristretto.Point, lenL)

for i := uint32(0); i < lenL; i++ {
var LBytes, RBytes [32]byte
Expand All @@ -456,31 +458,32 @@ func (p *Proof) Decode(r io.Reader) error {
if err != nil {
return err
}
p.L[i].SetBytes(&LBytes)
p.R[i].SetBytes(&RBytes)
proof.L[i].SetBytes(&LBytes)
proof.R[i].SetBytes(&RBytes)
}

return nil
}

func (p *Proof) Equals(other Proof) bool {
ok := p.A.Equals(&other.A)
// Equals tests for Equality two proofs
func (proof *Proof) Equals(other Proof) bool {
ok := proof.A.Equals(&other.A)
if !ok {
return ok
}

ok = p.B.Equals(&other.B)
ok = proof.B.Equals(&other.B)
if !ok {
return ok
}

for i := range p.L {
ok := p.L[i].Equals(&other.L[i])
for i := range proof.L {
ok := proof.L[i].Equals(&other.L[i])
if !ok {
return ok
}

ok = p.R[i].Equals(&other.R[i])
ok = proof.R[i].Equals(&other.R[i])
if !ok {
return ok
}
Expand All @@ -503,6 +506,8 @@ func isPower2(n uint32) bool {
return (n & (n - 1)) == 0
}

// DiffNextPow2 returns the difference between a given number and the next
// power of two
func DiffNextPow2(n uint32) uint32 {
pow2 := nextPow2(uint(n))
padAmount := uint32(pow2) - n + 1
Expand Down
9 changes: 6 additions & 3 deletions rangeproof/rangeproof.go
Expand Up @@ -69,7 +69,7 @@ func Prove(v []ristretto.Scalar, debug bool) (Proof, error) {
ped.BaseVector.Compute(uint32((N * M)))

// Hash for Fiat-Shamir
hs := fiatshamir.HashCacher{[]byte{}}
hs := fiatshamir.HashCacher{Cache: []byte{}}

for _, amount := range v {
// compute commmitment to v
Expand Down Expand Up @@ -335,7 +335,7 @@ func Verify(p Proof) (bool, error) {
H := ped2.BaseVector.Bases

// Reconstruct the challenges
hs := fiatshamir.HashCacher{[]byte{}}
hs := fiatshamir.HashCacher{Cache: []byte{}}
for _, V := range p.V {
hs.Append(V.Value.Bytes())
}
Expand Down Expand Up @@ -492,6 +492,7 @@ func megacheckWithC(ipproof *innerproduct.Proof, mu, x, y, z, t, taux, w ristret
return true, nil
}

// Encode a Proof
func (p *Proof) Encode(w io.Writer, includeCommits bool) error {

if includeCommits {
Expand Down Expand Up @@ -532,6 +533,7 @@ func (p *Proof) Encode(w io.Writer, includeCommits bool) error {
return p.IPProof.Encode(w)
}

// Decode a Proof
func (p *Proof) Decode(r io.Reader, includeCommits bool) error {

if p == nil {
Expand Down Expand Up @@ -578,6 +580,7 @@ func (p *Proof) Decode(r io.Reader, includeCommits bool) error {
return p.IPProof.Decode(r)
}

// Equals tests proof for equality
func (p *Proof) Equals(other Proof, includeCommits bool) bool {
if len(p.V) != len(other.V) && includeCommits {
return false
Expand Down Expand Up @@ -619,7 +622,7 @@ func (p *Proof) Equals(other Proof, includeCommits bool) bool {
return ok
}
return true
return p.IPProof.Equals(*other.IPProof)
//return p.IPProof.Equals(*other.IPProof)
}

func readerToPoint(r io.Reader, p *ristretto.Point) error {
Expand Down

0 comments on commit 6e4a110

Please sign in to comment.