Fixing overflow during multiplication (#1381)

This commit is contained in:
Tomasz Drwięga 2016-06-22 15:37:41 +02:00 committed by Gav Wood
parent 353b9e91e6
commit bc1f603e0c

View File

@ -425,12 +425,16 @@ macro_rules! uint_overflowing_mul_reg {
let (c_u, overflow_u) = mul_u32(a, b_u, c_l >> 32); let (c_u, overflow_u) = mul_u32(a, b_u, c_l >> 32);
ret[i + j] = (c_l & 0xFFFFFFFF) + (c_u << 32); ret[i + j] = (c_l & 0xFFFFFFFF) + (c_u << 32);
// Only single overflow possible here // No overflow here
let carry = (c_u >> 32) + (overflow_u << 32) + overflow_l + carry2; let res = (c_u >> 32) + (overflow_u << 32);
let (carry, o) = carry.overflowing_add(ret[i + j + 1]); // possible overflows
let (res, o1) = res.overflowing_add(overflow_l);
let (res, o2) = res.overflowing_add(carry2);
let (res, o3) = res.overflowing_add(ret[i + j + 1]);
ret[i + j + 1] = res;
ret[i + j + 1] = carry; // Only single overflow possible there
carry2 = o as u64; carry2 = (o1 | o2 | o3) as u64;
} }
} }
@ -1305,12 +1309,16 @@ impl U256 {
let (c_u, overflow_u) = mul_u32(a, b_u, c_l >> 32); let (c_u, overflow_u) = mul_u32(a, b_u, c_l >> 32);
ret[i + j] = (c_l & 0xFFFFFFFF) + (c_u << 32); ret[i + j] = (c_l & 0xFFFFFFFF) + (c_u << 32);
// Only single overflow possible here // No overflow here
let carry = (c_u >> 32) + (overflow_u << 32) + overflow_l + carry2; let res = (c_u >> 32) + (overflow_u << 32);
let (carry, o) = carry.overflowing_add(ret[i + j + 1]); // possible overflows
let (res, o1) = res.overflowing_add(overflow_l);
let (res, o2) = res.overflowing_add(carry2);
let (res, o3) = res.overflowing_add(ret[i + j + 1]);
ret[i + j + 1] = res;
ret[i + j + 1] = carry; // Only single overflow possible there
carry2 = o as u64; carry2 = (o1 | o2 | o3) as u64;
} }
} }