Skip to content

Commit e7df4de

Browse files
authored
Merge pull request #2 from Snowfork/fix-overflow
Fix overflow for build targets with 32-bit pointers
2 parents f8d72b1 + bd008d6 commit e7df4de

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

src/dag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct LightDAG<P: Patch> {
1616
cache: Vec<u8>,
1717
#[allow(dead_code)]
1818
cache_size: usize,
19-
full_size: usize,
19+
full_size: u64,
2020
_marker: PhantomData<P>
2121
}
2222

src/lib.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ use byteorder::{LittleEndian, ByteOrder};
1818
use rlp::Encodable;
1919
use core::ops::BitXor;
2020

21-
pub const DATASET_BYTES_INIT: usize = 1073741824; // 2 to the power of 30.
22-
pub const DATASET_BYTES_GROWTH: usize = 8388608; // 2 to the power of 23.
23-
pub const CACHE_BYTES_INIT: usize = 16777216; // 2 to the power of 24.
24-
pub const CACHE_BYTES_GROWTH: usize = 131072; // 2 to the power of 17.
21+
pub const DATASET_BYTES_INIT: u64 = 1073741824; // 2 to the power of 30.
22+
pub const DATASET_BYTES_GROWTH: u64 = 8388608; // 2 to the power of 23.
23+
pub const CACHE_BYTES_INIT: u64 = 16777216; // 2 to the power of 24.
24+
pub const CACHE_BYTES_GROWTH: u64 = 131072; // 2 to the power of 17.
2525
pub const CACHE_MULTIPLIER: usize = 1024;
2626
pub const MIX_BYTES: usize = 128;
2727
pub const WORD_BYTES: usize = 4;
@@ -32,20 +32,22 @@ pub const ACCESSES: usize = 64;
3232

3333
/// Get the cache size required given the block number.
3434
pub fn get_cache_size(epoch: usize) -> usize {
35-
let mut sz = CACHE_BYTES_INIT + CACHE_BYTES_GROWTH * epoch;
36-
sz -= HASH_BYTES;
37-
while !is_prime(sz / HASH_BYTES) {
38-
sz -= 2 * HASH_BYTES;
35+
let mut sz = CACHE_BYTES_INIT + CACHE_BYTES_GROWTH * (epoch as u64);
36+
let hash_bytes_64 = HASH_BYTES as u64;
37+
sz -= hash_bytes_64;
38+
while !is_prime(sz / hash_bytes_64) {
39+
sz -= 2 * hash_bytes_64;
3940
}
40-
sz
41+
sz as usize
4142
}
4243

4344
/// Get the full dataset size given the block number.
44-
pub fn get_full_size(epoch: usize) -> usize {
45-
let mut sz = DATASET_BYTES_INIT + DATASET_BYTES_GROWTH * epoch;
46-
sz -= MIX_BYTES;
47-
while !is_prime(sz / MIX_BYTES) {
48-
sz -= 2 * MIX_BYTES
45+
pub fn get_full_size(epoch: usize) -> u64 {
46+
let mut sz = DATASET_BYTES_INIT + DATASET_BYTES_GROWTH * (epoch as u64);
47+
let mix_bytes_64 = MIX_BYTES as u64;
48+
sz -= mix_bytes_64;
49+
while !is_prime(sz / mix_bytes_64) {
50+
sz -= 2 * mix_bytes_64
4951
}
5052
sz
5153
}
@@ -179,7 +181,7 @@ pub fn make_dataset(dataset: &mut [u8], cache: &[u8]) {
179181
/// "Main" function of Ethash, calculating the mix digest and result given the
180182
/// header and nonce.
181183
pub fn hashimoto<F: Fn(usize) -> H512>(
182-
header_hash: H256, nonce: H64, full_size: usize, lookup: F
184+
header_hash: H256, nonce: H64, full_size: u64, lookup: F
183185
) -> (H256, H256) {
184186
hashimoto_with_hasher(
185187
header_hash,
@@ -204,11 +206,12 @@ pub fn hashimoto<F: Fn(usize) -> H512>(
204206
}
205207

206208
pub fn hashimoto_with_hasher<F: Fn(usize) -> H512, HF256: Fn(&[u8]) -> [u8; 32], HF512: Fn(&[u8]) -> [u8; 64]>(
207-
header_hash: H256, nonce: H64, full_size: usize, lookup: F, hasher256: HF256, hasher512: HF512
209+
header_hash: H256, nonce: H64, full_size: u64, lookup: F, hasher256: HF256, hasher512: HF512
208210
) -> (H256, H256) {
209-
let n = full_size / HASH_BYTES;
211+
let n = full_size / (HASH_BYTES as u64);
210212
let w = MIX_BYTES / WORD_BYTES;
211213
const MIXHASHES: usize = MIX_BYTES / HASH_BYTES;
214+
const MIXHASHES_64: u64 = MIXHASHES as u64;
212215
let s = {
213216
let mut data = [0u8; 40];
214217
data[..32].copy_from_slice(&header_hash.0);
@@ -224,9 +227,9 @@ pub fn hashimoto_with_hasher<F: Fn(usize) -> H512, HF256: Fn(&[u8]) -> [u8; 32],
224227
}
225228

226229
for i in 0..ACCESSES {
227-
let p = (fnv((i as u32).bitxor(LittleEndian::read_u32(s.as_ref())),
230+
let p = ((fnv((i as u32).bitxor(LittleEndian::read_u32(s.as_ref())),
228231
LittleEndian::read_u32(&mix[(i % w * 4)..]))
229-
as usize) % (n / MIXHASHES) * MIXHASHES;
232+
as u64) % (n / MIXHASHES_64) * MIXHASHES_64) as usize;
230233
let mut newdata = [0u8; MIX_BYTES];
231234
for j in 0..MIXHASHES {
232235
let v = lookup(p + j);
@@ -258,7 +261,7 @@ pub fn hashimoto_with_hasher<F: Fn(usize) -> H512, HF256: Fn(&[u8]) -> [u8; 32],
258261
/// Ethash used by a light client. Only stores the 16MB cache rather than the
259262
/// full dataset.
260263
pub fn hashimoto_light(
261-
header_hash: H256, nonce: H64, full_size: usize, cache: &[u8]
264+
header_hash: H256, nonce: H64, full_size: u64, cache: &[u8]
262265
) -> (H256, H256) {
263266
hashimoto(header_hash, nonce, full_size, |i| {
264267
calc_dataset_item(cache, i)
@@ -267,7 +270,7 @@ pub fn hashimoto_light(
267270

268271
/// Ethash used by a full client. Stores the whole dataset in memory.
269272
pub fn hashimoto_full(
270-
header_hash: H256, nonce: H64, full_size: usize, dataset: &[u8]
273+
header_hash: H256, nonce: H64, full_size: u64, dataset: &[u8]
271274
) -> (H256, H256) {
272275
hashimoto(header_hash, nonce, full_size, |i| {
273276
let mut r = [0u8; 64];
@@ -290,7 +293,7 @@ pub fn cross_boundary(val: U256) -> U256 {
290293
/// Mine a nonce given the header, dataset, and the target. Target is derived
291294
/// from the difficulty.
292295
pub fn mine<T: Encodable>(
293-
header: &T, full_size: usize, dataset: &[u8], nonce_start: H64, difficulty: U256
296+
header: &T, full_size: u64, dataset: &[u8], nonce_start: H64, difficulty: U256
294297
) -> (H64, H256) {
295298
let target = cross_boundary(difficulty);
296299
let header = rlp::encode(header).to_vec();

src/miller_rabin.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn mod_exp(mut x: u64, mut d: u64, n: u64) -> u64 {
8080
ret
8181
}
8282

83-
pub fn is_prime(n: usize) -> bool {
83+
pub fn is_prime(n: u64) -> bool {
8484
const HINT: &'static [u64] = &[2];
8585

8686
// we have a strict upper bound, so we can just use the witness
@@ -108,23 +108,23 @@ pub fn is_prime(n: usize) -> bool {
108108
while d % 2 == 0 { d /= 2; s += 1 }
109109

110110
let witnesses =
111-
WITNESSES.iter().find(|&&(hi, _)| hi >= n as u64)
111+
WITNESSES.iter().find(|&&(hi, _)| hi >= n)
112112
.map(|&(_, wtnss)| wtnss).unwrap();
113113
'next_witness: for &a in witnesses.iter() {
114-
let mut power = mod_exp(a, d as u64, n as u64);
115-
assert!(power < n as u64);
116-
if power == 1 || power == n as u64 - 1 { continue 'next_witness }
114+
let mut power = mod_exp(a, d, n);
115+
assert!(power < n);
116+
if power == 1 || power == n - 1 { continue 'next_witness }
117117

118118
for _r in 0..s {
119-
power = mod_sqr(power, n as u64);
120-
assert!(power < n as u64);
119+
power = mod_sqr(power, n);
120+
assert!(power < n);
121121
if power == 1 { return false }
122-
if power == n as u64 - 1 {
122+
if power == n - 1 {
123123
continue 'next_witness
124124
}
125125
}
126126
return false
127127
}
128128

129129
true
130-
}
130+
}

0 commit comments

Comments
 (0)