```
Source File
util.go
Belonging Package
crypto/rand
```

`// Copyright 2011 The Go Authors. All rights reserved.`

`// Use of this source code is governed by a BSD-style`

`// license that can be found in the LICENSE file.`

`package rand`

`import (`

`)`

`// smallPrimes is a list of small, prime numbers that allows us to rapidly`

`// exclude some fraction of composite candidates when searching for a random`

`// prime. This list is truncated at the point where smallPrimesProduct exceeds`

`// a uint64. It does not include two because we ensure that the candidates are`

`// odd by construction.`

`var smallPrimes = []uint8{`

`3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,`

`}`

`// smallPrimesProduct is the product of the values in smallPrimes and allows us`

`// to reduce a candidate prime by this number and then determine whether it's`

`// coprime to all the elements of smallPrimes without further big.Int`

`// operations.`

`var smallPrimesProduct = new(big.Int).SetUint64(16294579238595022365)`

`// Prime returns a number, p, of the given size, such that p is prime`

`// with high probability.`

`// Prime will return error for any error returned by rand.Read or if bits < 2.`

`func ( io.Reader, int) ( *big.Int, error) {`

`if < 2 {`

`= errors.New("crypto/rand: prime size must be at least 2-bit")`

`return`

`}`

`:= uint( % 8)`

`if == 0 {`

`= 8`

`}`

`:= make([]byte, (+7)/8)`

`= new(big.Int)`

`:= new(big.Int)`

`for {`

`_, = io.ReadFull(, )`

`if != nil {`

`return nil,`

`}`

`// Clear bits in the first byte to make sure the candidate has a size <= bits.`

`[0] &= uint8(int(1<<) - 1)`

`// Don't let the value be too small, i.e, set the most significant two bits.`

`// Setting the top two bits, rather than just the top bit,`

`// means that when two of these values are multiplied together,`

`// the result isn't ever one bit short.`

`if >= 2 {`

`[0] |= 3 << ( - 2)`

`} else {`

`// Here b==1, because b cannot be zero.`

`[0] |= 1`

`if len() > 1 {`

`[1] |= 0x80`

`}`

`}`

`// Make the value odd since an even number this large certainly isn't prime.`

`[len()-1] |= 1`

`.SetBytes()`

`// Calculate the value mod the product of smallPrimes. If it's`

`// a multiple of any of these primes we add two until it isn't.`

`// The probability of overflowing is minimal and can be ignored`

`// because we still perform Miller-Rabin tests on the result.`

`.Mod(, smallPrimesProduct)`

`:= .Uint64()`

`:`

`for := uint64(0); < 1<<20; += 2 {`

`:= +`

`for , := range smallPrimes {`

`if %uint64() == 0 && ( > 6 || != uint64()) {`

`continue`

`}`

`}`

`if > 0 {`

`.SetUint64()`

`.Add(, )`

`}`

`break`

`}`

`// There is a tiny possibility that, by adding delta, we caused`

`// the number to be one bit too long. Thus we check BitLen`

`// here.`

`if .ProbablyPrime(20) && .BitLen() == {`

`return`

`}`

`}`

`}`

`// Int returns a uniform random value in [0, max). It panics if max <= 0.`

`func ( io.Reader, *big.Int) ( *big.Int, error) {`

`if .Sign() <= 0 {`

`panic("crypto/rand: argument to Int is <= 0")`

`}`

`= new(big.Int)`

`.Sub(, .SetUint64(1))`

`// bitLen is the maximum bit length needed to encode a value < max.`

`:= .BitLen()`

`if == 0 {`

`// the only valid result is 0`

`return`

`}`

`// k is the maximum byte length needed to encode a value < max.`

`:= ( + 7) / 8`

`// b is the number of bits in the most significant byte of max-1.`

`:= uint( % 8)`

`if == 0 {`

`= 8`

`}`

`:= make([]byte, )`

`for {`

`_, = io.ReadFull(, )`

`if != nil {`

`return nil,`

`}`

`// Clear bits in the first byte to increase the probability`

`// that the candidate is < max.`

`[0] &= uint8(int(1<<) - 1)`

`.SetBytes()`

`if .Cmp() < 0 {`

`return`

`}`

`}`

`}`

The pages are generated with Golds v0.4.5. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |