Skip to content

Commit e1986b4

Browse files
committed
Add SwiftSync aggregate class
1 parent a7e8067 commit e1986b4

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL
260260
rpc/txoutproof.cpp
261261
script/sigcache.cpp
262262
signet.cpp
263+
swiftsync.cpp
263264
torcontrol.cpp
264265
txdb.cpp
265266
txgraph.cpp

src/swiftsync.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "primitives/transaction.h"
2+
#include <array>
3+
#include <cstddef>
4+
#include <random.h>
5+
#include <swiftsync.h>
6+
7+
Aggregate::Aggregate() : limb0(0), limb1(0), limb2(0), limb3(0)
8+
{
9+
std::array<std::byte, 32> salt;
10+
FastRandomContext{}.fillrand(salt);
11+
HashWriter m_salted_hasher;
12+
m_salted_hasher.write(salt);
13+
}
14+
15+
void Aggregate::Add(const COutPoint& outpoint)
16+
{
17+
auto hash = (HashWriter(m_salted_hasher) << outpoint).GetSHA256();
18+
auto a0 = hash.GetUint64(0);
19+
auto a1 = hash.GetUint64(1);
20+
auto a2 = hash.GetUint64(2);
21+
auto a3 = hash.GetUint64(3);
22+
limb0 += a0;
23+
limb1 += a1;
24+
limb2 += a2;
25+
limb3 += a3;
26+
}
27+
28+
void Aggregate::Spend(const COutPoint& outpoint)
29+
{
30+
auto hash = (HashWriter(m_salted_hasher) << outpoint).GetSHA256();
31+
auto a0 = hash.GetUint64(0);
32+
auto a1 = hash.GetUint64(1);
33+
auto a2 = hash.GetUint64(2);
34+
auto a3 = hash.GetUint64(3);
35+
limb0 -= a0;
36+
limb1 -= a1;
37+
limb2 -= a2;
38+
limb3 -= a3;
39+
}

src/swiftsync.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "primitives/transaction.h"
2+
#include <hash.h>
3+
#include <cstdint>
4+
5+
/** An aggregate for the SwiftSync protocol.
6+
*This class is intentionally left opaque, as internal changes may occur,
7+
* but all aggregates will have the concept of "adding" and "spending" an
8+
* outpoint.
9+
*
10+
* The current implementation uses a salted SHA-256 hash, and updates 4
11+
* independent 32-bit limbs by dividing the hash into 4 parts and adding
12+
* or subtracting accordingly.
13+
* */
14+
class Aggregate
15+
{
16+
private:
17+
uint64_t limb0, limb1, limb2, limb3;
18+
HashWriter m_salted_hasher;
19+
public:
20+
Aggregate();
21+
bool IsZero() const { return limb0 == 0 && limb1 == 0 && limb2 == 0 && limb3 == 0; }
22+
void Add(const COutPoint& outpoint);
23+
void Spend(const COutPoint& outpoint);
24+
};

0 commit comments

Comments
 (0)