Skip to content

Commit

Permalink
Verify proof method + Refactor confidential pkg (#143)
Browse files Browse the repository at this point in the history
* Move functions for converting amounts from confidential to new pkg

* Refactor of confidential pkg
* rename unblinding method
* add unblinding method for issuance
* add method to verify surjection proof
* use internal functions to increase readability
* add tests for new methods

* Add mthod to verify blinding and use it in e2e tests
  • Loading branch information
altafan authored Nov 17, 2020
1 parent 31092ee commit c9c23e4
Show file tree
Hide file tree
Showing 13 changed files with 1,024 additions and 691 deletions.
586 changes: 391 additions & 195 deletions confidential/confidential.go

Large diffs are not rendered by default.

379 changes: 158 additions & 221 deletions confidential/confidential_test.go

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions confidential/data/confidential.json

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions internal/elementsutil/amount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package elementsutil

import (
"bytes"
"encoding/binary"
"errors"

"github.com/vulpemventures/go-elements/internal/bufferutil"
)

// SatoshiToElementsValue method converts Satoshi value to Elements value
func SatoshiToElementsValue(val uint64) ([]byte, error) {
unconfPrefix := byte(1)
b := bytes.NewBuffer([]byte{})
if err := bufferutil.BinarySerializer.PutUint64(b, binary.LittleEndian, val); err != nil {
return nil, err
}
res := append([]byte{unconfPrefix}, bufferutil.ReverseBytes(b.Bytes())...)
return res, nil
}

// ElementsToSatoshiValue method converts Elements value to Satoshi value
func ElementsToSatoshiValue(val []byte) (uint64, error) {
if len(val) != 9 {
return 0, errors.New("invalid elements value lenght")
}
if val[0] != byte(1) {
return 0, errors.New("invalid prefix")
}
reverseValueBuffer := bufferutil.ReverseBytes(val[1:])
d := bufferutil.NewDeserializer(bytes.NewBuffer(reverseValueBuffer))
return d.ReadUint64()
}
28 changes: 28 additions & 0 deletions internal/elementsutil/amount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package elementsutil

import (
"crypto/rand"
"math/big"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSatoshiToElementsValueRoundTrip(t *testing.T) {
bigInt, err := rand.Int(rand.Reader, big.NewInt(1000000000))
if err != nil {
panic(err)
}
satoshi := bigInt.Uint64()
elementsValue, err := SatoshiToElementsValue(satoshi)
if !assert.NoError(t, err) {
t.FailNow()
}

satoshiValue, err := ElementsToSatoshiValue(elementsValue)
if !assert.NoError(t, err) {
t.FailNow()
}

assert.Equal(t, satoshi, satoshiValue)
}
Loading

0 comments on commit c9c23e4

Please sign in to comment.