@mysten/seal 0.2.0 → 0.3.1

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 (108) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/bcs.d.ts +132 -0
  3. package/dist/cjs/bcs.js +67 -0
  4. package/dist/cjs/bcs.js.map +7 -0
  5. package/dist/cjs/client.d.ts +83 -0
  6. package/dist/cjs/client.js +283 -0
  7. package/dist/cjs/client.js.map +7 -0
  8. package/dist/cjs/decrypt.d.ts +15 -0
  9. package/dist/cjs/decrypt.js +94 -0
  10. package/dist/cjs/decrypt.js.map +7 -0
  11. package/dist/cjs/dem.d.ts +36 -0
  12. package/dist/cjs/dem.js +173 -0
  13. package/dist/cjs/dem.js.map +7 -0
  14. package/dist/cjs/elgamal.js.map +2 -2
  15. package/dist/cjs/encrypt.d.ts +14 -5
  16. package/dist/cjs/encrypt.js +52 -22
  17. package/dist/cjs/encrypt.js.map +3 -3
  18. package/dist/cjs/error.d.ts +58 -0
  19. package/dist/cjs/error.js +175 -0
  20. package/dist/cjs/error.js.map +7 -0
  21. package/dist/cjs/ibe.d.ts +13 -17
  22. package/dist/cjs/ibe.js +25 -28
  23. package/dist/cjs/ibe.js.map +2 -2
  24. package/dist/cjs/index.d.ts +3 -5
  25. package/dist/cjs/index.js +5 -11
  26. package/dist/cjs/index.js.map +2 -2
  27. package/dist/cjs/kdf.d.ts +7 -2
  28. package/dist/cjs/kdf.js +37 -2
  29. package/dist/cjs/kdf.js.map +3 -3
  30. package/dist/cjs/key-server.d.ts +5 -5
  31. package/dist/cjs/key-server.js +24 -21
  32. package/dist/cjs/key-server.js.map +2 -2
  33. package/dist/cjs/keys.d.ts +17 -0
  34. package/dist/cjs/keys.js +61 -0
  35. package/dist/cjs/keys.js.map +7 -0
  36. package/dist/cjs/session-key.d.ts +20 -14
  37. package/dist/cjs/session-key.js +90 -23
  38. package/dist/cjs/session-key.js.map +2 -2
  39. package/dist/cjs/types.d.ts +1 -86
  40. package/dist/cjs/types.js +0 -32
  41. package/dist/cjs/types.js.map +2 -2
  42. package/dist/cjs/utils.d.ts +2 -1
  43. package/dist/cjs/utils.js +17 -5
  44. package/dist/cjs/utils.js.map +2 -2
  45. package/dist/cjs/version.d.ts +1 -0
  46. package/dist/cjs/version.js +25 -0
  47. package/dist/cjs/version.js.map +7 -0
  48. package/dist/esm/bcs.d.ts +132 -0
  49. package/dist/esm/bcs.js +47 -0
  50. package/dist/esm/bcs.js.map +7 -0
  51. package/dist/esm/client.d.ts +83 -0
  52. package/dist/esm/client.js +268 -0
  53. package/dist/esm/client.js.map +7 -0
  54. package/dist/esm/decrypt.d.ts +15 -0
  55. package/dist/esm/decrypt.js +74 -0
  56. package/dist/esm/decrypt.js.map +7 -0
  57. package/dist/esm/dem.d.ts +36 -0
  58. package/dist/esm/dem.js +153 -0
  59. package/dist/esm/dem.js.map +7 -0
  60. package/dist/esm/elgamal.js.map +2 -2
  61. package/dist/esm/encrypt.d.ts +14 -5
  62. package/dist/esm/encrypt.js +49 -19
  63. package/dist/esm/encrypt.js.map +3 -3
  64. package/dist/esm/error.d.ts +58 -0
  65. package/dist/esm/error.js +155 -0
  66. package/dist/esm/error.js.map +7 -0
  67. package/dist/esm/ibe.d.ts +13 -17
  68. package/dist/esm/ibe.js +25 -28
  69. package/dist/esm/ibe.js.map +2 -2
  70. package/dist/esm/index.d.ts +3 -5
  71. package/dist/esm/index.js +5 -16
  72. package/dist/esm/index.js.map +2 -2
  73. package/dist/esm/kdf.d.ts +7 -2
  74. package/dist/esm/kdf.js +37 -2
  75. package/dist/esm/kdf.js.map +3 -3
  76. package/dist/esm/key-server.d.ts +5 -5
  77. package/dist/esm/key-server.js +29 -21
  78. package/dist/esm/key-server.js.map +2 -2
  79. package/dist/esm/keys.d.ts +17 -0
  80. package/dist/esm/keys.js +41 -0
  81. package/dist/esm/keys.js.map +7 -0
  82. package/dist/esm/session-key.d.ts +20 -14
  83. package/dist/esm/session-key.js +95 -24
  84. package/dist/esm/session-key.js.map +2 -2
  85. package/dist/esm/types.d.ts +1 -86
  86. package/dist/esm/types.js +0 -28
  87. package/dist/esm/types.js.map +3 -3
  88. package/dist/esm/utils.d.ts +2 -1
  89. package/dist/esm/utils.js +17 -5
  90. package/dist/esm/utils.js.map +2 -2
  91. package/dist/esm/version.d.ts +1 -0
  92. package/dist/esm/version.js +5 -0
  93. package/dist/esm/version.js.map +7 -0
  94. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  95. package/dist/tsconfig.tsbuildinfo +1 -1
  96. package/package.json +6 -5
  97. package/dist/cjs/aes.d.ts +0 -18
  98. package/dist/cjs/aes.js +0 -111
  99. package/dist/cjs/aes.js.map +0 -7
  100. package/dist/cjs/key-store.d.ts +0 -49
  101. package/dist/cjs/key-store.js +0 -203
  102. package/dist/cjs/key-store.js.map +0 -7
  103. package/dist/esm/aes.d.ts +0 -18
  104. package/dist/esm/aes.js +0 -91
  105. package/dist/esm/aes.js.map +0 -7
  106. package/dist/esm/key-store.d.ts +0 -49
  107. package/dist/esm/key-store.js +0 -183
  108. package/dist/esm/key-store.js.map +0 -7
@@ -0,0 +1,155 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
6
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
7
+ var _SealAPIError_static, generate_fn;
8
+ class SealError extends Error {
9
+ }
10
+ class UserError extends SealError {
11
+ }
12
+ const _SealAPIError = class _SealAPIError extends SealError {
13
+ constructor(message, requestId, status) {
14
+ super(message);
15
+ this.requestId = requestId;
16
+ this.status = status;
17
+ }
18
+ static async assertResponse(response, requestId) {
19
+ var _a;
20
+ if (response.ok) {
21
+ return;
22
+ }
23
+ let errorInstance;
24
+ try {
25
+ const text = await response.text();
26
+ const error = JSON.parse(text)["error"];
27
+ errorInstance = __privateMethod(_a = _SealAPIError, _SealAPIError_static, generate_fn).call(_a, error, requestId);
28
+ } catch (e) {
29
+ errorInstance = new GeneralError(response.statusText, requestId, response.status);
30
+ }
31
+ throw errorInstance;
32
+ }
33
+ };
34
+ _SealAPIError_static = new WeakSet();
35
+ generate_fn = function(message, requestId, status) {
36
+ switch (message) {
37
+ case "InvalidPTB":
38
+ return new InvalidPTBError(requestId);
39
+ case "InvalidPackage":
40
+ return new InvalidPackageError(requestId);
41
+ case "NoAccess":
42
+ return new NoAccessError(requestId);
43
+ case "InvalidCertificate":
44
+ return new ExpiredSessionKeyError(requestId);
45
+ case "OldPackageVersion":
46
+ return new OldPackageError(requestId);
47
+ case "InvalidSignature":
48
+ return new InvalidUserSignatureError(requestId);
49
+ case "InvalidSessionSignature":
50
+ return new InvalidSessionKeySignatureError(requestId);
51
+ case "Failure":
52
+ return new InternalError(requestId);
53
+ default:
54
+ return new GeneralError(message, requestId, status);
55
+ }
56
+ };
57
+ __privateAdd(_SealAPIError, _SealAPIError_static);
58
+ let SealAPIError = _SealAPIError;
59
+ class InvalidPTBError extends SealAPIError {
60
+ constructor(requestId) {
61
+ super("PTB does not conform to the expected format", requestId);
62
+ }
63
+ }
64
+ class InvalidPackageError extends SealAPIError {
65
+ constructor(requestId) {
66
+ super("Package ID used in PTB is invalid", requestId);
67
+ }
68
+ }
69
+ class OldPackageError extends SealAPIError {
70
+ constructor(requestId) {
71
+ super("PTB must call the latest version of the package", requestId);
72
+ }
73
+ }
74
+ class InvalidUserSignatureError extends SealAPIError {
75
+ constructor(requestId) {
76
+ super("User signature on the session key is invalid", requestId);
77
+ }
78
+ }
79
+ class InvalidSessionKeySignatureError extends SealAPIError {
80
+ constructor(requestId) {
81
+ super("Session key signature is invalid", requestId);
82
+ }
83
+ }
84
+ class NoAccessError extends SealAPIError {
85
+ constructor(requestId) {
86
+ super("User does not have access to one or more of the requested keys", requestId);
87
+ }
88
+ }
89
+ class ExpiredSessionKeyError extends SealAPIError {
90
+ constructor(requestId) {
91
+ super("Session key has expired", requestId);
92
+ }
93
+ }
94
+ class InternalError extends SealAPIError {
95
+ constructor(requestId) {
96
+ super("Internal server error, caller should retry", requestId);
97
+ }
98
+ }
99
+ class GeneralError extends SealAPIError {
100
+ }
101
+ class InvalidPersonalMessageSignatureError extends UserError {
102
+ }
103
+ class InvalidGetObjectError extends UserError {
104
+ }
105
+ class UnsupportedFeatureError extends UserError {
106
+ }
107
+ class UnsupportedNetworkError extends UserError {
108
+ }
109
+ class InvalidKeyServerError extends UserError {
110
+ }
111
+ class InvalidCiphertextError extends UserError {
112
+ }
113
+ class InvalidThresholdError extends UserError {
114
+ }
115
+ class InconsistentKeyServersError extends UserError {
116
+ }
117
+ function toMajorityError(errors) {
118
+ let maxCount = 0;
119
+ let majorityError = errors[0];
120
+ const counts = /* @__PURE__ */ new Map();
121
+ for (const error of errors) {
122
+ const errorName = error.constructor.name;
123
+ const newCount = (counts.get(errorName) || 0) + 1;
124
+ counts.set(errorName, newCount);
125
+ if (newCount > maxCount) {
126
+ maxCount = newCount;
127
+ majorityError = error;
128
+ }
129
+ }
130
+ return majorityError;
131
+ }
132
+ export {
133
+ ExpiredSessionKeyError,
134
+ GeneralError,
135
+ InconsistentKeyServersError,
136
+ InternalError,
137
+ InvalidCiphertextError,
138
+ InvalidGetObjectError,
139
+ InvalidKeyServerError,
140
+ InvalidPTBError,
141
+ InvalidPackageError,
142
+ InvalidPersonalMessageSignatureError,
143
+ InvalidSessionKeySignatureError,
144
+ InvalidThresholdError,
145
+ InvalidUserSignatureError,
146
+ NoAccessError,
147
+ OldPackageError,
148
+ SealAPIError,
149
+ SealError,
150
+ UnsupportedFeatureError,
151
+ UnsupportedNetworkError,
152
+ UserError,
153
+ toMajorityError
154
+ };
155
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/error.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport class SealError extends Error {}\n\nexport class UserError extends SealError {}\n\n// Errors returned by the Seal server\nexport class SealAPIError extends SealError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic requestId?: string,\n\t\tpublic status?: number,\n\t) {\n\t\tsuper(message);\n\t}\n\n\tstatic #generate(message: string, requestId: string, status?: number) {\n\t\tswitch (message) {\n\t\t\tcase 'InvalidPTB':\n\t\t\t\treturn new InvalidPTBError(requestId);\n\t\t\tcase 'InvalidPackage':\n\t\t\t\treturn new InvalidPackageError(requestId);\n\t\t\tcase 'NoAccess':\n\t\t\t\treturn new NoAccessError(requestId);\n\t\t\tcase 'InvalidCertificate':\n\t\t\t\treturn new ExpiredSessionKeyError(requestId);\n\t\t\tcase 'OldPackageVersion':\n\t\t\t\treturn new OldPackageError(requestId);\n\t\t\tcase 'InvalidSignature':\n\t\t\t\treturn new InvalidUserSignatureError(requestId);\n\t\t\tcase 'InvalidSessionSignature':\n\t\t\t\treturn new InvalidSessionKeySignatureError(requestId);\n\t\t\tcase 'Failure':\n\t\t\t\treturn new InternalError(requestId);\n\t\t\tdefault:\n\t\t\t\treturn new GeneralError(message, requestId, status);\n\t\t}\n\t}\n\n\tstatic async assertResponse(response: Response, requestId: string) {\n\t\tif (response.ok) {\n\t\t\treturn;\n\t\t}\n\t\tlet errorInstance: SealAPIError;\n\t\ttry {\n\t\t\tconst text = await response.text();\n\t\t\tconst error = JSON.parse(text)['error'];\n\t\t\terrorInstance = SealAPIError.#generate(error, requestId);\n\t\t} catch (e) {\n\t\t\t// If we can't parse the response as JSON or if it doesn't have the expected format,\n\t\t\t// fall back to using the status text\n\t\t\terrorInstance = new GeneralError(response.statusText, requestId, response.status);\n\t\t}\n\t\tthrow errorInstance;\n\t}\n}\n\n// Errors returned by the Seal server that indicate that the PTB is invalid\n\nexport class InvalidPTBError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('PTB does not conform to the expected format', requestId);\n\t}\n}\n\nexport class InvalidPackageError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('Package ID used in PTB is invalid', requestId);\n\t}\n}\n\nexport class OldPackageError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('PTB must call the latest version of the package', requestId);\n\t}\n}\n\n// Errors returned by the Seal server that indicate that the user's signature is invalid\n\nexport class InvalidUserSignatureError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('User signature on the session key is invalid', requestId);\n\t}\n}\n\nexport class InvalidSessionKeySignatureError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('Session key signature is invalid', requestId);\n\t}\n}\n\n/** Server error indicating that the user does not have access to one or more of the requested keys */\nexport class NoAccessError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('User does not have access to one or more of the requested keys', requestId);\n\t}\n}\n\n/** Server error indicating that the session key has expired */\nexport class ExpiredSessionKeyError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('Session key has expired', requestId);\n\t}\n}\n\n/** Internal server error, caller should retry */\nexport class InternalError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('Internal server error, caller should retry', requestId);\n\t}\n}\n\n/** General server errors that are not specific to the Seal API (e.g., 404 \"Not Found\") */\nexport class GeneralError extends SealAPIError {}\n\n// Errors returned by the SDK\nexport class InvalidPersonalMessageSignatureError extends UserError {}\nexport class InvalidGetObjectError extends UserError {}\nexport class UnsupportedFeatureError extends UserError {}\nexport class UnsupportedNetworkError extends UserError {}\nexport class InvalidKeyServerError extends UserError {}\nexport class InvalidCiphertextError extends UserError {}\nexport class InvalidThresholdError extends UserError {}\nexport class InconsistentKeyServersError extends UserError {}\n\nexport function toMajorityError(errors: Error[]): Error {\n\tlet maxCount = 0;\n\tlet majorityError = errors[0];\n\tconst counts = new Map<string, number>();\n\tfor (const error of errors) {\n\t\tconst errorName = error.constructor.name;\n\t\tconst newCount = (counts.get(errorName) || 0) + 1;\n\t\tcounts.set(errorName, newCount);\n\n\t\tif (newCount > maxCount) {\n\t\t\tmaxCount = newCount;\n\t\t\tmajorityError = error;\n\t\t}\n\t}\n\n\treturn majorityError;\n}\n"],
5
+ "mappings": "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",
6
+ "names": []
7
+ }
package/dist/esm/ibe.d.ts CHANGED
@@ -1,6 +1,6 @@
1
+ import type { IBEEncryptions } from './bcs.js';
1
2
  import { G1Element, G2Element } from './bls12381.js';
2
3
  import type { KeyServer } from './key-server.js';
3
- import type { IBEEncryptionsType } from './types.js';
4
4
  /**
5
5
  * The domain separation tag for the hash-to-group function.
6
6
  */
@@ -13,12 +13,8 @@ export declare const DST_POP: Uint8Array;
13
13
  * The interface for the key servers.
14
14
  */
15
15
  export declare abstract class IBEServers {
16
- protected readonly object_ids: Uint8Array[];
17
- protected constructor(object_ids: Uint8Array[]);
18
- /**
19
- * The object IDs of the key servers.
20
- */
21
- getObjectIds(): Uint8Array[];
16
+ objectIds: string[];
17
+ constructor(objectIds: string[]);
22
18
  /**
23
19
  * The number of key servers.
24
20
  */
@@ -27,25 +23,25 @@ export declare abstract class IBEServers {
27
23
  * Encrypt a batch of messages for the given identity.
28
24
  *
29
25
  * @param id The identity.
30
- * @param msg_and_infos The messages and an additional info parameter which will be included in the KDF.
26
+ * @param msgAndIndices The messages and the corresponding indices of the share being encrypted.
31
27
  * @returns The encrypted messages.
32
28
  */
33
- abstract encryptBatched(id: Uint8Array, msg_and_infos: {
29
+ abstract encryptBatched(id: Uint8Array, msgAndIndices: {
34
30
  msg: Uint8Array;
35
- info: Uint8Array;
36
- }[]): IBEEncryptionsType;
31
+ index: number;
32
+ }[], randomnessKey: Uint8Array): typeof IBEEncryptions.$inferType;
37
33
  }
38
34
  /**
39
35
  * Identity-based encryption based on the Boneh-Franklin IBE scheme.
40
36
  * This object represents a set of key servers that can be used to encrypt messages for a given identity.
41
37
  */
42
38
  export declare class BonehFranklinBLS12381Services extends IBEServers {
43
- readonly public_keys: G2Element[];
39
+ readonly publicKeys: G2Element[];
44
40
  constructor(services: KeyServer[]);
45
- encryptBatched(id: Uint8Array, msg_and_infos: {
41
+ encryptBatched(id: Uint8Array, msgAndIndices: {
46
42
  msg: Uint8Array;
47
- info: Uint8Array;
48
- }[]): IBEEncryptionsType;
43
+ index: number;
44
+ }[], randomnessKey: Uint8Array): typeof IBEEncryptions.$inferType;
49
45
  /**
50
46
  * Returns true if the user secret key is valid for the given public key and id.
51
47
  * @param user_secret_key - The user secret key.
@@ -53,7 +49,7 @@ export declare class BonehFranklinBLS12381Services extends IBEServers {
53
49
  * @param public_key - The public key.
54
50
  * @returns True if the user secret key is valid for the given public key and id.
55
51
  */
56
- static verifyUserSecretKey(user_secret_key: G1Element, id: Uint8Array, public_key: G2Element): boolean;
52
+ static verifyUserSecretKey(userSecretKey: G1Element, id: string, publicKey: G2Element): boolean;
57
53
  /**
58
54
  * Identity-based decryption.
59
55
  *
@@ -63,5 +59,5 @@ export declare class BonehFranklinBLS12381Services extends IBEServers {
63
59
  * @param info An info parameter also included in the KDF.
64
60
  * @returns The decrypted message.
65
61
  */
66
- static decrypt(nonce: G2Element, sk: G1Element, ciphertext: Uint8Array, info: Uint8Array): Uint8Array;
62
+ static decrypt(nonce: G2Element, sk: G1Element, ciphertext: Uint8Array, id: Uint8Array, [objectId, index]: [string, number]): Uint8Array;
67
63
  }
package/dist/esm/ibe.js CHANGED
@@ -1,42 +1,39 @@
1
+ import { fromHex } from "@mysten/bcs";
1
2
  import { G1Element, G2Element, Scalar } from "./bls12381.js";
2
3
  import { kdf } from "./kdf.js";
3
4
  import { xor } from "./utils.js";
4
5
  const DST = new TextEncoder().encode("SUI-SEAL-IBE-BLS12381-00");
5
- const DST_POP = new TextEncoder().encode("SUI-SEAL-IBE-BLS12381-00-POP");
6
+ const DST_POP = new TextEncoder().encode("SUI-SEAL-IBE-BLS12381-POP-00");
6
7
  class IBEServers {
7
- constructor(object_ids) {
8
- this.object_ids = object_ids;
9
- }
10
- /**
11
- * The object IDs of the key servers.
12
- */
13
- getObjectIds() {
14
- return this.object_ids;
8
+ constructor(objectIds) {
9
+ this.objectIds = objectIds;
15
10
  }
16
11
  /**
17
12
  * The number of key servers.
18
13
  */
19
14
  size() {
20
- return this.object_ids.length;
15
+ return this.objectIds.length;
21
16
  }
22
17
  }
23
18
  class BonehFranklinBLS12381Services extends IBEServers {
24
19
  constructor(services) {
25
20
  super(services.map((service) => service.objectId));
26
- this.public_keys = services.map((service) => G2Element.fromBytes(service.pk));
21
+ this.publicKeys = services.map((service) => G2Element.fromBytes(service.pk));
27
22
  }
28
- encryptBatched(id, msg_and_infos) {
29
- if (this.public_keys.length === 0 || this.public_keys.length !== msg_and_infos.length) {
30
- throw new Error("Invalid input");
23
+ encryptBatched(id, msgAndIndices, randomnessKey) {
24
+ if (this.publicKeys.length === 0 || this.publicKeys.length !== msgAndIndices.length) {
25
+ throw new Error("Invalid public keys");
31
26
  }
32
- const [nonce, keys] = encapBatched(this.public_keys, id);
33
- const encrypted_msgs = msg_and_infos.map(
34
- (msg_and_info, i) => xor(msg_and_info.msg, kdf(keys[i], msg_and_info.info))
27
+ const [r, nonce, keys] = encapBatched(this.publicKeys, id);
28
+ const encryptedShares = msgAndIndices.map(
29
+ (msgAndIndex, i) => xor(msgAndIndex.msg, kdf(keys[i], nonce, id, this.objectIds[i], msgAndIndex.index))
35
30
  );
31
+ const encryptedRandomness = xor(randomnessKey, r.toBytes());
36
32
  return {
37
33
  BonehFranklinBLS12381: {
38
- encapsulation: nonce.toBytes(),
39
- shares: encrypted_msgs
34
+ nonce: nonce.toBytes(),
35
+ encryptedShares,
36
+ encryptedRandomness
40
37
  },
41
38
  $kind: "BonehFranklinBLS12381"
42
39
  };
@@ -48,9 +45,9 @@ class BonehFranklinBLS12381Services extends IBEServers {
48
45
  * @param public_key - The public key.
49
46
  * @returns True if the user secret key is valid for the given public key and id.
50
47
  */
51
- static verifyUserSecretKey(user_secret_key, id, public_key) {
52
- const lhs = user_secret_key.pairing(G2Element.generator()).toBytes();
53
- const rhs = G1Element.hashToCurve(id).pairing(public_key).toBytes();
48
+ static verifyUserSecretKey(userSecretKey, id, publicKey) {
49
+ const lhs = userSecretKey.pairing(G2Element.generator()).toBytes();
50
+ const rhs = G1Element.hashToCurve(fromHex(id)).pairing(publicKey).toBytes();
54
51
  return lhs.length === rhs.length && lhs.every((value, index) => value === rhs[index]);
55
52
  }
56
53
  /**
@@ -62,18 +59,18 @@ class BonehFranklinBLS12381Services extends IBEServers {
62
59
  * @param info An info parameter also included in the KDF.
63
60
  * @returns The decrypted message.
64
61
  */
65
- static decrypt(nonce, sk, ciphertext, info) {
66
- return xor(ciphertext, kdf(decap(nonce, sk), info));
62
+ static decrypt(nonce, sk, ciphertext, id, [objectId, index]) {
63
+ return xor(ciphertext, kdf(decap(nonce, sk), nonce, id, objectId, index));
67
64
  }
68
65
  }
69
- function encapBatched(public_keys, id) {
70
- if (public_keys.length === 0) {
71
- throw new Error("Invalid input");
66
+ function encapBatched(publicKeys, id) {
67
+ if (publicKeys.length === 0) {
68
+ throw new Error("No public keys provided");
72
69
  }
73
70
  const r = Scalar.random();
74
71
  const nonce = G2Element.generator().multiply(r);
75
72
  const gid = G1Element.hashToCurve(id).multiply(r);
76
- return [nonce, public_keys.map((public_key) => gid.pairing(public_key))];
73
+ return [r, nonce, publicKeys.map((public_key) => gid.pairing(public_key))];
77
74
  }
78
75
  function decap(nonce, usk) {
79
76
  return usk.pairing(nonce);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/ibe.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { GTElement } from './bls12381.js';\nimport { G1Element, G2Element, Scalar } from './bls12381.js';\nimport { kdf } from './kdf.js';\nimport type { KeyServer } from './key-server.js';\nimport type { IBEEncryptionsType } from './types.js';\nimport { xor } from './utils.js';\n\n/**\n * The domain separation tag for the hash-to-group function.\n */\nexport const DST: Uint8Array = new TextEncoder().encode('SUI-SEAL-IBE-BLS12381-00');\n\n/**\n * The domain separation tag for the signing proof of possession.\n */\nexport const DST_POP: Uint8Array = new TextEncoder().encode('SUI-SEAL-IBE-BLS12381-00-POP');\n\n/**\n * The interface for the key servers.\n */\nexport abstract class IBEServers {\n\tprotected readonly object_ids: Uint8Array[];\n\n\tprotected constructor(object_ids: Uint8Array[]) {\n\t\tthis.object_ids = object_ids;\n\t}\n\n\t/**\n\t * The object IDs of the key servers.\n\t */\n\tgetObjectIds(): Uint8Array[] {\n\t\treturn this.object_ids;\n\t}\n\n\t/**\n\t * The number of key servers.\n\t */\n\tsize(): number {\n\t\treturn this.object_ids.length;\n\t}\n\n\t/**\n\t * Encrypt a batch of messages for the given identity.\n\t *\n\t * @param id The identity.\n\t * @param msg_and_infos The messages and an additional info parameter which will be included in the KDF.\n\t * @returns The encrypted messages.\n\t */\n\tabstract encryptBatched(\n\t\tid: Uint8Array,\n\t\tmsg_and_infos: { msg: Uint8Array; info: Uint8Array }[],\n\t): IBEEncryptionsType;\n}\n\n/**\n * Identity-based encryption based on the Boneh-Franklin IBE scheme.\n * This object represents a set of key servers that can be used to encrypt messages for a given identity.\n */\nexport class BonehFranklinBLS12381Services extends IBEServers {\n\treadonly public_keys: G2Element[];\n\n\tconstructor(services: KeyServer[]) {\n\t\tsuper(services.map((service) => service.objectId));\n\t\tthis.public_keys = services.map((service) => G2Element.fromBytes(service.pk));\n\t}\n\n\tencryptBatched(\n\t\tid: Uint8Array,\n\t\tmsg_and_infos: { msg: Uint8Array; info: Uint8Array }[],\n\t): IBEEncryptionsType {\n\t\tif (this.public_keys.length === 0 || this.public_keys.length !== msg_and_infos.length) {\n\t\t\tthrow new Error('Invalid input');\n\t\t}\n\t\tconst [nonce, keys] = encapBatched(this.public_keys, id);\n\t\tconst encrypted_msgs = msg_and_infos.map((msg_and_info, i) =>\n\t\t\txor(msg_and_info.msg, kdf(keys[i], msg_and_info.info)),\n\t\t);\n\n\t\treturn {\n\t\t\tBonehFranklinBLS12381: {\n\t\t\t\tencapsulation: nonce.toBytes(),\n\t\t\t\tshares: encrypted_msgs,\n\t\t\t},\n\t\t\t$kind: 'BonehFranklinBLS12381',\n\t\t};\n\t}\n\n\t/**\n\t * Returns true if the user secret key is valid for the given public key and id.\n\t * @param user_secret_key - The user secret key.\n\t * @param id - The identity.\n\t * @param public_key - The public key.\n\t * @returns True if the user secret key is valid for the given public key and id.\n\t */\n\tstatic verifyUserSecretKey(\n\t\tuser_secret_key: G1Element,\n\t\tid: Uint8Array,\n\t\tpublic_key: G2Element,\n\t): boolean {\n\t\tconst lhs = user_secret_key.pairing(G2Element.generator()).toBytes();\n\t\tconst rhs = G1Element.hashToCurve(id).pairing(public_key).toBytes();\n\t\treturn lhs.length === rhs.length && lhs.every((value, index) => value === rhs[index]);\n\t}\n\n\t/**\n\t * Identity-based decryption.\n\t *\n\t * @param nonce The encryption nonce.\n\t * @param sk The user secret key.\n\t * @param ciphertext The encrypted message.\n\t * @param info An info parameter also included in the KDF.\n\t * @returns The decrypted message.\n\t */\n\tstatic decrypt(\n\t\tnonce: G2Element,\n\t\tsk: G1Element,\n\t\tciphertext: Uint8Array,\n\t\tinfo: Uint8Array,\n\t): Uint8Array {\n\t\treturn xor(ciphertext, kdf(decap(nonce, sk), info));\n\t}\n}\n\n/**\n * Batched identity-based key-encapsulation mechanism: encapsulate multiple keys for given identity using different key servers.\n *\n * @param public_keys Public keys for a set of key servers.\n * @param id The identity used to encapsulate the keys.\n * @returns A common nonce of the keys and a list of keys, 32 bytes each.\n */\nfunction encapBatched(public_keys: G2Element[], id: Uint8Array): [G2Element, GTElement[]] {\n\tif (public_keys.length === 0) {\n\t\tthrow new Error('Invalid input');\n\t}\n\tconst r = Scalar.random();\n\tconst nonce = G2Element.generator().multiply(r);\n\tconst gid = G1Element.hashToCurve(id).multiply(r);\n\treturn [nonce, public_keys.map((public_key) => gid.pairing(public_key))];\n}\n\n/**\n * Decapsulate a key using a user secret key and the nonce.\n *\n * @param usk The user secret key.\n * @param nonce The nonce.\n * @returns The encapsulated key.\n */\nfunction decap(nonce: G2Element, usk: G1Element): GTElement {\n\treturn usk.pairing(nonce);\n}\n"],
5
- "mappings": "AAIA,SAAS,WAAW,WAAW,cAAc;AAC7C,SAAS,WAAW;AAGpB,SAAS,WAAW;AAKb,MAAM,MAAkB,IAAI,YAAY,EAAE,OAAO,0BAA0B;AAK3E,MAAM,UAAsB,IAAI,YAAY,EAAE,OAAO,8BAA8B;AAKnF,MAAe,WAAW;AAAA,EAGtB,YAAY,YAA0B;AAC/C,SAAK,aAAa;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA,EAKA,eAA6B;AAC5B,WAAO,KAAK;AAAA,EACb;AAAA;AAAA;AAAA;AAAA,EAKA,OAAe;AACd,WAAO,KAAK,WAAW;AAAA,EACxB;AAaD;AAMO,MAAM,sCAAsC,WAAW;AAAA,EAG7D,YAAY,UAAuB;AAClC,UAAM,SAAS,IAAI,CAAC,YAAY,QAAQ,QAAQ,CAAC;AACjD,SAAK,cAAc,SAAS,IAAI,CAAC,YAAY,UAAU,UAAU,QAAQ,EAAE,CAAC;AAAA,EAC7E;AAAA,EAEA,eACC,IACA,eACqB;AACrB,QAAI,KAAK,YAAY,WAAW,KAAK,KAAK,YAAY,WAAW,cAAc,QAAQ;AACtF,YAAM,IAAI,MAAM,eAAe;AAAA,IAChC;AACA,UAAM,CAAC,OAAO,IAAI,IAAI,aAAa,KAAK,aAAa,EAAE;AACvD,UAAM,iBAAiB,cAAc;AAAA,MAAI,CAAC,cAAc,MACvD,IAAI,aAAa,KAAK,IAAI,KAAK,CAAC,GAAG,aAAa,IAAI,CAAC;AAAA,IACtD;AAEA,WAAO;AAAA,MACN,uBAAuB;AAAA,QACtB,eAAe,MAAM,QAAQ;AAAA,QAC7B,QAAQ;AAAA,MACT;AAAA,MACA,OAAO;AAAA,IACR;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,OAAO,oBACN,iBACA,IACA,YACU;AACV,UAAM,MAAM,gBAAgB,QAAQ,UAAU,UAAU,CAAC,EAAE,QAAQ;AACnE,UAAM,MAAM,UAAU,YAAY,EAAE,EAAE,QAAQ,UAAU,EAAE,QAAQ;AAClE,WAAO,IAAI,WAAW,IAAI,UAAU,IAAI,MAAM,CAAC,OAAO,UAAU,UAAU,IAAI,KAAK,CAAC;AAAA,EACrF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,OAAO,QACN,OACA,IACA,YACA,MACa;AACb,WAAO,IAAI,YAAY,IAAI,MAAM,OAAO,EAAE,GAAG,IAAI,CAAC;AAAA,EACnD;AACD;AASA,SAAS,aAAa,aAA0B,IAA0C;AACzF,MAAI,YAAY,WAAW,GAAG;AAC7B,UAAM,IAAI,MAAM,eAAe;AAAA,EAChC;AACA,QAAM,IAAI,OAAO,OAAO;AACxB,QAAM,QAAQ,UAAU,UAAU,EAAE,SAAS,CAAC;AAC9C,QAAM,MAAM,UAAU,YAAY,EAAE,EAAE,SAAS,CAAC;AAChD,SAAO,CAAC,OAAO,YAAY,IAAI,CAAC,eAAe,IAAI,QAAQ,UAAU,CAAC,CAAC;AACxE;AASA,SAAS,MAAM,OAAkB,KAA2B;AAC3D,SAAO,IAAI,QAAQ,KAAK;AACzB;",
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex } from '@mysten/bcs';\n\nimport type { IBEEncryptions } from './bcs.js';\nimport type { GTElement } from './bls12381.js';\nimport { G1Element, G2Element, Scalar } from './bls12381.js';\nimport { kdf } from './kdf.js';\nimport type { KeyServer } from './key-server.js';\nimport { xor } from './utils.js';\n\n/**\n * The domain separation tag for the hash-to-group function.\n */\nexport const DST: Uint8Array = new TextEncoder().encode('SUI-SEAL-IBE-BLS12381-00');\n\n/**\n * The domain separation tag for the signing proof of possession.\n */\nexport const DST_POP: Uint8Array = new TextEncoder().encode('SUI-SEAL-IBE-BLS12381-POP-00');\n\n/**\n * The interface for the key servers.\n */\nexport abstract class IBEServers {\n\tobjectIds: string[];\n\n\tconstructor(objectIds: string[]) {\n\t\tthis.objectIds = objectIds;\n\t}\n\n\t/**\n\t * The number of key servers.\n\t */\n\tsize(): number {\n\t\treturn this.objectIds.length;\n\t}\n\n\t/**\n\t * Encrypt a batch of messages for the given identity.\n\t *\n\t * @param id The identity.\n\t * @param msgAndIndices The messages and the corresponding indices of the share being encrypted.\n\t * @returns The encrypted messages.\n\t */\n\tabstract encryptBatched(\n\t\tid: Uint8Array,\n\t\tmsgAndIndices: { msg: Uint8Array; index: number }[],\n\t\trandomnessKey: Uint8Array,\n\t): typeof IBEEncryptions.$inferType;\n}\n\n/**\n * Identity-based encryption based on the Boneh-Franklin IBE scheme.\n * This object represents a set of key servers that can be used to encrypt messages for a given identity.\n */\nexport class BonehFranklinBLS12381Services extends IBEServers {\n\treadonly publicKeys: G2Element[];\n\n\tconstructor(services: KeyServer[]) {\n\t\tsuper(services.map((service) => service.objectId));\n\t\tthis.publicKeys = services.map((service) => G2Element.fromBytes(service.pk));\n\t}\n\n\tencryptBatched(\n\t\tid: Uint8Array,\n\t\tmsgAndIndices: { msg: Uint8Array; index: number }[],\n\t\trandomnessKey: Uint8Array,\n\t): typeof IBEEncryptions.$inferType {\n\t\tif (this.publicKeys.length === 0 || this.publicKeys.length !== msgAndIndices.length) {\n\t\t\tthrow new Error('Invalid public keys');\n\t\t}\n\t\tconst [r, nonce, keys] = encapBatched(this.publicKeys, id);\n\t\tconst encryptedShares = msgAndIndices.map((msgAndIndex, i) =>\n\t\t\txor(msgAndIndex.msg, kdf(keys[i], nonce, id, this.objectIds[i], msgAndIndex.index)),\n\t\t);\n\t\tconst encryptedRandomness = xor(randomnessKey, r.toBytes());\n\n\t\treturn {\n\t\t\tBonehFranklinBLS12381: {\n\t\t\t\tnonce: nonce.toBytes(),\n\t\t\t\tencryptedShares,\n\t\t\t\tencryptedRandomness,\n\t\t\t},\n\t\t\t$kind: 'BonehFranklinBLS12381',\n\t\t};\n\t}\n\n\t/**\n\t * Returns true if the user secret key is valid for the given public key and id.\n\t * @param user_secret_key - The user secret key.\n\t * @param id - The identity.\n\t * @param public_key - The public key.\n\t * @returns True if the user secret key is valid for the given public key and id.\n\t */\n\tstatic verifyUserSecretKey(userSecretKey: G1Element, id: string, publicKey: G2Element): boolean {\n\t\tconst lhs = userSecretKey.pairing(G2Element.generator()).toBytes();\n\t\tconst rhs = G1Element.hashToCurve(fromHex(id)).pairing(publicKey).toBytes();\n\t\treturn lhs.length === rhs.length && lhs.every((value, index) => value === rhs[index]);\n\t}\n\n\t/**\n\t * Identity-based decryption.\n\t *\n\t * @param nonce The encryption nonce.\n\t * @param sk The user secret key.\n\t * @param ciphertext The encrypted message.\n\t * @param info An info parameter also included in the KDF.\n\t * @returns The decrypted message.\n\t */\n\tstatic decrypt(\n\t\tnonce: G2Element,\n\t\tsk: G1Element,\n\t\tciphertext: Uint8Array,\n\t\tid: Uint8Array,\n\t\t[objectId, index]: [string, number],\n\t): Uint8Array {\n\t\treturn xor(ciphertext, kdf(decap(nonce, sk), nonce, id, objectId, index));\n\t}\n}\n\n/**\n * Batched identity-based key-encapsulation mechanism: encapsulate multiple keys for given identity using different key servers.\n *\n * @param publicKeys Public keys for a set of key servers.\n * @param id The identity used to encapsulate the keys.\n * @returns A common nonce of the keys and a list of keys, 32 bytes each.\n */\nfunction encapBatched(publicKeys: G2Element[], id: Uint8Array): [Scalar, G2Element, GTElement[]] {\n\tif (publicKeys.length === 0) {\n\t\tthrow new Error('No public keys provided');\n\t}\n\tconst r = Scalar.random();\n\tconst nonce = G2Element.generator().multiply(r);\n\tconst gid = G1Element.hashToCurve(id).multiply(r);\n\treturn [r, nonce, publicKeys.map((public_key) => gid.pairing(public_key))];\n}\n\n/**\n * Decapsulate a key using a user secret key and the nonce.\n *\n * @param usk The user secret key.\n * @param nonce The nonce.\n * @returns The encapsulated key.\n */\nfunction decap(nonce: G2Element, usk: G1Element): GTElement {\n\treturn usk.pairing(nonce);\n}\n"],
5
+ "mappings": "AAGA,SAAS,eAAe;AAIxB,SAAS,WAAW,WAAW,cAAc;AAC7C,SAAS,WAAW;AAEpB,SAAS,WAAW;AAKb,MAAM,MAAkB,IAAI,YAAY,EAAE,OAAO,0BAA0B;AAK3E,MAAM,UAAsB,IAAI,YAAY,EAAE,OAAO,8BAA8B;AAKnF,MAAe,WAAW;AAAA,EAGhC,YAAY,WAAqB;AAChC,SAAK,YAAY;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA,EAKA,OAAe;AACd,WAAO,KAAK,UAAU;AAAA,EACvB;AAcD;AAMO,MAAM,sCAAsC,WAAW;AAAA,EAG7D,YAAY,UAAuB;AAClC,UAAM,SAAS,IAAI,CAAC,YAAY,QAAQ,QAAQ,CAAC;AACjD,SAAK,aAAa,SAAS,IAAI,CAAC,YAAY,UAAU,UAAU,QAAQ,EAAE,CAAC;AAAA,EAC5E;AAAA,EAEA,eACC,IACA,eACA,eACmC;AACnC,QAAI,KAAK,WAAW,WAAW,KAAK,KAAK,WAAW,WAAW,cAAc,QAAQ;AACpF,YAAM,IAAI,MAAM,qBAAqB;AAAA,IACtC;AACA,UAAM,CAAC,GAAG,OAAO,IAAI,IAAI,aAAa,KAAK,YAAY,EAAE;AACzD,UAAM,kBAAkB,cAAc;AAAA,MAAI,CAAC,aAAa,MACvD,IAAI,YAAY,KAAK,IAAI,KAAK,CAAC,GAAG,OAAO,IAAI,KAAK,UAAU,CAAC,GAAG,YAAY,KAAK,CAAC;AAAA,IACnF;AACA,UAAM,sBAAsB,IAAI,eAAe,EAAE,QAAQ,CAAC;AAE1D,WAAO;AAAA,MACN,uBAAuB;AAAA,QACtB,OAAO,MAAM,QAAQ;AAAA,QACrB;AAAA,QACA;AAAA,MACD;AAAA,MACA,OAAO;AAAA,IACR;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,OAAO,oBAAoB,eAA0B,IAAY,WAA+B;AAC/F,UAAM,MAAM,cAAc,QAAQ,UAAU,UAAU,CAAC,EAAE,QAAQ;AACjE,UAAM,MAAM,UAAU,YAAY,QAAQ,EAAE,CAAC,EAAE,QAAQ,SAAS,EAAE,QAAQ;AAC1E,WAAO,IAAI,WAAW,IAAI,UAAU,IAAI,MAAM,CAAC,OAAO,UAAU,UAAU,IAAI,KAAK,CAAC;AAAA,EACrF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,OAAO,QACN,OACA,IACA,YACA,IACA,CAAC,UAAU,KAAK,GACH;AACb,WAAO,IAAI,YAAY,IAAI,MAAM,OAAO,EAAE,GAAG,OAAO,IAAI,UAAU,KAAK,CAAC;AAAA,EACzE;AACD;AASA,SAAS,aAAa,YAAyB,IAAkD;AAChG,MAAI,WAAW,WAAW,GAAG;AAC5B,UAAM,IAAI,MAAM,yBAAyB;AAAA,EAC1C;AACA,QAAM,IAAI,OAAO,OAAO;AACxB,QAAM,QAAQ,UAAU,UAAU,EAAE,SAAS,CAAC;AAC9C,QAAM,MAAM,UAAU,YAAY,EAAE,EAAE,SAAS,CAAC;AAChD,SAAO,CAAC,GAAG,OAAO,WAAW,IAAI,CAAC,eAAe,IAAI,QAAQ,UAAU,CAAC,CAAC;AAC1E;AASA,SAAS,MAAM,OAAkB,KAA2B;AAC3D,SAAO,IAAI,QAAQ,KAAK;AACzB;",
6
6
  "names": []
7
7
  }
@@ -1,6 +1,4 @@
1
- export { AesGcm256 } from './aes.js';
2
- export { encrypt } from './encrypt.js';
3
- export { getAllowlistedKeyServers, retrieveKeyServers, verifyKeyServer, type KeyServer, } from './key-server.js';
4
- export { KeyStore } from './key-store.js';
1
+ export { getAllowlistedKeyServers } from './key-server.js';
2
+ export { SealClient, type SealClientOptions } from './client.js';
5
3
  export { SessionKey } from './session-key.js';
6
- export { EncryptedObject } from './types.js';
4
+ export * from './error.js';
package/dist/esm/index.js CHANGED
@@ -1,21 +1,10 @@
1
- import { AesGcm256 } from "./aes.js";
2
- import { encrypt } from "./encrypt.js";
3
- import {
4
- getAllowlistedKeyServers,
5
- retrieveKeyServers,
6
- verifyKeyServer
7
- } from "./key-server.js";
8
- import { KeyStore } from "./key-store.js";
1
+ import { getAllowlistedKeyServers } from "./key-server.js";
2
+ import { SealClient } from "./client.js";
9
3
  import { SessionKey } from "./session-key.js";
10
- import { EncryptedObject } from "./types.js";
4
+ export * from "./error.js";
11
5
  export {
12
- AesGcm256,
13
- EncryptedObject,
14
- KeyStore,
6
+ SealClient,
15
7
  SessionKey,
16
- encrypt,
17
- getAllowlistedKeyServers,
18
- retrieveKeyServers,
19
- verifyKeyServer
8
+ getAllowlistedKeyServers
20
9
  };
21
10
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { AesGcm256 } from './aes.js';\nexport { encrypt } from './encrypt.js';\nexport {\n\tgetAllowlistedKeyServers,\n\tretrieveKeyServers,\n\tverifyKeyServer,\n\ttype KeyServer,\n} from './key-server.js';\nexport { KeyStore } from './key-store.js';\nexport { SessionKey } from './session-key.js';\nexport { EncryptedObject } from './types.js';\n"],
5
- "mappings": "AAGA,SAAS,iBAAiB;AAC1B,SAAS,eAAe;AACxB;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OAEM;AACP,SAAS,gBAAgB;AACzB,SAAS,kBAAkB;AAC3B,SAAS,uBAAuB;",
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { getAllowlistedKeyServers } from './key-server.js';\nexport { SealClient, type SealClientOptions } from './client.js';\nexport { SessionKey } from './session-key.js';\nexport * from './error.js';\n"],
5
+ "mappings": "AAGA,SAAS,gCAAgC;AACzC,SAAS,kBAA0C;AACnD,SAAS,kBAAkB;AAC3B,cAAc;",
6
6
  "names": []
7
7
  }
package/dist/esm/kdf.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { GTElement } from './bls12381.js';
1
+ import type { G2Element, GTElement } from './bls12381.js';
2
2
  /**
3
3
  * The default key derivation function.
4
4
  *
@@ -6,4 +6,9 @@ import type { GTElement } from './bls12381.js';
6
6
  * @param info Optional context and application specific information.
7
7
  * @returns The derived key.
8
8
  */
9
- export declare function kdf(element: GTElement, info: Uint8Array): Uint8Array;
9
+ export declare function kdf(element: GTElement, nonce: G2Element, id: Uint8Array, objectId: string, index: number): Uint8Array;
10
+ export declare enum KeyPurpose {
11
+ EncryptedRandomness = 0,
12
+ DEM = 1
13
+ }
14
+ export declare function deriveKey(purpose: KeyPurpose, baseKey: Uint8Array): Uint8Array;
package/dist/esm/kdf.js CHANGED
@@ -1,9 +1,44 @@
1
+ import { fromHex } from "@mysten/bcs";
1
2
  import { hkdf } from "@noble/hashes/hkdf";
3
+ import { hmac } from "@noble/hashes/hmac";
2
4
  import { sha3_256 } from "@noble/hashes/sha3";
3
- function kdf(element, info) {
4
- return hkdf(sha3_256, element.toBytes(), "", info, 32);
5
+ import { G1Element } from "./bls12381.js";
6
+ function kdf(element, nonce, id, objectId, index) {
7
+ const GT_ELEMENT_BYTE_LENGTH = 576;
8
+ const PERMUTATION = [0, 2, 4, 1, 3, 5];
9
+ const COEFFICIENT_SIZE = GT_ELEMENT_BYTE_LENGTH / PERMUTATION.length;
10
+ const bytes = element.toBytes();
11
+ let permutedBytes = new Uint8Array(GT_ELEMENT_BYTE_LENGTH);
12
+ PERMUTATION.forEach((pi, i) => {
13
+ permutedBytes.set(
14
+ bytes.slice(i * COEFFICIENT_SIZE, (i + 1) * COEFFICIENT_SIZE),
15
+ pi * COEFFICIENT_SIZE
16
+ );
17
+ });
18
+ const inputBytes = new Uint8Array([
19
+ ...permutedBytes,
20
+ ...nonce.toBytes(),
21
+ ...G1Element.hashToCurve(id).toBytes()
22
+ ]);
23
+ const info = new Uint8Array([...fromHex(objectId), index]);
24
+ return hkdf(sha3_256, inputBytes, "", info, 32);
25
+ }
26
+ var KeyPurpose = /* @__PURE__ */ ((KeyPurpose2) => {
27
+ KeyPurpose2[KeyPurpose2["EncryptedRandomness"] = 0] = "EncryptedRandomness";
28
+ KeyPurpose2[KeyPurpose2["DEM"] = 1] = "DEM";
29
+ return KeyPurpose2;
30
+ })(KeyPurpose || {});
31
+ function deriveKey(purpose, baseKey) {
32
+ switch (purpose) {
33
+ case 0 /* EncryptedRandomness */:
34
+ return hmac(sha3_256, baseKey, new Uint8Array([0]));
35
+ case 1 /* DEM */:
36
+ return hmac(sha3_256, baseKey, new Uint8Array([1]));
37
+ }
5
38
  }
6
39
  export {
40
+ KeyPurpose,
41
+ deriveKey,
7
42
  kdf
8
43
  };
9
44
  //# sourceMappingURL=kdf.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/kdf.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { hkdf } from '@noble/hashes/hkdf';\nimport { sha3_256 } from '@noble/hashes/sha3';\n\nimport type { GTElement } from './bls12381.js';\n\n/**\n * The default key derivation function.\n *\n * @param element The GTElement to derive the key from.\n * @param info Optional context and application specific information.\n * @returns The derived key.\n */\nexport function kdf(element: GTElement, info: Uint8Array): Uint8Array {\n\treturn hkdf(sha3_256, element.toBytes(), '', info, 32);\n}\n"],
5
- "mappings": "AAGA,SAAS,YAAY;AACrB,SAAS,gBAAgB;AAWlB,SAAS,IAAI,SAAoB,MAA8B;AACrE,SAAO,KAAK,UAAU,QAAQ,QAAQ,GAAG,IAAI,MAAM,EAAE;AACtD;",
6
- "names": []
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex } from '@mysten/bcs';\nimport { hkdf } from '@noble/hashes/hkdf';\nimport { hmac } from '@noble/hashes/hmac';\nimport { sha3_256 } from '@noble/hashes/sha3';\n\nimport { G1Element } from './bls12381.js';\nimport type { G2Element, GTElement } from './bls12381.js';\n\n/**\n * The default key derivation function.\n *\n * @param element The GTElement to derive the key from.\n * @param info Optional context and application specific information.\n * @returns The derived key.\n */\nexport function kdf(\n\telement: GTElement,\n\tnonce: G2Element,\n\tid: Uint8Array,\n\tobjectId: string,\n\tindex: number,\n): Uint8Array {\n\t// This permutation flips the order of 6 pairs of coefficients of the GT element.\n\t// The permutation may be computed as:\n\t// for i in 0..3 {\n\t// for j in 0..2 {\n\t// PERMUTATION[i + j * 3] = i * 2 + j;\n\t// }\n\t// }\n\tconst GT_ELEMENT_BYTE_LENGTH = 576;\n\tconst PERMUTATION = [0, 2, 4, 1, 3, 5];\n\tconst COEFFICIENT_SIZE = GT_ELEMENT_BYTE_LENGTH / PERMUTATION.length;\n\n\tconst bytes = element.toBytes();\n\tlet permutedBytes = new Uint8Array(GT_ELEMENT_BYTE_LENGTH);\n\tPERMUTATION.forEach((pi, i) => {\n\t\tpermutedBytes.set(\n\t\t\tbytes.slice(i * COEFFICIENT_SIZE, (i + 1) * COEFFICIENT_SIZE),\n\t\t\tpi * COEFFICIENT_SIZE,\n\t\t);\n\t});\n\tconst inputBytes = new Uint8Array([\n\t\t...permutedBytes,\n\t\t...nonce.toBytes(),\n\t\t...G1Element.hashToCurve(id).toBytes(),\n\t]);\n\tconst info = new Uint8Array([...fromHex(objectId), index]);\n\treturn hkdf(sha3_256, inputBytes, '', info, 32);\n}\n\nexport enum KeyPurpose {\n\tEncryptedRandomness,\n\tDEM,\n}\n\nexport function deriveKey(purpose: KeyPurpose, baseKey: Uint8Array): Uint8Array {\n\tswitch (purpose) {\n\t\tcase KeyPurpose.EncryptedRandomness:\n\t\t\treturn hmac(sha3_256, baseKey, new Uint8Array([0]));\n\t\tcase KeyPurpose.DEM:\n\t\t\treturn hmac(sha3_256, baseKey, new Uint8Array([1]));\n\t}\n}\n"],
5
+ "mappings": "AAGA,SAAS,eAAe;AACxB,SAAS,YAAY;AACrB,SAAS,YAAY;AACrB,SAAS,gBAAgB;AAEzB,SAAS,iBAAiB;AAUnB,SAAS,IACf,SACA,OACA,IACA,UACA,OACa;AAQb,QAAM,yBAAyB;AAC/B,QAAM,cAAc,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AACrC,QAAM,mBAAmB,yBAAyB,YAAY;AAE9D,QAAM,QAAQ,QAAQ,QAAQ;AAC9B,MAAI,gBAAgB,IAAI,WAAW,sBAAsB;AACzD,cAAY,QAAQ,CAAC,IAAI,MAAM;AAC9B,kBAAc;AAAA,MACb,MAAM,MAAM,IAAI,mBAAmB,IAAI,KAAK,gBAAgB;AAAA,MAC5D,KAAK;AAAA,IACN;AAAA,EACD,CAAC;AACD,QAAM,aAAa,IAAI,WAAW;AAAA,IACjC,GAAG;AAAA,IACH,GAAG,MAAM,QAAQ;AAAA,IACjB,GAAG,UAAU,YAAY,EAAE,EAAE,QAAQ;AAAA,EACtC,CAAC;AACD,QAAM,OAAO,IAAI,WAAW,CAAC,GAAG,QAAQ,QAAQ,GAAG,KAAK,CAAC;AACzD,SAAO,KAAK,UAAU,YAAY,IAAI,MAAM,EAAE;AAC/C;AAEO,IAAK,aAAL,kBAAKA,gBAAL;AACN,EAAAA,wBAAA;AACA,EAAAA,wBAAA;AAFW,SAAAA;AAAA,GAAA;AAKL,SAAS,UAAU,SAAqB,SAAiC;AAC/E,UAAQ,SAAS;AAAA,IAChB,KAAK;AACJ,aAAO,KAAK,UAAU,SAAS,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;AAAA,IACnD,KAAK;AACJ,aAAO,KAAK,UAAU,SAAS,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;AAAA,EACpD;AACD;",
6
+ "names": ["KeyPurpose"]
7
7
  }
@@ -1,6 +1,6 @@
1
1
  import type { SuiClient } from '@mysten/sui/client';
2
2
  export type KeyServer = {
3
- objectId: Uint8Array;
3
+ objectId: string;
4
4
  name: string;
5
5
  url: string;
6
6
  keyType: KeyServerType;
@@ -14,7 +14,7 @@ export declare enum KeyServerType {
14
14
  * @param network - The network to use.
15
15
  * @returns The object id's of the key servers.
16
16
  */
17
- export declare function getAllowlistedKeyServers(network: 'testnet' | 'mainnet'): Uint8Array[];
17
+ export declare function getAllowlistedKeyServers(network: 'testnet' | 'mainnet'): string[];
18
18
  /**
19
19
  * Given a list of key server object IDs, returns a list of SealKeyServer
20
20
  * from onchain state containing name, objectId, URL and pk.
@@ -24,15 +24,15 @@ export declare function getAllowlistedKeyServers(network: 'testnet' | 'mainnet')
24
24
  * @returns - An array of SealKeyServer.
25
25
  */
26
26
  export declare function retrieveKeyServers({ objectIds, client, }: {
27
- objectIds: Uint8Array[];
27
+ objectIds: string[];
28
28
  client: SuiClient;
29
29
  }): Promise<KeyServer[]>;
30
30
  /**
31
- * Given a KeyServer, fetch the proof of possesion (PoP) from the URL and verify it
31
+ * Given a KeyServer, fetch the proof of possession (PoP) from the URL and verify it
32
32
  * against the pubkey. This should be used only rarely when the dapp uses a dynamic
33
33
  * set of key servers.
34
34
  *
35
35
  * @param server - The KeyServer to verify.
36
36
  * @returns - True if the key server is valid, false otherwise.
37
37
  */
38
- export declare function verifyKeyServer(server: KeyServer): Promise<boolean>;
38
+ export declare function verifyKeyServer(server: KeyServer, timeout: number): Promise<boolean>;