go-iden3-crypto/babyjub/eddsa_test.go
babyjub/eddsa_test.goRaw
1package babyjub
2
3import (
4 "crypto/rand"
5 "encoding/hex"
6 "fmt"
7
8 "github.com/iden3/go-iden3-crypto/constants"
9 "github.com/iden3/go-iden3-crypto/utils"
10 "github.com/stretchr/testify/assert"
11
12 "math/big"
13 "testing"
14)
15
16func genInputs() (*PrivateKey, *big.Int) {
17 k := NewRandPrivKey()
18 fmt.Println("k", hex.EncodeToString(k[:]))
19
20 msgBuf := [32]byte{}
21 rand.Read(msgBuf[:])
22 msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf[:])
23 msg.Mod(msg, constants.Q)
24 fmt.Println("msg", msg)
25
26 return &k, msg
27}
28
29func TestSignVerify1(t *testing.T) {
30 var k PrivateKey
31 hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
32 msgBuf, err := hex.DecodeString("00010203040506070809")
33 if err != nil {
34 panic(err)
35 }
36 msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
37
38 pk := k.Public()
39 assert.Equal(t,
40 "2610057752638682202795145288373380503107623443963127956230801721756904484787",
41 pk.X.String())
42 assert.Equal(t,
43 "16617171478497210597712478520507818259149717466230047843969353176573634386897",
44 pk.Y.String())
45
46 sig := k.SignMimc7(msg)
47 assert.Equal(t,
48 "4974729414807584049518234760796200867685098748448054182902488636762478901554",
49 sig.R8.X.String())
50 assert.Equal(t,
51 "18714049394522540751536514815950425694461287643205706667341348804546050128733",
52 sig.R8.Y.String())
53 assert.Equal(t,
54 "2171284143457722024136077617757713039502332290425057126942676527240038689549",
55 sig.S.String())
56
57 ok := pk.VerifyMimc7(msg, sig)
58 assert.Equal(t, true, ok)
59
60 sigBuf := sig.Compress()
61 sig2, err := new(Signature).Decompress(sigBuf)
62 assert.Equal(t, nil, err)
63
64 assert.Equal(t, ""+
65 "5dfb6f843c023fe3e52548ccf22e55c81b426f7af81b4f51f7152f2fcfc65f29"+
66 "0dab19c5a0a75973cd75a54780de0c3a41ede6f57396fe99b5307fff3ce7cc04",
67 hex.EncodeToString(sigBuf[:]))
68
69 ok = pk.VerifyMimc7(msg, sig2)
70 assert.Equal(t, true, ok)
71}
72
73func TestCompressDecompress(t *testing.T) {
74 var k PrivateKey
75 hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
76 pk := k.Public()
77 for i := 0; i < 64; i++ {
78 msgBuf, err := hex.DecodeString(fmt.Sprintf("000102030405060708%02d", i))
79 if err != nil {
80 panic(err)
81 }
82 msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
83 sig := k.SignMimc7(msg)
84 sigBuf := sig.Compress()
85 sig2, err := new(Signature).Decompress(sigBuf)
86 assert.Equal(t, nil, err)
87 ok := pk.VerifyMimc7(msg, sig2)
88 assert.Equal(t, true, ok)
89 }
90}