arnaucube
arnaucube/go-iden3-cryptoPublic Self-hosted repository
go-iden3-crypto/babyjub/babyjub_test.go
babyjub/babyjub_test.goRaw
  1package babyjub
  2
  3import (
  4	"encoding/hex"
  5	"math/big"
  6	"math/rand"
  7	"testing"
  8
  9	"github.com/iden3/go-iden3-crypto/constants"
 10	"github.com/iden3/go-iden3-crypto/utils"
 11	"github.com/stretchr/testify/assert"
 12)
 13
 14func TestAdd1(t *testing.T) {
 15	a := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
 16	b := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
 17
 18	c := NewPoint().Projective().Add(a.Projective(), b.Projective())
 19	// fmt.Printf("%v = 2 * %v", *c, *a)
 20	assert.Equal(t, "0", c.X.String())
 21	assert.Equal(t, "1", c.Y.String())
 22}
 23
 24func TestAdd2(t *testing.T) {
 25	aX := utils.NewIntFromString(
 26		"17777552123799933955779906779655732241715742912184938656739573121738514868268")
 27	aY := utils.NewIntFromString(
 28		"2626589144620713026669568689430873010625803728049924121243784502389097019475")
 29	a := &Point{X: aX, Y: aY}
 30
 31	bX := utils.NewIntFromString(
 32		"17777552123799933955779906779655732241715742912184938656739573121738514868268")
 33	bY := utils.NewIntFromString(
 34		"2626589144620713026669568689430873010625803728049924121243784502389097019475")
 35	b := &Point{X: bX, Y: bY}
 36
 37	c := NewPoint().Projective().Add(a.Projective(), b.Projective()).Affine()
 38	// fmt.Printf("%v = 2 * %v", *c, *a)
 39	assert.Equal(t,
 40		"6890855772600357754907169075114257697580319025794532037257385534741338397365",
 41		c.X.String())
 42	assert.Equal(t,
 43		"4338620300185947561074059802482547481416142213883829469920100239455078257889",
 44		c.Y.String())
 45
 46	d := NewPointProjective().Add(c.Projective(), c.Projective()).Affine()
 47	assert.Equal(t, "2f6458832049e917c95867185a96621336df33e13c98e81d1ef4928cdbb77772", hex.EncodeToString(d.X.Bytes()))
 48
 49	// Projective
 50	aP := a.Projective()
 51	bP := b.Projective()
 52	cP := NewPointProjective().Add(aP, bP)
 53	c2 := cP.Affine()
 54	assert.Equal(t, c, c2)
 55
 56}
 57
 58func TestAdd3(t *testing.T) {
 59	aX := utils.NewIntFromString(
 60		"17777552123799933955779906779655732241715742912184938656739573121738514868268")
 61	aY := utils.NewIntFromString(
 62		"2626589144620713026669568689430873010625803728049924121243784502389097019475")
 63	a := &Point{X: aX, Y: aY}
 64
 65	bX := utils.NewIntFromString(
 66		"16540640123574156134436876038791482806971768689494387082833631921987005038935")
 67	bY := utils.NewIntFromString(
 68		"20819045374670962167435360035096875258406992893633759881276124905556507972311")
 69	b := &Point{X: bX, Y: bY}
 70
 71	c := NewPoint().Projective().Add(a.Projective(), b.Projective()).Affine()
 72	// fmt.Printf("%v = 2 * %v", *c, *a)
 73	assert.Equal(t,
 74		"7916061937171219682591368294088513039687205273691143098332585753343424131937",
 75		c.X.String())
 76	assert.Equal(t,
 77		"14035240266687799601661095864649209771790948434046947201833777492504781204499",
 78		c.Y.String())
 79}
 80
 81func TestAdd4(t *testing.T) {
 82	aX := utils.NewIntFromString(
 83		"0")
 84	aY := utils.NewIntFromString(
 85		"1")
 86	a := &Point{X: aX, Y: aY}
 87
 88	bX := utils.NewIntFromString(
 89		"16540640123574156134436876038791482806971768689494387082833631921987005038935")
 90	bY := utils.NewIntFromString(
 91		"20819045374670962167435360035096875258406992893633759881276124905556507972311")
 92	b := &Point{X: bX, Y: bY}
 93
 94	c := NewPoint().Projective().Add(a.Projective(), b.Projective()).Affine()
 95	// fmt.Printf("%v = 2 * %v", *c, *a)
 96	assert.Equal(t,
 97		"16540640123574156134436876038791482806971768689494387082833631921987005038935",
 98		c.X.String())
 99	assert.Equal(t,
100		"20819045374670962167435360035096875258406992893633759881276124905556507972311",
101		c.Y.String())
102}
103
104func TestInCurve1(t *testing.T) {
105	p := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
106	assert.Equal(t, true, p.InCurve())
107}
108
109func TestInCurve2(t *testing.T) {
110	p := &Point{X: big.NewInt(1), Y: big.NewInt(0)}
111	assert.Equal(t, false, p.InCurve())
112}
113
114func TestMul0(t *testing.T) {
115	x := utils.NewIntFromString(
116		"17777552123799933955779906779655732241715742912184938656739573121738514868268")
117	y := utils.NewIntFromString(
118		"2626589144620713026669568689430873010625803728049924121243784502389097019475")
119	p := &Point{X: x, Y: y}
120	s := utils.NewIntFromString("3")
121
122	r2 := NewPoint().Projective().Add(p.Projective(), p.Projective()).Affine()
123	r2 = NewPoint().Projective().Add(r2.Projective(), p.Projective()).Affine()
124	r := NewPoint().Mul(s, p)
125	assert.Equal(t, r2.X.String(), r.X.String())
126	assert.Equal(t, r2.Y.String(), r.Y.String())
127
128	assert.Equal(t,
129		"19372461775513343691590086534037741906533799473648040012278229434133483800898",
130		r.X.String())
131	assert.Equal(t,
132		"9458658722007214007257525444427903161243386465067105737478306991484593958249",
133		r.Y.String())
134}
135
136func TestMul1(t *testing.T) {
137	x := utils.NewIntFromString(
138		"17777552123799933955779906779655732241715742912184938656739573121738514868268")
139	y := utils.NewIntFromString(
140		"2626589144620713026669568689430873010625803728049924121243784502389097019475")
141	p := &Point{X: x, Y: y}
142	s := utils.NewIntFromString(
143		"14035240266687799601661095864649209771790948434046947201833777492504781204499")
144	r := NewPoint().Mul(s, p)
145	assert.Equal(t,
146		"17070357974431721403481313912716834497662307308519659060910483826664480189605",
147		r.X.String())
148	assert.Equal(t,
149		"4014745322800118607127020275658861516666525056516280575712425373174125159339",
150		r.Y.String())
151}
152
153func TestMul2(t *testing.T) {
154	x := utils.NewIntFromString(
155		"6890855772600357754907169075114257697580319025794532037257385534741338397365")
156	y := utils.NewIntFromString(
157		"4338620300185947561074059802482547481416142213883829469920100239455078257889")
158	p := &Point{X: x, Y: y}
159	s := utils.NewIntFromString(
160		"20819045374670962167435360035096875258406992893633759881276124905556507972311")
161	r := NewPoint().Mul(s, p)
162	assert.Equal(t,
163		"13563888653650925984868671744672725781658357821216877865297235725727006259983",
164		r.X.String())
165	assert.Equal(t,
166		"8442587202676550862664528699803615547505326611544120184665036919364004251662",
167		r.Y.String())
168}
169
170func TestInCurve3(t *testing.T) {
171	x := utils.NewIntFromString(
172		"17777552123799933955779906779655732241715742912184938656739573121738514868268")
173	y := utils.NewIntFromString(
174		"2626589144620713026669568689430873010625803728049924121243784502389097019475")
175	p := &Point{X: x, Y: y}
176	assert.Equal(t, true, p.InCurve())
177}
178
179func TestInCurve4(t *testing.T) {
180	x := utils.NewIntFromString(
181		"6890855772600357754907169075114257697580319025794532037257385534741338397365")
182	y := utils.NewIntFromString(
183		"4338620300185947561074059802482547481416142213883829469920100239455078257889")
184	p := &Point{X: x, Y: y}
185	assert.Equal(t, true, p.InCurve())
186}
187
188func TestInSubGroup1(t *testing.T) {
189	x := utils.NewIntFromString(
190		"17777552123799933955779906779655732241715742912184938656739573121738514868268")
191	y := utils.NewIntFromString(
192		"2626589144620713026669568689430873010625803728049924121243784502389097019475")
193	p := &Point{X: x, Y: y}
194	assert.Equal(t, true, p.InSubGroup())
195}
196
197func TestInSubGroup2(t *testing.T) {
198	x := utils.NewIntFromString(
199		"6890855772600357754907169075114257697580319025794532037257385534741338397365")
200	y := utils.NewIntFromString(
201		"4338620300185947561074059802482547481416142213883829469920100239455078257889")
202	p := &Point{X: x, Y: y}
203	assert.Equal(t, true, p.InSubGroup())
204}
205
206func TestPointFromSignAndy(t *testing.T) {
207	x := utils.NewIntFromString(
208		"17777552123799933955779906779655732241715742912184938656739573121738514868268")
209	y := utils.NewIntFromString(
210		"2626589144620713026669568689430873010625803728049924121243784502389097019475")
211	p := &Point{X: x, Y: y}
212
213	sign := PointCoordSign(p.X)
214	p2, err := PointFromSignAndY(sign, p.Y)
215	assert.Equal(t, nil, err)
216	assert.Equal(t, p.X.String(), p2.X.String())
217	assert.Equal(t, p.Y.String(), p2.Y.String())
218}
219
220func TestCompressDecompress1(t *testing.T) {
221	x := utils.NewIntFromString(
222		"17777552123799933955779906779655732241715742912184938656739573121738514868268")
223	y := utils.NewIntFromString(
224		"2626589144620713026669568689430873010625803728049924121243784502389097019475")
225	p := &Point{X: x, Y: y}
226
227	buf := p.Compress()
228	assert.Equal(t, "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85", hex.EncodeToString(buf[:]))
229
230	p2, err := NewPoint().Decompress(buf)
231	assert.Equal(t, nil, err)
232	assert.Equal(t, p.X.String(), p2.X.String())
233	assert.Equal(t, p.Y.String(), p2.Y.String())
234}
235
236func TestCompressDecompress2(t *testing.T) {
237	x := utils.NewIntFromString(
238		"6890855772600357754907169075114257697580319025794532037257385534741338397365")
239	y := utils.NewIntFromString(
240		"4338620300185947561074059802482547481416142213883829469920100239455078257889")
241	p := &Point{X: x, Y: y}
242
243	buf := p.Compress()
244	assert.Equal(t, "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709", hex.EncodeToString(buf[:]))
245
246	p2, err := NewPoint().Decompress(buf)
247	assert.Equal(t, nil, err)
248	assert.Equal(t, p.X.String(), p2.X.String())
249	assert.Equal(t, p.Y.String(), p2.Y.String())
250}
251
252func TestCompressDecompressRnd(t *testing.T) {
253	for i := 0; i < 64; i++ {
254		p1 := NewPoint().Mul(big.NewInt(int64(i)), B8)
255		buf := p1.Compress()
256		p2, err := NewPoint().Decompress(buf)
257		assert.Equal(t, nil, err)
258		assert.Equal(t, p1.X.Bytes(), p2.X.Bytes())
259		assert.Equal(t, p1.Y.Bytes(), p2.Y.Bytes())
260	}
261}
262
263func BenchmarkBabyjub(b *testing.B) {
264	const n = 256
265
266	rnd := rand.New(rand.NewSource(42))
267
268	var badpoints [n]*Point
269	for i := 0; i < n; i++ {
270		x := new(big.Int).Rand(rnd, constants.Q)
271		y := new(big.Int).Rand(rnd, constants.Q)
272		badpoints[i] = &Point{X: x, Y: y}
273	}
274
275	var points [n]*Point
276	var pointsProj [n]*PointProjective
277	baseX := utils.NewIntFromString(
278		"17777552123799933955779906779655732241715742912184938656739573121738514868268")
279	baseY := utils.NewIntFromString(
280		"2626589144620713026669568689430873010625803728049924121243784502389097019475")
281	base := &Point{X: baseX, Y: baseY}
282	for i := 0; i < n; i++ {
283		s := new(big.Int).Rand(rnd, constants.Q)
284		points[i] = NewPoint().Mul(s, base)
285		pointsProj[i] = NewPoint().Mul(s, base).Projective()
286	}
287
288	var scalars [n]*big.Int
289	for i := 0; i < n; i++ {
290		scalars[i] = new(big.Int).Rand(rnd, constants.Q)
291	}
292
293	b.Run("AddConst", func(b *testing.B) {
294		p0 := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
295		p1 := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
296		p0Proj := p0.Projective()
297		p1Proj := p1.Projective()
298
299		p2 := NewPoint().Projective()
300		for i := 0; i < b.N; i++ {
301			p2.Add(p0Proj, p1Proj)
302		}
303	})
304
305	b.Run("AddRnd", func(b *testing.B) {
306		res := NewPoint().Projective()
307		for i := 0; i < b.N; i++ {
308			res.Add(pointsProj[i%(n/2)], pointsProj[i%(n/2)+1])
309		}
310	})
311
312	b.Run("MulRnd", func(b *testing.B) {
313		res := NewPoint()
314		for i := 0; i < b.N; i++ {
315			res.Mul(scalars[i%n], points[i%n])
316		}
317	})
318
319	b.Run("Compress", func(b *testing.B) {
320		for i := 0; i < b.N; i++ {
321			points[i%n].Compress()
322		}
323	})
324
325	b.Run("InCurve", func(b *testing.B) {
326		for i := 0; i < b.N; i++ {
327			badpoints[i%n].InCurve()
328		}
329	})
330
331	b.Run("InSubGroup", func(b *testing.B) {
332		for i := 0; i < b.N; i++ {
333			points[i%n].InCurve()
334		}
335	})
336}