Skip to content

Commit a64d07a

Browse files
authored
Merge pull request #53 from rustaceanrob/11-11-no-wrapping
accumulator: Change to multiple rotating field elm
2 parents b7e09d0 + 1f608c9 commit a64d07a

File tree

1 file changed

+7
-27
lines changed

1 file changed

+7
-27
lines changed

accumulator/src/lib.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,14 @@ impl Accumulator {
5454
// ref: https://github.com/rust-bitcoin/rust-bitcoin/blob/7bbb9085c63dc69e9da16ec9c11c698d6236c95c/bitcoin/src/pow.rs#L658
5555
pub fn add(&mut self, outpoint: OutPoint) {
5656
let hash = hash_outpoint(outpoint);
57-
let (big, little) = Self::create_rhs(hash);
58-
let (high, low) = Self::add_internal(self.high, self.low, big, little);
59-
*self = Self { high, low };
57+
self.add_hashed_outpoint(hash);
6058
}
6159

6260
/// Add a pre-hashed outpoint to the accumulator.
6361
pub fn add_hashed_outpoint(&mut self, hash: [u8; 32]) {
6462
let (big, little) = Self::create_rhs(hash);
65-
let (high, low) = Self::add_internal(self.high, self.low, big, little);
63+
let high = self.high.wrapping_add(big);
64+
let low = self.low.wrapping_add(little);
6665
*self = Self { high, low };
6766
}
6867

@@ -77,36 +76,17 @@ impl Accumulator {
7776
/// Spend the inputs in a block by subtracing them from the accumulator.
7877
pub fn spend(&mut self, outpoint: OutPoint) {
7978
let hash = hash_outpoint(outpoint);
80-
let (high, low) = Self::create_rhs(hash);
81-
let high_inv = !high;
82-
let low_inv = !low;
83-
let (high, low) = Self::add_internal(self.high, self.low, high_inv, low_inv);
84-
let (high, low) = Self::add_internal(high, low, 0, 1);
85-
*self = Self { high, low }
79+
self.spend_hashed_outpoint(hash);
8680
}
8781

8882
/// Spend a pre-hashed outpoint from the accumulator.
8983
pub fn spend_hashed_outpoint(&mut self, hash: [u8; 32]) {
90-
let (high, low) = Self::create_rhs(hash);
91-
let high_inv = !high;
92-
let low_inv = !low;
93-
let (high, low) = Self::add_internal(self.high, self.low, high_inv, low_inv);
94-
let (high, low) = Self::add_internal(high, low, 0, 1);
84+
let (big, little) = Self::create_rhs(hash);
85+
let high = self.high.wrapping_sub(big);
86+
let low = self.low.wrapping_sub(little);
9587
*self = Self { high, low }
9688
}
9789

98-
// Add LHS to RHS, wrapping around if necessary
99-
fn add_internal(lhs_high: u128, lhs_low: u128, rhs_high: u128, rhs_low: u128) -> (u128, u128) {
100-
let high = lhs_high.wrapping_add(rhs_high);
101-
let mut ret_high = high;
102-
let (low, low_bits_overflow) = lhs_low.overflowing_add(rhs_low);
103-
if low_bits_overflow {
104-
// Carry
105-
ret_high = ret_high.wrapping_add(1);
106-
}
107-
(ret_high, low)
108-
}
109-
11090
fn create_rhs(hash: [u8; 32]) -> (u128, u128) {
11191
let (high, low) = split_in_half(hash);
11292
let big = u128::from_be_bytes(high);

0 commit comments

Comments
 (0)