arnaucube
arnaucube/eth-kzg-ceremony-altPublic Self-hosted repository
eth-kzg-ceremony-alt/parsers_test.go
parsers_test.goRaw
 1package kzgceremony
 2
 3import (
 4	"encoding/hex"
 5	"encoding/json"
 6	"io/ioutil"
 7	"strings"
 8	"testing"
 9
10	qt "github.com/frankban/quicktest"
11	bls12381 "github.com/kilic/bls12-381"
12)
13
14func TestStateMarshalers(t *testing.T) {
15	c := qt.New(t)
16	j, err := ioutil.ReadFile("current_state_10.json")
17	c.Assert(err, qt.IsNil)
18
19	state := &State{}
20	err = json.Unmarshal(j, state)
21	c.Assert(err, qt.IsNil)
22
23	b, err := json.Marshal(state)
24	c.Assert(err, qt.IsNil)
25	err = ioutil.WriteFile("parsed_state.json", b, 0600)
26	c.Assert(err, qt.IsNil)
27}
28
29func TestParseCompressedG1Point(t *testing.T) {
30	// this test is just to check that github.com/kilic/bls12-381 is
31	// compatible with the compressed points from zkcrypto/bls12-381
32	c := qt.New(t)
33
34	g1 := bls12381.NewG1()
35
36	p1Str := "0x97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb"
37	g1Bytes, err := hex.DecodeString(strings.TrimPrefix(p1Str, "0x"))
38	c.Assert(err, qt.IsNil)
39	g1Point, err := g1.FromCompressed(g1Bytes)
40	c.Assert(err, qt.IsNil)
41
42	recompressed := g1.ToCompressed(g1Point)
43	c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p1Str)
44
45	p1Str = "0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
46	g1Bytes, err = hex.DecodeString(strings.TrimPrefix(p1Str, "0x"))
47	c.Assert(err, qt.IsNil)
48	g1Point, err = g1.FromCompressed(g1Bytes)
49	c.Assert(err, qt.IsNil)
50
51	recompressed = g1.ToCompressed(g1Point)
52	c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p1Str)
53	// additionally check that g1Point is zero
54	c.Assert(g1.Equal(g1Point, g1.Zero()), qt.IsTrue)
55}
56
57func TestParseCompressedG2Point(t *testing.T) {
58	// this test is just to check that github.com/kilic/bls12-381 is
59	// compatible with the compressed points from zkcrypto/bls12-381
60	c := qt.New(t)
61
62	g2 := bls12381.NewG2()
63
64	p2Str := "0x93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8"
65	g2Bytes, err := hex.DecodeString(strings.TrimPrefix(p2Str, "0x"))
66	c.Assert(err, qt.IsNil)
67	g2Point, err := g2.FromCompressed(g2Bytes)
68	c.Assert(err, qt.IsNil)
69
70	recompressed := g2.ToCompressed(g2Point)
71	c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p2Str)
72
73	p2Str = "0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
74	g2Bytes, err = hex.DecodeString(strings.TrimPrefix(p2Str, "0x"))
75	c.Assert(err, qt.IsNil)
76	g2Point, err = g2.FromCompressed(g2Bytes)
77	c.Assert(err, qt.IsNil)
78
79	recompressed = g2.ToCompressed(g2Point)
80	c.Assert("0x"+hex.EncodeToString(recompressed), qt.Equals, p2Str)
81	// additionally check that g1Point is zero
82	c.Assert(g2.Equal(g2Point, g2.Zero()), qt.IsTrue)
83}