github.com/dusk-network/dusk-crypto@v0.1.3/rangeproof/rangeproof_test.go (about)

     1  package rangeproof
     2  
     3  import (
     4  	"bytes"
     5  	"math/big"
     6  	"math/rand"
     7  	"testing"
     8  
     9  	ristretto "github.com/bwesterb/go-ristretto"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestProveBulletProof(t *testing.T) {
    15  
    16  	p := generateProof(3, t)
    17  
    18  	// Verify
    19  	ok, err := Verify(*p)
    20  	assert.Equal(t, nil, err)
    21  	assert.Equal(t, true, ok)
    22  
    23  }
    24  
    25  func TestEncodeDecode(t *testing.T) {
    26  	p := generateProof(4, t)
    27  	includeCommits := false
    28  
    29  	buf := &bytes.Buffer{}
    30  	err := p.Encode(buf, includeCommits)
    31  	assert.Nil(t, err)
    32  
    33  	var decodedProof Proof
    34  	err = decodedProof.Decode(buf, includeCommits)
    35  	assert.Nil(t, err)
    36  
    37  	ok := decodedProof.Equals(*p, includeCommits)
    38  	assert.True(t, ok)
    39  }
    40  
    41  func TestComputeMu(t *testing.T) {
    42  	var one ristretto.Scalar
    43  	one.SetOne()
    44  
    45  	var expected ristretto.Scalar
    46  	expected.SetBigInt(big.NewInt(2))
    47  
    48  	res := computeMu(one, one, one)
    49  
    50  	ok := expected.Equals(&res)
    51  
    52  	assert.Equal(t, true, ok)
    53  }
    54  
    55  func generateProof(m int, t *testing.T) *Proof {
    56  
    57  	// XXX: m must be a multiple of two due to inner product proof
    58  	amounts := []ristretto.Scalar{}
    59  
    60  	for i := 0; i < m; i++ {
    61  
    62  		var amount ristretto.Scalar
    63  		n := rand.Int63()
    64  		amount.SetBigInt(big.NewInt(n))
    65  
    66  		amounts = append(amounts, amount)
    67  	}
    68  
    69  	// Prove
    70  	p, err := Prove(amounts, true)
    71  	require.Nil(t, err)
    72  	return &p
    73  }
    74  func BenchmarkProve(b *testing.B) {
    75  
    76  	var amount ristretto.Scalar
    77  
    78  	amount.SetBigInt(big.NewInt(100000))
    79  
    80  	for i := 0; i < 100; i++ {
    81  
    82  		// Prove
    83  		Prove([]ristretto.Scalar{amount}, false)
    84  	}
    85  
    86  }
    87  func BenchmarkVerify(b *testing.B) {
    88  
    89  	var amount ristretto.Scalar
    90  
    91  	amount.SetBigInt(big.NewInt(100000))
    92  	p, _ := Prove([]ristretto.Scalar{amount}, false)
    93  
    94  	b.ResetTimer()
    95  
    96  	for i := 0; i < 100; i++ {
    97  		// Verify
    98  		Verify(p)
    99  	}
   100  
   101  }