@open-e2ee/signal-protocol-sdk 0.1.0-alpha.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ADAPTERS.md +12 -12
- package/ARCHITECTURE.md +12 -4
- package/CHANGELOG.md +457 -0
- package/README.md +25 -15
- package/SECURITY.md +1 -1
- package/client/README.md +2 -2
- package/device/README.md +3 -1
- package/dist/client/client.d.ts +6 -6
- package/dist/client/client.js +6 -6
- package/dist/client/compose.d.ts +2 -2
- package/dist/client/config.d.ts +17 -1
- package/dist/client/config.d.ts.map +1 -1
- package/dist/client/config.js.map +1 -1
- package/dist/client/index.d.ts +2 -2
- package/dist/device/expo-metadata.d.ts +26 -0
- package/dist/device/expo-metadata.d.ts.map +1 -0
- package/dist/device/expo-metadata.js +33 -0
- package/dist/device/expo-metadata.js.map +1 -0
- package/dist/device/index.d.ts +1 -1
- package/dist/device/index.d.ts.map +1 -1
- package/dist/device/index.js +5 -1
- package/dist/device/index.js.map +1 -1
- package/dist/device/provisioning.d.ts +4 -12
- package/dist/device/provisioning.d.ts.map +1 -1
- package/dist/device/provisioning.js +4 -21
- package/dist/device/provisioning.js.map +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +5 -5
- package/dist/internal/crypto/symmetric/aes.d.ts.map +1 -1
- package/dist/internal/crypto/symmetric/aes.js +27 -30
- package/dist/internal/crypto/symmetric/aes.js.map +1 -1
- package/dist/internal/crypto/utils.d.ts.map +1 -1
- package/dist/internal/crypto/utils.js +26 -1
- package/dist/internal/crypto/utils.js.map +1 -1
- package/dist/internal/encoding/proto/pq-ratchet-serialize.d.ts.map +1 -1
- package/dist/internal/encoding/proto/pq-ratchet-serialize.js +12 -4
- package/dist/internal/encoding/proto/pq-ratchet-serialize.js.map +1 -1
- package/dist/internal/encoding/proto/primitives.d.ts +261 -6
- package/dist/internal/encoding/proto/primitives.d.ts.map +1 -1
- package/dist/internal/encoding/proto/primitives.js +468 -6
- package/dist/internal/encoding/proto/primitives.js.map +1 -1
- package/dist/internal/encoding/proto/sender-key-message.d.ts +4 -3
- package/dist/internal/encoding/proto/sender-key-message.d.ts.map +1 -1
- package/dist/internal/encoding/proto/sender-key-message.js +97 -76
- package/dist/internal/encoding/proto/sender-key-message.js.map +1 -1
- package/dist/internal/encoding/proto/signal-message.d.ts +7 -2
- package/dist/internal/encoding/proto/signal-message.d.ts.map +1 -1
- package/dist/internal/encoding/proto/signal-message.js +194 -110
- package/dist/internal/encoding/proto/signal-message.js.map +1 -1
- package/dist/internal/manager/manager.d.ts +2 -2
- package/dist/internal/manager/manager.js +1 -1
- package/dist/internal/manager/manager.js.map +1 -1
- package/dist/internal/protocol/sealed-sender/proto/index.d.ts +50 -4
- package/dist/internal/protocol/sealed-sender/proto/index.d.ts.map +1 -1
- package/dist/internal/protocol/sealed-sender/proto/index.js +273 -151
- package/dist/internal/protocol/sealed-sender/proto/index.js.map +1 -1
- package/dist/internal/protocol/spqr/braid.d.ts.map +1 -1
- package/dist/internal/protocol/spqr/braid.js.map +1 -1
- package/dist/internal/protocol/spqr/index.d.ts +1 -1
- package/dist/internal/protocol/spqr/index.d.ts.map +1 -1
- package/dist/internal/protocol/spqr/index.js +0 -2
- package/dist/internal/protocol/spqr/index.js.map +1 -1
- package/dist/internal/protocol/spqr/message.d.ts +0 -18
- package/dist/internal/protocol/spqr/message.d.ts.map +1 -1
- package/dist/internal/protocol/spqr/message.js +1 -37
- package/dist/internal/protocol/spqr/message.js.map +1 -1
- package/dist/internal/protocol/spqr/ml-kem-braid/progress.d.ts +30 -0
- package/dist/internal/protocol/spqr/ml-kem-braid/progress.d.ts.map +1 -0
- package/dist/internal/protocol/spqr/ml-kem-braid/progress.js +45 -0
- package/dist/internal/protocol/spqr/ml-kem-braid/progress.js.map +1 -0
- package/dist/internal/protocol/spqr/ml-kem-braid/rs/codec.d.ts +11 -8
- package/dist/internal/protocol/spqr/ml-kem-braid/rs/codec.d.ts.map +1 -1
- package/dist/internal/protocol/spqr/ml-kem-braid/rs/codec.js +11 -8
- package/dist/internal/protocol/spqr/ml-kem-braid/rs/codec.js.map +1 -1
- package/dist/internal/protocol/spqr/ml-kem-braid/serialize.d.ts +7 -1
- package/dist/internal/protocol/spqr/ml-kem-braid/serialize.d.ts.map +1 -1
- package/dist/internal/protocol/spqr/ml-kem-braid/serialize.js +342 -152
- package/dist/internal/protocol/spqr/ml-kem-braid/serialize.js.map +1 -1
- package/dist/internal/protocol/spqr/ml-kem-braid/types.d.ts +9 -6
- package/dist/internal/protocol/spqr/ml-kem-braid/types.d.ts.map +1 -1
- package/dist/internal/protocol/spqr/ml-kem-braid/types.js.map +1 -1
- package/dist/internal/protocol/spqr/spqr.d.ts +5 -3
- package/dist/internal/protocol/spqr/spqr.d.ts.map +1 -1
- package/dist/internal/protocol/spqr/spqr.js +62 -7
- package/dist/internal/protocol/spqr/spqr.js.map +1 -1
- package/dist/internal/session/cipher.d.ts +4 -0
- package/dist/internal/session/cipher.d.ts.map +1 -1
- package/dist/internal/session/cipher.js +8 -3
- package/dist/internal/session/cipher.js.map +1 -1
- package/dist/internal/session/types.d.ts +8 -0
- package/dist/internal/session/types.d.ts.map +1 -1
- package/dist/layers.d.ts +2 -2
- package/dist/layers.d.ts.map +1 -1
- package/dist/layers.js +2 -2
- package/dist/layers.js.map +1 -1
- package/dist/local/store/index.d.ts +3 -3
- package/dist/local/store/index.d.ts.map +1 -1
- package/dist/local/store/index.js +4 -4
- package/dist/local/store/index.js.map +1 -1
- package/dist/local/store/{mock → memory}/adapter.d.ts +10 -10
- package/dist/local/store/memory/adapter.d.ts.map +1 -0
- package/dist/local/store/{mock → memory}/adapter.js +5 -5
- package/dist/local/store/memory/adapter.js.map +1 -0
- package/dist/local/store/{mock → memory}/failures.d.ts +6 -6
- package/dist/local/store/memory/failures.d.ts.map +1 -0
- package/dist/local/store/{mock → memory}/failures.js +6 -6
- package/dist/local/store/memory/failures.js.map +1 -0
- package/dist/local/store/memory/index.d.ts +11 -0
- package/dist/local/store/memory/index.d.ts.map +1 -0
- package/dist/local/store/memory/index.js +7 -0
- package/dist/local/store/memory/index.js.map +1 -0
- package/dist/local/store/node/database.d.ts +1 -1
- package/dist/local/store/node/database.js +1 -1
- package/dist/local/store/react-native/adapter.d.ts.map +1 -1
- package/dist/local/store/react-native/adapter.js +69 -1
- package/dist/local/store/react-native/adapter.js.map +1 -1
- package/dist/local/store/react-native/backend-conformance.d.ts +66 -0
- package/dist/local/store/react-native/backend-conformance.d.ts.map +1 -0
- package/dist/local/store/react-native/backend-conformance.js +326 -0
- package/dist/local/store/react-native/backend-conformance.js.map +1 -0
- package/dist/local/store/react-native/index.d.ts +9 -4
- package/dist/local/store/react-native/index.d.ts.map +1 -1
- package/dist/local/store/react-native/index.js +2 -0
- package/dist/local/store/react-native/index.js.map +1 -1
- package/dist/local/store/react-native/reference-backend.d.ts +42 -0
- package/dist/local/store/react-native/reference-backend.d.ts.map +1 -0
- package/dist/local/store/react-native/reference-backend.js +177 -0
- package/dist/local/store/react-native/reference-backend.js.map +1 -0
- package/dist/local/store/react-native/storage.d.ts +9 -0
- package/dist/local/store/react-native/storage.d.ts.map +1 -1
- package/dist/local/store/web/adapter.d.ts +11 -1
- package/dist/local/store/web/adapter.d.ts.map +1 -1
- package/dist/local/store/web/adapter.js +226 -50
- package/dist/local/store/web/adapter.js.map +1 -1
- package/dist/local/store/web/index.d.ts +3 -2
- package/dist/local/store/web/index.d.ts.map +1 -1
- package/dist/local/store/web/index.js.map +1 -1
- package/dist/local/vault/index.d.ts +5 -1
- package/dist/local/vault/index.d.ts.map +1 -1
- package/dist/local/vault/index.js +1 -1
- package/dist/local/vault/index.js.map +1 -1
- package/dist/remote/relay/convex/component/certificates.d.ts.map +1 -1
- package/dist/remote/relay/convex/component/certificates.js +26 -31
- package/dist/remote/relay/convex/component/certificates.js.map +1 -1
- package/dist/remote/relay/index.d.ts +1 -1
- package/dist/remote/relay/index.d.ts.map +1 -1
- package/dist/remote/relay/index.js +2 -2
- package/dist/remote/relay/index.js.map +1 -1
- package/dist/remote/relay/{mock → memory}/adapter.d.ts +17 -17
- package/dist/remote/relay/memory/adapter.d.ts.map +1 -0
- package/dist/remote/relay/{mock → memory}/adapter.js +17 -17
- package/dist/remote/relay/memory/adapter.js.map +1 -0
- package/dist/remote/relay/{mock → memory}/failures.d.ts +5 -5
- package/dist/remote/relay/memory/failures.d.ts.map +1 -0
- package/dist/remote/relay/{mock → memory}/failures.js +1 -1
- package/dist/remote/relay/memory/failures.js.map +1 -0
- package/dist/remote/relay/{mock → memory}/group-server.d.ts +1 -1
- package/dist/remote/relay/memory/group-server.d.ts.map +1 -0
- package/dist/remote/relay/{mock → memory}/group-server.js +1 -1
- package/dist/remote/relay/memory/group-server.js.map +1 -0
- package/dist/remote/relay/memory/index.d.ts +11 -0
- package/dist/remote/relay/memory/index.d.ts.map +1 -0
- package/dist/remote/relay/memory/index.js +7 -0
- package/dist/remote/relay/memory/index.js.map +1 -0
- package/dist/types/errors.d.ts +33 -0
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +36 -0
- package/dist/types/errors.js.map +1 -1
- package/dist/types/protocol-config.d.ts +44 -1
- package/dist/types/protocol-config.d.ts.map +1 -1
- package/dist/types/protocol-config.js +1 -0
- package/dist/types/protocol-config.js.map +1 -1
- package/dist/types/session.d.ts +1 -1
- package/dist/types/session.js +1 -1
- package/docs/ASSURANCE.md +49 -14
- package/docs/CLIENT_COMPOSITION.md +3 -3
- package/docs/DEVIATIONS.md +72 -31
- package/docs/GETTING_STARTED.md +11 -11
- package/docs/PACKAGE_SURFACE.md +10 -8
- package/docs/PROTOCOL_POLICY.md +6 -0
- package/docs/RECIPES.md +12 -12
- package/local/store/README.md +95 -8
- package/local/store/{mock → memory}/README.md +8 -8
- package/local/store/react-native/README.md +34 -2
- package/local/store/web/README.md +30 -3
- package/package.json +14 -11
- package/remote/README.md +7 -7
- package/remote/relay/README.md +5 -5
- package/remote/relay/{mock → memory}/README.md +8 -8
- package/dist/local/store/mock/adapter.d.ts.map +0 -1
- package/dist/local/store/mock/adapter.js.map +0 -1
- package/dist/local/store/mock/failures.d.ts.map +0 -1
- package/dist/local/store/mock/failures.js.map +0 -1
- package/dist/local/store/mock/index.d.ts +0 -11
- package/dist/local/store/mock/index.d.ts.map +0 -1
- package/dist/local/store/mock/index.js +0 -7
- package/dist/local/store/mock/index.js.map +0 -1
- package/dist/remote/relay/mock/adapter.d.ts.map +0 -1
- package/dist/remote/relay/mock/adapter.js.map +0 -1
- package/dist/remote/relay/mock/failures.d.ts.map +0 -1
- package/dist/remote/relay/mock/failures.js.map +0 -1
- package/dist/remote/relay/mock/group-server.d.ts.map +0 -1
- package/dist/remote/relay/mock/group-server.js.map +0 -1
- package/dist/remote/relay/mock/index.d.ts +0 -11
- package/dist/remote/relay/mock/index.d.ts.map +0 -1
- package/dist/remote/relay/mock/index.js +0 -7
- package/dist/remote/relay/mock/index.js.map +0 -1
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @module ml-kem-braid/serialize
|
|
10
10
|
*/
|
|
11
|
-
import
|
|
11
|
+
import { ProtoReader, concatFields, encodeBoolField, encodeBytesField, encodeEnumField, encodeMessageField, encodeRepeatedBytesField, encodeRepeatedMessageField, encodeUint32Field, encodeUint64Field, } from '../../../encoding/proto/primitives.js';
|
|
12
12
|
import { assertBraidChunkIndex, assertBraidEncoderCursor } from './chunk-domain.js';
|
|
13
13
|
import { MessageType, PROTOCOL_CONSTANTS } from './types.js';
|
|
14
14
|
const MAX_WIRE_INPUT_BYTES = 64 * 1024;
|
|
@@ -241,102 +241,302 @@ export function deserializeMessageBinary(bytes) {
|
|
|
241
241
|
// =============================================================================
|
|
242
242
|
// Protobuf Format Serialization
|
|
243
243
|
// =============================================================================
|
|
244
|
-
// Lazy-loaded protobuf root
|
|
245
|
-
let protoRoot = null;
|
|
246
|
-
let V1MsgType = null;
|
|
247
|
-
let AuthenticatorType = null;
|
|
248
|
-
let PolynomialEncoderType = null;
|
|
249
|
-
let PolynomialDecoderType = null;
|
|
250
244
|
/**
|
|
251
|
-
*
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
245
|
+
* Field numbers from `proto/pq_ratchet.proto`. They are frozen: the same wire
|
|
246
|
+
* is read by the reference implementation, and `Authenticator`,
|
|
247
|
+
* `PolynomialEncoder`, and `PolynomialDecoder` are the persisted ratchet
|
|
248
|
+
* state, so an installed client must keep decoding what it wrote yesterday.
|
|
249
|
+
*/
|
|
250
|
+
const V1MSG_FIELDS = {
|
|
251
|
+
epoch: 1,
|
|
252
|
+
index: 2,
|
|
253
|
+
hdr: 3,
|
|
254
|
+
ek: 4,
|
|
255
|
+
ekCt1Ack: 5,
|
|
256
|
+
ct1Ack: 6,
|
|
257
|
+
ct1: 7,
|
|
258
|
+
ct2: 8,
|
|
259
|
+
versionCapability: 9,
|
|
260
|
+
};
|
|
261
|
+
const CHUNK_FIELDS = { index: 1, data: 2 };
|
|
262
|
+
const VERSION_CAPABILITY_FIELDS = { maxVersion: 1, minVersion: 2 };
|
|
263
|
+
const AUTHENTICATOR_FIELDS = { rootKey: 1, macKey: 2 };
|
|
264
|
+
const POLYNOMIAL_ENCODER_FIELDS = { idx: 1, pts: 2, polys: 3, messageSize: 4 };
|
|
265
|
+
const POLYNOMIAL_DECODER_FIELDS = {
|
|
266
|
+
ptsNeeded: 1,
|
|
267
|
+
polys: 2,
|
|
268
|
+
chunks: 3,
|
|
269
|
+
isComplete: 4,
|
|
270
|
+
messageSize: 5,
|
|
271
|
+
};
|
|
272
|
+
/** `Version.V_1`; `V_0 = 0` is reserved and the runtime never emits it. */
|
|
273
|
+
const VERSION_V1 = 1;
|
|
274
|
+
/**
|
|
275
|
+
* The `V1Msg.inner_msg` oneof, in field order. Setting one arm clears the
|
|
276
|
+
* others, on encode and on decode alike: a message that arrives carrying two
|
|
277
|
+
* arms keeps only the last one on the wire.
|
|
278
|
+
*/
|
|
279
|
+
const V1MSG_ONEOF_ARMS = ['hdr', 'ek', 'ekCt1Ack', 'ct1Ack', 'ct1', 'ct2'];
|
|
280
|
+
function assertProtoBytes(value, label) {
|
|
281
|
+
if (!(value instanceof Uint8Array)) {
|
|
282
|
+
throw new Error(`${label} must be a Uint8Array`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function assertProtoUint32(value, label) {
|
|
286
|
+
if (!Number.isInteger(value) || value < 0 || value > 0xffffffff) {
|
|
287
|
+
throw new Error(`${label} must be an integer between 0 and 4294967295`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/** Assign one oneof arm, clearing whichever sibling was set before. */
|
|
291
|
+
function setInnerMsg(msg, arm, value) {
|
|
292
|
+
for (const other of V1MSG_ONEOF_ARMS) {
|
|
293
|
+
delete msg[other];
|
|
294
|
+
}
|
|
295
|
+
msg[arm] = value;
|
|
296
|
+
}
|
|
297
|
+
function encodeChunk(chunk) {
|
|
298
|
+
const fields = [];
|
|
299
|
+
if (chunk.index !== undefined) {
|
|
300
|
+
fields.push(encodeUint32Field(CHUNK_FIELDS.index, chunk.index));
|
|
301
|
+
}
|
|
302
|
+
if (chunk.data !== undefined) {
|
|
303
|
+
fields.push(encodeBytesField(CHUNK_FIELDS.data, chunk.data));
|
|
304
|
+
}
|
|
305
|
+
return concatFields(...fields);
|
|
306
|
+
}
|
|
307
|
+
function decodeChunk(bytes) {
|
|
308
|
+
const chunk = {};
|
|
309
|
+
const reader = new ProtoReader(bytes);
|
|
310
|
+
while (reader.hasMore()) {
|
|
311
|
+
const { fieldNumber } = reader.readTag();
|
|
312
|
+
switch (fieldNumber) {
|
|
313
|
+
case CHUNK_FIELDS.index:
|
|
314
|
+
chunk.index = reader.readUint32();
|
|
315
|
+
break;
|
|
316
|
+
case CHUNK_FIELDS.data:
|
|
317
|
+
chunk.data = reader.readBytes();
|
|
318
|
+
break;
|
|
319
|
+
default:
|
|
320
|
+
reader.skipField();
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return chunk;
|
|
324
|
+
}
|
|
325
|
+
function encodeVersionCapability(capability) {
|
|
326
|
+
const fields = [];
|
|
327
|
+
if (capability.maxVersion !== undefined) {
|
|
328
|
+
fields.push(encodeEnumField(VERSION_CAPABILITY_FIELDS.maxVersion, capability.maxVersion));
|
|
329
|
+
}
|
|
330
|
+
if (capability.minVersion !== undefined) {
|
|
331
|
+
fields.push(encodeEnumField(VERSION_CAPABILITY_FIELDS.minVersion, capability.minVersion));
|
|
332
|
+
}
|
|
333
|
+
return concatFields(...fields);
|
|
334
|
+
}
|
|
335
|
+
function decodeVersionCapability(bytes) {
|
|
336
|
+
const capability = {};
|
|
337
|
+
const reader = new ProtoReader(bytes);
|
|
338
|
+
while (reader.hasMore()) {
|
|
339
|
+
const { fieldNumber } = reader.readTag();
|
|
340
|
+
switch (fieldNumber) {
|
|
341
|
+
case VERSION_CAPABILITY_FIELDS.maxVersion:
|
|
342
|
+
capability.maxVersion = reader.readEnum();
|
|
343
|
+
break;
|
|
344
|
+
case VERSION_CAPABILITY_FIELDS.minVersion:
|
|
345
|
+
capability.minVersion = reader.readEnum();
|
|
346
|
+
break;
|
|
347
|
+
default:
|
|
348
|
+
reader.skipField();
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return capability;
|
|
352
|
+
}
|
|
353
|
+
function encodeV1Msg(msg) {
|
|
354
|
+
const fields = [];
|
|
355
|
+
if (msg.epoch !== undefined) {
|
|
356
|
+
fields.push(encodeUint64Field(V1MSG_FIELDS.epoch, msg.epoch));
|
|
357
|
+
}
|
|
358
|
+
if (msg.index !== undefined) {
|
|
359
|
+
fields.push(encodeUint32Field(V1MSG_FIELDS.index, msg.index));
|
|
360
|
+
}
|
|
361
|
+
if (msg.hdr !== undefined) {
|
|
362
|
+
fields.push(encodeMessageField(V1MSG_FIELDS.hdr, encodeChunk(msg.hdr)));
|
|
363
|
+
}
|
|
364
|
+
if (msg.ek !== undefined) {
|
|
365
|
+
fields.push(encodeMessageField(V1MSG_FIELDS.ek, encodeChunk(msg.ek)));
|
|
366
|
+
}
|
|
367
|
+
if (msg.ekCt1Ack !== undefined) {
|
|
368
|
+
fields.push(encodeMessageField(V1MSG_FIELDS.ekCt1Ack, encodeChunk(msg.ekCt1Ack)));
|
|
369
|
+
}
|
|
370
|
+
if (msg.ct1Ack !== undefined) {
|
|
371
|
+
fields.push(encodeBoolField(V1MSG_FIELDS.ct1Ack, msg.ct1Ack));
|
|
372
|
+
}
|
|
373
|
+
if (msg.ct1 !== undefined) {
|
|
374
|
+
fields.push(encodeMessageField(V1MSG_FIELDS.ct1, encodeChunk(msg.ct1)));
|
|
375
|
+
}
|
|
376
|
+
if (msg.ct2 !== undefined) {
|
|
377
|
+
fields.push(encodeMessageField(V1MSG_FIELDS.ct2, encodeChunk(msg.ct2)));
|
|
378
|
+
}
|
|
379
|
+
if (msg.versionCapability !== undefined) {
|
|
380
|
+
fields.push(encodeMessageField(V1MSG_FIELDS.versionCapability, encodeVersionCapability(msg.versionCapability)));
|
|
381
|
+
}
|
|
382
|
+
return concatFields(...fields);
|
|
383
|
+
}
|
|
384
|
+
function decodeV1Msg(bytes) {
|
|
385
|
+
const msg = {};
|
|
386
|
+
const reader = new ProtoReader(bytes);
|
|
387
|
+
while (reader.hasMore()) {
|
|
388
|
+
const { fieldNumber } = reader.readTag();
|
|
389
|
+
switch (fieldNumber) {
|
|
390
|
+
case V1MSG_FIELDS.epoch:
|
|
391
|
+
msg.epoch = reader.readUint64();
|
|
392
|
+
break;
|
|
393
|
+
case V1MSG_FIELDS.index:
|
|
394
|
+
msg.index = reader.readUint32();
|
|
395
|
+
break;
|
|
396
|
+
case V1MSG_FIELDS.hdr:
|
|
397
|
+
setInnerMsg(msg, 'hdr', decodeChunk(reader.readMessage()));
|
|
398
|
+
break;
|
|
399
|
+
case V1MSG_FIELDS.ek:
|
|
400
|
+
setInnerMsg(msg, 'ek', decodeChunk(reader.readMessage()));
|
|
401
|
+
break;
|
|
402
|
+
case V1MSG_FIELDS.ekCt1Ack:
|
|
403
|
+
setInnerMsg(msg, 'ekCt1Ack', decodeChunk(reader.readMessage()));
|
|
404
|
+
break;
|
|
405
|
+
case V1MSG_FIELDS.ct1Ack:
|
|
406
|
+
setInnerMsg(msg, 'ct1Ack', reader.readBool());
|
|
407
|
+
break;
|
|
408
|
+
case V1MSG_FIELDS.ct1:
|
|
409
|
+
setInnerMsg(msg, 'ct1', decodeChunk(reader.readMessage()));
|
|
410
|
+
break;
|
|
411
|
+
case V1MSG_FIELDS.ct2:
|
|
412
|
+
setInnerMsg(msg, 'ct2', decodeChunk(reader.readMessage()));
|
|
413
|
+
break;
|
|
414
|
+
case V1MSG_FIELDS.versionCapability:
|
|
415
|
+
msg.versionCapability = decodeVersionCapability(reader.readMessage());
|
|
416
|
+
break;
|
|
417
|
+
default:
|
|
418
|
+
reader.skipField();
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return msg;
|
|
422
|
+
}
|
|
423
|
+
function encodeAuthenticator(auth) {
|
|
424
|
+
const fields = [];
|
|
425
|
+
if (auth.rootKey !== undefined) {
|
|
426
|
+
fields.push(encodeBytesField(AUTHENTICATOR_FIELDS.rootKey, auth.rootKey));
|
|
427
|
+
}
|
|
428
|
+
if (auth.macKey !== undefined) {
|
|
429
|
+
fields.push(encodeBytesField(AUTHENTICATOR_FIELDS.macKey, auth.macKey));
|
|
430
|
+
}
|
|
431
|
+
return concatFields(...fields);
|
|
432
|
+
}
|
|
433
|
+
function decodeAuthenticator(bytes) {
|
|
434
|
+
const auth = {};
|
|
435
|
+
const reader = new ProtoReader(bytes);
|
|
436
|
+
while (reader.hasMore()) {
|
|
437
|
+
const { fieldNumber } = reader.readTag();
|
|
438
|
+
switch (fieldNumber) {
|
|
439
|
+
case AUTHENTICATOR_FIELDS.rootKey:
|
|
440
|
+
auth.rootKey = reader.readBytes();
|
|
441
|
+
break;
|
|
442
|
+
case AUTHENTICATOR_FIELDS.macKey:
|
|
443
|
+
auth.macKey = reader.readBytes();
|
|
444
|
+
break;
|
|
445
|
+
default:
|
|
446
|
+
reader.skipField();
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return auth;
|
|
450
|
+
}
|
|
451
|
+
function encodePolynomialEncoder(state) {
|
|
452
|
+
const fields = [];
|
|
453
|
+
if (state.idx !== undefined) {
|
|
454
|
+
fields.push(encodeUint32Field(POLYNOMIAL_ENCODER_FIELDS.idx, state.idx));
|
|
455
|
+
}
|
|
456
|
+
fields.push(encodeRepeatedBytesField(POLYNOMIAL_ENCODER_FIELDS.pts, state.pts));
|
|
457
|
+
fields.push(encodeRepeatedBytesField(POLYNOMIAL_ENCODER_FIELDS.polys, state.polys));
|
|
458
|
+
if (state.messageSize !== undefined) {
|
|
459
|
+
fields.push(encodeUint32Field(POLYNOMIAL_ENCODER_FIELDS.messageSize, state.messageSize));
|
|
460
|
+
}
|
|
461
|
+
return concatFields(...fields);
|
|
462
|
+
}
|
|
463
|
+
function decodePolynomialEncoder(bytes) {
|
|
464
|
+
const state = { pts: [], polys: [] };
|
|
465
|
+
const reader = new ProtoReader(bytes);
|
|
466
|
+
while (reader.hasMore()) {
|
|
467
|
+
const { fieldNumber } = reader.readTag();
|
|
468
|
+
switch (fieldNumber) {
|
|
469
|
+
case POLYNOMIAL_ENCODER_FIELDS.idx:
|
|
470
|
+
state.idx = reader.readUint32();
|
|
471
|
+
break;
|
|
472
|
+
case POLYNOMIAL_ENCODER_FIELDS.pts:
|
|
473
|
+
state.pts.push(reader.readBytes());
|
|
474
|
+
break;
|
|
475
|
+
case POLYNOMIAL_ENCODER_FIELDS.polys:
|
|
476
|
+
state.polys.push(reader.readBytes());
|
|
477
|
+
break;
|
|
478
|
+
case POLYNOMIAL_ENCODER_FIELDS.messageSize:
|
|
479
|
+
state.messageSize = reader.readUint32();
|
|
480
|
+
break;
|
|
481
|
+
default:
|
|
482
|
+
reader.skipField();
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return state;
|
|
486
|
+
}
|
|
487
|
+
function encodePolynomialDecoder(state) {
|
|
488
|
+
const fields = [];
|
|
489
|
+
if (state.ptsNeeded !== undefined) {
|
|
490
|
+
fields.push(encodeUint32Field(POLYNOMIAL_DECODER_FIELDS.ptsNeeded, state.ptsNeeded));
|
|
491
|
+
}
|
|
492
|
+
if (state.polys !== undefined) {
|
|
493
|
+
fields.push(encodeUint32Field(POLYNOMIAL_DECODER_FIELDS.polys, state.polys));
|
|
494
|
+
}
|
|
495
|
+
fields.push(encodeRepeatedMessageField(POLYNOMIAL_DECODER_FIELDS.chunks, state.chunks.map(encodeChunk)));
|
|
496
|
+
if (state.isComplete !== undefined) {
|
|
497
|
+
fields.push(encodeBoolField(POLYNOMIAL_DECODER_FIELDS.isComplete, state.isComplete));
|
|
498
|
+
}
|
|
499
|
+
if (state.messageSize !== undefined) {
|
|
500
|
+
fields.push(encodeUint32Field(POLYNOMIAL_DECODER_FIELDS.messageSize, state.messageSize));
|
|
501
|
+
}
|
|
502
|
+
return concatFields(...fields);
|
|
503
|
+
}
|
|
504
|
+
function decodePolynomialDecoder(bytes) {
|
|
505
|
+
const state = { chunks: [] };
|
|
506
|
+
const reader = new ProtoReader(bytes);
|
|
507
|
+
while (reader.hasMore()) {
|
|
508
|
+
const { fieldNumber } = reader.readTag();
|
|
509
|
+
switch (fieldNumber) {
|
|
510
|
+
case POLYNOMIAL_DECODER_FIELDS.ptsNeeded:
|
|
511
|
+
state.ptsNeeded = reader.readUint32();
|
|
512
|
+
break;
|
|
513
|
+
case POLYNOMIAL_DECODER_FIELDS.polys:
|
|
514
|
+
state.polys = reader.readUint32();
|
|
515
|
+
break;
|
|
516
|
+
case POLYNOMIAL_DECODER_FIELDS.chunks:
|
|
517
|
+
state.chunks.push(decodeChunk(reader.readMessage()));
|
|
518
|
+
break;
|
|
519
|
+
case POLYNOMIAL_DECODER_FIELDS.isComplete:
|
|
520
|
+
state.isComplete = reader.readBool();
|
|
521
|
+
break;
|
|
522
|
+
case POLYNOMIAL_DECODER_FIELDS.messageSize:
|
|
523
|
+
state.messageSize = reader.readUint32();
|
|
524
|
+
break;
|
|
525
|
+
default:
|
|
526
|
+
reader.skipField();
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return state;
|
|
319
530
|
}
|
|
320
531
|
/**
|
|
321
532
|
* Serialize a message with the ML-KEM Braid protobuf schema.
|
|
322
533
|
*/
|
|
323
534
|
export async function serializeMessageProto(msg) {
|
|
324
535
|
validateMessage(msg);
|
|
325
|
-
|
|
326
|
-
//
|
|
327
|
-
// For larger values, we'd need to use protobufjs's Long type
|
|
328
|
-
let epochValue = Number(msg.epoch);
|
|
329
|
-
if (msg.epoch > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
330
|
-
// Use Long from protobufjs for large values
|
|
331
|
-
const Long = protobuf.util.Long;
|
|
332
|
-
if (Long) {
|
|
333
|
-
const high = Number(msg.epoch >> 32n);
|
|
334
|
-
const low = Number(msg.epoch & 0xffffffffn);
|
|
335
|
-
epochValue = new Long(low, high, true);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
536
|
+
// Both scalars are written even at zero: the epoch and the chunk index are
|
|
537
|
+
// always set on the wire, never elided as proto3 defaults.
|
|
338
538
|
const protoMsg = {
|
|
339
|
-
epoch:
|
|
539
|
+
epoch: msg.epoch,
|
|
340
540
|
index: msg.chunkIndex ?? 0,
|
|
341
541
|
};
|
|
342
542
|
// Map MessageType to oneof field
|
|
@@ -370,29 +570,19 @@ export async function serializeMessageProto(msg) {
|
|
|
370
570
|
if (msg.versionCapability) {
|
|
371
571
|
assertV1VersionCapability(msg.versionCapability);
|
|
372
572
|
protoMsg.versionCapability = {
|
|
373
|
-
maxVersion:
|
|
374
|
-
minVersion:
|
|
573
|
+
maxVersion: VERSION_V1,
|
|
574
|
+
minVersion: VERSION_V1,
|
|
375
575
|
};
|
|
376
576
|
}
|
|
377
|
-
|
|
378
|
-
if (errMsg) {
|
|
379
|
-
throw new Error(`Invalid message: ${errMsg}`);
|
|
380
|
-
}
|
|
381
|
-
const message = V1MsgType.create(protoMsg);
|
|
382
|
-
return V1MsgType.encode(message).finish();
|
|
577
|
+
return encodeV1Msg(protoMsg);
|
|
383
578
|
}
|
|
384
579
|
/**
|
|
385
580
|
* Deserialize protobuf message
|
|
386
581
|
*/
|
|
387
582
|
export async function deserializeMessageProto(bytes) {
|
|
388
583
|
assertWireInput(bytes, 'ML-KEM Braid protobuf message');
|
|
389
|
-
|
|
390
|
-
const
|
|
391
|
-
const obj = V1MsgType.toObject(decoded, {
|
|
392
|
-
longs: String, // bigint support
|
|
393
|
-
bytes: Uint8Array,
|
|
394
|
-
});
|
|
395
|
-
const epoch = typeof obj.epoch === 'string' ? BigInt(obj.epoch) : BigInt(obj.epoch || 0);
|
|
584
|
+
const obj = decodeV1Msg(bytes);
|
|
585
|
+
const epoch = obj.epoch ?? 0n;
|
|
396
586
|
let type = MessageType.None;
|
|
397
587
|
let chunkIndex;
|
|
398
588
|
let data;
|
|
@@ -447,34 +637,26 @@ export async function deserializeMessageProto(bytes) {
|
|
|
447
637
|
* Serialize authenticator state to protobuf
|
|
448
638
|
*/
|
|
449
639
|
export async function serializeAuthenticatorProto(auth) {
|
|
450
|
-
|
|
451
|
-
|
|
640
|
+
assertProtoBytes(auth.root_key, 'Authenticator root key');
|
|
641
|
+
assertProtoBytes(auth.mac_key, 'Authenticator MAC key');
|
|
642
|
+
return encodeAuthenticator({
|
|
452
643
|
rootKey: auth.root_key,
|
|
453
644
|
macKey: auth.mac_key,
|
|
454
|
-
};
|
|
455
|
-
const errMsg = AuthenticatorType.verify(protoMsg);
|
|
456
|
-
if (errMsg) {
|
|
457
|
-
throw new Error(`Invalid authenticator: ${errMsg}`);
|
|
458
|
-
}
|
|
459
|
-
const message = AuthenticatorType.create(protoMsg);
|
|
460
|
-
return AuthenticatorType.encode(message).finish();
|
|
645
|
+
});
|
|
461
646
|
}
|
|
462
647
|
/**
|
|
463
648
|
* Deserialize protobuf authenticator state
|
|
464
649
|
*/
|
|
465
650
|
export async function deserializeAuthenticatorProto(bytes) {
|
|
466
651
|
assertWireInput(bytes, 'ML-KEM Braid authenticator protobuf');
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
const obj = AuthenticatorType.toObject(decoded, { bytes: Uint8Array });
|
|
470
|
-
const result = {
|
|
471
|
-
root_key: obj.rootKey,
|
|
472
|
-
mac_key: obj.macKey,
|
|
473
|
-
};
|
|
474
|
-
if (result.root_key?.length !== 32 || result.mac_key?.length !== 32) {
|
|
652
|
+
const { rootKey, macKey } = decodeAuthenticator(bytes);
|
|
653
|
+
if (rootKey?.length !== 32 || macKey?.length !== 32) {
|
|
475
654
|
throw new Error('Authenticator keys must each contain exactly 32 bytes');
|
|
476
655
|
}
|
|
477
|
-
return
|
|
656
|
+
return {
|
|
657
|
+
root_key: rootKey,
|
|
658
|
+
mac_key: macKey,
|
|
659
|
+
};
|
|
478
660
|
}
|
|
479
661
|
// =============================================================================
|
|
480
662
|
// JSON Format Serialization
|
|
@@ -626,30 +808,32 @@ export function deserializeDecoderJSON(jsonStr) {
|
|
|
626
808
|
*/
|
|
627
809
|
export async function serializeEncoderProto(state) {
|
|
628
810
|
assertBraidEncoderCursor(state.currentChunkIndex, 'Encoder state index');
|
|
629
|
-
|
|
630
|
-
const
|
|
811
|
+
assertProtoUint32(state.originalDataSize, 'Encoder state message size');
|
|
812
|
+
for (const [position, chunk] of state.dataChunks.entries()) {
|
|
813
|
+
assertProtoBytes(chunk, `Encoder state evaluation point ${position}`);
|
|
814
|
+
}
|
|
815
|
+
return encodePolynomialEncoder({
|
|
631
816
|
idx: state.currentChunkIndex,
|
|
632
817
|
pts: state.dataChunks,
|
|
633
818
|
polys: state.polynomials.map((p) => new Uint8Array(p.coefficients.flatMap((c) => [(c >> 8) & 0xff, c & 0xff]))),
|
|
634
819
|
messageSize: state.originalDataSize,
|
|
635
|
-
};
|
|
636
|
-
const errMsg = PolynomialEncoderType.verify(protoMsg);
|
|
637
|
-
if (errMsg) {
|
|
638
|
-
throw new Error(`Invalid encoder state: ${errMsg}`);
|
|
639
|
-
}
|
|
640
|
-
const message = PolynomialEncoderType.create(protoMsg);
|
|
641
|
-
return PolynomialEncoderType.encode(message).finish();
|
|
820
|
+
});
|
|
642
821
|
}
|
|
643
822
|
/**
|
|
644
823
|
* Deserialize protobuf encoder state
|
|
645
824
|
*/
|
|
646
825
|
export async function deserializeEncoderProto(bytes) {
|
|
647
826
|
assertWireInput(bytes, 'ML-KEM Braid encoder protobuf state');
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
//
|
|
652
|
-
|
|
827
|
+
const obj = decodePolynomialEncoder(bytes);
|
|
828
|
+
// Convert polys back to coefficient arrays. Each element is a run of
|
|
829
|
+
// big-endian 16-bit coefficients, so an empty or odd-length run is not a
|
|
830
|
+
// polynomial. Odd length used to read one byte past the end and fold
|
|
831
|
+
// `undefined` into the low half of a coefficient, inventing a final
|
|
832
|
+
// coefficient the encoder never wrote.
|
|
833
|
+
const polys = obj.polys.map((polyBytes, position) => {
|
|
834
|
+
if (polyBytes.length === 0 || polyBytes.length % 2 !== 0) {
|
|
835
|
+
throw new Error(`Encoder state polynomial ${position} must be a non-empty whole number of 16-bit coefficients`);
|
|
836
|
+
}
|
|
653
837
|
const coeffs = [];
|
|
654
838
|
for (let i = 0; i < polyBytes.length; i += 2) {
|
|
655
839
|
coeffs.push((polyBytes[i] << 8) | polyBytes[i + 1]);
|
|
@@ -669,40 +853,40 @@ export async function deserializeEncoderProto(bytes) {
|
|
|
669
853
|
* Serialize decoder state to protobuf
|
|
670
854
|
*/
|
|
671
855
|
export async function serializeDecoderProto(state) {
|
|
672
|
-
await initProto();
|
|
673
856
|
const chunks = [];
|
|
674
857
|
for (const [index, data] of state.receivedChunks) {
|
|
675
858
|
assertBraidChunkIndex(index, 'Decoder chunk index');
|
|
859
|
+
assertProtoBytes(data, `Decoder chunk ${index} data`);
|
|
676
860
|
chunks.push({ index, data });
|
|
677
861
|
}
|
|
678
|
-
|
|
862
|
+
assertProtoUint32(state.config.dataChunks, 'Decoder state points needed');
|
|
863
|
+
assertProtoUint32(state.messageSize, 'Decoder state message size');
|
|
864
|
+
return encodePolynomialDecoder({
|
|
679
865
|
ptsNeeded: state.config.dataChunks,
|
|
680
866
|
polys: 16,
|
|
681
867
|
chunks,
|
|
682
868
|
isComplete: state.receivedChunks.size >= state.config.dataChunks,
|
|
683
869
|
messageSize: state.messageSize,
|
|
684
|
-
};
|
|
685
|
-
const errMsg = PolynomialDecoderType.verify(protoMsg);
|
|
686
|
-
if (errMsg) {
|
|
687
|
-
throw new Error(`Invalid decoder state: ${errMsg}`);
|
|
688
|
-
}
|
|
689
|
-
const message = PolynomialDecoderType.create(protoMsg);
|
|
690
|
-
return PolynomialDecoderType.encode(message).finish();
|
|
870
|
+
});
|
|
691
871
|
}
|
|
692
872
|
/**
|
|
693
873
|
* Deserialize protobuf decoder state
|
|
694
874
|
*/
|
|
695
875
|
export async function deserializeDecoderProto(bytes) {
|
|
696
876
|
assertWireInput(bytes, 'ML-KEM Braid decoder protobuf state');
|
|
697
|
-
|
|
698
|
-
const decoded = PolynomialDecoderType.decode(bytes);
|
|
699
|
-
const obj = PolynomialDecoderType.toObject(decoded, { bytes: Uint8Array });
|
|
877
|
+
const obj = decodePolynomialDecoder(bytes);
|
|
700
878
|
const chunks = new Map();
|
|
701
|
-
for (const chunk of obj.chunks
|
|
879
|
+
for (const chunk of obj.chunks) {
|
|
702
880
|
const index = assertBraidChunkIndex(chunk.index, 'Decoder chunk index');
|
|
703
881
|
if (chunks.has(index)) {
|
|
704
882
|
throw new Error(`Decoder state contains duplicate chunk index ${index}`);
|
|
705
883
|
}
|
|
884
|
+
// A chunk entry with no data field at all is malformed state. The
|
|
885
|
+
// reflection codec used to admit it and put `undefined` in the map, which
|
|
886
|
+
// surfaced as a failure inside the Reed-Solomon decoder instead of here.
|
|
887
|
+
if (chunk.data === undefined) {
|
|
888
|
+
throw new Error(`Decoder state chunk ${index} is missing its data`);
|
|
889
|
+
}
|
|
706
890
|
chunks.set(index, chunk.data);
|
|
707
891
|
}
|
|
708
892
|
return {
|
|
@@ -717,15 +901,21 @@ export async function deserializeDecoderProto(bytes) {
|
|
|
717
901
|
// Initialization
|
|
718
902
|
// =============================================================================
|
|
719
903
|
/**
|
|
720
|
-
* Initialize serialization module
|
|
904
|
+
* Initialize serialization module.
|
|
905
|
+
*
|
|
906
|
+
* Nothing is loaded any more — the codecs above are static functions, so there
|
|
907
|
+
* is no schema to build at runtime and no first-call latency to pay. Kept, and
|
|
908
|
+
* kept async, because callers await it before serializing.
|
|
721
909
|
*/
|
|
722
910
|
export async function initSerialization() {
|
|
723
|
-
|
|
911
|
+
return Promise.resolve();
|
|
724
912
|
}
|
|
725
913
|
/**
|
|
726
914
|
* Check if serialization is ready
|
|
915
|
+
*
|
|
916
|
+
* Always true: with static codecs there is no state that could be missing.
|
|
727
917
|
*/
|
|
728
918
|
export function isSerializationReady() {
|
|
729
|
-
return
|
|
919
|
+
return true;
|
|
730
920
|
}
|
|
731
921
|
//# sourceMappingURL=serialize.js.map
|