github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/src/math/big/prime_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package big
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  var primes = []string{
    13  	"2",
    14  	"3",
    15  	"5",
    16  	"7",
    17  	"11",
    18  
    19  	"13756265695458089029",
    20  	"13496181268022124907",
    21  	"10953742525620032441",
    22  	"17908251027575790097",
    23  
    24  	// https://golang.org/issue/638
    25  	"18699199384836356663",
    26  
    27  	"98920366548084643601728869055592650835572950932266967461790948584315647051443",
    28  	"94560208308847015747498523884063394671606671904944666360068158221458669711639",
    29  
    30  	// http://primes.utm.edu/lists/small/small3.html
    31  	"449417999055441493994709297093108513015373787049558499205492347871729927573118262811508386655998299074566974373711472560655026288668094291699357843464363003144674940345912431129144354948751003607115263071543163",
    32  	"230975859993204150666423538988557839555560243929065415434980904258310530753006723857139742334640122533598517597674807096648905501653461687601339782814316124971547968912893214002992086353183070342498989426570593",
    33  	"5521712099665906221540423207019333379125265462121169655563495403888449493493629943498064604536961775110765377745550377067893607246020694972959780839151452457728855382113555867743022746090187341871655890805971735385789993",
    34  	"203956878356401977405765866929034577280193993314348263094772646453283062722701277632936616063144088173312372882677123879538709400158306567338328279154499698366071906766440037074217117805690872792848149112022286332144876183376326512083574821647933992961249917319836219304274280243803104015000563790123",
    35  
    36  	// ECC primes: http://tools.ietf.org/html/draft-ladd-safecurves-02
    37  	"3618502788666131106986593281521497120414687020801267626233049500247285301239",                                                                                  // Curve1174: 2^251-9
    38  	"57896044618658097711785492504343953926634992332820282019728792003956564819949",                                                                                 // Curve25519: 2^255-19
    39  	"9850501549098619803069760025035903451269934817616361666987073351061430442874302652853566563721228910201656997576599",                                           // E-382: 2^382-105
    40  	"42307582002575910332922579714097346549017899709713998034217522897561970639123926132812109468141778230245837569601494931472367",                                 // Curve41417: 2^414-17
    41  	"6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151", // E-521: 2^521-1
    42  }
    43  
    44  var composites = []string{
    45  	"0",
    46  	"1",
    47  	"21284175091214687912771199898307297748211672914763848041968395774954376176754",
    48  	"6084766654921918907427900243509372380954290099172559290432744450051395395951",
    49  	"84594350493221918389213352992032324280367711247940675652888030554255915464401",
    50  	"82793403787388584738507275144194252681",
    51  }
    52  
    53  func TestProbablyPrime(t *testing.T) {
    54  	nreps := 20
    55  	if testing.Short() {
    56  		nreps = 1
    57  	}
    58  	for i, s := range primes {
    59  		p, _ := new(Int).SetString(s, 10)
    60  		if !p.ProbablyPrime(nreps) {
    61  			t.Errorf("#%d prime found to be non-prime (%s)", i, s)
    62  		}
    63  	}
    64  
    65  	for i, s := range composites {
    66  		c, _ := new(Int).SetString(s, 10)
    67  		if c.ProbablyPrime(nreps) {
    68  			t.Errorf("#%d composite found to be prime (%s)", i, s)
    69  		}
    70  		if testing.Short() {
    71  			break
    72  		}
    73  	}
    74  
    75  	// check that ProbablyPrime panics if n <= 0
    76  	c := NewInt(11) // a prime
    77  	for _, n := range []int{-1, 0, 1} {
    78  		func() {
    79  			defer func() {
    80  				if n <= 0 && recover() == nil {
    81  					t.Fatalf("expected panic from ProbablyPrime(%d)", n)
    82  				}
    83  			}()
    84  			if !c.ProbablyPrime(n) {
    85  				t.Fatalf("%v should be a prime", c)
    86  			}
    87  		}()
    88  	}
    89  }
    90  
    91  func BenchmarkProbablyPrime(b *testing.B) {
    92  	p, _ := new(Int).SetString("203956878356401977405765866929034577280193993314348263094772646453283062722701277632936616063144088173312372882677123879538709400158306567338328279154499698366071906766440037074217117805690872792848149112022286332144876183376326512083574821647933992961249917319836219304274280243803104015000563790123", 10)
    93  	for _, rep := range []int{1, 5, 10, 20} {
    94  		b.Run(fmt.Sprintf("Rep=%d", rep), func(b *testing.B) {
    95  			for i := 0; i < b.N; i++ {
    96  				p.ProbablyPrime(rep)
    97  			}
    98  		})
    99  	}
   100  }