arnaucube
arnaucube/go-blindsecp256k1Public Self-hosted repository
go-blindsecp256k1/blindsecp256k1_test.go
blindsecp256k1_test.goRaw
 1package blindsecp256k1
 2
 3import (
 4	"math/big"
 5	"testing"
 6
 7	"github.com/stretchr/testify/assert"
 8	"github.com/stretchr/testify/require"
 9)
10
11func TestFlow(t *testing.T) {
12	// signer: create new signer key pair
13	sk := NewPrivateKey()
14	signerPubK := sk.Public()
15
16	// signer: when user requests new R parameter to blind a new msg,
17	// create new signerR (public) with its secret k
18	k, signerR := NewRequestParameters()
19
20	// user: blinds the msg using signer's R
21	msg := new(big.Int).SetBytes([]byte("test"))
22	msgBlinded, userSecretData, err := Blind(msg, signerR)
23	require.Nil(t, err)
24
25	// signer: signs the blinded message using its private key & secret k
26	sBlind, err := sk.BlindSign(msgBlinded, k)
27	require.Nil(t, err)
28
29	// user: unblinds the blinded signature
30	sig := Unblind(sBlind, userSecretData)
31	sigB := sig.Bytes()
32	sig2, err := NewSignatureFromBytes(sigB)
33	assert.Nil(t, err)
34	assert.Equal(t, sig, sig2)
35
36	// signature can be verified with signer PublicKey
37	verified := Verify(msg, sig, signerPubK)
38	assert.True(t, verified)
39}
40
41// func newBigIntWithBitLen(n int) *big.Int {
42//         b := make([]byte, n/8)
43//         for i := 0; i < len(b); i++ {
44//                 b[i] = 255
45//         }
46//         bi := new(big.Int).SetBytes(b[:])
47//         return bi
48// }
49//
50// func TestMinBigIntBytesLen(t *testing.T) {
51//         k := big.NewInt(1)
52//         sk := PrivateKey(*k)
53//
54//         mBlinded := newBigIntWithBitLen(MinBigIntBytesLen)
55//         require.Equal(t, MinBigIntBytesLen, mBlinded.BitLen())
56//         _, err := sk.BlindSign(mBlinded, k)
57//         assert.Nil(t, err)
58//
59//         mBlinded = new(big.Int).Div(mBlinded, big.NewInt(2))
60//         require.Equal(t, MinBigIntBytesLen-1, mBlinded.BitLen())
61//         _, err = sk.BlindSign(mBlinded, k)
62//         assert.Equal(t, "mBlinded too small", err.Error())
63// }