summaryrefslogtreecommitdiff
path: root/crypto/rand.go
blob: 99f22e1457ccd8e472bf75c76aeaed21df62d107 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package crypto

import (
	"crypto/rand"
	"encoding/binary"

	"github.com/davidlazar/go-crypto/drbg"

	"github.com/algorand/go-algorand/logging"
)

// RNG represents a randomness source.  This could be either a system-wide
// randomness source (like what gets exposed by crypto/rand), or a PRNG that
// we use for testing.
type RNG interface {
	RandBytes([]byte)
}

// PRNG is a pseudo-random implementation of RNG, used for deterministic testing.
type PRNG struct {
	d *drbg.DRBG
}

// SystemRNG implements the RNG interface using the system-wide randomness
// source (from Go's crypto/rand).
var SystemRNG = &systemRNG{}

type systemRNG struct{}

// RandUint64 returns a random 64-bit unsigned integer
func RandUint64() uint64 {
	var eightbytes [8]byte
	_, err := rand.Read(eightbytes[:])
	if err != nil {
		logging.Base().Fatal("cannot read random number")
	}
	return binary.LittleEndian.Uint64(eightbytes[:])
}

// RandUint63 returns a random 64-bit unsigned integer which can be stored in a 64-bit signed integer without any data loss.
func RandUint63() uint64 {
	// use the RandUint64() function and clear the highest bit.
	return RandUint64() & ((1 << 63) - 1)
}

// RandBytes fills the provided structure with a set of random bytes
func RandBytes(buf []byte) {
	_, err := rand.Read(buf)
	if err != nil {
		logging.Base().Fatal("cannot read random bytes")
	}
}

// MakePRNG creates a new PRNG from an initial seed.  The implementation is
// based on HMAC_DRBG.  All random bytes from the PRNG will be determined by
// the initial seed value. Used by test code only.
func MakePRNG(seed []byte) *PRNG {
	return &PRNG{
		d: drbg.New(seed),
	}
}

// RandBytes implements the RNG interface for the PRNG. Used by test code only.
func (prng *PRNG) RandBytes(buf []byte) {
	n, err := prng.d.Read(buf)
	if err != nil {
		logging.Base().Panicf("PRNG.RandBytes: %v", err)
	}

	if n != len(buf) {
		logging.Base().Panicf("PRNG.RandBytes: short read: %v != %v", n, len(buf))
	}
}

// System-wide RNG
func (rng *systemRNG) RandBytes(buf []byte) {
	RandBytes(buf)
}