Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v7): seq_hi shift for byte 6 #775

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/v7.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function v7(options, buf, offset) {
b[i++] = _msecs & 0xff;

// [byte 6] - set 4 bits of version (7) with first 4 bits seq_hi
b[i++] = ((seqHigh >>> 4) & 0x0f) | 0x70;
b[i++] = ((seqHigh >>> 8) & 0x0f) | 0x70;

// [byte 7] remaining 8 bits of seq_hi
b[i++] = seqHigh & 0xff;
Expand Down
45 changes: 44 additions & 1 deletion test/unit/v7.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert';
import v7 from '../../src/v7.js';
import stringify from '../../src/stringify.js';

/**
* fixture bit layout:
Expand Down Expand Up @@ -154,7 +155,7 @@ describe('v7', () => {
seq,
});

assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7fff-bfff-f');
assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7dff-bfff-f');
broofa marked this conversation as resolved.
Show resolved Hide resolved
});

test('internal seq is reset upon timestamp change', () => {
Expand All @@ -169,4 +170,46 @@ describe('v7', () => {

assert(uuid.indexOf('fff') !== 15);
});

test('flipping bits changes the result', () => {
// convert uint8array to BigInt (BE)
const asBigInt = (buf) => buf.reduce((acc, v) => (acc << 8n) | BigInt(v), 0n);

// convert the given number of bits (LE) to number
const asNumber = (bits, data) => Number(BigInt.asUintN(bits, data));

// flip the nth bit (BE) in a BigInt
const flip = (data, n) => data ^ (1n << BigInt(127 - n));

// Extract v7 `options` from a (BigInt) UUID
const optionsFrom = (data) => {
broofa marked this conversation as resolved.
Show resolved Hide resolved
const ms = asNumber(48, data >> (128n - 48n));
const hi = asNumber(12, data >> (43n + 19n + 2n));
const lo = asNumber(19, data >> 43n);
const r = BigInt.asUintN(43, data);
return {
msecs: ms,
seq: (hi << 19) | lo,
random: [
...Array(10).fill(0),
...Array(6)
.fill(0)
.map((_, i) => asNumber(8, r >> (BigInt(i) * 8n)))
.reverse(),
],
};
};
const buf = new Uint8Array(16);
const data = asBigInt(v7({}, buf));
const id = stringify(buf);
const reserved = [48, 49, 50, 51, 64, 65];
for (let i = 0; i < 128; ++i) {
broofa marked this conversation as resolved.
Show resolved Hide resolved
if (reserved.includes(i)) {
continue; // skip bits used for version and variant
}
const flipped = flip(data, i);
assert.strictEqual(asBigInt(v7(optionsFrom(flipped), buf)), flipped, i);
assert.notStrictEqual(stringify(buf), id);
}
});
});