@vex-chat/libvex 9.1.1 → 10.0.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.
Files changed (43) hide show
  1. package/README.md +1 -1
  2. package/dist/Client.d.ts +3 -21
  3. package/dist/Client.d.ts.map +1 -1
  4. package/dist/Client.js +35 -117
  5. package/dist/Client.js.map +1 -1
  6. package/dist/__tests__/harness/memory-storage.d.ts.map +1 -1
  7. package/dist/__tests__/harness/memory-storage.js +6 -19
  8. package/dist/__tests__/harness/memory-storage.js.map +1 -1
  9. package/dist/preset/node.d.ts.map +1 -1
  10. package/dist/preset/node.js +1 -2
  11. package/dist/preset/node.js.map +1 -1
  12. package/dist/preset/test.d.ts.map +1 -1
  13. package/dist/preset/test.js +1 -2
  14. package/dist/preset/test.js.map +1 -1
  15. package/dist/storage/sqlite.d.ts.map +1 -1
  16. package/dist/storage/sqlite.js +5 -15
  17. package/dist/storage/sqlite.js.map +1 -1
  18. package/dist/utils/resolveAtRestAesKey.d.ts +1 -3
  19. package/dist/utils/resolveAtRestAesKey.d.ts.map +1 -1
  20. package/dist/utils/resolveAtRestAesKey.js +7 -13
  21. package/dist/utils/resolveAtRestAesKey.js.map +1 -1
  22. package/dist/utils/verifyKeyBundle.d.ts +1 -2
  23. package/dist/utils/verifyKeyBundle.d.ts.map +1 -1
  24. package/dist/utils/verifyKeyBundle.js +7 -10
  25. package/dist/utils/verifyKeyBundle.js.map +1 -1
  26. package/package.json +3 -3
  27. package/src/Client.ts +47 -181
  28. package/src/__tests__/harness/memory-storage.ts +17 -48
  29. package/src/__tests__/harness/shared-suite.ts +1 -137
  30. package/src/__tests__/platform-browser.test.ts +1 -6
  31. package/src/__tests__/platform-node.test.ts +1 -7
  32. package/src/__tests__/prekey-reuse.test.ts +8 -77
  33. package/src/__tests__/verifyKeyBundle.test.ts +15 -43
  34. package/src/preset/node.ts +1 -5
  35. package/src/preset/test.ts +1 -5
  36. package/src/storage/sqlite.ts +4 -20
  37. package/src/utils/resolveAtRestAesKey.ts +7 -23
  38. package/src/utils/verifyKeyBundle.ts +2 -16
  39. package/dist/utils/fipsMailExtra.d.ts +0 -30
  40. package/dist/utils/fipsMailExtra.d.ts.map +0 -1
  41. package/dist/utils/fipsMailExtra.js +0 -114
  42. package/dist/utils/fipsMailExtra.js.map +0 -1
  43. package/src/utils/fipsMailExtra.ts +0 -164
@@ -4,11 +4,9 @@
4
4
  * Commercial licenses available at vex.wtf
5
5
  */
6
6
 
7
- import type { CryptoProfile } from "@vex-chat/crypto";
8
7
  import type { Device, KeyBundle, KeyBundleEntry } from "@vex-chat/types";
9
8
 
10
9
  import {
11
- fipsEcdhRawPublicKeyFromEcdsaSpkiAsync,
12
10
  xPreKeySignaturePayload,
13
11
  xSignOpenAsync,
14
12
  XUtils,
@@ -17,15 +15,10 @@ import {
17
15
  export async function verifyKeyBundleSignatures(
18
16
  keyBundle: KeyBundle,
19
17
  device: Device,
20
- cryptoProfile: CryptoProfile,
21
18
  ): Promise<void> {
22
19
  const deviceSignKey = XUtils.decodeHex(device.signKey);
23
- const expectedIdentity =
24
- cryptoProfile === "fips"
25
- ? await fipsEcdhRawPublicKeyFromEcdsaSpkiAsync(deviceSignKey)
26
- : deviceSignKey;
27
20
 
28
- if (!XUtils.bytesEqual(expectedIdentity, keyBundle.signKey)) {
21
+ if (!XUtils.bytesEqual(deviceSignKey, keyBundle.signKey)) {
29
22
  throw new Error("Key bundle identity key does not match device.");
30
23
  }
31
24
 
@@ -33,7 +26,6 @@ export async function verifyKeyBundleSignatures(
33
26
  keyBundle.preKey,
34
27
  device,
35
28
  deviceSignKey,
36
- cryptoProfile,
37
29
  "signed",
38
30
  );
39
31
 
@@ -42,7 +34,6 @@ export async function verifyKeyBundleSignatures(
42
34
  keyBundle.otk,
43
35
  device,
44
36
  deviceSignKey,
45
- cryptoProfile,
46
37
  "one-time",
47
38
  );
48
39
  }
@@ -52,7 +43,6 @@ async function verifyKeyBundleEntrySignature(
52
43
  entry: KeyBundleEntry,
53
44
  device: Device,
54
45
  deviceSignKey: Uint8Array,
55
- cryptoProfile: CryptoProfile,
56
46
  kind: "one-time" | "signed",
57
47
  ): Promise<void> {
58
48
  if (entry.deviceID !== device.deviceID) {
@@ -61,11 +51,7 @@ async function verifyKeyBundleEntrySignature(
61
51
  );
62
52
  }
63
53
 
64
- const payload = xPreKeySignaturePayload(
65
- entry.publicKey,
66
- kind,
67
- cryptoProfile,
68
- );
54
+ const payload = xPreKeySignaturePayload(entry.publicKey, kind);
69
55
  const opened = await xSignOpenAsync(entry.signature, deviceSignKey);
70
56
 
71
57
  if (!opened || !XUtils.bytesEqual(opened, payload)) {
@@ -1,30 +0,0 @@
1
- /**
2
- * Copyright (c) 2020-2026 Vex Heavy Industries LLC
3
- * Licensed under AGPL-3.0. See LICENSE for details.
4
- * Commercial licenses available at vex.wtf
5
- */
6
- export declare const FIPS_INITIAL_EXTRA_V1: readonly [number, number, number];
7
- /**
8
- * @returns [signKey, ephKey, ad, index6] (Uint8Array slices, index is always length 6)
9
- */
10
- export declare function decodeFipsInitialExtraV1(extra: Uint8Array): [Uint8Array, Uint8Array, Uint8Array, Uint8Array];
11
- export declare function decodeFipsSubsequentExtraV1(extra: Uint8Array): Uint8Array;
12
- /**
13
- * P-256 fips (Web Crypto) initial mail `extra` bytes (version 1). Length-prefixed segments
14
- * (variable lengths) plus a 6-byte OTK index, AD length includes both identity raw publics.
15
- */
16
- export declare function encodeFipsInitialExtraV1(signPub: Uint8Array, ephPub: Uint8Array, pk: Uint8Array, ad: Uint8Array, index6: Uint8Array): Uint8Array;
17
- /**
18
- * P-256 fips subsequent mail: session lookup key (variable, typically 65B uncompressed ECDH public).
19
- */
20
- export declare function encodeFipsSubsequentExtraV1(sessionPub: Uint8Array): Uint8Array;
21
- /**
22
- * P-256 identity AD: concatenation of our and their P-256 ECDH identity publics (uncompressed, 65B each if standard).
23
- * Used instead of `xEncode` + X25519 for FIPS.
24
- */
25
- export declare function fipsP256AdFromIdentityPubs(ourIdentityRaw: Uint8Array, theirIdentityFromBundle: Uint8Array): Uint8Array;
26
- /** P-256: message bytes signed to bind a prekey (FIPS) — 1-byte tag + uncompressed public. */
27
- export declare function fipsP256PreKeySignPayload(preKeyRawPublic: Uint8Array): Uint8Array;
28
- export declare function isFipsInitialExtraV1(buf: Uint8Array): boolean;
29
- export declare function isFipsSubsequentExtraV1(buf: Uint8Array): boolean;
30
- //# sourceMappingURL=fipsMailExtra.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fipsMailExtra.d.ts","sourceRoot":"","sources":["../../src/utils/fipsMailExtra.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,eAAO,MAAM,qBAAqB,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAE1D,CAAC;AAMX;;GAEG;AACH,wBAAgB,wBAAwB,CACpC,KAAK,EAAE,UAAU,GAClB,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,CAAC,CAgClD;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAazE;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CACpC,OAAO,EAAE,UAAU,EACnB,MAAM,EAAE,UAAU,EAClB,EAAE,EAAE,UAAU,EACd,EAAE,EAAE,UAAU,EACd,MAAM,EAAE,UAAU,GACnB,UAAU,CAiBZ;AAED;;GAEG;AACH,wBAAgB,2BAA2B,CACvC,UAAU,EAAE,UAAU,GACvB,UAAU,CASZ;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CACtC,cAAc,EAAE,UAAU,EAC1B,uBAAuB,EAAE,UAAU,GACpC,UAAU,CAEZ;AAED,8FAA8F;AAC9F,wBAAgB,yBAAyB,CACrC,eAAe,EAAE,UAAU,GAC5B,UAAU,CAEZ;AAED,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAO7D;AAED,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAOhE"}
@@ -1,114 +0,0 @@
1
- /**
2
- * Copyright (c) 2020-2026 Vex Heavy Industries LLC
3
- * Licensed under AGPL-3.0. See LICENSE for details.
4
- * Commercial licenses available at vex.wtf
5
- */
6
- import { xConcat } from "@vex-chat/crypto";
7
- export const FIPS_INITIAL_EXTRA_V1 = [
8
- 0xf1, 0x02, 0x01,
9
- ];
10
- const FIPS_SUBSEQUENT_V1 = [
11
- 0xf1, 0x03, 0x01,
12
- ];
13
- /**
14
- * @returns [signKey, ephKey, ad, index6] (Uint8Array slices, index is always length 6)
15
- */
16
- export function decodeFipsInitialExtraV1(extra) {
17
- if (!isFipsInitialExtraV1(extra)) {
18
- throw new Error("FIPS: not a v1 initial extra payload.");
19
- }
20
- let p = 3;
21
- const readSeg = (label) => {
22
- const len = u16read(extra, p);
23
- if (len < 0) {
24
- throw new Error(`FIPS: extra too short (need len for ${label} at ${String(p)}).`);
25
- }
26
- p += 2;
27
- if (p + len > extra.length) {
28
- throw new Error(`FIPS: truncated ${label} segment (need ${String(len + p)} but got ${String(extra.length)}).`);
29
- }
30
- const seg = extra.subarray(p, p + len);
31
- p += len;
32
- return seg;
33
- };
34
- const signKey = readSeg("sign");
35
- const ephKey = readSeg("eph");
36
- const _pk = readSeg("PK");
37
- const ad = readSeg("ad");
38
- void _pk; // not required for DH / open (matches tweetnacl extra layout)
39
- if (p + 6 > extra.length) {
40
- throw new Error("FIPS: missing 6-byte OTK index at end of extra.");
41
- }
42
- const index6 = extra.subarray(p, p + 6);
43
- return [signKey, ephKey, ad, index6];
44
- }
45
- export function decodeFipsSubsequentExtraV1(extra) {
46
- if (!isFipsSubsequentExtraV1(extra)) {
47
- throw new Error("FIPS: not a v1 subsequent extra.");
48
- }
49
- const len = u16read(extra, 3);
50
- if (len < 0) {
51
- throw new Error("FIPS: bad subsequent extra length field.");
52
- }
53
- const p = 5;
54
- if (p + len > extra.length) {
55
- throw new Error("FIPS: subsequent extra truncated.");
56
- }
57
- return extra.subarray(p, p + len);
58
- }
59
- /**
60
- * P-256 fips (Web Crypto) initial mail `extra` bytes (version 1). Length-prefixed segments
61
- * (variable lengths) plus a 6-byte OTK index, AD length includes both identity raw publics.
62
- */
63
- export function encodeFipsInitialExtraV1(signPub, ephPub, pk, ad, index6) {
64
- if (index6.length !== 6) {
65
- throw new Error("FIPS: OTK index must be 6 bytes.");
66
- }
67
- const a = (len) => new Uint8Array([...u16be(len)]);
68
- return xConcat(Uint8Array.from([...FIPS_INITIAL_EXTRA_V1]), a(signPub.length), signPub, a(ephPub.length), ephPub, a(pk.length), pk, a(ad.length), ad, index6);
69
- }
70
- /**
71
- * P-256 fips subsequent mail: session lookup key (variable, typically 65B uncompressed ECDH public).
72
- */
73
- export function encodeFipsSubsequentExtraV1(sessionPub) {
74
- if (sessionPub.length > 65535) {
75
- throw new Error("FIPS: session public key is too long.");
76
- }
77
- return xConcat(Uint8Array.from([...FIPS_SUBSEQUENT_V1]), new Uint8Array([...u16be(sessionPub.length)]), sessionPub);
78
- }
79
- /**
80
- * P-256 identity AD: concatenation of our and their P-256 ECDH identity publics (uncompressed, 65B each if standard).
81
- * Used instead of `xEncode` + X25519 for FIPS.
82
- */
83
- export function fipsP256AdFromIdentityPubs(ourIdentityRaw, theirIdentityFromBundle) {
84
- return xConcat(ourIdentityRaw, theirIdentityFromBundle);
85
- }
86
- /** P-256: message bytes signed to bind a prekey (FIPS) — 1-byte tag + uncompressed public. */
87
- export function fipsP256PreKeySignPayload(preKeyRawPublic) {
88
- return xConcat(new Uint8Array([0xa1]), preKeyRawPublic);
89
- }
90
- export function isFipsInitialExtraV1(buf) {
91
- return (buf.length >= 3 &&
92
- buf[0] === FIPS_INITIAL_EXTRA_V1[0] &&
93
- buf[1] === FIPS_INITIAL_EXTRA_V1[1] &&
94
- buf[2] === FIPS_INITIAL_EXTRA_V1[2]);
95
- }
96
- export function isFipsSubsequentExtraV1(buf) {
97
- return (buf.length >= 3 &&
98
- buf[0] === FIPS_SUBSEQUENT_V1[0] &&
99
- buf[1] === FIPS_SUBSEQUENT_V1[1] &&
100
- buf[2] === FIPS_SUBSEQUENT_V1[2]);
101
- }
102
- function u16be(n) {
103
- if (!Number.isInteger(n) || n < 0 || n > 65535) {
104
- throw new Error(`FIPS: invalid u16 length: ${String(n)}`);
105
- }
106
- return [n >> 8, n & 0xff];
107
- }
108
- function u16read(buf, offset) {
109
- if (offset + 2 > buf.length) {
110
- return -1;
111
- }
112
- return ((buf[offset] ?? 0) * 256 + (buf[offset + 1] ?? 0)) >>> 0;
113
- }
114
- //# sourceMappingURL=fipsMailExtra.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fipsMailExtra.js","sourceRoot":"","sources":["../../src/utils/fipsMailExtra.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,MAAM,CAAC,MAAM,qBAAqB,GAAsC;IACpE,IAAI,EAAE,IAAI,EAAE,IAAI;CACV,CAAC;AAEX,MAAM,kBAAkB,GAAsC;IAC1D,IAAI,EAAE,IAAI,EAAE,IAAI;CACV,CAAC;AAEX;;GAEG;AACH,MAAM,UAAU,wBAAwB,CACpC,KAAiB;IAEjB,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC7D,CAAC;IACD,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,MAAM,OAAO,GAAG,CAAC,KAAa,EAAE,EAAE;QAC9B,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAC9B,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC;YACV,MAAM,IAAI,KAAK,CACX,uCAAuC,KAAK,OAAO,MAAM,CAAC,CAAC,CAAC,IAAI,CACnE,CAAC;QACN,CAAC;QACD,CAAC,IAAI,CAAC,CAAC;QACP,IAAI,CAAC,GAAG,GAA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E,CAAC,GAAG,IAAI,CAAC,CAAC;AAC9B,CAAC;AAED,SAAS,OAAO,CAAC,GAAe,EAAE,MAAc;IAC5C,IAAI,MAAM,GAAG,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC;QAC1B,OAAO,CAAC,CAAC,CAAC;IACd,CAAC;IACD,OAAO,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AACrE,CAAC"}
@@ -1,164 +0,0 @@
1
- /**
2
- * Copyright (c) 2020-2026 Vex Heavy Industries LLC
3
- * Licensed under AGPL-3.0. See LICENSE for details.
4
- * Commercial licenses available at vex.wtf
5
- */
6
-
7
- import { xConcat } from "@vex-chat/crypto";
8
-
9
- export const FIPS_INITIAL_EXTRA_V1: readonly [number, number, number] = [
10
- 0xf1, 0x02, 0x01,
11
- ] as const;
12
-
13
- const FIPS_SUBSEQUENT_V1: readonly [number, number, number] = [
14
- 0xf1, 0x03, 0x01,
15
- ] as const;
16
-
17
- /**
18
- * @returns [signKey, ephKey, ad, index6] (Uint8Array slices, index is always length 6)
19
- */
20
- export function decodeFipsInitialExtraV1(
21
- extra: Uint8Array,
22
- ): [Uint8Array, Uint8Array, Uint8Array, Uint8Array] {
23
- if (!isFipsInitialExtraV1(extra)) {
24
- throw new Error("FIPS: not a v1 initial extra payload.");
25
- }
26
- let p = 3;
27
- const readSeg = (label: string) => {
28
- const len = u16read(extra, p);
29
- if (len < 0) {
30
- throw new Error(
31
- `FIPS: extra too short (need len for ${label} at ${String(p)}).`,
32
- );
33
- }
34
- p += 2;
35
- if (p + len > extra.length) {
36
- throw new Error(
37
- `FIPS: truncated ${label} segment (need ${String(len + p)} but got ${String(extra.length)}).`,
38
- );
39
- }
40
- const seg = extra.subarray(p, p + len);
41
- p += len;
42
- return seg;
43
- };
44
- const signKey = readSeg("sign");
45
- const ephKey = readSeg("eph");
46
- const _pk = readSeg("PK");
47
- const ad = readSeg("ad");
48
- void _pk; // not required for DH / open (matches tweetnacl extra layout)
49
- if (p + 6 > extra.length) {
50
- throw new Error("FIPS: missing 6-byte OTK index at end of extra.");
51
- }
52
- const index6 = extra.subarray(p, p + 6);
53
- return [signKey, ephKey, ad, index6];
54
- }
55
-
56
- export function decodeFipsSubsequentExtraV1(extra: Uint8Array): Uint8Array {
57
- if (!isFipsSubsequentExtraV1(extra)) {
58
- throw new Error("FIPS: not a v1 subsequent extra.");
59
- }
60
- const len = u16read(extra, 3);
61
- if (len < 0) {
62
- throw new Error("FIPS: bad subsequent extra length field.");
63
- }
64
- const p = 5;
65
- if (p + len > extra.length) {
66
- throw new Error("FIPS: subsequent extra truncated.");
67
- }
68
- return extra.subarray(p, p + len);
69
- }
70
-
71
- /**
72
- * P-256 fips (Web Crypto) initial mail `extra` bytes (version 1). Length-prefixed segments
73
- * (variable lengths) plus a 6-byte OTK index, AD length includes both identity raw publics.
74
- */
75
- export function encodeFipsInitialExtraV1(
76
- signPub: Uint8Array,
77
- ephPub: Uint8Array,
78
- pk: Uint8Array,
79
- ad: Uint8Array,
80
- index6: Uint8Array,
81
- ): Uint8Array {
82
- if (index6.length !== 6) {
83
- throw new Error("FIPS: OTK index must be 6 bytes.");
84
- }
85
- const a = (len: number) => new Uint8Array([...u16be(len)]);
86
- return xConcat(
87
- Uint8Array.from([...FIPS_INITIAL_EXTRA_V1]),
88
- a(signPub.length),
89
- signPub,
90
- a(ephPub.length),
91
- ephPub,
92
- a(pk.length),
93
- pk,
94
- a(ad.length),
95
- ad,
96
- index6,
97
- );
98
- }
99
-
100
- /**
101
- * P-256 fips subsequent mail: session lookup key (variable, typically 65B uncompressed ECDH public).
102
- */
103
- export function encodeFipsSubsequentExtraV1(
104
- sessionPub: Uint8Array,
105
- ): Uint8Array {
106
- if (sessionPub.length > 65535) {
107
- throw new Error("FIPS: session public key is too long.");
108
- }
109
- return xConcat(
110
- Uint8Array.from([...FIPS_SUBSEQUENT_V1]),
111
- new Uint8Array([...u16be(sessionPub.length)]),
112
- sessionPub,
113
- );
114
- }
115
-
116
- /**
117
- * P-256 identity AD: concatenation of our and their P-256 ECDH identity publics (uncompressed, 65B each if standard).
118
- * Used instead of `xEncode` + X25519 for FIPS.
119
- */
120
- export function fipsP256AdFromIdentityPubs(
121
- ourIdentityRaw: Uint8Array,
122
- theirIdentityFromBundle: Uint8Array,
123
- ): Uint8Array {
124
- return xConcat(ourIdentityRaw, theirIdentityFromBundle);
125
- }
126
-
127
- /** P-256: message bytes signed to bind a prekey (FIPS) — 1-byte tag + uncompressed public. */
128
- export function fipsP256PreKeySignPayload(
129
- preKeyRawPublic: Uint8Array,
130
- ): Uint8Array {
131
- return xConcat(new Uint8Array([0xa1]), preKeyRawPublic);
132
- }
133
-
134
- export function isFipsInitialExtraV1(buf: Uint8Array): boolean {
135
- return (
136
- buf.length >= 3 &&
137
- buf[0] === FIPS_INITIAL_EXTRA_V1[0] &&
138
- buf[1] === FIPS_INITIAL_EXTRA_V1[1] &&
139
- buf[2] === FIPS_INITIAL_EXTRA_V1[2]
140
- );
141
- }
142
-
143
- export function isFipsSubsequentExtraV1(buf: Uint8Array): boolean {
144
- return (
145
- buf.length >= 3 &&
146
- buf[0] === FIPS_SUBSEQUENT_V1[0] &&
147
- buf[1] === FIPS_SUBSEQUENT_V1[1] &&
148
- buf[2] === FIPS_SUBSEQUENT_V1[2]
149
- );
150
- }
151
-
152
- function u16be(n: number): [number, number] {
153
- if (!Number.isInteger(n) || n < 0 || n > 65535) {
154
- throw new Error(`FIPS: invalid u16 length: ${String(n)}`);
155
- }
156
- return [n >> 8, n & 0xff];
157
- }
158
-
159
- function u16read(buf: Uint8Array, offset: number): number {
160
- if (offset + 2 > buf.length) {
161
- return -1;
162
- }
163
- return ((buf[offset] ?? 0) * 256 + (buf[offset + 1] ?? 0)) >>> 0;
164
- }