arnaucube
arnaucube/go-blindsecp256k1Public Self-hosted repository
go-blindsecp256k1/parsers_test.go
parsers_test.goRaw
 1package blindsecp256k1
 2
 3import (
 4	"encoding/hex"
 5	"encoding/json"
 6	"math/big"
 7	"testing"
 8
 9	"github.com/stretchr/testify/assert"
10	"github.com/stretchr/testify/require"
11)
12
13func TestMarshalers(t *testing.T) {
14	// Point
15	p := G.Mul(big.NewInt(1234))
16	b, err := json.Marshal(p)
17	require.Nil(t, err)
18	assert.Equal(t,
19		`{"x":"102884003323827292915668239759940053105992008087520207150474896054185180420338","y":"49384988101491619794462775601349526588349137780292274540231125201115197157452"}`, //nolint:lll
20		string(b))
21
22	var p2 *Point
23	err = json.Unmarshal(b, &p2)
24	require.Nil(t, err)
25	assert.Equal(t, p, p2)
26
27	// PublicKey
28	pk := PublicKey(*p)
29	b, err = json.Marshal(pk)
30	require.Nil(t, err)
31	assert.Equal(t,
32		`{"x":"102884003323827292915668239759940053105992008087520207150474896054185180420338","y":"49384988101491619794462775601349526588349137780292274540231125201115197157452"}`, //nolint:lll
33		string(b))
34
35	var pk2 PublicKey
36	err = json.Unmarshal(b, &pk2)
37	require.Nil(t, err)
38	assert.Equal(t, pk, pk2)
39
40	// Signature
41	sig := Signature{
42		S: big.NewInt(9876),
43		F: p,
44	}
45	b, err = json.Marshal(sig)
46	require.Nil(t, err)
47	assert.Equal(t,
48		`{"s":"9876","f":{"x":"102884003323827292915668239759940053105992008087520207150474896054185180420338","y":"49384988101491619794462775601349526588349137780292274540231125201115197157452"}}`, //nolint:lll
49		string(b))
50
51	var sig2 Signature
52	err = json.Unmarshal(b, &sig2)
53	require.Nil(t, err)
54	assert.Equal(t, sig, sig2)
55}
56
57func TestBytes(t *testing.T) {
58	// Point
59	p := &Point{
60		X: big.NewInt(3),
61		Y: big.NewInt(3),
62	}
63	b := p.Bytes()
64	assert.Equal(t, "03000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(b)) //nolint:lll
65	p2, err := NewPointFromBytes(b)
66	assert.Nil(t, err)
67	assert.Equal(t, p, p2)
68
69	p = G.Mul(big.NewInt(1234))
70	b = p.Bytes()
71	assert.Equal(t, "f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
72	p2, err = NewPointFromBytes(b)
73	assert.Nil(t, err)
74	assert.Equal(t, p, p2)
75
76	// PublicKey
77	pk := PublicKey(*p)
78	b = pk.Bytes()
79	assert.Equal(t, "f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
80	pk2, err := NewPublicKeyFromBytes(b)
81	assert.Nil(t, err)
82	assert.Equal(t, &pk, pk2)
83
84	// Signature
85	sig := Signature{
86		S: big.NewInt(9876),
87		F: p,
88	}
89	b = sig.Bytes()
90	assert.Equal(t, "9426000000000000000000000000000000000000000000000000000000000000f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
91	sig2, err := NewSignatureFromBytes(b)
92	assert.Nil(t, err)
93	assert.Equal(t, &sig, sig2)
94}