@@ -418,41 +418,62 @@ func TestP256Mult(t *testing.T) {
418
418
}
419
419
}
420
420
421
- func TestInfinity (t * testing.T ) {
422
- tests := []struct {
423
- name string
424
- curve Curve
425
- }{
426
- {"p224" , P224 ()},
427
- {"p256" , P256 ()},
421
+ func testInfinity (t * testing.T , curve Curve ) {
422
+ _ , x , y , _ := GenerateKey (curve , rand .Reader )
423
+ x , y = curve .ScalarMult (x , y , curve .Params ().N .Bytes ())
424
+ if x .Sign () != 0 || y .Sign () != 0 {
425
+ t .Errorf ("x^q != ∞" )
428
426
}
429
427
430
- for _ , test := range tests {
431
- curve := test .curve
432
- x , y := curve .ScalarBaseMult (nil )
433
- if x .Sign () != 0 || y .Sign () != 0 {
434
- t .Errorf ("%s: x^0 != ∞" , test .name )
435
- }
428
+ x , y = curve .ScalarBaseMult ([]byte {0 })
429
+ if x .Sign () != 0 || y .Sign () != 0 {
430
+ t .Errorf ("b^0 != ∞" )
436
431
x .SetInt64 (0 )
437
432
y .SetInt64 (0 )
433
+ }
438
434
439
- x2 , y2 := curve .Double (x , y )
440
- if x2 .Sign () != 0 || y2 .Sign () != 0 {
441
- t .Errorf ("%s: 2∞ != ∞" , test . name )
442
- }
435
+ x2 , y2 := curve .Double (x , y )
436
+ if x2 .Sign () != 0 || y2 .Sign () != 0 {
437
+ t .Errorf ("2∞ != ∞" )
438
+ }
443
439
444
- baseX := curve .Params ().Gx
445
- baseY := curve .Params ().Gy
440
+ baseX := curve .Params ().Gx
441
+ baseY := curve .Params ().Gy
446
442
447
- x3 , y3 := curve .Add (baseX , baseY , x , y )
448
- if x3 .Cmp (baseX ) != 0 || y3 .Cmp (baseY ) != 0 {
449
- t .Errorf ("%s: x+∞ != x" , test . name )
450
- }
443
+ x3 , y3 := curve .Add (baseX , baseY , x , y )
444
+ if x3 .Cmp (baseX ) != 0 || y3 .Cmp (baseY ) != 0 {
445
+ t .Errorf ("x+∞ != x" )
446
+ }
451
447
452
- x4 , y4 := curve .Add (x , y , baseX , baseY )
453
- if x4 .Cmp (baseX ) != 0 || y4 .Cmp (baseY ) != 0 {
454
- t .Errorf ("%s: ∞+x != x" , test .name )
455
- }
448
+ x4 , y4 := curve .Add (x , y , baseX , baseY )
449
+ if x4 .Cmp (baseX ) != 0 || y4 .Cmp (baseY ) != 0 {
450
+ t .Errorf ("∞+x != x" )
451
+ }
452
+
453
+ if curve .IsOnCurve (x , y ) {
454
+ t .Errorf ("IsOnCurve(∞) == true" )
455
+ }
456
+ }
457
+
458
+ func TestInfinity (t * testing.T ) {
459
+ tests := []struct {
460
+ name string
461
+ curve Curve
462
+ }{
463
+ {"P-224" , P224 ()},
464
+ {"P-256" , P256 ()},
465
+ {"P-256/Generic" , P256 ().Params ()},
466
+ {"P-384" , P384 ()},
467
+ {"P-521" , P521 ()},
468
+ }
469
+ if testing .Short () {
470
+ tests = tests [:1 ]
471
+ }
472
+ for _ , test := range tests {
473
+ curve := test .curve
474
+ t .Run (test .name , func (t * testing.T ) {
475
+ testInfinity (t , curve )
476
+ })
456
477
}
457
478
}
458
479
0 commit comments