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
use solana_sdk::pubkey::Pubkey;
pub(crate) struct PubkeyBinCalculator16 {
shift_bits: u32,
}
impl PubkeyBinCalculator16 {
const fn num_bits<T>() -> usize {
std::mem::size_of::<T>() * 8
}
fn log_2(x: u32) -> u32 {
assert!(x > 0);
Self::num_bits::<u32>() as u32 - x.leading_zeros() - 1
}
pub fn new(bins: usize) -> Self {
const MAX_BITS: u32 = 16;
assert!(bins > 0);
let max_plus_1 = 1 << MAX_BITS;
assert!(bins <= max_plus_1);
assert!(bins.is_power_of_two());
let bits = Self::log_2(bins as u32);
Self {
shift_bits: MAX_BITS - bits,
}
}
pub fn bin_from_pubkey(&self, pubkey: &Pubkey) -> usize {
let as_ref = pubkey.as_ref();
((as_ref[0] as usize * 256 + as_ref[1] as usize) as usize) >> self.shift_bits
}
}
#[cfg(test)]
pub mod tests {
use super::*;
#[test]
fn test_pubkey_bins_log2() {
assert_eq!(PubkeyBinCalculator16::num_bits::<u8>(), 8);
assert_eq!(PubkeyBinCalculator16::num_bits::<u32>(), 32);
for i in 0..32 {
assert_eq!(PubkeyBinCalculator16::log_2(2u32.pow(i)), i);
}
}
#[test]
fn test_pubkey_bins() {
for i in 0..=16 {
let bins = 2u32.pow(i);
let calc = PubkeyBinCalculator16::new(bins as usize);
assert_eq!(calc.shift_bits, 16 - i, "i: {}", i);
}
}
#[test]
fn test_pubkey_bins_pubkeys() {
let mut pk = Pubkey::new(&[0; 32]);
for i in 0..=8 {
let bins = 2usize.pow(i);
let calc = PubkeyBinCalculator16::new(bins);
let shift_bits = calc.shift_bits - 8;
pk.as_mut()[0] = 0;
assert_eq!(0, calc.bin_from_pubkey(&pk));
pk.as_mut()[0] = 0xff;
assert_eq!(bins - 1, calc.bin_from_pubkey(&pk));
for bin in 0..bins {
pk.as_mut()[0] = (bin << shift_bits) as u8;
assert_eq!(
bin,
calc.bin_from_pubkey(&pk),
"bin: {}/{}, bits: {}, val: {}",
bin,
bins,
shift_bits,
pk.as_ref()[0]
);
if bin > 0 {
pk.as_mut()[0] = ((bin << shift_bits) - 1) as u8;
assert_eq!(bin - 1, calc.bin_from_pubkey(&pk));
}
}
}
for i in 9..=16 {
let mut pk = Pubkey::new(&[0; 32]);
let bins = 2usize.pow(i);
let calc = PubkeyBinCalculator16::new(bins);
let shift_bits = calc.shift_bits;
pk.as_mut()[1] = 0;
assert_eq!(0, calc.bin_from_pubkey(&pk));
pk.as_mut()[0] = 0xff;
pk.as_mut()[1] = 0xff;
assert_eq!(bins - 1, calc.bin_from_pubkey(&pk));
let mut pk = Pubkey::new(&[0; 32]);
for bin in 0..bins {
let mut target = (bin << shift_bits) as u16;
pk.as_mut()[0] = (target / 256) as u8;
pk.as_mut()[1] = (target % 256) as u8;
assert_eq!(
bin,
calc.bin_from_pubkey(&pk),
"bin: {}/{}, bits: {}, val: {}",
bin,
bins,
shift_bits,
pk.as_ref()[0]
);
if bin > 0 {
target -= 1;
pk.as_mut()[0] = (target / 256) as u8;
pk.as_mut()[1] = (target % 256) as u8;
assert_eq!(bin - 1, calc.bin_from_pubkey(&pk));
}
}
}
}
#[test]
#[should_panic(expected = "bins.is_power_of_two()")]
fn test_pubkey_bins_illegal_bins3() {
PubkeyBinCalculator16::new(3);
}
#[test]
#[should_panic(expected = "bins <= max_plus_1")]
fn test_pubkey_bins_illegal_bins2() {
PubkeyBinCalculator16::new(65537);
}
#[test]
#[should_panic(expected = "bins > 0")]
fn test_pubkey_bins_illegal_bins() {
PubkeyBinCalculator16::new(0);
}
}