@t3foundry/tasra-sdk 0.0.0-stage → 0.3.0-next.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 (322) hide show
  1. package/CHANGELOG.md +148 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +94 -0
  4. package/README.md +137 -2
  5. package/SECURITY.md +115 -0
  6. package/dist/app/approvals.d.ts +50 -0
  7. package/dist/app/approvals.js +90 -0
  8. package/dist/app/authorization.d.ts +19 -0
  9. package/dist/app/authorization.js +20 -0
  10. package/dist/app/client.d.ts +231 -0
  11. package/dist/app/client.js +272 -0
  12. package/dist/app/creation.d.ts +73 -0
  13. package/dist/app/creation.js +77 -0
  14. package/dist/app/deployment.d.ts +36 -0
  15. package/dist/app/deployment.js +18 -0
  16. package/dist/app/identity.d.ts +211 -0
  17. package/dist/app/identity.js +287 -0
  18. package/dist/app/index.d.ts +22 -0
  19. package/dist/app/index.js +12 -0
  20. package/dist/app/manifest.d.ts +74 -0
  21. package/dist/app/manifest.js +120 -0
  22. package/dist/app/node.d.ts +30 -0
  23. package/dist/app/node.js +159 -0
  24. package/dist/app/ready-slot.d.ts +157 -0
  25. package/dist/app/ready-slot.js +199 -0
  26. package/dist/app/tasra-client.d.ts +125 -0
  27. package/dist/app/tasra-client.js +79 -0
  28. package/dist/app/viem.d.ts +10 -0
  29. package/dist/app/viem.js +32 -0
  30. package/dist/app/wallet.d.ts +126 -0
  31. package/dist/app/wallet.js +137 -0
  32. package/dist/auth/dpop.d.ts +86 -0
  33. package/dist/auth/dpop.js +142 -0
  34. package/dist/auth/holderProof.d.ts +102 -0
  35. package/dist/auth/holderProof.js +161 -0
  36. package/dist/auth/identityScope.d.ts +19 -0
  37. package/dist/auth/identityScope.js +82 -0
  38. package/dist/auth/index.d.ts +11 -0
  39. package/dist/auth/index.js +13 -0
  40. package/dist/auth/oid4vp.d.ts +230 -0
  41. package/dist/auth/oid4vp.js +780 -0
  42. package/dist/auth/rego/wasm/PINS.json +11 -0
  43. package/dist/auth/rego/wasm/keykeeper_rego_wasm.d.ts +76 -0
  44. package/dist/auth/rego/wasm/keykeeper_rego_wasm.js +379 -0
  45. package/dist/auth/rego/wasm/keykeeper_rego_wasm_bg.wasm +0 -0
  46. package/dist/auth/rego.d.ts +192 -0
  47. package/dist/auth/rego.js +492 -0
  48. package/dist/auth/verifier.d.ts +166 -0
  49. package/dist/auth/verifier.js +211 -0
  50. package/dist/chain/abis/accountantSetRegistry.d.ts +441 -0
  51. package/dist/chain/abis/accountantSetRegistry.js +574 -0
  52. package/dist/chain/abis/accountantSlashing.d.ts +2071 -0
  53. package/dist/chain/abis/accountantSlashing.js +2670 -0
  54. package/dist/chain/abis/bondingCurve.d.ts +398 -0
  55. package/dist/chain/abis/bondingCurve.js +521 -0
  56. package/dist/chain/abis/equivocationSlasher.d.ts +292 -0
  57. package/dist/chain/abis/equivocationSlasher.js +379 -0
  58. package/dist/chain/abis/fixedTasraPriceOracle.d.ts +52 -0
  59. package/dist/chain/abis/fixedTasraPriceOracle.js +72 -0
  60. package/dist/chain/abis/index.d.ts +12801 -0
  61. package/dist/chain/abis/index.js +64 -0
  62. package/dist/chain/abis/keeperShareRegistry.d.ts +519 -0
  63. package/dist/chain/abis/keeperShareRegistry.js +675 -0
  64. package/dist/chain/abis/keyRegistry.d.ts +2421 -0
  65. package/dist/chain/abis/keyRegistry.js +3114 -0
  66. package/dist/chain/abis/livenessRegistry.d.ts +402 -0
  67. package/dist/chain/abis/livenessRegistry.js +524 -0
  68. package/dist/chain/abis/mockEurc.d.ts +210 -0
  69. package/dist/chain/abis/mockEurc.js +276 -0
  70. package/dist/chain/abis/nodeRegistry.d.ts +1630 -0
  71. package/dist/chain/abis/nodeRegistry.js +2113 -0
  72. package/dist/chain/abis/platformExecutor.d.ts +328 -0
  73. package/dist/chain/abis/platformExecutor.js +429 -0
  74. package/dist/chain/abis/prevrandaoSaltBeacon.d.ts +16 -0
  75. package/dist/chain/abis/prevrandaoSaltBeacon.js +24 -0
  76. package/dist/chain/abis/serviceRegistry.d.ts +556 -0
  77. package/dist/chain/abis/serviceRegistry.js +719 -0
  78. package/dist/chain/abis/settlement.d.ts +1624 -0
  79. package/dist/chain/abis/settlement.js +2096 -0
  80. package/dist/chain/abis/tasraSwapRouter.d.ts +92 -0
  81. package/dist/chain/abis/tasraSwapRouter.js +122 -0
  82. package/dist/chain/abis/tasraToken.d.ts +455 -0
  83. package/dist/chain/abis/tasraToken.js +599 -0
  84. package/dist/chain/abis/tasraVestingVault.d.ts +165 -0
  85. package/dist/chain/abis/tasraVestingVault.js +215 -0
  86. package/dist/chain/abis/thresholdRandomBeacon.d.ts +378 -0
  87. package/dist/chain/abis/thresholdRandomBeacon.js +494 -0
  88. package/dist/chain/abis/treasury.d.ts +301 -0
  89. package/dist/chain/abis/treasury.js +390 -0
  90. package/dist/chain/abis/verifierSetRegistry.d.ts +445 -0
  91. package/dist/chain/abis/verifierSetRegistry.js +579 -0
  92. package/dist/chain/client.d.ts +212 -0
  93. package/dist/chain/client.js +576 -0
  94. package/dist/chain/commitmentRecovery.d.ts +21 -0
  95. package/dist/chain/commitmentRecovery.js +44 -0
  96. package/dist/chain/committeeClient.d.ts +102 -0
  97. package/dist/chain/committeeClient.js +137 -0
  98. package/dist/chain/deployments.d.ts +56 -0
  99. package/dist/chain/deployments.js +269 -0
  100. package/dist/chain/discovery.d.ts +49 -0
  101. package/dist/chain/discovery.js +107 -0
  102. package/dist/chain/events.d.ts +58 -0
  103. package/dist/chain/events.js +125 -0
  104. package/dist/chain/format.d.ts +25 -0
  105. package/dist/chain/format.js +51 -0
  106. package/dist/chain/index.d.ts +41 -0
  107. package/dist/chain/index.js +56 -0
  108. package/dist/chain/manifest.d.ts +165 -0
  109. package/dist/chain/manifest.js +132 -0
  110. package/dist/chain/network-profiles.json +75 -0
  111. package/dist/chain/networks.d.ts +73 -0
  112. package/dist/chain/networks.js +62 -0
  113. package/dist/chain/node.d.ts +44 -0
  114. package/dist/chain/node.js +176 -0
  115. package/dist/chain/offchain.d.ts +149 -0
  116. package/dist/chain/offchain.js +73 -0
  117. package/dist/chain/provisionRule.d.ts +164 -0
  118. package/dist/chain/provisionRule.js +212 -0
  119. package/dist/chain/registeredAgent.d.ts +64 -0
  120. package/dist/chain/registeredAgent.js +140 -0
  121. package/dist/chain/registeredOperation.d.ts +49 -0
  122. package/dist/chain/registeredOperation.js +136 -0
  123. package/dist/chain/registeredRelay.d.ts +206 -0
  124. package/dist/chain/registeredRelay.js +312 -0
  125. package/dist/chain/revertReason.d.ts +14 -0
  126. package/dist/chain/revertReason.js +62 -0
  127. package/dist/chain/ruleAmendment.d.ts +80 -0
  128. package/dist/chain/ruleAmendment.js +195 -0
  129. package/dist/chain/ruleAmendmentCore.d.ts +129 -0
  130. package/dist/chain/ruleAmendmentCore.js +152 -0
  131. package/dist/chain/ruleInput.d.ts +10 -0
  132. package/dist/chain/ruleInput.js +13 -0
  133. package/dist/chain/serviceIdentity.d.ts +176 -0
  134. package/dist/chain/serviceIdentity.js +237 -0
  135. package/dist/chain/serviceProfiles.d.ts +39 -0
  136. package/dist/chain/serviceProfiles.js +74 -0
  137. package/dist/chain/services.d.ts +141 -0
  138. package/dist/chain/services.js +79 -0
  139. package/dist/chain/slotClient.d.ts +55 -0
  140. package/dist/chain/slotClient.js +62 -0
  141. package/dist/chain/slotSeed.d.ts +31 -0
  142. package/dist/chain/slotSeed.js +98 -0
  143. package/dist/chain/write.d.ts +431 -0
  144. package/dist/chain/write.js +958 -0
  145. package/dist/client/client.d.ts +112 -0
  146. package/dist/client/client.js +111 -0
  147. package/dist/client/index.d.ts +3 -0
  148. package/dist/client/index.js +2 -0
  149. package/dist/client/session.d.ts +67 -0
  150. package/dist/client/session.js +238 -0
  151. package/dist/committee/client.d.ts +284 -0
  152. package/dist/committee/client.js +351 -0
  153. package/dist/committee/dual-sign.d.ts +110 -0
  154. package/dist/committee/dual-sign.js +202 -0
  155. package/dist/committee/ecdsa.d.ts +36 -0
  156. package/dist/committee/ecdsa.js +45 -0
  157. package/dist/committee/extraction.d.ts +82 -0
  158. package/dist/committee/extraction.js +96 -0
  159. package/dist/committee/index.d.ts +19 -0
  160. package/dist/committee/index.js +13 -0
  161. package/dist/committee/oauth.d.ts +51 -0
  162. package/dist/committee/oauth.js +90 -0
  163. package/dist/committee/receipts.d.ts +51 -0
  164. package/dist/committee/receipts.js +59 -0
  165. package/dist/committee/request.d.ts +264 -0
  166. package/dist/committee/request.js +305 -0
  167. package/dist/committee/token.d.ts +274 -0
  168. package/dist/committee/token.js +418 -0
  169. package/dist/crypto/constants.d.ts +10 -0
  170. package/dist/crypto/constants.js +32 -0
  171. package/dist/crypto/detect.d.ts +20 -0
  172. package/dist/crypto/detect.js +44 -0
  173. package/dist/crypto/envelope.d.ts +60 -0
  174. package/dist/crypto/envelope.js +243 -0
  175. package/dist/crypto/frost.d.ts +49 -0
  176. package/dist/crypto/frost.js +152 -0
  177. package/dist/crypto/hex.d.ts +7 -0
  178. package/dist/crypto/hex.js +24 -0
  179. package/dist/crypto/ibe-blob.d.ts +114 -0
  180. package/dist/crypto/ibe-blob.js +208 -0
  181. package/dist/crypto/ibe.d.ts +88 -0
  182. package/dist/crypto/ibe.js +262 -0
  183. package/dist/crypto/index.d.ts +12 -0
  184. package/dist/crypto/index.js +12 -0
  185. package/dist/crypto/kem.d.ts +55 -0
  186. package/dist/crypto/kem.js +241 -0
  187. package/dist/decryption/client.d.ts +64 -0
  188. package/dist/decryption/client.js +82 -0
  189. package/dist/decryption/index.d.ts +2 -0
  190. package/dist/decryption/index.js +2 -0
  191. package/dist/errors.d.ts +124 -0
  192. package/dist/errors.js +167 -0
  193. package/dist/index.d.ts +43 -0
  194. package/dist/index.js +98 -0
  195. package/dist/keys/index.d.ts +1 -0
  196. package/dist/keys/index.js +4 -0
  197. package/dist/keys/node-client.d.ts +27 -0
  198. package/dist/keys/node-client.js +97 -0
  199. package/dist/oid4vp/binding.d.ts +76 -0
  200. package/dist/oid4vp/binding.js +118 -0
  201. package/dist/oid4vp/did-web.d.ts +49 -0
  202. package/dist/oid4vp/did-web.js +57 -0
  203. package/dist/oid4vp/index.d.ts +9 -0
  204. package/dist/oid4vp/index.js +10 -0
  205. package/dist/oid4vp/jose.d.ts +166 -0
  206. package/dist/oid4vp/jose.js +242 -0
  207. package/dist/oid4vp/jwe.d.ts +30 -0
  208. package/dist/oid4vp/jwe.js +93 -0
  209. package/dist/oid4vp/oid4vci.d.ts +84 -0
  210. package/dist/oid4vp/oid4vci.js +85 -0
  211. package/dist/oid4vp/request-object.d.ts +109 -0
  212. package/dist/oid4vp/request-object.js +96 -0
  213. package/dist/oid4vp/sd-jwt.d.ts +177 -0
  214. package/dist/oid4vp/sd-jwt.js +208 -0
  215. package/dist/oid4vp/verifier-agent.d.ts +161 -0
  216. package/dist/oid4vp/verifier-agent.js +132 -0
  217. package/dist/oid4vp/wallet.d.ts +116 -0
  218. package/dist/oid4vp/wallet.js +108 -0
  219. package/dist/recipient/store.d.ts +67 -0
  220. package/dist/recipient/store.js +98 -0
  221. package/dist/signing/ecdsa.d.ts +49 -0
  222. package/dist/signing/ecdsa.js +79 -0
  223. package/dist/signing/frost.d.ts +88 -0
  224. package/dist/signing/frost.js +145 -0
  225. package/dist/signing/index.d.ts +4 -0
  226. package/dist/signing/index.js +3 -0
  227. package/dist/slots/faucet.d.ts +23 -0
  228. package/dist/slots/faucet.js +35 -0
  229. package/dist/slots/index.d.ts +2 -0
  230. package/dist/slots/index.js +4 -0
  231. package/dist/verifier-agent/index.d.ts +241 -0
  232. package/dist/verifier-agent/index.js +411 -0
  233. package/docs/DEVELOPER-EXPERIENCE.md +62 -0
  234. package/docs/README.md +33 -0
  235. package/docs/access-control.md +76 -0
  236. package/docs/ai-development.md +292 -0
  237. package/docs/api.md +338 -0
  238. package/docs/application-api.md +335 -0
  239. package/docs/architecture.md +65 -0
  240. package/docs/basics.md +48 -0
  241. package/docs/capabilities.md +86 -0
  242. package/docs/chain.md +153 -0
  243. package/docs/compatibility.md +36 -0
  244. package/docs/consumer-migrations.md +80 -0
  245. package/docs/create-slot.md +104 -0
  246. package/docs/document-signing.md +243 -0
  247. package/docs/encrypted-notes.md +129 -0
  248. package/docs/encryption.md +100 -0
  249. package/docs/errors.md +76 -0
  250. package/docs/flows.md +289 -0
  251. package/docs/fuji.md +127 -0
  252. package/docs/funding.md +126 -0
  253. package/docs/getting-started.md +94 -0
  254. package/docs/glossary.md +44 -0
  255. package/docs/installation.md +132 -0
  256. package/docs/manage-slot.md +95 -0
  257. package/docs/native-approvals.md +124 -0
  258. package/docs/prerequisites.md +137 -0
  259. package/docs/read-slot.md +41 -0
  260. package/docs/reference/README.md +30 -0
  261. package/docs/reference/app-node.md +102 -0
  262. package/docs/reference/app.md +1737 -0
  263. package/docs/reference/chain-node.md +159 -0
  264. package/docs/reference/chain.md +3609 -0
  265. package/docs/reference/committee.md +2176 -0
  266. package/docs/reference/main.md +4068 -0
  267. package/docs/reference/oid4vp.md +2547 -0
  268. package/docs/reference/verifier-agent.md +498 -0
  269. package/docs/shared-account.md +168 -0
  270. package/docs/signing-and-encryption.md +52 -0
  271. package/docs/signing.md +172 -0
  272. package/examples/committee-slot.ts +16 -0
  273. package/examples/connect-fuji.ts +41 -0
  274. package/examples/connect-network.ts +14 -0
  275. package/examples/credential-approvals.ts +46 -0
  276. package/examples/document-signing/README.md +59 -0
  277. package/examples/document-signing/backend.ts +132 -0
  278. package/examples/document-signing/build.mjs +19 -0
  279. package/examples/document-signing/config.ts +34 -0
  280. package/examples/document-signing/gitignore.template +9 -0
  281. package/examples/document-signing/model.ts +140 -0
  282. package/examples/document-signing/network.ts +58 -0
  283. package/examples/document-signing/package.json +25 -0
  284. package/examples/document-signing/public/index.html +27 -0
  285. package/examples/document-signing/public/sample.pdf +31 -0
  286. package/examples/document-signing/public/styles.css +531 -0
  287. package/examples/document-signing/server.ts +358 -0
  288. package/examples/document-signing/setup.ts +111 -0
  289. package/examples/document-signing/store.ts +105 -0
  290. package/examples/document-signing/tsconfig.json +24 -0
  291. package/examples/document-signing/verify.ts +24 -0
  292. package/examples/document-signing/web/app.ts +312 -0
  293. package/examples/document-signing/web/preview.ts +67 -0
  294. package/examples/document-signing/web/wallet.ts +146 -0
  295. package/examples/document-signing/workflow.test.ts +228 -0
  296. package/examples/document-signing/workflow.ts +241 -0
  297. package/examples/document-signing.ts +96 -0
  298. package/examples/encrypted-notes.ts +86 -0
  299. package/examples/ethers-signer.ts +204 -0
  300. package/examples/fund-slot.ts +73 -0
  301. package/examples/getting-started.ts +25 -0
  302. package/examples/learning-authorize.ts +23 -0
  303. package/examples/learning-cancel-slot.ts +11 -0
  304. package/examples/learning-context.ts +28 -0
  305. package/examples/learning-create-slot.ts +25 -0
  306. package/examples/learning-read-slot.ts +8 -0
  307. package/examples/learning-renew-slot.ts +12 -0
  308. package/examples/learning-sign.ts +8 -0
  309. package/examples/live-config.ts +71 -0
  310. package/examples/minimal.ts +6 -0
  311. package/examples/native-approvals.ts +57 -0
  312. package/examples/network.ts +58 -0
  313. package/examples/offline.ts +16 -0
  314. package/examples/personal-vault.ts +20 -0
  315. package/examples/provision-slot.ts +47 -0
  316. package/examples/shared-account.ts +46 -0
  317. package/examples/slot-address.ts +13 -0
  318. package/examples/slot-creation.ts +9 -0
  319. package/examples/tutorial-negative.ts +26 -0
  320. package/examples/tutorial-support.ts +76 -0
  321. package/examples/viem-account.ts +203 -0
  322. package/package.json +303 -4
@@ -0,0 +1,208 @@
1
+ // Identity-scoped ENVELOPE encryption for large objects (images, scans, documents).
2
+ //
3
+ // `ibeEncrypt` is a single-shot AEAD over the whole message: right for a record of a few
4
+ // kilobytes, wrong for a 500 MB CT series (whole-message in memory, no random access, and a
5
+ // JavaScript ChaCha20). This module keeps the identity binding where it is - a BF-IBE
6
+ // ciphertext - but applies it to a 32-byte DATA KEY, and encrypts the body with that key as
7
+ // AES-256-GCM CHUNKS through WebCrypto (hardware-speed in every browser and in Node at least 20):
8
+ //
9
+ // header = { v, blobId, identity, contentType, size, chunkSize, chunkCount, wrappedKey }
10
+ // body = chunk_0 || chunk_1 || ... each = AES-256-GCM(dek, nonce_i, aad_i, plain_i) || tag(16)
11
+ // nonce_i = blobId[0..8] || be32(i) (the data key is per object, so this is unique)
12
+ // aad_i = "keykeeper/ibe-blob/v1" || blobId || be32(i) || last(1) || identity
13
+ //
14
+ // Each chunk is independently decryptable (range requests, streaming), and the associated
15
+ // data binds every chunk to its position, to "is this the last one" (truncation fails), and to
16
+ // the object + identity (a chunk cannot be moved between blobs or identities). Only the
17
+ // wrapped key ever needs an identity key: unwrapping is one pairing regardless of size, and
18
+ // one extracted `sk_ID` opens every object written to that identity.
19
+ //
20
+ // Confidentiality only. Encrypting to an identity is permissionless (anyone holding the
21
+ // class public key can do it), so WHO produced a blob needs a separate signature by the
22
+ // producer over `sha256(body)` + the header - an application manifest, not this layer.
23
+ import { randomBytes } from '@noble/hashes/utils';
24
+ import { sha256 } from '@noble/hashes/sha256';
25
+ import { base64Encode, base64Decode } from './envelope.js';
26
+ import { ibeDecryptWithKey, ibeEncrypt } from './ibe.js';
27
+ export const IBE_BLOB_VERSION = 1;
28
+ export const IBE_BLOB_AAD_DOMAIN = 'keykeeper/ibe-blob/v1';
29
+ /** Default chunk: 1 MiB of plaintext (+16-byte tag on the wire). */
30
+ export const IBE_BLOB_DEFAULT_CHUNK = 1024 * 1024;
31
+ const TAG = 16;
32
+ const utf8 = (s) => new TextEncoder().encode(s);
33
+ const be32 = (n) => new Uint8Array([(n >>> 24) & 255, (n >>> 16) & 255, (n >>> 8) & 255, n & 255]);
34
+ // Takes an array, never spread arguments: a blob body has one part per chunk, and a call with
35
+ // ~100k arguments (a large blob at the minimum chunk size) overflows the stack.
36
+ function concat(parts) {
37
+ const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
38
+ let o = 0;
39
+ for (const p of parts) {
40
+ out.set(p, o);
41
+ o += p.length;
42
+ }
43
+ return out;
44
+ }
45
+ /** A copy backed by a plain ArrayBuffer - WebCrypto's `BufferSource` refuses views over
46
+ * `ArrayBufferLike` (a possibly-shared buffer) at the type level. */
47
+ function buf(u8) {
48
+ return new Uint8Array(u8);
49
+ }
50
+ function subtle() {
51
+ const s = globalThis.crypto?.subtle;
52
+ if (!s)
53
+ throw new Error('WebCrypto (crypto.subtle) is required for ibe-blob');
54
+ return s;
55
+ }
56
+ function nonceFor(blobId, index) {
57
+ return concat([blobId.subarray(0, 8), be32(index)]);
58
+ }
59
+ function aadFor(blobId, index, last, identity) {
60
+ return concat([utf8(IBE_BLOB_AAD_DOMAIN), blobId, be32(index), new Uint8Array([last ? 1 : 0]), utf8(identity)]);
61
+ }
62
+ /**
63
+ * Return a chunk's encrypted byte range as [start, end), including its 16-byte tag.
64
+ * The final chunk has header.size - index * header.chunkSize plaintext bytes.
65
+ * An empty object has one chunk: start 0, end 16, plainLength 0.
66
+ * Use a trusted header and validate that index is a safe integer before calling.
67
+ * Negative indices and indices at or beyond header.chunkCount throw RangeError.
68
+ * For an HTTP Range header, use end - 1 as the inclusive last byte.
69
+ *
70
+ * @param header - Blob header describing plaintext length and chunk size.
71
+ * @param index - Zero-based integer chunk index below header.chunkCount.
72
+ */
73
+ export function ibeBlobChunkRange(header, index) {
74
+ if (index < 0 || index >= header.chunkCount)
75
+ throw new RangeError(`chunk ${index} out of ${header.chunkCount}`);
76
+ const start = index * (header.chunkSize + TAG);
77
+ const plainLength = index === header.chunkCount - 1 ? header.size - index * header.chunkSize : header.chunkSize;
78
+ return { start, end: start + plainLength + TAG, plainLength };
79
+ }
80
+ /**
81
+ * Seal `plaintext` to `identity` under the slot's master public key: a fresh data key,
82
+ * IBE-wrapped, and the body as independently-decryptable AES-256-GCM chunks. Offline and
83
+ * permissionless, like `ibeEncrypt`.
84
+ * Empty plaintext produces one authenticated chunk with no plaintext and a 16-byte tag.
85
+ * An exact multiple of chunkSize has no extra chunk; otherwise the last chunk is shorter.
86
+ * Retain a trusted header, or authenticate a manifest containing both the header
87
+ * and body digest. A signed body digest alone does not authenticate the media type.
88
+ *
89
+ * @param mpkBytes - 96-byte compressed BLS master public key.
90
+ * @param identity - Identity string used to wrap the object data key.
91
+ * @param plaintext - Complete plaintext object bytes.
92
+ * @param opts - Optional media type and integer plaintext chunk size of at least 1024 bytes; default chunk size is 1 MiB.
93
+ */
94
+ export async function ibeSealBlob(mpkBytes, identity, plaintext, opts = {}) {
95
+ const chunkSize = opts.chunkSize ?? IBE_BLOB_DEFAULT_CHUNK;
96
+ if (!Number.isInteger(chunkSize) || chunkSize < 1024)
97
+ throw new RangeError('chunkSize must be an integer ≥ 1024');
98
+ const blobId = randomBytes(16);
99
+ const dek = randomBytes(32);
100
+ const key = await subtle().importKey('raw', buf(dek), { name: 'AES-GCM' }, false, ['encrypt']);
101
+ const chunkCount = Math.max(1, Math.ceil(plaintext.length / chunkSize));
102
+ const parts = [];
103
+ for (let i = 0; i < chunkCount; i++) {
104
+ const plain = plaintext.subarray(i * chunkSize, Math.min(plaintext.length, (i + 1) * chunkSize));
105
+ const ct = await subtle().encrypt({ name: 'AES-GCM', iv: buf(nonceFor(blobId, i)), additionalData: buf(aadFor(blobId, i, i === chunkCount - 1, identity)), tagLength: 128 }, key, buf(plain));
106
+ parts.push(new Uint8Array(ct));
107
+ }
108
+ const wrapped = ibeEncrypt(mpkBytes, utf8(identity), dek);
109
+ dek.fill(0);
110
+ return {
111
+ header: {
112
+ v: IBE_BLOB_VERSION,
113
+ blobId: base64Encode(blobId),
114
+ identity,
115
+ contentType: opts.contentType ?? 'application/octet-stream',
116
+ size: plaintext.length,
117
+ chunkSize,
118
+ chunkCount,
119
+ wrappedKey: { u: base64Encode(wrapped.u), nonce: base64Encode(wrapped.nonce), aead_ct: base64Encode(wrapped.aeadCt) },
120
+ },
121
+ body: concat(parts),
122
+ };
123
+ }
124
+ /**
125
+ * The blob's IBE-wrapped data key as an `IbeCiphertext` (what `ibeDecryptWithKey` takes).
126
+ *
127
+ * @param header - Blob header containing the wrapped data key.
128
+ */
129
+ export function ibeBlobWrappedKey(header) {
130
+ return { u: base64Decode(header.wrappedKey.u), nonce: base64Decode(header.wrappedKey.nonce), aeadCt: base64Decode(header.wrappedKey.aead_ct) };
131
+ }
132
+ /**
133
+ * Unwrap the data key with the identity's extracted key `sk_ID` (48-byte compressed G1 -
134
+ * `ibeCombineExtract`'s output). One pairing; the caller keeps the returned key in memory only
135
+ * as long as it decrypts, then zeroizes it.
136
+ *
137
+ * @param skIdBytes - 48-byte compressed extracted identity key.
138
+ * @param header - Header containing the wrapped data key and identity.
139
+ */
140
+ export function ibeUnwrapBlobKey(skIdBytes, header) {
141
+ const dek = ibeDecryptWithKey(skIdBytes, ibeBlobWrappedKey(header), utf8(header.identity));
142
+ if (dek.length !== 32)
143
+ throw new Error('ibe-blob: wrapped key did not unwrap to 32 bytes');
144
+ return dek;
145
+ }
146
+ /**
147
+ * A WebCrypto key for `dek`, importable once per blob and reused across chunks.
148
+ *
149
+ * @param dek - 32-byte unwrapped AES data key to import for decryption.
150
+ */
151
+ export async function ibeBlobDecryptKey(dek) {
152
+ return subtle().importKey('raw', buf(dek), { name: 'AES-GCM' }, false, ['decrypt']);
153
+ }
154
+ /**
155
+ * Decrypt one chunk (its exact body slice, see {@link ibeBlobChunkRange}).
156
+ *
157
+ * @param key - AES-GCM decryption key imported from the unwrapped data key.
158
+ * @param header - Header providing chunk identity and authentication context.
159
+ * @param index - Zero-based integer chunk index below header.chunkCount.
160
+ * @param chunk - Complete encrypted chunk including its authentication tag.
161
+ */
162
+ export async function ibeDecryptBlobChunk(key, header, index, chunk) {
163
+ const blobId = base64Decode(header.blobId);
164
+ const { plainLength } = ibeBlobChunkRange(header, index);
165
+ if (chunk.length !== plainLength + TAG)
166
+ throw new Error(`ibe-blob: chunk ${index} is ${chunk.length} bytes, expected ${plainLength + TAG}`);
167
+ try {
168
+ const plain = await subtle().decrypt({ name: 'AES-GCM', iv: buf(nonceFor(blobId, index)), additionalData: buf(aadFor(blobId, index, index === header.chunkCount - 1, header.identity)), tagLength: 128 }, key, buf(chunk));
169
+ return new Uint8Array(plain);
170
+ }
171
+ catch {
172
+ throw new Error(`ibe-blob: chunk ${index} failed authentication (tampered, reordered, or not this blob)`);
173
+ }
174
+ }
175
+ /**
176
+ * Open a whole sealed blob with `sk_ID`: unwrap the key, decrypt every chunk, return the
177
+ * plaintext. Streaming consumers use `ibeUnwrapBlobKey` + `ibeDecryptBlobChunk` per range.
178
+ *
179
+ * @param skIdBytes - 48-byte compressed extracted identity key.
180
+ * @param header - Header returned when the blob was sealed.
181
+ * @param body - Concatenated encrypted chunks in their original order.
182
+ */
183
+ export async function ibeOpenBlob(skIdBytes, header, body) {
184
+ const expected = header.size + header.chunkCount * TAG;
185
+ if (body.length !== expected)
186
+ throw new Error(`ibe-blob: body is ${body.length} bytes, header says ${expected}`);
187
+ const dek = ibeUnwrapBlobKey(skIdBytes, header);
188
+ try {
189
+ const key = await ibeBlobDecryptKey(dek);
190
+ const out = new Uint8Array(header.size);
191
+ for (let i = 0; i < header.chunkCount; i++) {
192
+ const { start, end } = ibeBlobChunkRange(header, i);
193
+ out.set(await ibeDecryptBlobChunk(key, header, i, body.subarray(start, end)), i * header.chunkSize);
194
+ }
195
+ return out;
196
+ }
197
+ finally {
198
+ dek.fill(0);
199
+ }
200
+ }
201
+ /**
202
+ * `sha256(body)` - what a producer signs in its manifest so a reader can check provenance.
203
+ *
204
+ * @param body - Complete encrypted blob body to hash.
205
+ */
206
+ export function ibeBlobDigest(body) {
207
+ return sha256(body);
208
+ }
@@ -0,0 +1,88 @@
1
+ /** Domain tag for the KDF/nonce derivations (mirrors `IBE_DOMAIN`). */
2
+ export declare const IBE_DOMAIN: Uint8Array<ArrayBuffer>;
3
+ /** RFC 9380 DST for identity hashing (mirrors `IBE_HASH_DST`). */
4
+ export declare const IBE_HASH_DST = "keykeeper/BLS12381-BF-IBE-HashToG1-v1";
5
+ /** Encrypted message - wire shape of `bls::ibe::Ciphertext`. */
6
+ export interface IbeCiphertext {
7
+ /** 96-byte compressed G2 ephemeral public key U = r*G2. */
8
+ u: Uint8Array;
9
+ /** 12-byte ChaCha20-Poly1305 nonce, derived deterministically from U. */
10
+ nonce: Uint8Array;
11
+ /** AEAD output (plaintext.len + 16-byte tag). */
12
+ aeadCt: Uint8Array;
13
+ }
14
+ /** One node's partial `D_i = sk_i * Q_ID` (48-byte compressed G1). */
15
+ export interface IbeDecryptionShare {
16
+ /** 1-indexed BLS group identifier (the extract reply's `identifier`). */
17
+ identifier: number;
18
+ /** 48-byte compressed G1. */
19
+ value: Uint8Array;
20
+ }
21
+ /** A node's dual-group verifying share - only the 96-byte G2 half is needed here. */
22
+ export type IbeVerifyingShares = ReadonlyMap<number, Uint8Array>;
23
+ /** `Q_ID` as 48 compressed bytes - pins the hash-to-curve suite + DST cross-language. */
24
+ export declare function ibeHashIdentityToG1(identity: Uint8Array): Uint8Array;
25
+ /**
26
+ * `gtBytes(e(Q_ID, r*mpk))` through the shipped internals - pins the Fp12 byte layout
27
+ * (the `gt_shared` vector field) so a serializer drift is diagnosed at the intermediate
28
+ * rather than as an opaque final-ciphertext mismatch.
29
+ */
30
+ export declare function ibeSharedGtBytes(mpkBytes: Uint8Array, identity: Uint8Array, r: bigint): Uint8Array;
31
+ /**
32
+ * Encrypt `message` to `identity` under the slot's master public key (96-byte
33
+ * compressed G2). Offline and permissionless - the identity's key need not exist yet.
34
+ *
35
+ * @param mpkBytes - 96-byte compressed BLS master public key.
36
+ * @param identity - Exact identity bytes that decryption must use.
37
+ * @param message - Plaintext bytes to encrypt.
38
+ */
39
+ export declare function ibeEncrypt(mpkBytes: Uint8Array, identity: Uint8Array, message: Uint8Array): IbeCiphertext;
40
+ /**
41
+ * Encrypt to an identity with a caller-supplied ephemeral scalar for deterministic
42
+ * interoperability checks. Never reuse the scalar across encryptions.
43
+ */
44
+ export declare function ibeEncryptWithScalar(mpkBytes: Uint8Array, identity: Uint8Array, message: Uint8Array, r: bigint): IbeCiphertext;
45
+ /**
46
+ * Verify one extraction partial against its node's dual-group verifying share (the
47
+ * 96-byte G2 half): `e(D_i, G2) == e(Q_ID, Y_i)`. Throws naming the identifier -
48
+ * identifiable abort: the caller knows WHICH node served a bad share.
49
+ *
50
+ * @param verifyingShares - Independently established participant identifiers and G2 verifying shares.
51
+ * @param identity - Exact bytes of the requested identity.
52
+ * @param share - Identity-key share to verify.
53
+ */
54
+ export declare function ibeVerifyShare(verifyingShares: IbeVerifyingShares, identity: Uint8Array, share: IbeDecryptionShare): void;
55
+ /**
56
+ * Lagrange-combine k verified partials into the identity key `sk_ID = msk * Q_ID`
57
+ * (48-byte compressed G1).
58
+ *
59
+ * Holding `sk_ID` is a DURABLE capability over every past and future ciphertext to
60
+ * this identity - prefer {@link ibeCombineDecrypt}, which uses and drops it. Verifies
61
+ * every share first (a caller combining unverified shares could be fed garbage that
62
+ * silently fails the AEAD later, unattributed).
63
+ *
64
+ * @param verifyingShares - Participant identifiers and authenticated G2 verifying shares.
65
+ * @param shares - Distinct identity-key shares sufficient for the slot threshold.
66
+ * @param identity - Exact bytes of the requested identity.
67
+ */
68
+ export declare function ibeCombineExtract(verifyingShares: IbeVerifyingShares, shares: IbeDecryptionShare[], identity: Uint8Array): Uint8Array;
69
+ /**
70
+ * Combine k extraction partials and AEAD-decrypt `ct` - mirrors
71
+ * `bls::ibe::combine_decrypt` (verify every share to Lagrange-combine to `T = e(sk_ID,U)`
72
+ * to KDF to open). The intermediate `sk_ID` never leaves this function.
73
+ *
74
+ * @param verifyingShares - Participant identifiers and authenticated G2 verifying shares.
75
+ * @param shares - Distinct identity-key shares sufficient for the slot threshold.
76
+ * @param ct - IBE ciphertext to decrypt.
77
+ * @param identity - Identity bytes used during encryption.
78
+ */
79
+ export declare function ibeCombineDecrypt(verifyingShares: IbeVerifyingShares, shares: IbeDecryptionShare[], ct: IbeCiphertext, identity: Uint8Array): Uint8Array;
80
+ /**
81
+ * Decrypt with an already-extracted identity key (48-byte compressed G1) - the
82
+ * custody-opt-in path pairing with {@link ibeCombineExtract}.
83
+ *
84
+ * @param skIdBytes - 48-byte compressed extracted identity key.
85
+ * @param ct - IBE ciphertext to decrypt.
86
+ * @param identity - Identity bytes used during encryption.
87
+ */
88
+ export declare function ibeDecryptWithKey(skIdBytes: Uint8Array, ct: IbeCiphertext, identity: Uint8Array): Uint8Array;
@@ -0,0 +1,262 @@
1
+ // BF-IBE (Boneh-Franklin BasicIdent) over BLS12-381 - mirrors the reference IBE implementation.
2
+ //
3
+ // THIS FILE MUST AGREE WITH THE RUST CRATE BYTE-FOR-BYTE. Real IBE, not the ElGamal
4
+ // KEM in ./kem.ts: the ciphertext is bound to the IDENTITY at the pairing level, and
5
+ // only sk_ID = msk*Q_ID (extracted by k threshold nodes for THAT identity via
6
+ // POST /v1/shards/ibe/extract) decrypts. The guard against drift is the
7
+ // shared corpus test/ibe-vectors.json - a byte-identical copy of the reference implementation's
8
+ // vector file, run through THESE shipped functions by test/ibe.conformance.ts.
9
+ //
10
+ // Encrypt(mpk, identity, message):
11
+ // Q_ID = HashToCurveG1(identity, IBE_HASH_DST) (RFC 9380, SHA-256 XMD)
12
+ // r ← Fr
13
+ // U = r * G2
14
+ // T = e(Q_ID, r * mpk) in Gt (bilinearity: e(Q_ID,mpk)^r)
15
+ // k = SHA256(IBE_DOMAIN || "key" || u64le(|identity|) || identity
16
+ // || u64le(576) || gtBytes(T))
17
+ // n = SHA256(IBE_DOMAIN || "nonce" || compress(U))[0..12]
18
+ // ct = ChaCha20Poly1305(k, n, aad=identity).encrypt(message)
19
+ //
20
+ // CombineDecrypt(verifyingShares, [(id, D_i)], {U,n,ct}, identity):
21
+ // verify each: e(D_i, G2) == e(Q_ID, Y_i_g2) (identifiable abort)
22
+ // sk_ID = Σ λ_i * D_i (Lagrange at 0 over Fr)
23
+ // T = e(sk_ID, U); k as above; AEAD open.
24
+ //
25
+ // gtBytes: `bls12_381_plus::Gt::to_bytes()` has NO standard encoding - it is the 12
26
+ // Fp tower components in order c0.c0.c0, c0.c0.c1, c0.c1.c0, ... c1.c2.c1, each 48-byte
27
+ // BIG-endian. Determined EMPIRICALLY against the reference vectors (fwd-be matched; three
28
+ // other candidate layouts did not) and pinned by the `gt_shared` vector field, so a
29
+ // noble upgrade that reshapes the tower fails loudly there.
30
+ import { chacha20poly1305 } from '@noble/ciphers/chacha';
31
+ import { bls12_381 } from '@noble/curves/bls12-381';
32
+ import { sha256 } from '@noble/hashes/sha256';
33
+ import { randomBytes } from '@noble/hashes/utils';
34
+ const { Fr, Fp12 } = bls12_381.fields;
35
+ const G1 = bls12_381.G1;
36
+ const G2 = bls12_381.G2;
37
+ /** Domain tag for the KDF/nonce derivations (mirrors `IBE_DOMAIN`). */
38
+ export const IBE_DOMAIN = new TextEncoder().encode('keykeeper/BLS12381-BF-IBE-v1');
39
+ /** RFC 9380 DST for identity hashing (mirrors `IBE_HASH_DST`). */
40
+ export const IBE_HASH_DST = 'keykeeper/BLS12381-BF-IBE-HashToG1-v1';
41
+ function hashToG1(identity) {
42
+ const p = bls12_381.G1.hashToCurve(identity, { DST: IBE_HASH_DST });
43
+ return G1.ProjectivePoint.fromAffine(p.toAffine());
44
+ }
45
+ function decodeG1(bytes, what) {
46
+ if (bytes.length !== 48)
47
+ throw new Error(`${what}: expected 48 bytes, got ${bytes.length}`);
48
+ const p = G1.ProjectivePoint.fromHex(bytes);
49
+ // Reject the identity, mirroring the reference decode_g1 - an infinity D_i defeats
50
+ // identifiable abort (it "verifies" against an infinity verifying share).
51
+ if (p.equals(G1.ProjectivePoint.ZERO))
52
+ throw new Error(`${what}: point at infinity`);
53
+ return p;
54
+ }
55
+ function decodeG2(bytes, what) {
56
+ if (bytes.length !== 96)
57
+ throw new Error(`${what}: expected 96 bytes, got ${bytes.length}`);
58
+ const p = G2.ProjectivePoint.fromHex(bytes);
59
+ // Reject the identity, mirroring the reference decode_g2 - a U = O ciphertext "decrypts"
60
+ // under every key.
61
+ if (p.equals(G2.ProjectivePoint.ZERO))
62
+ throw new Error(`${what}: point at infinity`);
63
+ return p;
64
+ }
65
+ /**
66
+ * `bls12_381_plus::Gt::to_bytes()` - 576 bytes: the 12 Fp tower components in order,
67
+ * each 48-byte big-endian. See the module header for how this layout was established.
68
+ */
69
+ function gtToBytes(gt) {
70
+ const out = new Uint8Array(576);
71
+ const g = gt;
72
+ const comps = [];
73
+ for (const six of [g.c0, g.c1]) {
74
+ for (const two of [six.c0, six.c1, six.c2]) {
75
+ comps.push(two.c0, two.c1);
76
+ }
77
+ }
78
+ comps.forEach((c, i) => {
79
+ let v = c;
80
+ for (let j = 47; j >= 0; j--) {
81
+ out[i * 48 + j] = Number(v & 0xffn);
82
+ v >>= 8n;
83
+ }
84
+ });
85
+ return out;
86
+ }
87
+ function deriveKey(identity, gtBytes) {
88
+ const len = (n) => {
89
+ const b = new Uint8Array(8);
90
+ new DataView(b.buffer).setBigUint64(0, BigInt(n), true);
91
+ return b;
92
+ };
93
+ const h = sha256.create();
94
+ h.update(IBE_DOMAIN);
95
+ h.update(new TextEncoder().encode('key'));
96
+ h.update(len(identity.length));
97
+ h.update(identity);
98
+ h.update(len(gtBytes.length));
99
+ h.update(gtBytes);
100
+ return h.digest();
101
+ }
102
+ function deriveNonce(uBytes) {
103
+ const h = sha256.create();
104
+ h.update(IBE_DOMAIN);
105
+ h.update(new TextEncoder().encode('nonce'));
106
+ h.update(uBytes);
107
+ return h.digest().slice(0, 12);
108
+ }
109
+ function lagrangeAtZero(id, xs) {
110
+ let num = 1n;
111
+ let den = 1n;
112
+ for (const xj of xs) {
113
+ if (xj === id)
114
+ continue;
115
+ num = Fr.mul(num, Fr.neg(xj));
116
+ den = Fr.mul(den, Fr.sub(id, xj));
117
+ }
118
+ return Fr.mul(num, Fr.inv(den));
119
+ }
120
+ function randomScalar() {
121
+ while (true) {
122
+ const bytes = randomBytes(64);
123
+ let n = 0n;
124
+ for (const b of bytes)
125
+ n = (n << 8n) | BigInt(b);
126
+ const r = n % Fr.ORDER;
127
+ if (r !== 0n)
128
+ return r;
129
+ }
130
+ }
131
+ // conformance seams (mirror the reference helpers)
132
+ /** `Q_ID` as 48 compressed bytes - pins the hash-to-curve suite + DST cross-language. */
133
+ export function ibeHashIdentityToG1(identity) {
134
+ return hashToG1(identity).toRawBytes(true);
135
+ }
136
+ /**
137
+ * `gtBytes(e(Q_ID, r*mpk))` through the shipped internals - pins the Fp12 byte layout
138
+ * (the `gt_shared` vector field) so a serializer drift is diagnosed at the intermediate
139
+ * rather than as an opaque final-ciphertext mismatch.
140
+ */
141
+ export function ibeSharedGtBytes(mpkBytes, identity, r) {
142
+ const qId = hashToG1(identity);
143
+ const mpk = decodeG2(mpkBytes, 'mpk');
144
+ return gtToBytes(bls12_381.pairing(qId, mpk.multiply(r)));
145
+ }
146
+ // encrypt
147
+ /**
148
+ * Encrypt `message` to `identity` under the slot's master public key (96-byte
149
+ * compressed G2). Offline and permissionless - the identity's key need not exist yet.
150
+ *
151
+ * @param mpkBytes - 96-byte compressed BLS master public key.
152
+ * @param identity - Exact identity bytes that decryption must use.
153
+ * @param message - Plaintext bytes to encrypt.
154
+ */
155
+ export function ibeEncrypt(mpkBytes, identity, message) {
156
+ return ibeEncryptWithScalar(mpkBytes, identity, message, randomScalar());
157
+ }
158
+ /**
159
+ * Encrypt to an identity with a caller-supplied ephemeral scalar for deterministic
160
+ * interoperability checks. Never reuse the scalar across encryptions.
161
+ */
162
+ export function ibeEncryptWithScalar(mpkBytes, identity, message, r) {
163
+ const qId = hashToG1(identity);
164
+ const mpk = decodeG2(mpkBytes, 'mpk');
165
+ const uPoint = G2.ProjectivePoint.BASE.multiply(r);
166
+ const uBytes = uPoint.toRawBytes(true);
167
+ // Bilinearity: e(Q_ID, mpk)^r = e(Q_ID, r*mpk).
168
+ const shared = bls12_381.pairing(qId, mpk.multiply(r));
169
+ const aeadKey = deriveKey(identity, gtToBytes(shared));
170
+ const nonce = deriveNonce(uBytes);
171
+ const aeadCt = chacha20poly1305(aeadKey, nonce, identity).encrypt(message);
172
+ return { u: uBytes, nonce, aeadCt };
173
+ }
174
+ // verify + combine
175
+ /**
176
+ * Verify one extraction partial against its node's dual-group verifying share (the
177
+ * 96-byte G2 half): `e(D_i, G2) == e(Q_ID, Y_i)`. Throws naming the identifier -
178
+ * identifiable abort: the caller knows WHICH node served a bad share.
179
+ *
180
+ * @param verifyingShares - Independently established participant identifiers and G2 verifying shares.
181
+ * @param identity - Exact bytes of the requested identity.
182
+ * @param share - Identity-key share to verify.
183
+ */
184
+ export function ibeVerifyShare(verifyingShares, identity, share) {
185
+ const vsBytes = verifyingShares.get(share.identifier);
186
+ if (!vsBytes)
187
+ throw new Error(`ibeVerifyShare: unknown identifier ${share.identifier}`);
188
+ const dI = decodeG1(share.value, `share ${share.identifier}`);
189
+ const yI = decodeG2(vsBytes, `verifying share ${share.identifier}`);
190
+ const qId = hashToG1(identity);
191
+ const lhs = bls12_381.pairing(dI, G2.ProjectivePoint.BASE);
192
+ const rhs = bls12_381.pairing(qId, yI);
193
+ if (!Fp12.eql(lhs, rhs)) {
194
+ throw new Error(`ibeVerifyShare: invalid share from identifier ${share.identifier}`);
195
+ }
196
+ }
197
+ /**
198
+ * Lagrange-combine k verified partials into the identity key `sk_ID = msk * Q_ID`
199
+ * (48-byte compressed G1).
200
+ *
201
+ * Holding `sk_ID` is a DURABLE capability over every past and future ciphertext to
202
+ * this identity - prefer {@link ibeCombineDecrypt}, which uses and drops it. Verifies
203
+ * every share first (a caller combining unverified shares could be fed garbage that
204
+ * silently fails the AEAD later, unattributed).
205
+ *
206
+ * @param verifyingShares - Participant identifiers and authenticated G2 verifying shares.
207
+ * @param shares - Distinct identity-key shares sufficient for the slot threshold.
208
+ * @param identity - Exact bytes of the requested identity.
209
+ */
210
+ export function ibeCombineExtract(verifyingShares, shares, identity) {
211
+ if (shares.length === 0)
212
+ throw new Error('ibeCombineExtract: no shares');
213
+ const ids = shares.map(s => s.identifier);
214
+ if (new Set(ids).size !== ids.length) {
215
+ throw new Error('ibeCombineExtract: duplicate identifier');
216
+ }
217
+ for (const s of shares)
218
+ ibeVerifyShare(verifyingShares, identity, s);
219
+ const xs = ids.map(BigInt);
220
+ let skId = G1.ProjectivePoint.ZERO;
221
+ for (const s of shares) {
222
+ const lambda = lagrangeAtZero(BigInt(s.identifier), xs);
223
+ skId = skId.add(decodeG1(s.value, `share ${s.identifier}`).multiply(lambda));
224
+ }
225
+ return skId.toRawBytes(true);
226
+ }
227
+ /**
228
+ * Combine k extraction partials and AEAD-decrypt `ct` - mirrors
229
+ * `bls::ibe::combine_decrypt` (verify every share to Lagrange-combine to `T = e(sk_ID,U)`
230
+ * to KDF to open). The intermediate `sk_ID` never leaves this function.
231
+ *
232
+ * @param verifyingShares - Participant identifiers and authenticated G2 verifying shares.
233
+ * @param shares - Distinct identity-key shares sufficient for the slot threshold.
234
+ * @param ct - IBE ciphertext to decrypt.
235
+ * @param identity - Identity bytes used during encryption.
236
+ */
237
+ export function ibeCombineDecrypt(verifyingShares, shares, ct, identity) {
238
+ const skIdBytes = ibeCombineExtract(verifyingShares, shares, identity);
239
+ try {
240
+ return ibeDecryptWithKey(skIdBytes, ct, identity);
241
+ }
242
+ finally {
243
+ skIdBytes.fill(0);
244
+ }
245
+ }
246
+ /**
247
+ * Decrypt with an already-extracted identity key (48-byte compressed G1) - the
248
+ * custody-opt-in path pairing with {@link ibeCombineExtract}.
249
+ *
250
+ * @param skIdBytes - 48-byte compressed extracted identity key.
251
+ * @param ct - IBE ciphertext to decrypt.
252
+ * @param identity - Identity bytes used during encryption.
253
+ */
254
+ export function ibeDecryptWithKey(skIdBytes, ct, identity) {
255
+ const skId = decodeG1(skIdBytes, 'sk_ID');
256
+ const u = decodeG2(ct.u, 'ciphertext.u');
257
+ const shared = bls12_381.pairing(skId, u);
258
+ const aeadKey = deriveKey(identity, gtToBytes(shared));
259
+ if (ct.nonce.length !== 12)
260
+ throw new Error('ciphertext.nonce: expected 12 bytes');
261
+ return chacha20poly1305(aeadKey, ct.nonce, identity).decrypt(ct.aeadCt);
262
+ }
@@ -0,0 +1,12 @@
1
+ export { encryptEnvelope, toBytes, fromBytes, MAX_PLAINTEXT_LEN } from './envelope.js';
2
+ export type { GroupEnvelope } from './envelope.js';
3
+ export { decryptWithMasterKey, combineDecryptShares, verifyDecryptShare } from './kem.js';
4
+ export type { DecryptShare, Ciphertext } from './kem.js';
5
+ export { ibeEncrypt, ibeVerifyShare, ibeCombineExtract, ibeCombineDecrypt, ibeDecryptWithKey, IBE_DOMAIN, IBE_HASH_DST, } from './ibe.js';
6
+ export type { IbeCiphertext, IbeDecryptionShare, IbeVerifyingShares } from './ibe.js';
7
+ export { buildTasraText, parseTasraPost, isTasraPost } from './detect.js';
8
+ export { hexToBytes } from './hex.js';
9
+ export { aggregate, verify } from './frost.js';
10
+ export type { FrostSignature, FrostCommitment, FrostShare } from './frost.js';
11
+ export { ibeSealBlob, ibeOpenBlob, ibeUnwrapBlobKey, ibeBlobDecryptKey, ibeDecryptBlobChunk, ibeBlobChunkRange, ibeBlobWrappedKey, ibeBlobDigest, IBE_BLOB_DEFAULT_CHUNK, } from './ibe-blob.js';
12
+ export type { IbeBlobHeader, SealedBlob } from './ibe-blob.js';
@@ -0,0 +1,12 @@
1
+ // Pure cryptographic primitives - NO network I/O. BLS KEM + envelope wire format,
2
+ // FROST-Ed25519 signature aggregation, [KK] envelope detection, hex helpers.
3
+ export { encryptEnvelope, toBytes, fromBytes, MAX_PLAINTEXT_LEN } from './envelope.js';
4
+ export { decryptWithMasterKey, combineDecryptShares, verifyDecryptShare } from './kem.js';
5
+ // BF-IBE: real identity-bound encryption (pairing-level), not the KEM above.
6
+ export { ibeEncrypt, ibeVerifyShare, ibeCombineExtract, ibeCombineDecrypt, ibeDecryptWithKey, IBE_DOMAIN, IBE_HASH_DST, } from './ibe.js';
7
+ export { buildTasraText, parseTasraPost, isTasraPost } from './detect.js';
8
+ export { hexToBytes } from './hex.js';
9
+ export { aggregate, verify } from './frost.js';
10
+ // Identity-scoped envelope encryption for large objects (images, scans): IBE-wrapped data key
11
+ // + chunked AES-256-GCM body (WebCrypto).
12
+ export { ibeSealBlob, ibeOpenBlob, ibeUnwrapBlobKey, ibeBlobDecryptKey, ibeDecryptBlobChunk, ibeBlobChunkRange, ibeBlobWrappedKey, ibeBlobDigest, IBE_BLOB_DEFAULT_CHUNK, } from './ibe-blob.js';
@@ -0,0 +1,55 @@
1
+ export declare function leToScalar(leBytes: Uint8Array): bigint;
2
+ export declare function scalarToLe(n: bigint): Uint8Array;
3
+ /** BLS group encryption ciphertext containing an ephemeral key, nonce and authenticated payload. */
4
+ export interface Ciphertext {
5
+ /** 96-byte compressed G2 ephemeral public key U = r*G2. */
6
+ u: Uint8Array;
7
+ /** 12-byte ChaCha20-Poly1305 nonce, derived from U. */
8
+ nonce: Uint8Array;
9
+ /** AEAD ciphertext: plaintext.len + 16 (Poly1305 tag). */
10
+ aeadCt: Uint8Array;
11
+ }
12
+ export interface Shard {
13
+ /** 1-indexed participant identifier (u16, non-zero). */
14
+ id: number;
15
+ /** 32-byte little-endian BLS12-381 Fr scalar (sk_i). */
16
+ bytes: Uint8Array;
17
+ }
18
+ export declare function assembleKey(shards: Shard[]): Uint8Array;
19
+ export declare function encrypt(mpkBytes: Uint8Array, identity: Uint8Array, plaintext: Uint8Array): Ciphertext;
20
+ /**
21
+ * Decrypt with a reconstructed BLS master secret key and the original associated data. Reject malformed keys or failed authentication.
22
+ *
23
+ * @param mskBytes - 32-byte little-endian master secret scalar.
24
+ * @param ct - Group ciphertext to decrypt.
25
+ * @param identity - Original associated data supplied during encryption.
26
+ */
27
+ export declare function decryptWithMasterKey(mskBytes: Uint8Array, ct: Ciphertext, identity: Uint8Array): Uint8Array;
28
+ /** One participant's partial BLS decryption and optional public verification material. */
29
+ export interface DecryptShare {
30
+ /** 1-indexed BLS participant identifier (u16). */
31
+ id: number;
32
+ /** 96-byte compressed G2 partial decryption D_i = sk_i*U. */
33
+ decryptionShare: Uint8Array;
34
+ /** 144-byte verifying share: 96B compressed G2 (sk_i*g2) || 48B compressed G1
35
+ * (sk_i*g1). Required only for verifyDecryptShare. */
36
+ verifyingShare?: Uint8Array;
37
+ }
38
+ /**
39
+ * Check a partial decryption against its supplied verifying share using a pairing. Return false for missing or malformed material.
40
+ *
41
+ * @param share - Partial decryption and its public verifying share.
42
+ * @param u - 96-byte compressed ephemeral G2 key from the ciphertext.
43
+ */
44
+ export declare function verifyDecryptShare(share: DecryptShare, u: Uint8Array): boolean;
45
+ /**
46
+ * Interpolate distinct partial decryptions and authenticate the plaintext. Enable verify to check each share before combining; the caller supplies a sufficient threshold.
47
+ *
48
+ * @param shares - Distinct participant shares sufficient for the slot threshold.
49
+ * @param ct - Ciphertext associated with the partial decryptions.
50
+ * @param identity - Original encryption associated data.
51
+ * @param opts - Whether to verify each partial decryption before combining.
52
+ */
53
+ export declare function combineDecryptShares(shares: DecryptShare[], ct: Ciphertext, identity: Uint8Array, opts?: {
54
+ verify?: boolean;
55
+ }): Uint8Array;