{"version":3,"file":"rng-DNn90Ptn.mjs","names":[],"sources":["../src/wire/timestamp-bytes.ts","../src/codecs/_kernel/rng.ts"],"sourcesContent":["import { IdsError } from \"../error.js\";\nimport { decodeBase32 } from \"./base32.js\";\n\n// Timestamp byte layout: first N bytes of the plaintext payload encode a\n// big-endian Unix-ms timestamp. Shared by timestamp-family layouts.\nexport const timestampByteLength = 6 as const;\n\nconst timestampBase32Length: number = Math.ceil((timestampByteLength * 8) / 5);\n\n/** Write the timestamp in big-endian; encoded via mod-256 to avoid 32-bit bitwise coercion. */\nexport function writeTimestamp(ms: number, buffer: Uint8Array): void {\n  if (!Number.isInteger(ms)) throw new IdsError(\"invalid_timestamp\", \"timestamp is not an integer\");\n  if (ms < 0) throw new IdsError(\"invalid_timestamp\", \"timestamp is negative\");\n  if (ms >= 2 ** (timestampByteLength * 8)) {\n    throw new IdsError(\"invalid_timestamp\", \"timestamp exceeds 48-bit range\");\n  }\n  for (let i = timestampByteLength - 1; i >= 0; i--) {\n    buffer[i] = ms % 256;\n    ms = Math.floor(ms / 256);\n  }\n}\n\n/** Decode the first `timestampByteLength` bytes of a buffer as a big-endian unsigned millisecond timestamp. */\nexport function readTimestampMs(buffer: Uint8Array): number {\n  let ms = 0;\n  for (let i = 0; i < timestampByteLength; i++) ms = ms * 256 + buffer[i]!;\n  return ms;\n}\n\n/** Decodes ms from the first 10 base32 chars of a payload suffix (partial decode).\n * Callers always pass `id.slice(prefix.length)` where `id: Id<Brand>` — the Id\n * brand guarantees that safeParse() / parse() normalised any alias chars at the\n * parse boundary (is() rejects aliases rather than normalising them), so the\n * leading 10-char timestamp slice is canonical Crockford alphabet and safe to\n * pass to decodeBase32 without further guards.\n */\nexport function readTimestampMsFromBase32Suffix(base32Suffix: string): number {\n  return readTimestampMs(decodeBase32(base32Suffix.slice(0, timestampBase32Length)));\n}\n","/** Default RNG: writes cryptographically random bytes via `crypto.getRandomValues`. */\nexport function defaultRng(target: Uint8Array): void {\n  crypto.getRandomValues(target as Uint8Array<ArrayBuffer>);\n}\n\n// hex charCode → 0–15 nibble, for harvesting bytes out of a UUIDv4 string.\n// Covers ['0'-'9' = 48–57] and ['a'-'f' = 97–102]; randomUUID is lowercase per spec.\nconst hexCharCodeToNibble = new Uint8Array(128);\nfor (let i = 0; i < 10; i++) hexCharCodeToNibble[48 + i] = i;\nfor (let i = 0; i < 6; i++) hexCharCodeToNibble[97 + i] = 10 + i;\n\n/**\n * Harvests 10 random bytes from a UUID string into `target[0..9]`.\n *\n * Reads from string positions 0–7 (bytes 0–3), 9–12 (bytes 4–5), and 24–31\n * (bytes 6–9), skipping the version nibble at hex position 14 and the variant\n * nibble at hex position 19 in the UUID layout `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`.\n *\n * @internal\n */\nexport function harvestUUIDBytes(uuid: string, target: Uint8Array): void {\n  target[0] =\n    (hexCharCodeToNibble[uuid.charCodeAt(0)]! << 4) | hexCharCodeToNibble[uuid.charCodeAt(1)]!;\n  target[1] =\n    (hexCharCodeToNibble[uuid.charCodeAt(2)]! << 4) | hexCharCodeToNibble[uuid.charCodeAt(3)]!;\n  target[2] =\n    (hexCharCodeToNibble[uuid.charCodeAt(4)]! << 4) | hexCharCodeToNibble[uuid.charCodeAt(5)]!;\n  target[3] =\n    (hexCharCodeToNibble[uuid.charCodeAt(6)]! << 4) | hexCharCodeToNibble[uuid.charCodeAt(7)]!;\n  target[4] =\n    (hexCharCodeToNibble[uuid.charCodeAt(9)]! << 4) | hexCharCodeToNibble[uuid.charCodeAt(10)]!;\n  target[5] =\n    (hexCharCodeToNibble[uuid.charCodeAt(11)]! << 4) | hexCharCodeToNibble[uuid.charCodeAt(12)]!;\n  target[6] =\n    (hexCharCodeToNibble[uuid.charCodeAt(24)]! << 4) | hexCharCodeToNibble[uuid.charCodeAt(25)]!;\n  target[7] =\n    (hexCharCodeToNibble[uuid.charCodeAt(26)]! << 4) | hexCharCodeToNibble[uuid.charCodeAt(27)]!;\n  target[8] =\n    (hexCharCodeToNibble[uuid.charCodeAt(28)]! << 4) | hexCharCodeToNibble[uuid.charCodeAt(29)]!;\n  target[9] =\n    (hexCharCodeToNibble[uuid.charCodeAt(30)]! << 4) | hexCharCodeToNibble[uuid.charCodeAt(31)]!;\n}\n\n/**\n * Fast RNG for the 10-byte random tail shared by the plaintext timestamp layouts\n * (Timestamp and Reverse Timestamp codecs). Writes exactly `target[0..9]`.\n *\n * `crypto.randomUUID()` is ~7× faster than `crypto.getRandomValues` in Node 24\n * (~84 ns vs ~610 ns for a 16-byte fill — the UUID path has a tight fixed-format\n * fast path). A UUIDv4 string carries 122 cryptographically-random bits; we\n * harvest 10 fully-random bytes from positions where no version (hex 12) or\n * variant (hex 16) bits sit. String layout: `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`\n * — bytes 0–5 are string[0..7]+string[9..12], bytes 6–9 are string[24..31].\n *\n * Security-equivalent to `defaultRng` for a 10-byte tail (both are CSPRNG-backed,\n * fully-random bytes); the only difference is throughput.\n */\n// Deliberate: 80-bit harvest from randomUUID — CSPRNG-backed, non-keyed codecs only; reviewed and accepted. See CONTEXT.md postures.\nexport function fastTenByteRng(target: Uint8Array): void {\n  harvestUUIDBytes(crypto.randomUUID(), target);\n}\n"],"mappings":";;AAOA,MAAM,wBAAgC,KAAK,KAAA,KAAiC,CAAC;;AAG7E,SAAgB,eAAe,IAAY,QAA0B;CACnE,IAAI,CAAC,OAAO,UAAU,EAAE,GAAG,MAAM,IAAI,SAAS,qBAAqB,6BAA6B;CAChG,IAAI,KAAK,GAAG,MAAM,IAAI,SAAS,qBAAqB,uBAAuB;CAC3E,IAAI,MAAM,KAAA,IACR,MAAM,IAAI,SAAS,qBAAqB,gCAAgC;CAE1E,KAAK,IAAI,IAAA,GAA6B,KAAK,GAAG,KAAK;EACjD,OAAO,KAAK,KAAK;EACjB,KAAK,KAAK,MAAM,KAAK,GAAG;CAC1B;AACF;;AAGA,SAAgB,gBAAgB,QAA4B;CAC1D,IAAI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