-
Notifications
You must be signed in to change notification settings - Fork 1
/
cprng.go
40 lines (32 loc) · 834 Bytes
/
cprng.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) 2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package txauthor
import (
"crypto/rand"
"sync"
"github.com/EXCCoin/exccwallet/v2/internal/uniformprng"
)
// cprng is a cryptographically random-seeded prng. It is seeded during package
// init. Any initialization errors result in panics. It is safe for concurrent
// access.
var cprng = cprngType{}
type cprngType struct {
r *uniformprng.Source
mu sync.Mutex
}
func init() {
r, err := uniformprng.RandSource(rand.Reader)
if err != nil {
panic("Failed to seed prng: " + err.Error())
}
cprng.r = r
}
func (c *cprngType) Int31n(n int32) int32 {
defer c.mu.Unlock()
c.mu.Lock()
if n <= 0 {
panic("Int31n: non-positive n")
}
return int32(c.r.Uint32n(uint32(n)))
}