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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use ffi;
#[cfg(not(feature = "std"))]
use prelude::*;
pub const SEEDBYTES: usize = ffi::randombytes_SEEDBYTES as usize;
pub fn randombytes(size: usize) -> Vec<u8> {
unsafe {
let mut buf = vec![0u8; size];
ffi::randombytes_buf(buf.as_mut_ptr() as *mut _, size);
buf
}
}
pub fn randombytes_into(buf: &mut [u8]) {
unsafe {
ffi::randombytes_buf(buf.as_mut_ptr() as *mut _, buf.len());
}
}
pub fn randombytes_uniform(upper_bound: u32) -> u32 {
unsafe { ffi::randombytes_uniform(upper_bound) }
}
new_type! {
secret Seed(SEEDBYTES);
}
pub fn randombytes_buf_deterministic(size: usize, seed: &Seed) -> Vec<u8> {
unsafe {
let mut buf = vec![0u8; size];
ffi::randombytes_buf_deterministic(buf.as_mut_ptr() as *mut _, size, seed.0.as_ptr());
buf
}
}
pub fn randombytes_buf_deterministic_into(buf: &mut [u8], seed: &Seed) {
unsafe {
ffi::randombytes_buf_deterministic(buf.as_mut_ptr() as *mut _, buf.len(), seed.0.as_ptr());
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_randombytes_uniform_0() {
::init().unwrap();
assert_eq!(randombytes_uniform(0), 0);
}
#[test]
fn test_randombytes_uniform_1() {
::init().unwrap();
assert_eq!(randombytes_uniform(1), 0);
}
#[test]
fn test_randombytes_uniform_7() {
::init().unwrap();
assert!(randombytes_uniform(7) < 7);
}
#[test]
fn test_randombytes_buf_deterministic() {
::init().unwrap();
let seed = Seed([0u8; SEEDBYTES]);
let res_1 = randombytes_buf_deterministic(10, &seed);
let res_2 = randombytes_buf_deterministic(10, &seed);
assert_eq!(res_1, res_2);
}
#[test]
fn test_randombytes_buf_deterministic_into() {
::init().unwrap();
let seed = Seed([0u8; SEEDBYTES]);
let mut buf_1 = vec![0u8; 10];
let mut buf_2 = vec![0u8; 10];
randombytes_buf_deterministic_into(buf_1.as_mut_slice(), &seed);
randombytes_buf_deterministic_into(buf_2.as_mut_slice(), &seed);
assert_eq!(buf_1, buf_2);
}
#[test]
fn test_randombytes_buf_deterministic_unique_given_seed() {
::init().unwrap();
let seed_1 = Seed([
0, 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,
]);
let seed_2 = Seed([
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,
]);
let res_1 = randombytes_buf_deterministic(1 << 10, &seed_1);
let res_2 = randombytes_buf_deterministic(1 << 10, &seed_2);
assert_ne!(res_1, res_2);
}
}