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 "13277427435165878497778222415993513565335242147425444199013288855685581939618",
41 pk.X.String())
42 assert.Equal(t,
43 "13622229784656158136036771217484571176836296686641868549125388198837476602820",
44 pk.Y.String())
45
46 sig := k.SignMimc7(msg)
47 assert.Equal(t,
48 "11384336176656855268977457483345535180380036354188103142384839473266348197733",
49 sig.R8.X.String())
50 assert.Equal(t,
51 "15383486972088797283337779941324724402501462225528836549661220478783371668959",
52 sig.R8.Y.String())
53 assert.Equal(t,
54 "2523202440825208709475937830811065542425109372212752003460238913256192595070",
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 "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
66 "7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405",
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}