2023-03-10 17:19:11 +00:00
|
|
|
// (C) 2023 Doug Hoyte. MIT license
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
const PROTOCOL_VERSION = 0x61; // Version 1
|
|
|
|
const ID_SIZE = 32;
|
|
|
|
const FINGERPRINT_SIZE = 16;
|
2023-09-15 21:44:43 +00:00
|
|
|
|
|
|
|
const Mode = {
|
|
|
|
Skip: 0,
|
|
|
|
Fingerprint: 1,
|
|
|
|
IdList: 2,
|
|
|
|
};
|
|
|
|
|
2023-05-07 13:03:07 +00:00
|
|
|
class WrappedBuffer {
|
|
|
|
constructor(buffer) {
|
2023-11-01 20:53:46 +00:00
|
|
|
this._raw = new Uint8Array(buffer || 512);
|
2023-05-07 13:03:07 +00:00
|
|
|
this.length = buffer ? buffer.length : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unwrap() {
|
2023-09-02 08:06:05 +00:00
|
|
|
return this._raw.subarray(0, this.length);
|
2023-05-07 13:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get capacity() {
|
2023-09-02 08:06:05 +00:00
|
|
|
return this._raw.byteLength;
|
2023-05-07 13:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extend(buf) {
|
2023-09-02 08:06:05 +00:00
|
|
|
if (buf._raw) buf = buf.unwrap();
|
2023-11-01 20:53:46 +00:00
|
|
|
if (typeof(buf.length) !== 'number') throw Error("bad length");
|
2023-05-07 13:03:07 +00:00
|
|
|
const targetSize = buf.length + this.length;
|
|
|
|
if (this.capacity < targetSize) {
|
|
|
|
const oldRaw = this._raw;
|
2023-09-02 08:06:05 +00:00
|
|
|
const newCapacity = Math.max(this.capacity * 2, targetSize);
|
2023-05-07 13:03:07 +00:00
|
|
|
this._raw = new Uint8Array(newCapacity);
|
|
|
|
this._raw.set(oldRaw);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._raw.set(buf, this.length);
|
|
|
|
this.length += buf.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
shift() {
|
2023-09-02 08:06:05 +00:00
|
|
|
const first = this._raw[0];
|
|
|
|
this._raw = this._raw.subarray(1);
|
|
|
|
this.length--;
|
|
|
|
return first;
|
2023-05-07 13:03:07 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
shiftN(n = 1) {
|
|
|
|
const firstSubarray = this._raw.subarray(0, n);
|
|
|
|
this._raw = this._raw.subarray(n);
|
|
|
|
this.length -= n;
|
|
|
|
return firstSubarray;
|
2023-05-07 13:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
function decodeVarInt(buf) {
|
|
|
|
let res = 0;
|
2023-05-07 13:03:07 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
while (1) {
|
|
|
|
if (buf.length === 0) throw Error("parse ends prematurely");
|
|
|
|
let byte = buf.shift();
|
|
|
|
res = (res << 7) | (byte & 127);
|
|
|
|
if ((byte & 128) === 0) break;
|
|
|
|
}
|
2023-09-02 08:06:05 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeVarInt(n) {
|
|
|
|
if (n === 0) return new WrappedBuffer([0]);
|
|
|
|
|
|
|
|
let o = [];
|
|
|
|
|
|
|
|
while (n !== 0) {
|
|
|
|
o.push(n & 127);
|
|
|
|
n >>>= 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
o.reverse();
|
|
|
|
|
|
|
|
for (let i = 0; i < o.length - 1; i++) o[i] |= 128;
|
|
|
|
|
|
|
|
return new WrappedBuffer(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getByte(buf) {
|
|
|
|
return getBytes(buf, 1)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBytes(buf, n) {
|
|
|
|
if (buf.length < n) throw Error("parse ends prematurely");
|
|
|
|
return buf.shiftN(n);
|
|
|
|
}
|
2023-09-02 08:06:05 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
|
|
|
|
class Accumulator {
|
|
|
|
constructor() {
|
|
|
|
this.setToZero();
|
2023-09-14 21:01:01 +00:00
|
|
|
|
|
|
|
if (typeof window === 'undefined') { // node.js
|
|
|
|
const crypto = require('crypto');
|
|
|
|
this.sha256 = async (slice) => new Uint8Array(crypto.createHash('sha256').update(slice).digest());
|
|
|
|
} else { // browser
|
|
|
|
this.sha256 = async (slice) => new Uint8Array(await crypto.subtle.digest("SHA-256", slice));
|
|
|
|
}
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
setToZero() {
|
|
|
|
this.buf = new Uint8Array(ID_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
add(otherBuf) {
|
|
|
|
let currCarry = 0, nextCarry = 0;
|
|
|
|
let p = new DataView(this.buf.buffer);
|
|
|
|
let po = new DataView(otherBuf.buffer);
|
|
|
|
|
|
|
|
for (let i = 0; i < 8; i++) {
|
|
|
|
let offset = i * 4;
|
|
|
|
let orig = p.getUint32(offset, true);
|
|
|
|
let otherV = po.getUint32(offset, true);
|
2023-09-02 08:06:05 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
let next = orig;
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
next += currCarry;
|
|
|
|
next += otherV;
|
|
|
|
if (next > 0xFFFFFFFF) nextCarry = 1;
|
|
|
|
|
|
|
|
p.setUint32(offset, next & 0xFFFFFFFF, true);
|
|
|
|
currCarry = nextCarry;
|
|
|
|
nextCarry = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
negate() {
|
|
|
|
let p = new DataView(this.buf.buffer);
|
|
|
|
|
|
|
|
for (let i = 0; i < 8; i++) {
|
|
|
|
let offset = i * 4;
|
|
|
|
p.setUint32(offset, ~p.getUint32(offset, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
let one = new Uint8Array(ID_SIZE);
|
|
|
|
one[0] = 1;
|
|
|
|
this.add(one);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getFingerprint(n) {
|
|
|
|
let input = new WrappedBuffer();
|
|
|
|
input.extend(this.buf);
|
|
|
|
input.extend(encodeVarInt(n));
|
|
|
|
|
|
|
|
let hash = await this.sha256(input.unwrap());
|
|
|
|
|
|
|
|
return hash.subarray(0, FINGERPRINT_SIZE);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class NegentropyStorageVector {
|
|
|
|
constructor() {
|
|
|
|
this.items = [];
|
|
|
|
this.sealed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
insert(timestamp, id) {
|
|
|
|
if (this.sealed) throw Error("already sealed");
|
|
|
|
id = loadInputBuffer(id);
|
|
|
|
if (id.byteLength !== ID_SIZE) throw Error("bad id size for added item");
|
|
|
|
this.items.push({ timestamp, id });
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
seal() {
|
|
|
|
if (this.sealed) throw Error("already sealed");
|
|
|
|
this.sealed = true;
|
2023-09-14 21:01:01 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
this.items.sort(itemCompare);
|
2023-09-14 21:01:01 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
for (let i = 1; i < this.items.length; i++) {
|
|
|
|
if (itemCompare(this.items[i - 1], this.items[i]) === 0) throw Error("duplicate item inserted");
|
2023-09-14 21:01:01 +00:00
|
|
|
}
|
2023-11-01 20:53:46 +00:00
|
|
|
}
|
2023-09-14 21:01:01 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
unseal() {
|
|
|
|
this.sealed = false;
|
|
|
|
}
|
2023-09-14 21:01:01 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
size() {
|
|
|
|
this._checkSealed();
|
|
|
|
return this.items.length;
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
getItem(i) {
|
|
|
|
this._checkSealed();
|
|
|
|
if (i >= this.items.length) throw Error("out of range");
|
|
|
|
return this.items[i];
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
iterate(begin, end, cb) {
|
|
|
|
this._checkSealed();
|
|
|
|
this._checkBounds(begin, end);
|
|
|
|
|
|
|
|
for (let i = begin; i < end; ++i) {
|
|
|
|
if (!cb(this.items[i], i)) break;
|
|
|
|
}
|
2023-05-07 13:03:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
findLowerBound(begin, end, bound) {
|
|
|
|
this._checkSealed();
|
|
|
|
this._checkBounds(begin, end);
|
|
|
|
|
|
|
|
return this._binarySearch(this.items, begin, end, (a) => itemCompare(a, bound) < 0);
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
async fingerprint(begin, end) {
|
|
|
|
let out = new Accumulator();
|
|
|
|
out.setToZero();
|
|
|
|
|
|
|
|
this.iterate(begin, end, (item, i) => {
|
|
|
|
out.add(item.id);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return await out.getFingerprint(end - begin);
|
2023-09-02 08:06:05 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
_checkSealed() {
|
|
|
|
if (!this.sealed) throw Error("not sealed");
|
2023-09-14 21:01:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
_checkBounds(begin, end) {
|
|
|
|
if (begin > end || end > this.items.length) throw Error("bad range");
|
2023-09-14 21:01:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
_binarySearch(arr, first, last, cmp) {
|
|
|
|
let count = last - first;
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
let it = first;
|
|
|
|
let step = Math.floor(count / 2);
|
|
|
|
it += step;
|
|
|
|
|
|
|
|
if (cmp(arr[it])) {
|
|
|
|
first = ++it;
|
|
|
|
count -= step + 1;
|
|
|
|
} else {
|
|
|
|
count = step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return first;
|
2023-09-14 21:01:01 +00:00
|
|
|
}
|
2023-11-01 20:53:46 +00:00
|
|
|
}
|
2023-09-14 21:01:01 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
|
|
|
|
class Negentropy {
|
|
|
|
constructor(storage, frameSizeLimit = 0) {
|
|
|
|
if (frameSizeLimit !== 0 && frameSizeLimit < 4096) throw Error("frameSizeLimit too small");
|
|
|
|
|
|
|
|
this.storage = storage;
|
|
|
|
this.frameSizeLimit = frameSizeLimit;
|
|
|
|
|
|
|
|
this.lastTimestampIn = 0;
|
|
|
|
this.lastTimestampOut = 0;
|
2023-09-14 21:01:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
_bound(timestamp, id) {
|
|
|
|
return { timestamp, id: id ? id : new Uint8Array(0) };
|
2023-09-14 21:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async initiate() {
|
2023-11-01 20:53:46 +00:00
|
|
|
if (this.isInitiator) throw Error("already initiated");
|
2023-03-10 17:19:11 +00:00
|
|
|
this.isInitiator = true;
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
let output = new WrappedBuffer();
|
|
|
|
output.extend([ PROTOCOL_VERSION ]);
|
|
|
|
|
|
|
|
await this.splitRange(0, this.storage.size(), this._bound(Number.MAX_VALUE), output);
|
|
|
|
|
|
|
|
return this._renderOutput(output);
|
|
|
|
}
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
setInitiator() {
|
|
|
|
this.isInitiator = true;
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 21:01:01 +00:00
|
|
|
async reconcile(query) {
|
2023-03-10 17:19:11 +00:00
|
|
|
let haveIds = [], needIds = [];
|
2023-11-01 20:53:46 +00:00
|
|
|
query = new WrappedBuffer(loadInputBuffer(query));
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
this.lastTimestampIn = this.lastTimestampOut = 0; // reset for each message
|
2023-09-02 08:06:05 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
let fullOutput = new WrappedBuffer();
|
|
|
|
fullOutput.extend([ PROTOCOL_VERSION ]);
|
|
|
|
|
|
|
|
let protocolVersion = getByte(query);
|
|
|
|
if (protocolVersion < 0x60 || protocolVersion > 0x6F) throw Error("invalid negentropy protocol version byte");
|
|
|
|
if (protocolVersion !== PROTOCOL_VERSION) {
|
|
|
|
if (this.isInitiator) throw Error("unsupported negentropy protocol version requested: " + (protocolVersion - 0x60));
|
|
|
|
else return [this._renderOutput(fullOutput), haveIds, needIds];
|
2023-09-15 21:44:43 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
let storageSize = this.storage.size();
|
|
|
|
let prevBound = this._bound(0);
|
|
|
|
let prevIndex = 0;
|
|
|
|
let skip = false;
|
|
|
|
|
2023-03-10 17:19:11 +00:00
|
|
|
while (query.length !== 0) {
|
2023-11-01 20:53:46 +00:00
|
|
|
let o = new WrappedBuffer();
|
|
|
|
|
|
|
|
let doSkip = () => {
|
|
|
|
if (skip) {
|
|
|
|
skip = false;
|
|
|
|
o.extend(this.encodeBound(prevBound));
|
|
|
|
o.extend(encodeVarInt(Mode.Skip));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let currBound = this.decodeBound(query);
|
|
|
|
let mode = decodeVarInt(query);
|
2023-03-10 17:19:11 +00:00
|
|
|
|
|
|
|
let lower = prevIndex;
|
2023-11-01 20:53:46 +00:00
|
|
|
let upper = this.storage.findLowerBound(prevIndex, storageSize, currBound);
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-09-15 21:44:43 +00:00
|
|
|
if (mode === Mode.Skip) {
|
2023-11-01 20:53:46 +00:00
|
|
|
skip = true;
|
2023-09-15 21:44:43 +00:00
|
|
|
} else if (mode === Mode.Fingerprint) {
|
2023-11-01 20:53:46 +00:00
|
|
|
let theirFingerprint = getBytes(query, FINGERPRINT_SIZE);
|
|
|
|
let ourFingerprint = await this.storage.fingerprint(lower, upper);
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-09-14 21:01:01 +00:00
|
|
|
if (compareUint8Array(theirFingerprint, ourFingerprint) !== 0) {
|
2023-11-01 20:53:46 +00:00
|
|
|
doSkip();
|
|
|
|
await this.splitRange(lower, upper, currBound, o);
|
|
|
|
} else {
|
|
|
|
skip = true;
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
2023-09-15 21:44:43 +00:00
|
|
|
} else if (mode === Mode.IdList) {
|
2023-11-01 20:53:46 +00:00
|
|
|
let numIds = decodeVarInt(query);
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
let theirElems = {}; // stringified Uint8Array -> original Uint8Array (or hex)
|
2023-09-02 08:06:05 +00:00
|
|
|
for (let i = 0; i < numIds; i++) {
|
2023-11-01 20:53:46 +00:00
|
|
|
let e = getBytes(query, ID_SIZE);
|
2023-09-02 08:06:05 +00:00
|
|
|
theirElems[e] = e;
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
this.storage.iterate(lower, upper, (item) => {
|
|
|
|
let k = item.id;
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
if (!theirElems[k]) {
|
2023-03-10 17:19:11 +00:00
|
|
|
// ID exists on our side, but not their side
|
2023-09-02 08:06:05 +00:00
|
|
|
if (this.isInitiator) haveIds.push(this.wantUint8ArrayOutput ? k : uint8ArrayToHex(k));
|
2023-03-10 17:19:11 +00:00
|
|
|
} else {
|
|
|
|
// ID exists on both sides
|
2023-09-02 08:06:05 +00:00
|
|
|
delete theirElems[k];
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
2023-11-01 20:53:46 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
if (this.isInitiator) {
|
2023-11-01 20:53:46 +00:00
|
|
|
skip = true;
|
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
for (let v of Object.values(theirElems)) {
|
2023-11-01 20:53:46 +00:00
|
|
|
// ID exists on their side, but not our side
|
2023-09-02 08:06:05 +00:00
|
|
|
needIds.push(this.wantUint8ArrayOutput ? v : uint8ArrayToHex(v));
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
2023-09-02 08:06:05 +00:00
|
|
|
} else {
|
2023-11-01 20:53:46 +00:00
|
|
|
doSkip();
|
|
|
|
|
|
|
|
let responseIds = new WrappedBuffer();
|
|
|
|
let numResponseIds = 0;
|
|
|
|
let endBound = currBound;
|
|
|
|
|
|
|
|
this.storage.iterate(lower, upper, (item, index) => {
|
|
|
|
if (this.exceededFrameSizeLimit(fullOutput.length + responseIds.length)) {
|
|
|
|
endBound = item;
|
|
|
|
upper = index; // shrink upper so that remaining range gets correct fingerprint
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
responseIds.extend(item.id);
|
|
|
|
numResponseIds++;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
o.extend(this.encodeBound(endBound));
|
|
|
|
o.extend(encodeVarInt(Mode.IdList));
|
|
|
|
o.extend(encodeVarInt(numResponseIds));
|
|
|
|
o.extend(responseIds);
|
|
|
|
|
|
|
|
fullOutput.extend(o);
|
|
|
|
o = new WrappedBuffer();
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw Error("unexpected mode");
|
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
if (this.exceededFrameSizeLimit(fullOutput.length + o.length)) {
|
|
|
|
// frameSizeLimit exceeded: Stop range processing and return a fingerprint for the remaining range
|
|
|
|
let remainingFingerprint = await this.storage.fingerprint(upper, storageSize);
|
|
|
|
|
|
|
|
fullOutput.extend(this.encodeBound(this._bound(Number.MAX_VALUE)));
|
|
|
|
fullOutput.extend(encodeVarInt(Mode.Fingerprint));
|
|
|
|
fullOutput.extend(remainingFingerprint);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
fullOutput.extend(o);
|
|
|
|
}
|
|
|
|
|
2023-03-10 17:19:11 +00:00
|
|
|
prevIndex = upper;
|
|
|
|
prevBound = currBound;
|
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
return [fullOutput.length === 1 && this.isInitiator ? null : this._renderOutput(fullOutput), haveIds, needIds];
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
async splitRange(lower, upper, upperBound, o) {
|
2023-03-10 17:19:11 +00:00
|
|
|
let numElems = upper - lower;
|
|
|
|
let buckets = 16;
|
|
|
|
|
|
|
|
if (numElems < buckets * 2) {
|
2023-11-01 20:53:46 +00:00
|
|
|
o.extend(this.encodeBound(upperBound));
|
|
|
|
o.extend(encodeVarInt(Mode.IdList));
|
|
|
|
|
|
|
|
o.extend(encodeVarInt(numElems));
|
|
|
|
this.storage.iterate(lower, upper, (item) => {
|
|
|
|
o.extend(item.id);
|
|
|
|
return true;
|
2023-09-14 21:01:01 +00:00
|
|
|
});
|
2023-03-10 17:19:11 +00:00
|
|
|
} else {
|
|
|
|
let itemsPerBucket = Math.floor(numElems / buckets);
|
|
|
|
let bucketsWithExtra = numElems % buckets;
|
|
|
|
let curr = lower;
|
|
|
|
|
|
|
|
for (let i = 0; i < buckets; i++) {
|
2023-09-14 21:01:01 +00:00
|
|
|
let bucketSize = itemsPerBucket + (i < bucketsWithExtra ? 1 : 0);
|
2023-11-01 20:53:46 +00:00
|
|
|
let ourFingerprint = await this.storage.fingerprint(curr, curr + bucketSize);
|
2023-09-14 21:01:01 +00:00
|
|
|
curr += bucketSize;
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
let nextBound;
|
2023-09-02 08:06:05 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
if (curr === upper) {
|
|
|
|
nextBound = upperBound;
|
|
|
|
} else {
|
|
|
|
let prevItem, currItem;
|
2023-09-02 08:06:05 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
this.storage.iterate(curr - 1, curr + 1, (item, index) => {
|
|
|
|
if (index === curr - 1) prevItem = item;
|
|
|
|
else currItem = item;
|
|
|
|
return true;
|
|
|
|
});
|
2023-09-02 08:06:05 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
nextBound = this.getMinimalBound(prevItem, currItem);
|
|
|
|
}
|
2023-09-02 08:06:05 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
o.extend(this.encodeBound(nextBound));
|
|
|
|
o.extend(encodeVarInt(Mode.Fingerprint));
|
|
|
|
o.extend(ourFingerprint);
|
2023-09-02 08:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
_renderOutput(o) {
|
|
|
|
o = o.unwrap();
|
|
|
|
if (!this.wantUint8ArrayOutput) o = uint8ArrayToHex(o);
|
|
|
|
return o;
|
2023-09-14 21:01:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
exceededFrameSizeLimit(n) {
|
|
|
|
return this.frameSizeLimit && n > this.frameSizeLimit - 200;
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
// Decoding
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
decodeTimestampIn(encoded) {
|
|
|
|
let timestamp = decodeVarInt(encoded);
|
2023-03-10 17:19:11 +00:00
|
|
|
timestamp = timestamp === 0 ? Number.MAX_VALUE : timestamp - 1;
|
2023-11-01 20:53:46 +00:00
|
|
|
if (this.lastTimestampIn === Number.MAX_VALUE || timestamp === Number.MAX_VALUE) {
|
|
|
|
this.lastTimestampIn = Number.MAX_VALUE;
|
2023-03-10 17:19:11 +00:00
|
|
|
return Number.MAX_VALUE;
|
|
|
|
}
|
2023-11-01 20:53:46 +00:00
|
|
|
timestamp += this.lastTimestampIn;
|
|
|
|
this.lastTimestampIn = timestamp;
|
2023-03-10 17:19:11 +00:00
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
decodeBound(encoded) {
|
|
|
|
let timestamp = this.decodeTimestampIn(encoded);
|
|
|
|
let len = decodeVarInt(encoded);
|
|
|
|
if (len > ID_SIZE) throw Error("bound key too long");
|
|
|
|
let id = getBytes(encoded, len);
|
2023-05-07 13:03:07 +00:00
|
|
|
return { timestamp, id };
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encoding
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
encodeTimestampOut(timestamp) {
|
2023-03-10 17:19:11 +00:00
|
|
|
if (timestamp === Number.MAX_VALUE) {
|
2023-11-01 20:53:46 +00:00
|
|
|
this.lastTimestampOut = Number.MAX_VALUE;
|
|
|
|
return encodeVarInt(0);
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let temp = timestamp;
|
2023-11-01 20:53:46 +00:00
|
|
|
timestamp -= this.lastTimestampOut;
|
|
|
|
this.lastTimestampOut = temp;
|
|
|
|
return encodeVarInt(timestamp + 1);
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
encodeBound(key) {
|
2023-09-02 08:06:05 +00:00
|
|
|
let output = new WrappedBuffer();
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
output.extend(this.encodeTimestampOut(key.timestamp));
|
|
|
|
output.extend(encodeVarInt(key.id.length));
|
2023-05-07 13:03:07 +00:00
|
|
|
output.extend(key.id);
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
return output;
|
|
|
|
}
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
getMinimalBound(prev, curr) {
|
2023-03-10 17:19:11 +00:00
|
|
|
if (curr.timestamp !== prev.timestamp) {
|
2023-11-01 20:53:46 +00:00
|
|
|
return this._bound(curr.timestamp);
|
2023-03-10 17:19:11 +00:00
|
|
|
} else {
|
|
|
|
let sharedPrefixBytes = 0;
|
2023-11-01 20:53:46 +00:00
|
|
|
let currKey = curr.id;
|
|
|
|
let prevKey = prev.id;
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
for (let i = 0; i < ID_SIZE; i++) {
|
|
|
|
if (currKey[i] !== prevKey[i]) break;
|
2023-03-10 17:19:11 +00:00
|
|
|
sharedPrefixBytes++;
|
|
|
|
}
|
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
return this._bound(curr.timestamp, curr.id.subarray(0, sharedPrefixBytes + 1));
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
};
|
2023-09-02 08:06:05 +00:00
|
|
|
}
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
function loadInputBuffer(inp) {
|
|
|
|
if (typeof(inp) === 'string') inp = hexToUint8Array(inp);
|
|
|
|
else if (__proto__ !== Uint8Array.prototype) inp = new Uint8Array(inp); // node Buffer?
|
|
|
|
return inp;
|
|
|
|
}
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
function hexToUint8Array(h) {
|
|
|
|
if (h.startsWith('0x')) h = h.substr(2);
|
|
|
|
if (h.length % 2 === 1) throw Error("odd length of hex string");
|
|
|
|
let arr = new Uint8Array(h.length / 2);
|
|
|
|
for (let i = 0; i < arr.length; i++) arr[i] = parseInt(h.substr(i * 2, 2), 16);
|
|
|
|
return arr;
|
|
|
|
}
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
const uint8ArrayToHexLookupTable = new Array(256);
|
|
|
|
{
|
|
|
|
const hexAlphabet = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
|
|
|
|
for (let i = 0; i < 256; i++) {
|
|
|
|
uint8ArrayToHexLookupTable[i] = hexAlphabet[(i >>> 4) & 0xF] + hexAlphabet[i & 0xF];
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
2023-09-02 08:06:05 +00:00
|
|
|
}
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
function uint8ArrayToHex(arr) {
|
|
|
|
let out = '';
|
|
|
|
for (let i = 0, edx = arr.length; i < edx; i++) {
|
|
|
|
out += uint8ArrayToHexLookupTable[arr[i]];
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
2023-09-02 08:06:05 +00:00
|
|
|
return out;
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
function compareUint8Array(a, b) {
|
|
|
|
for (let i = 0; i < a.byteLength; i++) {
|
|
|
|
if (a[i] < b[i]) return -1;
|
|
|
|
if (a[i] > b[i]) return 1;
|
|
|
|
}
|
2023-03-10 17:19:11 +00:00
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
if (a.byteLength > b.byteLength) return 1;
|
|
|
|
if (a.byteLength < b.byteLength) return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-03-10 17:19:11 +00:00
|
|
|
|
|
|
|
function itemCompare(a, b) {
|
|
|
|
if (a.timestamp === b.timestamp) {
|
2023-09-02 08:06:05 +00:00
|
|
|
return compareUint8Array(a.id, b.id);
|
2023-03-10 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return a.timestamp - b.timestamp;
|
|
|
|
}
|
|
|
|
|
2023-09-02 08:06:05 +00:00
|
|
|
|
2023-11-01 20:53:46 +00:00
|
|
|
module.exports = { Negentropy, NegentropyStorageVector, };
|