arnaucube
arnaucube/kestoPublic Self-hosted repository
kesto/pubk-comp-ethereum-blindsigsecp256k1/check_test.go
pubk-comp-ethereum-blindsigsecp256k1/check_test.goRaw
 1package main
 2
 3import (
 4	"crypto/ecdsa"
 5	"math/big"
 6	"testing"
 7
 8	"github.com/arnaucube/go-blindsecp256k1"
 9	"github.com/btcsuite/btcd/btcec"
10	"github.com/stretchr/testify/require"
11	"gotest.tools/assert"
12)
13
14func calculateKeysFromK(t *testing.T, k *big.Int) (*btcec.PublicKey, *blindsecp256k1.PublicKey) {
15	curve := btcec.S256()
16
17	x, y := curve.ScalarBaseMult(k.Bytes())
18
19	skEth := (*btcec.PrivateKey)(&ecdsa.PrivateKey{
20		PublicKey: ecdsa.PublicKey{
21			Curve: curve,
22			X:     x,
23			Y:     y,
24		},
25		D: k,
26	})
27	pkEthAux := (*btcec.PublicKey)(&skEth.PublicKey)
28	pkEth := skEth.PubKey()
29	assert.Equal(t, pkEth, pkEthAux)
30
31	skBlind := blindsecp256k1.PrivateKey(*k)
32	pkBlind := skBlind.Public()
33
34	return pkEth, pkBlind
35}
36
37func TestCompatibleKeys(t *testing.T) {
38	// using incremental k
39	for i := 0; i < 10000; i++ {
40		k := big.NewInt(int64(i))
41
42		pkEth, pkBlind := calculateKeysFromK(t, k)
43		assert.Equal(t, pkEth.X.String(), pkBlind.X.String())
44		assert.Equal(t, pkEth.Y.String(), pkBlind.Y.String())
45		// fmt.Println("k", k)
46		// fmt.Println(pkEth)
47		// fmt.Println(pkBlind)
48	}
49
50	// using random k
51	for i := 0; i < 10000; i++ {
52		kaux, err := btcec.NewPrivateKey(btcec.S256())
53		require.NoError(t, err)
54		k := kaux.D
55
56		pkEth, pkBlind := calculateKeysFromK(t, k)
57		assert.Equal(t, pkEth.X.String(), pkBlind.X.String())
58		assert.Equal(t, pkEth.Y.String(), pkBlind.Y.String())
59		// fmt.Println("k", k)
60		// fmt.Println(pkEth)
61		// fmt.Println(pkBlind)
62	}
63}