@mysten/seal 0.1.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/CHANGELOG.md +13 -0
- package/README.md +4 -0
- package/dist/cjs/aes.d.ts +18 -0
- package/dist/cjs/aes.js +111 -0
- package/dist/cjs/aes.js.map +7 -0
- package/dist/cjs/bls12381.d.ts +37 -0
- package/dist/cjs/bls12381.js +110 -0
- package/dist/cjs/bls12381.js.map +7 -0
- package/dist/cjs/elgamal.d.ts +11 -0
- package/dist/cjs/elgamal.js +46 -0
- package/dist/cjs/elgamal.js.map +7 -0
- package/dist/cjs/encrypt.d.ts +24 -0
- package/dist/cjs/encrypt.js +88 -0
- package/dist/cjs/encrypt.js.map +7 -0
- package/dist/cjs/ibe.d.ts +67 -0
- package/dist/cjs/ibe.js +107 -0
- package/dist/cjs/ibe.js.map +7 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +37 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/kdf.d.ts +9 -0
- package/dist/cjs/kdf.js +29 -0
- package/dist/cjs/kdf.js.map +7 -0
- package/dist/cjs/key-server.d.ts +38 -0
- package/dist/cjs/key-server.js +98 -0
- package/dist/cjs/key-server.js.map +7 -0
- package/dist/cjs/key-store.d.ts +49 -0
- package/dist/cjs/key-store.js +203 -0
- package/dist/cjs/key-store.js.map +7 -0
- package/dist/cjs/package.json +5 -0
- package/dist/cjs/session-key.d.ts +36 -0
- package/dist/cjs/session-key.js +73 -0
- package/dist/cjs/session-key.js.map +7 -0
- package/dist/cjs/types.d.ts +86 -0
- package/dist/cjs/types.js +49 -0
- package/dist/cjs/types.js.map +7 -0
- package/dist/cjs/utils.d.ts +9 -0
- package/dist/cjs/utils.js +39 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/esm/aes.d.ts +18 -0
- package/dist/esm/aes.js +91 -0
- package/dist/esm/aes.js.map +7 -0
- package/dist/esm/bls12381.d.ts +37 -0
- package/dist/esm/bls12381.js +90 -0
- package/dist/esm/bls12381.js.map +7 -0
- package/dist/esm/elgamal.d.ts +11 -0
- package/dist/esm/elgamal.js +26 -0
- package/dist/esm/elgamal.js.map +7 -0
- package/dist/esm/encrypt.d.ts +24 -0
- package/dist/esm/encrypt.js +68 -0
- package/dist/esm/encrypt.js.map +7 -0
- package/dist/esm/ibe.d.ts +67 -0
- package/dist/esm/ibe.js +87 -0
- package/dist/esm/ibe.js.map +7 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/kdf.d.ts +9 -0
- package/dist/esm/kdf.js +9 -0
- package/dist/esm/kdf.js.map +7 -0
- package/dist/esm/key-server.d.ts +38 -0
- package/dist/esm/key-server.js +78 -0
- package/dist/esm/key-server.js.map +7 -0
- package/dist/esm/key-store.d.ts +49 -0
- package/dist/esm/key-store.js +183 -0
- package/dist/esm/key-store.js.map +7 -0
- package/dist/esm/package.json +5 -0
- package/dist/esm/session-key.d.ts +36 -0
- package/dist/esm/session-key.js +53 -0
- package/dist/esm/session-key.js.map +7 -0
- package/dist/esm/types.d.ts +86 -0
- package/dist/esm/types.js +29 -0
- package/dist/esm/types.js.map +7 -0
- package/dist/esm/utils.d.ts +9 -0
- package/dist/esm/utils.js +19 -0
- package/dist/esm/utils.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +58 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CiphertextType } from './types.js';
|
|
2
|
+
export declare const iv: Uint8Array<ArrayBuffer>;
|
|
3
|
+
export interface EncryptionInput {
|
|
4
|
+
encrypt(key: Uint8Array): Promise<CiphertextType>;
|
|
5
|
+
generateKey(): Promise<Uint8Array>;
|
|
6
|
+
}
|
|
7
|
+
export declare class AesGcm256 implements EncryptionInput {
|
|
8
|
+
readonly plaintext: Uint8Array;
|
|
9
|
+
readonly aad: Uint8Array;
|
|
10
|
+
constructor(msg: Uint8Array, aad: Uint8Array);
|
|
11
|
+
generateKey(): Promise<Uint8Array>;
|
|
12
|
+
encrypt(key: Uint8Array): Promise<CiphertextType>;
|
|
13
|
+
static decrypt(key: Uint8Array, ciphertext: CiphertextType): Promise<Uint8Array>;
|
|
14
|
+
}
|
|
15
|
+
export declare class Plain implements EncryptionInput {
|
|
16
|
+
encrypt(_key: Uint8Array): Promise<CiphertextType>;
|
|
17
|
+
generateKey(): Promise<Uint8Array>;
|
|
18
|
+
}
|
package/dist/cjs/aes.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var aes_exports = {};
|
|
20
|
+
__export(aes_exports, {
|
|
21
|
+
AesGcm256: () => AesGcm256,
|
|
22
|
+
Plain: () => Plain,
|
|
23
|
+
iv: () => iv
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(aes_exports);
|
|
26
|
+
const iv = Uint8Array.from([
|
|
27
|
+
138,
|
|
28
|
+
55,
|
|
29
|
+
153,
|
|
30
|
+
253,
|
|
31
|
+
198,
|
|
32
|
+
46,
|
|
33
|
+
121,
|
|
34
|
+
219,
|
|
35
|
+
160,
|
|
36
|
+
128,
|
|
37
|
+
89,
|
|
38
|
+
7,
|
|
39
|
+
214,
|
|
40
|
+
156,
|
|
41
|
+
148,
|
|
42
|
+
220
|
|
43
|
+
]);
|
|
44
|
+
async function generateAesKey() {
|
|
45
|
+
const key = await crypto.subtle.generateKey(
|
|
46
|
+
{
|
|
47
|
+
name: "AES-GCM",
|
|
48
|
+
length: 256
|
|
49
|
+
},
|
|
50
|
+
true,
|
|
51
|
+
["encrypt", "decrypt"]
|
|
52
|
+
);
|
|
53
|
+
return await crypto.subtle.exportKey("raw", key).then((keyData) => new Uint8Array(keyData));
|
|
54
|
+
}
|
|
55
|
+
class AesGcm256 {
|
|
56
|
+
constructor(msg, aad) {
|
|
57
|
+
this.plaintext = new Uint8Array(msg);
|
|
58
|
+
this.aad = aad;
|
|
59
|
+
}
|
|
60
|
+
generateKey() {
|
|
61
|
+
return generateAesKey();
|
|
62
|
+
}
|
|
63
|
+
async encrypt(key) {
|
|
64
|
+
const aesCryptoKey = await crypto.subtle.importKey("raw", key, "AES-GCM", false, ["encrypt"]);
|
|
65
|
+
const blob = new Uint8Array(
|
|
66
|
+
await crypto.subtle.encrypt(
|
|
67
|
+
{
|
|
68
|
+
name: "AES-GCM",
|
|
69
|
+
iv,
|
|
70
|
+
additionalData: this.aad
|
|
71
|
+
},
|
|
72
|
+
aesCryptoKey,
|
|
73
|
+
this.plaintext
|
|
74
|
+
)
|
|
75
|
+
);
|
|
76
|
+
return {
|
|
77
|
+
Aes256Gcm: {
|
|
78
|
+
blob,
|
|
79
|
+
aad: this.aad ?? []
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
static async decrypt(key, ciphertext) {
|
|
84
|
+
if (!("Aes256Gcm" in ciphertext)) {
|
|
85
|
+
throw new Error("Invalid ciphertext");
|
|
86
|
+
}
|
|
87
|
+
const aesCryptoKey = await crypto.subtle.importKey("raw", key, "AES-GCM", false, ["decrypt"]);
|
|
88
|
+
return new Uint8Array(
|
|
89
|
+
await crypto.subtle.decrypt(
|
|
90
|
+
{
|
|
91
|
+
name: "AES-GCM",
|
|
92
|
+
iv,
|
|
93
|
+
additionalData: new Uint8Array(ciphertext.Aes256Gcm.aad ?? [])
|
|
94
|
+
},
|
|
95
|
+
aesCryptoKey,
|
|
96
|
+
new Uint8Array(ciphertext.Aes256Gcm.blob)
|
|
97
|
+
)
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
class Plain {
|
|
102
|
+
async encrypt(_key) {
|
|
103
|
+
return {
|
|
104
|
+
Plain: {}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
generateKey() {
|
|
108
|
+
return generateAesKey();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=aes.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/aes.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { CiphertextType } from './types.js';\n\n// Use a fixed IV for AES.\nexport const iv = Uint8Array.from([\n\t138, 55, 153, 253, 198, 46, 121, 219, 160, 128, 89, 7, 214, 156, 148, 220,\n]);\n\nasync function generateAesKey(): Promise<Uint8Array> {\n\tconst key = await crypto.subtle.generateKey(\n\t\t{\n\t\t\tname: 'AES-GCM',\n\t\t\tlength: 256,\n\t\t},\n\t\ttrue,\n\t\t['encrypt', 'decrypt'],\n\t);\n\treturn await crypto.subtle.exportKey('raw', key).then((keyData) => new Uint8Array(keyData));\n}\n\nexport interface EncryptionInput {\n\tencrypt(key: Uint8Array): Promise<CiphertextType>;\n\tgenerateKey(): Promise<Uint8Array>;\n}\n\nexport class AesGcm256 implements EncryptionInput {\n\treadonly plaintext: Uint8Array;\n\treadonly aad: Uint8Array;\n\n\tconstructor(msg: Uint8Array, aad: Uint8Array) {\n\t\tthis.plaintext = new Uint8Array(msg);\n\t\tthis.aad = aad;\n\t}\n\n\tgenerateKey(): Promise<Uint8Array> {\n\t\treturn generateAesKey();\n\t}\n\n\tasync encrypt(key: Uint8Array): Promise<CiphertextType> {\n\t\tconst aesCryptoKey = await crypto.subtle.importKey('raw', key, 'AES-GCM', false, ['encrypt']);\n\n\t\tconst blob = new Uint8Array(\n\t\t\tawait crypto.subtle.encrypt(\n\t\t\t\t{\n\t\t\t\t\tname: 'AES-GCM',\n\t\t\t\t\tiv,\n\t\t\t\t\tadditionalData: this.aad,\n\t\t\t\t},\n\t\t\t\taesCryptoKey,\n\t\t\t\tthis.plaintext,\n\t\t\t),\n\t\t);\n\n\t\treturn {\n\t\t\tAes256Gcm: {\n\t\t\t\tblob,\n\t\t\t\taad: this.aad ?? [],\n\t\t\t},\n\t\t};\n\t}\n\n\tstatic async decrypt(key: Uint8Array, ciphertext: CiphertextType): Promise<Uint8Array> {\n\t\tif (!('Aes256Gcm' in ciphertext)) {\n\t\t\tthrow new Error('Invalid ciphertext');\n\t\t}\n\n\t\tconst aesCryptoKey = await crypto.subtle.importKey('raw', key, 'AES-GCM', false, ['decrypt']);\n\n\t\t// TODO: add test to check if aad is wrong does throw an error.\n\t\treturn new Uint8Array(\n\t\t\tawait crypto.subtle.decrypt(\n\t\t\t\t{\n\t\t\t\t\tname: 'AES-GCM',\n\t\t\t\t\tiv,\n\t\t\t\t\tadditionalData: new Uint8Array(ciphertext.Aes256Gcm.aad ?? []),\n\t\t\t\t},\n\t\t\t\taesCryptoKey,\n\t\t\t\tnew Uint8Array(ciphertext.Aes256Gcm.blob),\n\t\t\t),\n\t\t);\n\t}\n}\n\nexport class Plain implements EncryptionInput {\n\tasync encrypt(_key: Uint8Array):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KAAK,OAAO,CAAC;AAAA,MACnB;AAAA,IACD;AAAA,EACD;AAAA,EAEA,aAAa,QAAQ,KAAiB,YAAiD;AACtF,QAAI,EAAE,eAAe,aAAa;AACjC,YAAM,IAAI,MAAM,oBAAoB;AAAA,IACrC;AAEA,UAAM,eAAe,MAAM,OAAO,OAAO,UAAU,OAAO,KAAK,WAAW,OAAO,CAAC,SAAS,CAAC;AAG5F,WAAO,IAAI;AAAA,MACV,MAAM,OAAO,OAAO;AAAA,QACnB;AAAA,UACC,MAAM;AAAA,UACN;AAAA,UACA,gBAAgB,IAAI,WAAW,WAAW,UAAU,OAAO,CAAC,CAAC;AAAA,QAC9D;AAAA,QACA;AAAA,QACA,IAAI,WAAW,WAAW,UAAU,IAAI;AAAA,MACzC;AAAA,IACD;AAAA,EACD;AACD;AAEO,MAAM,MAAiC;AAAA,EAC7C,MAAM,QAAQ,MAA2C;AACxD,WAAO;AAAA,MACN,OAAO,CAAC;AAAA,IACT;AAAA,EACD;AAAA,EAEA,cAAmC;AAClC,WAAO,eAAe;AAAA,EACvB;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Fp2, Fp12 } from '@noble/curves/abstract/tower';
|
|
2
|
+
import type { ProjPointType } from '@noble/curves/abstract/weierstrass';
|
|
3
|
+
export declare class G1Element {
|
|
4
|
+
point: ProjPointType<bigint>;
|
|
5
|
+
constructor(point: ProjPointType<bigint>);
|
|
6
|
+
static generator(): G1Element;
|
|
7
|
+
static fromBytes(bytes: Uint8Array): G1Element;
|
|
8
|
+
toBytes(): Uint8Array;
|
|
9
|
+
multiply(scalar: Scalar): G1Element;
|
|
10
|
+
add(other: G1Element): G1Element;
|
|
11
|
+
subtract(other: G1Element): G1Element;
|
|
12
|
+
static hashToCurve(data: Uint8Array): G1Element;
|
|
13
|
+
pairing(other: G2Element): GTElement;
|
|
14
|
+
}
|
|
15
|
+
export declare class G2Element {
|
|
16
|
+
point: ProjPointType<Fp2>;
|
|
17
|
+
constructor(point: ProjPointType<Fp2>);
|
|
18
|
+
static generator(): G2Element;
|
|
19
|
+
static fromBytes(bytes: Uint8Array): G2Element;
|
|
20
|
+
toBytes(): Uint8Array;
|
|
21
|
+
multiply(scalar: Scalar): G2Element;
|
|
22
|
+
add(other: G2Element): G2Element;
|
|
23
|
+
hashToCurve(data: Uint8Array): G2Element;
|
|
24
|
+
}
|
|
25
|
+
export declare class GTElement {
|
|
26
|
+
element: Fp12;
|
|
27
|
+
constructor(element: Fp12);
|
|
28
|
+
toBytes(): Uint8Array;
|
|
29
|
+
}
|
|
30
|
+
export declare class Scalar {
|
|
31
|
+
scalar: bigint;
|
|
32
|
+
constructor(scalar: bigint);
|
|
33
|
+
static random(): Scalar;
|
|
34
|
+
toBytes(): Uint8Array;
|
|
35
|
+
static fromBytes(bytes: Uint8Array): Scalar;
|
|
36
|
+
static fromNumber(num: number): Scalar;
|
|
37
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var bls12381_exports = {};
|
|
20
|
+
__export(bls12381_exports, {
|
|
21
|
+
G1Element: () => G1Element,
|
|
22
|
+
G2Element: () => G2Element,
|
|
23
|
+
GTElement: () => GTElement,
|
|
24
|
+
Scalar: () => Scalar
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(bls12381_exports);
|
|
27
|
+
var import_bcs = require("@mysten/bcs");
|
|
28
|
+
var import_bls12_381 = require("@noble/curves/bls12-381");
|
|
29
|
+
class G1Element {
|
|
30
|
+
constructor(point) {
|
|
31
|
+
this.point = point;
|
|
32
|
+
}
|
|
33
|
+
static generator() {
|
|
34
|
+
return new G1Element(import_bls12_381.bls12_381.G1.ProjectivePoint.BASE);
|
|
35
|
+
}
|
|
36
|
+
static fromBytes(bytes) {
|
|
37
|
+
return new G1Element(import_bls12_381.bls12_381.G1.ProjectivePoint.fromHex((0, import_bcs.toHex)(bytes)));
|
|
38
|
+
}
|
|
39
|
+
toBytes() {
|
|
40
|
+
return this.point.toRawBytes();
|
|
41
|
+
}
|
|
42
|
+
multiply(scalar) {
|
|
43
|
+
return new G1Element(this.point.multiply(scalar.scalar));
|
|
44
|
+
}
|
|
45
|
+
add(other) {
|
|
46
|
+
return new G1Element(this.point.add(other.point));
|
|
47
|
+
}
|
|
48
|
+
subtract(other) {
|
|
49
|
+
return new G1Element(this.point.subtract(other.point));
|
|
50
|
+
}
|
|
51
|
+
static hashToCurve(data) {
|
|
52
|
+
return new G1Element(
|
|
53
|
+
import_bls12_381.bls12_381.G1.ProjectivePoint.fromAffine(import_bls12_381.bls12_381.G1.hashToCurve(data).toAffine())
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
pairing(other) {
|
|
57
|
+
return new GTElement(import_bls12_381.bls12_381.pairing(this.point, other.point));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
class G2Element {
|
|
61
|
+
constructor(point) {
|
|
62
|
+
this.point = point;
|
|
63
|
+
}
|
|
64
|
+
static generator() {
|
|
65
|
+
return new G2Element(import_bls12_381.bls12_381.G2.ProjectivePoint.BASE);
|
|
66
|
+
}
|
|
67
|
+
static fromBytes(bytes) {
|
|
68
|
+
return new G2Element(import_bls12_381.bls12_381.G2.ProjectivePoint.fromHex((0, import_bcs.toHex)(bytes)));
|
|
69
|
+
}
|
|
70
|
+
toBytes() {
|
|
71
|
+
return this.point.toRawBytes();
|
|
72
|
+
}
|
|
73
|
+
multiply(scalar) {
|
|
74
|
+
return new G2Element(this.point.multiply(scalar.scalar));
|
|
75
|
+
}
|
|
76
|
+
add(other) {
|
|
77
|
+
return new G2Element(this.point.add(other.point));
|
|
78
|
+
}
|
|
79
|
+
hashToCurve(data) {
|
|
80
|
+
return new G2Element(
|
|
81
|
+
import_bls12_381.bls12_381.G2.ProjectivePoint.fromAffine(import_bls12_381.bls12_381.G2.hashToCurve(data).toAffine())
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
class GTElement {
|
|
86
|
+
constructor(element) {
|
|
87
|
+
this.element = element;
|
|
88
|
+
}
|
|
89
|
+
toBytes() {
|
|
90
|
+
return import_bls12_381.bls12_381.fields.Fp12.toBytes(this.element);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
class Scalar {
|
|
94
|
+
constructor(scalar) {
|
|
95
|
+
this.scalar = scalar;
|
|
96
|
+
}
|
|
97
|
+
static random() {
|
|
98
|
+
return Scalar.fromBytes(import_bls12_381.bls12_381.utils.randomPrivateKey());
|
|
99
|
+
}
|
|
100
|
+
toBytes() {
|
|
101
|
+
return new Uint8Array(import_bls12_381.bls12_381.fields.Fr.toBytes(this.scalar));
|
|
102
|
+
}
|
|
103
|
+
static fromBytes(bytes) {
|
|
104
|
+
return new Scalar(import_bls12_381.bls12_381.fields.Fr.fromBytes(bytes));
|
|
105
|
+
}
|
|
106
|
+
static fromNumber(num) {
|
|
107
|
+
return new Scalar(BigInt(num));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=bls12381.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/bls12381.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { toHex } from '@mysten/bcs';\nimport type { Fp2, Fp12 } from '@noble/curves/abstract/tower';\nimport type { ProjPointType } from '@noble/curves/abstract/weierstrass';\nimport { bls12_381 } from '@noble/curves/bls12-381';\n\nexport class G1Element {\n\tpoint: ProjPointType<bigint>;\n\n\tconstructor(point: ProjPointType<bigint>) {\n\t\tthis.point = point;\n\t}\n\n\tstatic generator(): G1Element {\n\t\treturn new G1Element(bls12_381.G1.ProjectivePoint.BASE);\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): G1Element {\n\t\treturn new G1Element(bls12_381.G1.ProjectivePoint.fromHex(toHex(bytes)));\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\treturn this.point.toRawBytes();\n\t}\n\n\tmultiply(scalar: Scalar): G1Element {\n\t\treturn new G1Element(this.point.multiply(scalar.scalar));\n\t}\n\n\tadd(other: G1Element): G1Element {\n\t\treturn new G1Element(this.point.add(other.point));\n\t}\n\n\tsubtract(other: G1Element): G1Element {\n\t\treturn new G1Element(this.point.subtract(other.point));\n\t}\n\n\tstatic hashToCurve(data: Uint8Array): G1Element {\n\t\treturn new G1Element(\n\t\t\tbls12_381.G1.ProjectivePoint.fromAffine(bls12_381.G1.hashToCurve(data).toAffine()),\n\t\t);\n\t}\n\n\tpairing(other: G2Element): GTElement {\n\t\treturn new GTElement(bls12_381.pairing(this.point, other.point));\n\t}\n}\n\nexport class G2Element {\n\tpoint: ProjPointType<Fp2>;\n\n\tconstructor(point: ProjPointType<Fp2>) {\n\t\tthis.point = point;\n\t}\n\n\tstatic generator(): G2Element {\n\t\treturn new G2Element(bls12_381.G2.ProjectivePoint.BASE);\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): G2Element {\n\t\treturn new G2Element(bls12_381.G2.ProjectivePoint.fromHex(toHex(bytes)));\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\treturn this.point.toRawBytes();\n\t}\n\n\tmultiply(scalar: Scalar): G2Element {\n\t\treturn new G2Element(this.point.multiply(scalar.scalar));\n\t}\n\n\tadd(other: G2Element): G2Element {\n\t\treturn new G2Element(this.point.add(other.point));\n\t}\n\n\thashToCurve(data: Uint8Array): G2Element {\n\t\treturn new G2Element(\n\t\t\tbls12_381.G2.ProjectivePoint.fromAffine(bls12_381.G2.hashToCurve(data).toAffine()),\n\t\t);\n\t}\n}\n\nexport class GTElement {\n\telement: Fp12;\n\n\tconstructor(element: Fp12) {\n\t\tthis.element = element;\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\treturn bls12_381.fields.Fp12.toBytes(this.element);\n\t}\n}\n\nexport class Scalar {\n\tscalar: bigint;\n\n\tconstructor(scalar: bigint) {\n\t\tthis.scalar = scalar;\n\t}\n\n\tstatic random(): Scalar {\n\t\treturn Scalar.fromBytes(bls12_381.utils.randomPrivateKey());\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\treturn new Uint8Array(bls12_381.fields.Fr.toBytes(this.scalar));\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): Scalar {\n\t\treturn new Scalar(bls12_381.fields.Fr.fromBytes(bytes));\n\t}\n\n\tstatic fromNumber(num: number): Scalar {\n\t\treturn new Scalar(BigInt(num));\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAsB;AAGtB,uBAA0B;AAEnB,MAAM,UAAU;AAAA,EAGtB,YAAY,OAA8B;AACzC,SAAK,QAAQ;AAAA,EACd;AAAA,EAEA,OAAO,YAAuB;AAC7B,WAAO,IAAI,UAAU,2BAAU,GAAG,gBAAgB,IAAI;AAAA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AAO,IAAI,UAAU,KAAK,MAAM,IAAI,MAAM,KAAK,CAAC;AAAA,EACjD;AAAA,EAEA,YAAY,MAA6B;AACxC,WAAO,IAAI;AAAA,MACV,2BAAU,GAAG,gBAAgB,WAAW,2BAAU,GAAG,YAAY,IAAI,EAAE,SAAS,CAAC;AAAA,IAClF;AAAA,EACD;AACD;AAEO,MAAM,UAAU;AAAA,EAGtB,YAAY,SAAe;AAC1B,SAAK,UAAU;AAAA,EAChB;AAAA,EAEA,UAAsB;AACrB,WAAO,2BAAU,OAAO,KAAK,QAAQ,KAAK,OAAO;AAAA,EAClD;AACD;AAEO,MAAM,OAAO;AAAA,EAGnB,YAAY,QAAgB;AAC3B,SAAK,SAAS;AAAA,EACf;AAAA,EAEA,OAAO,SAAiB;AACvB,WAAO,OAAO,UAAU,2BAAU,MAAM,iBAAiB,CAAC;AAAA,EAC3D;AAAA,EAEA,UAAsB;AACrB,WAAO,IAAI,WAAW,2BAAU,OAAO,GAAG,QAAQ,KAAK,MAAM,CAAC;AAAA,EAC/D;AAAA,EAEA,OAAO,UAAU,OAA2B;AAC3C,WAAO,IAAI,OAAO,2BAAU,OAAO,GAAG,UAAU,KAAK,CAAC;AAAA,EACvD;AAAA,EAEA,OAAO,WAAW,KAAqB;AACtC,WAAO,IAAI,OAAO,OAAO,GAAG,CAAC;AAAA,EAC9B;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decrypt a ciphertext with a given secret key. The secret key must be a 32-byte scalar.
|
|
3
|
+
* The ciphertext is a pair of G1Elements (48 bytes).
|
|
4
|
+
*/
|
|
5
|
+
export declare function elgamalDecrypt(sk: Uint8Array, ciphertext: [Uint8Array, Uint8Array]): Uint8Array;
|
|
6
|
+
/** Generate a random secret key. */
|
|
7
|
+
export declare function generateSecretKey(): Uint8Array;
|
|
8
|
+
/** Derive the BLS public key for a given secret key. */
|
|
9
|
+
export declare function toPublicKey(sk: Uint8Array): Uint8Array;
|
|
10
|
+
/** Derive the BLS verification key for a given secret key. */
|
|
11
|
+
export declare function toVerificationKey(sk: Uint8Array): Uint8Array;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var elgamal_exports = {};
|
|
20
|
+
__export(elgamal_exports, {
|
|
21
|
+
elgamalDecrypt: () => elgamalDecrypt,
|
|
22
|
+
generateSecretKey: () => generateSecretKey,
|
|
23
|
+
toPublicKey: () => toPublicKey,
|
|
24
|
+
toVerificationKey: () => toVerificationKey
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(elgamal_exports);
|
|
27
|
+
var import_bls12381 = require("./bls12381.js");
|
|
28
|
+
function elgamalDecrypt(sk, ciphertext) {
|
|
29
|
+
return decrypt(import_bls12381.Scalar.fromBytes(sk), [
|
|
30
|
+
import_bls12381.G1Element.fromBytes(ciphertext[0]),
|
|
31
|
+
import_bls12381.G1Element.fromBytes(ciphertext[1])
|
|
32
|
+
]).toBytes();
|
|
33
|
+
}
|
|
34
|
+
function decrypt(sk, encryption) {
|
|
35
|
+
return encryption[1].subtract(encryption[0].multiply(sk));
|
|
36
|
+
}
|
|
37
|
+
function generateSecretKey() {
|
|
38
|
+
return import_bls12381.Scalar.random().toBytes();
|
|
39
|
+
}
|
|
40
|
+
function toPublicKey(sk) {
|
|
41
|
+
return import_bls12381.G1Element.generator().multiply(import_bls12381.Scalar.fromBytes(sk)).toBytes();
|
|
42
|
+
}
|
|
43
|
+
function toVerificationKey(sk) {
|
|
44
|
+
return import_bls12381.G2Element.generator().multiply(import_bls12381.Scalar.fromBytes(sk)).toBytes();
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=elgamal.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/elgamal.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { G1Element, G2Element, Scalar } from './bls12381.js';\n\n/**\n * Decrypt a ciphertext with a given secret key. The secret key must be a 32-byte scalar.\n * The ciphertext is a pair of G1Elements (48 bytes).\n */\nexport function elgamalDecrypt(sk: Uint8Array, ciphertext: [Uint8Array, Uint8Array]): Uint8Array {\n\treturn decrypt(Scalar.fromBytes(sk), [\n\t\tG1Element.fromBytes(ciphertext[0]),\n\t\tG1Element.fromBytes(ciphertext[1]),\n\t]).toBytes();\n}\n\n/**\n * Decrypt a ciphertext with a given secret key. The secret key must be a 32-byte scalar.\n * The ciphertext is a pair of G1Elements (48 bytes).\n */\nfunction decrypt(sk: Scalar, encryption: [G1Element, G1Element]): G1Element {\n\treturn encryption[1].subtract(encryption[0].multiply(sk));\n}\n\n// /**\n// * Encrypt a message with a given public key. Both the public key and the message must a compressed G1Element (48 bytes).\n// */\n// function elgamal_encrypt(pk: Uint8Array, message: Uint8Array): [Uint8Array, Uint8Array] {\n// \tconst ciphertext = encrypt(G1Element.fromBytes(pk), G1Element.fromBytes(message));\n// \treturn [ciphertext[0].toBytes(), ciphertext[1].toBytes()];\n// }\n\n// /**\n// * Encrypt a message with a given public key. Both the public key and the message must a compressed G1Element (48 bytes).\n// */\n// function encrypt(pk: G1Element, message: G1Element): [G1Element, G1Element] {\n// \tconst r = Scalar.random();\n// \treturn [G1Element.generator().multiply(r), pk.multiply(r).add(message)];\n// }\n\n/** Generate a random secret key. */\nexport function generateSecretKey(): Uint8Array {\n\treturn Scalar.random().toBytes();\n}\n\n/** Derive the BLS public key for a given secret key. */\nexport function toPublicKey(sk: Uint8Array): Uint8Array {\n\treturn G1Element.generator().multiply(Scalar.fromBytes(sk)).toBytes();\n}\n\n/** Derive the BLS verification key for a given secret key. */\nexport function toVerificationKey(sk: Uint8Array): Uint8Array {\n\treturn G2Element.generator().multiply(Scalar.fromBytes(sk)).toBytes();\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,sBAA6C;AAMtC,SAAS,eAAe,IAAgB,YAAkD;AAChG,SAAO,QAAQ,uBAAO,UAAU,EAAE,GAAG;AAAA,IACpC,0BAAU,UAAU,WAAW,CAAC,CAAC;AAAA,IACjC,0BAAU,UAAU,WAAW,CAAC,CAAC;AAAA,EAClC,CAAC,EAAE,QAAQ;AACZ;AAMA,SAAS,QAAQ,IAAY,YAA+C;AAC3E,SAAO,WAAW,CAAC,EAAE,SAAS,WAAW,CAAC,EAAE,SAAS,EAAE,CAAC;AACzD;AAmBO,SAAS,oBAAgC;AAC/C,SAAO,uBAAO,OAAO,EAAE,QAAQ;AAChC;AAGO,SAAS,YAAY,IAA4B;AACvD,SAAO,0BAAU,UAAU,EAAE,SAAS,uBAAO,UAAU,EAAE,CAAC,EAAE,QAAQ;AACrE;AAGO,SAAS,kBAAkB,IAA4B;AAC7D,SAAO,0BAAU,UAAU,EAAE,SAAS,uBAAO,UAAU,EAAE,CAAC,EAAE,QAAQ;AACrE;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { EncryptionInput } from './aes.js';
|
|
2
|
+
import type { KeyServer } from './key-server.js';
|
|
3
|
+
export declare const MAX_U8 = 255;
|
|
4
|
+
/**
|
|
5
|
+
* Given full ID and what key servers to use, return the encrypted message under the identity and return the bcs bytes of the encrypted object.
|
|
6
|
+
*
|
|
7
|
+
* @param keyServers - A list of KeyServers (same server can be used multiple times)
|
|
8
|
+
* @param packageId - packageId
|
|
9
|
+
* @param id - id
|
|
10
|
+
* @param encryptionInput - Input to the encryption. Should be one of the EncryptionInput types, AesGcmEncryptionInput or Plain.
|
|
11
|
+
* @param threshold - The threshold for the TSS encryption.
|
|
12
|
+
* @returns The bcs bytes of the encrypted object containing all metadata and the 256-bit symmetric key that was used to encrypt the object.
|
|
13
|
+
* Since the key can be used to decrypt, it should not be shared but can be used eg. for backup.
|
|
14
|
+
*/
|
|
15
|
+
export declare function encrypt<Input extends EncryptionInput>({ keyServers, threshold, packageId, id, encryptionInput, }: {
|
|
16
|
+
keyServers: KeyServer[];
|
|
17
|
+
threshold: number;
|
|
18
|
+
packageId: Uint8Array;
|
|
19
|
+
id: Uint8Array;
|
|
20
|
+
encryptionInput: Input;
|
|
21
|
+
}): Promise<{
|
|
22
|
+
encryptedObject: Uint8Array;
|
|
23
|
+
key: Uint8Array;
|
|
24
|
+
}>;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var encrypt_exports = {};
|
|
20
|
+
__export(encrypt_exports, {
|
|
21
|
+
MAX_U8: () => MAX_U8,
|
|
22
|
+
encrypt: () => encrypt
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(encrypt_exports);
|
|
25
|
+
var import_shamir_secret_sharing = require("shamir-secret-sharing");
|
|
26
|
+
var import_ibe = require("./ibe.js");
|
|
27
|
+
var import_key_server = require("./key-server.js");
|
|
28
|
+
var import_types = require("./types.js");
|
|
29
|
+
var import_utils = require("./utils.js");
|
|
30
|
+
const MAX_U8 = 255;
|
|
31
|
+
async function encrypt({
|
|
32
|
+
keyServers,
|
|
33
|
+
threshold,
|
|
34
|
+
packageId,
|
|
35
|
+
id,
|
|
36
|
+
encryptionInput
|
|
37
|
+
}) {
|
|
38
|
+
if (keyServers.length < threshold || threshold === 0 || keyServers.length > MAX_U8 || threshold > MAX_U8 || packageId.length !== 32) {
|
|
39
|
+
throw new Error("Invalid input");
|
|
40
|
+
}
|
|
41
|
+
if (keyServers.some((server) => server.keyType !== import_key_server.KeyServerType.BonehFranklinBLS12381)) {
|
|
42
|
+
throw new Error("Key type is not supported");
|
|
43
|
+
}
|
|
44
|
+
const ibeServers = new import_ibe.BonehFranklinBLS12381Services(keyServers);
|
|
45
|
+
const key = await encryptionInput.generateKey();
|
|
46
|
+
const ciphertext = await encryptionInput.encrypt(key);
|
|
47
|
+
const shares = await split(key, ibeServers.size(), threshold);
|
|
48
|
+
const fullId = (0, import_utils.createFullId)(import_ibe.DST, packageId, id);
|
|
49
|
+
const encrypted_shares = ibeServers.encryptBatched(
|
|
50
|
+
fullId,
|
|
51
|
+
shares.map(({ share, index }) => ({
|
|
52
|
+
msg: share,
|
|
53
|
+
info: new Uint8Array([index])
|
|
54
|
+
}))
|
|
55
|
+
);
|
|
56
|
+
const services = ibeServers.getObjectIds().map((id2, i) => [id2, shares[i].index]);
|
|
57
|
+
return {
|
|
58
|
+
encryptedObject: import_types.EncryptedObject.serialize({
|
|
59
|
+
version: 0,
|
|
60
|
+
package_id: packageId,
|
|
61
|
+
id,
|
|
62
|
+
services,
|
|
63
|
+
threshold,
|
|
64
|
+
encrypted_shares,
|
|
65
|
+
ciphertext
|
|
66
|
+
}).toBytes(),
|
|
67
|
+
key
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
async function split(secret, n, threshold) {
|
|
71
|
+
if (n === 0 || threshold === 0 || threshold > n) {
|
|
72
|
+
throw new Error("Invalid input");
|
|
73
|
+
} else if (threshold === 1) {
|
|
74
|
+
const result = [];
|
|
75
|
+
for (let i = 0; i < n; i++) {
|
|
76
|
+
result.push({ share: secret, index: i });
|
|
77
|
+
}
|
|
78
|
+
return Promise.resolve(result);
|
|
79
|
+
}
|
|
80
|
+
return (0, import_shamir_secret_sharing.split)(secret, n, threshold).then(
|
|
81
|
+
(share) => share.map((s) => ({
|
|
82
|
+
share: s.subarray(0, s.length - 1),
|
|
83
|
+
// split() returns the share index in the last byte
|
|
84
|
+
index: s[s.length - 1]
|
|
85
|
+
}))
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=encrypt.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/encrypt.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { split as externalSplit } from 'shamir-secret-sharing';\n\nimport type { EncryptionInput } from './aes.js';\nimport { BonehFranklinBLS12381Services, DST } from './ibe.js';\nimport type { KeyServer } from './key-server.js';\nimport { KeyServerType } from './key-server.js';\nimport { EncryptedObject } from './types.js';\nimport { createFullId } from './utils.js';\n\nexport const MAX_U8 = 255;\n\n/**\n * Given full ID and what key servers to use, return the encrypted message under the identity and return the bcs bytes of the encrypted object.\n *\n * @param keyServers - A list of KeyServers (same server can be used multiple times)\n * @param packageId - packageId\n * @param id - id\n * @param encryptionInput - Input to the encryption. Should be one of the EncryptionInput types, AesGcmEncryptionInput or Plain.\n * @param threshold - The threshold for the TSS encryption.\n * @returns The bcs bytes of the encrypted object containing all metadata and the 256-bit symmetric key that was used to encrypt the object.\n * Since the key can be used to decrypt, it should not be shared but can be used eg. for backup.\n */\nexport async function encrypt<Input extends EncryptionInput>({\n\tkeyServers,\n\tthreshold,\n\tpackageId,\n\tid,\n\tencryptionInput,\n}: {\n\tkeyServers: KeyServer[];\n\tthreshold: number;\n\tpackageId: Uint8Array;\n\tid: Uint8Array;\n\tencryptionInput: Input;\n}): Promise<{\n\tencryptedObject: Uint8Array;\n\tkey: Uint8Array;\n}> {\n\t// Check inputs\n\tif (\n\t\tkeyServers.length < threshold ||\n\t\tthreshold === 0 ||\n\t\tkeyServers.length > MAX_U8 ||\n\t\tthreshold > MAX_U8 ||\n\t\tpackageId.length !== 32\n\t) {\n\t\tthrow new Error('Invalid input');\n\t}\n\tif (keyServers.some((server) => server.keyType !== KeyServerType.BonehFranklinBLS12381)) {\n\t\tthrow new Error('Key type is not supported');\n\t}\n\tconst ibeServers = new BonehFranklinBLS12381Services(keyServers);\n\n\t// Generate a random symmetric key and encrypt the encryption input using this key.\n\tconst key = await encryptionInput.generateKey();\n\tconst ciphertext = await encryptionInput.encrypt(key);\n\n\t// Split the symmetric key into shares and encrypt each share with the public keys of the key servers.\n\tconst shares = await split(key, ibeServers.size(), threshold);\n\n\t// Encrypt the shares with the public keys of the key servers.\n\tconst fullId = createFullId(DST, packageId, id);\n\tconst encrypted_shares = ibeServers.encryptBatched(\n\t\tfullId,\n\t\tshares.map(({ share, index }) => ({\n\t\t\tmsg: share,\n\t\t\tinfo: new Uint8Array([index]),\n\t\t})),\n\t);\n\n\t// Services and indices of their shares are stored as a tuple\n\tconst services: [Uint8Array, number][] = ibeServers\n\t\t.getObjectIds()\n\t\t.map((id, i) => [id, shares[i].index]);\n\n\treturn {\n\t\tencryptedObject: EncryptedObject.serialize({\n\t\t\tversion: 0,\n\t\t\tpackage_id: packageId,\n\t\t\tid,\n\t\t\tservices,\n\t\t\tthreshold,\n\t\t\tencrypted_shares,\n\t\t\tciphertext,\n\t\t}).toBytes(),\n\t\tkey,\n\t};\n}\n\nasync function split(\n\tsecret: Uint8Array,\n\tn: number,\n\tthreshold: number,\n): Promise<{ index: number; share: Uint8Array }[]> {\n\t// The externalSplit function is from the 'shamir-secret-sharing' package and requires t > 1 and n >= 2.\n\t// So we handle the special cases here.\n\tif (n === 0 || threshold === 0 || threshold > n) {\n\t\tthrow new Error('Invalid input');\n\t} else if (threshold === 1) {\n\t\t// If the threshold is 1, the secret is not split.\n\t\tconst result = [];\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\t// The shared polynomial is a constant in this case, so the index doesn't matter.\n\t\t\t// To make sure they are unique, we use a counter.\n\t\t\tresult.push({ share: secret, index: i });\n\t\t}\n\t\treturn Promise.resolve(result);\n\t}\n\n\treturn externalSplit(secret, n, threshold).then((share) =>\n\t\tshare.map((s) => ({\n\t\t\tshare: s.subarray(0, s.length - 1),\n\t\t\t// split() returns the share index in the last byte\n\t\t\tindex: s[s.length - 1],\n\t\t})),\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,mCAAuC;AAGvC,iBAAmD;AAEnD,wBAA8B;AAC9B,mBAAgC;AAChC,mBAA6B;AAEtB,MAAM,SAAS;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AAG,IAAI,GAAG,KAAK;AAG3B,aAAO,KAAK,EAAE,OAAO,QAAQ,OAAO,EAAE,CAAC;AAAA,IACxC;AACA,WAAO,QAAQ,QAAQ,MAAM;AAAA,EAC9B;AAEA,aAAO,6BAAAC,OAAc,QAAQ,GAAG,SAAS,EAAE;AAAA,IAAK,CAAC,UAChD,MAAM,IAAI,CAAC,OAAO;AAAA,MACjB,OAAO,EAAE,SAAS,GAAG,EAAE,SAAS,CAAC;AAAA;AAAA,MAEjC,OAAO,EAAE,EAAE,SAAS,CAAC;AAAA,IACtB,EAAE;AAAA,EACH;AACD;",
|
|
6
|
+
"names": ["id", "externalSplit"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { G1Element, G2Element } from './bls12381.js';
|
|
2
|
+
import type { KeyServer } from './key-server.js';
|
|
3
|
+
import type { IBEEncryptionsType } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* The domain separation tag for the hash-to-group function.
|
|
6
|
+
*/
|
|
7
|
+
export declare const DST: Uint8Array;
|
|
8
|
+
/**
|
|
9
|
+
* The domain separation tag for the signing proof of possession.
|
|
10
|
+
*/
|
|
11
|
+
export declare const DST_POP: Uint8Array;
|
|
12
|
+
/**
|
|
13
|
+
* The interface for the key servers.
|
|
14
|
+
*/
|
|
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[];
|
|
22
|
+
/**
|
|
23
|
+
* The number of key servers.
|
|
24
|
+
*/
|
|
25
|
+
size(): number;
|
|
26
|
+
/**
|
|
27
|
+
* Encrypt a batch of messages for the given identity.
|
|
28
|
+
*
|
|
29
|
+
* @param id The identity.
|
|
30
|
+
* @param msg_and_infos The messages and an additional info parameter which will be included in the KDF.
|
|
31
|
+
* @returns The encrypted messages.
|
|
32
|
+
*/
|
|
33
|
+
abstract encryptBatched(id: Uint8Array, msg_and_infos: {
|
|
34
|
+
msg: Uint8Array;
|
|
35
|
+
info: Uint8Array;
|
|
36
|
+
}[]): IBEEncryptionsType;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Identity-based encryption based on the Boneh-Franklin IBE scheme.
|
|
40
|
+
* This object represents a set of key servers that can be used to encrypt messages for a given identity.
|
|
41
|
+
*/
|
|
42
|
+
export declare class BonehFranklinBLS12381Services extends IBEServers {
|
|
43
|
+
readonly public_keys: G2Element[];
|
|
44
|
+
constructor(services: KeyServer[]);
|
|
45
|
+
encryptBatched(id: Uint8Array, msg_and_infos: {
|
|
46
|
+
msg: Uint8Array;
|
|
47
|
+
info: Uint8Array;
|
|
48
|
+
}[]): IBEEncryptionsType;
|
|
49
|
+
/**
|
|
50
|
+
* Returns true if the user secret key is valid for the given public key and id.
|
|
51
|
+
* @param user_secret_key - The user secret key.
|
|
52
|
+
* @param id - The identity.
|
|
53
|
+
* @param public_key - The public key.
|
|
54
|
+
* @returns True if the user secret key is valid for the given public key and id.
|
|
55
|
+
*/
|
|
56
|
+
static verifyUserSecretKey(user_secret_key: G1Element, id: Uint8Array, public_key: G2Element): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Identity-based decryption.
|
|
59
|
+
*
|
|
60
|
+
* @param nonce The encryption nonce.
|
|
61
|
+
* @param sk The user secret key.
|
|
62
|
+
* @param ciphertext The encrypted message.
|
|
63
|
+
* @param info An info parameter also included in the KDF.
|
|
64
|
+
* @returns The decrypted message.
|
|
65
|
+
*/
|
|
66
|
+
static decrypt(nonce: G2Element, sk: G1Element, ciphertext: Uint8Array, info: Uint8Array): Uint8Array;
|
|
67
|
+
}
|