@7h3/protocol 0.5.0 → 0.5.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 (294) hide show
  1. package/README.md +6 -1317
  2. package/agentAdapter.d.ts +26 -0
  3. package/auditLog.d.ts +54 -0
  4. package/capability.d.ts +67 -0
  5. package/cborCodec.d.ts +34 -0
  6. package/conformanceVectors.d.ts +20 -0
  7. package/encryption.d.ts +85 -0
  8. package/envelopeCbor.d.ts +33 -0
  9. package/frameworkAdapters.d.ts +72 -0
  10. package/gateway.d.ts +53 -0
  11. package/grpcBinding.d.ts +29 -0
  12. package/httpBinding.d.ts +50 -0
  13. package/index.d.ts +39 -0
  14. package/index.js +3662 -0
  15. package/keyInfra.d.ts +64 -0
  16. package/keyRegistry.d.ts +9 -0
  17. package/keyRotation.d.ts +20 -0
  18. package/mcpGateway.d.ts +37 -0
  19. package/mcpTransports.d.ts +62 -0
  20. package/mcpWrapper.d.ts +83 -0
  21. package/otel.d.ts +65 -0
  22. package/package.json +13 -121
  23. package/policyEnforcer.d.ts +50 -0
  24. package/policyTelemetryFeedback.d.ts +11 -0
  25. package/{src/protocol.d.ts → protocol.d.ts} +1 -2
  26. package/protocolAgent.d.ts +58 -0
  27. package/protocolBinary.d.ts +8 -0
  28. package/protocolCapabilities.d.ts +24 -0
  29. package/protocolReplay.d.ts +35 -0
  30. package/protocolTransport.d.ts +73 -0
  31. package/queueBinding.d.ts +43 -0
  32. package/rateLimiter.d.ts +18 -0
  33. package/{src/redisClient.ts → redisClient.d.ts} +25 -75
  34. package/replayStores.d.ts +98 -0
  35. package/revocation.d.ts +71 -0
  36. package/routePolicy.d.ts +30 -0
  37. package/runtimePolicy.d.ts +24 -0
  38. package/runtimePolicyManager.d.ts +15 -0
  39. package/runtimePolicyPresets.d.ts +11 -0
  40. package/signedResponse.d.ts +21 -0
  41. package/stream.d.ts +59 -0
  42. package/telemetry.d.ts +79 -0
  43. package/webhookBinding.d.ts +30 -0
  44. package/wsBinding.d.ts +52 -0
  45. package/.dockerignore +0 -19
  46. package/.github/dependabot.yml +0 -32
  47. package/.github/workflows/ci.yml +0 -31
  48. package/.github/workflows/publish.yml +0 -59
  49. package/.github/workflows/scorecard.yml +0 -37
  50. package/7h3.example.yaml +0 -125
  51. package/CHANGELOG.md +0 -152
  52. package/CONTRIBUTING.md +0 -82
  53. package/Dockerfile +0 -73
  54. package/GOVERNANCE.md +0 -62
  55. package/SECURITY.md +0 -70
  56. package/bench-results/replay-cache-full-1777891033256.json +0 -10
  57. package/bench-results/replay-cache-full-1777896317488.json +0 -10
  58. package/bench-results/replay-cache-full-1777900993184.json +0 -10
  59. package/bench-results/replay-cache-full-1777901019285.json +0 -10
  60. package/bench-results/replay-cache-quick-1777870170126.json +0 -10
  61. package/bench-results/signature-profiles-quick-1775875160079.json +0 -85
  62. package/bench-results/signature-profiles-quick-1775983539716.json +0 -85
  63. package/bench-results/signature-profiles-quick-1776237913190.json +0 -85
  64. package/bench-results/wire-codecs-full-1777891019803.json +0 -93
  65. package/bench-results/wire-codecs-full-1777896260964.json +0 -93
  66. package/bench-results/wire-codecs-full-1777901004247.json +0 -93
  67. package/bench-results/wire-codecs-quick-1775972879056.json +0 -93
  68. package/bench-results/wire-codecs-quick-1775983541111.json +0 -93
  69. package/bench-results/wire-codecs-quick-1776237914299.json +0 -93
  70. package/bench-results/wire-codecs-quick-1777841285236.json +0 -93
  71. package/bench-results/wire-codecs-quick-1777841321772.json +0 -93
  72. package/bench-results/wire-codecs-quick-1777841330408.json +0 -93
  73. package/bench-results/wire-codecs-quick-1777852886082.json +0 -93
  74. package/bench-results/wire-codecs-quick-1777852988773.json +0 -93
  75. package/bench-results/wire-codecs-quick-1777870188095.json +0 -93
  76. package/bench-results/wire-codecs-quick-1777870263918.json +0 -93
  77. package/bench-results/wire-codecs-quick-1777870455034.json +0 -93
  78. package/bench-results/wire-codecs-quick-1778816163081.json +0 -93
  79. package/bench-results/wire-codecs-quick-1778843936130.json +0 -93
  80. package/bin/7h3.ts +0 -406
  81. package/conformance/7h3_v0_1.json +0 -77
  82. package/conformance/7h3_v0_1_binary.json +0 -20
  83. package/conformance/aip_v0_1_binary.json +0 -20
  84. package/docker-compose.yaml +0 -77
  85. package/docs/ADOPTION_PLAN.md +0 -120
  86. package/docs/AGENTS.md +0 -77
  87. package/docs/AIP_RFC_v0.1.md +0 -97
  88. package/docs/AI_DECISION_CARD.md +0 -122
  89. package/docs/AI_RUNTIME_POLICY.json +0 -126
  90. package/docs/AI_RUNTIME_POLICY.yaml +0 -110
  91. package/docs/BACKPRESSURE_TUNING.md +0 -65
  92. package/docs/BENCHMARK_CLAIM_MATRIX.md +0 -42
  93. package/docs/BENCHMARK_REPORT_TEMPLATE.md +0 -169
  94. package/docs/BINARY_CODEC_BENCH.md +0 -23
  95. package/docs/CLEAN_CLONE_RUNBOOK.md +0 -36
  96. package/docs/CLOCK_SKEW_POLICY.md +0 -30
  97. package/docs/DISTRIBUTED_REPLAY.md +0 -142
  98. package/docs/FUZZ_CAMPAIGN.md +0 -121
  99. package/docs/GATEWAY.md +0 -195
  100. package/docs/KEY_MANAGEMENT_POLICY.md +0 -53
  101. package/docs/KEY_REVOCATION.md +0 -69
  102. package/docs/MCP_WRAPPER.md +0 -159
  103. package/docs/MIGRATION_GUIDE.md +0 -40
  104. package/docs/OPERATORS.md +0 -184
  105. package/docs/PERF_REGRESSION_POLICY.md +0 -34
  106. package/docs/PROJECT_EXAMINATION_2026-05-31.md +0 -219
  107. package/docs/RELEASE_BENCHMARK_REPORT_2026-05-15.md +0 -135
  108. package/docs/RELEASE_GATE.md +0 -25
  109. package/docs/RELEASE_NOTES_v0.1.0.md +0 -54
  110. package/docs/SECURITY_REVIEW_2026-06-05.md +0 -165
  111. package/docs/TELEMETRY.md +0 -41
  112. package/docs/THREAT_MODEL.md +0 -89
  113. package/docs/VERSIONING_POLICY.md +0 -30
  114. package/docs/assets/banner-github.png +0 -0
  115. package/docs/assets/banner.png +0 -0
  116. package/docs/assets/banner.svg +0 -123
  117. package/eslint.config.js +0 -15
  118. package/fuzz/ts/harness-decode.ts +0 -136
  119. package/fuzz/ts/harness-verify.ts +0 -121
  120. package/fuzz/ts/run.ts +0 -35
  121. package/mcp-server/README.md +0 -38
  122. package/mcp-server/package-lock.json +0 -1187
  123. package/mcp-server/package.json +0 -35
  124. package/mcp-server/src/index.ts +0 -236
  125. package/mcp-server/tsconfig.json +0 -14
  126. package/scripts/aip-framework-quickstart.ts +0 -110
  127. package/scripts/aip-mcp-gateway.ts +0 -38
  128. package/scripts/aip-mcp-wrap-demo.ts +0 -72
  129. package/scripts/aip-quickstart.ts +0 -60
  130. package/scripts/bench-diff.ts +0 -118
  131. package/scripts/bench-protocol-e2e.ts +0 -937
  132. package/scripts/bench-protocol-openloop.ts +0 -1397
  133. package/scripts/bench-replay-cache.ts +0 -76
  134. package/scripts/bench-signature-profiles.ts +0 -180
  135. package/scripts/bench-wire-codecs.ts +0 -161
  136. package/scripts/build-binary-conformance.ts +0 -36
  137. package/scripts/build-release-dashboard.ts +0 -175
  138. package/scripts/canary-rollout.ts +0 -38
  139. package/scripts/mcpGatewayCli.test.ts +0 -116
  140. package/scripts/prepare-aip-package.ts +0 -88
  141. package/scripts/regen-conformance-sigs.ts +0 -18
  142. package/scripts/release-gate.ts +0 -19
  143. package/scripts/validate-runtime-policy.ts +0 -18
  144. package/sdk/browser/index.test.ts +0 -162
  145. package/sdk/browser/index.ts +0 -257
  146. package/sdk/browser/package.json +0 -13
  147. package/sdk/go/cbor.go +0 -551
  148. package/sdk/go/cbor_test.go +0 -232
  149. package/sdk/go/encryption.go +0 -280
  150. package/sdk/go/encryption_test.go +0 -318
  151. package/sdk/go/go.mod +0 -7
  152. package/sdk/go/go.sum +0 -4
  153. package/sdk/go/http.go +0 -135
  154. package/sdk/go/protocol.go +0 -324
  155. package/sdk/go/protocol_test.go +0 -334
  156. package/sdk/go/replay.go +0 -121
  157. package/sdk/go/replay_test.go +0 -149
  158. package/sdk/go/webhook.go +0 -136
  159. package/sdk/pq/package-lock.json +0 -1358
  160. package/sdk/pq/package.json +0 -42
  161. package/sdk/pq/src/index.test.ts +0 -143
  162. package/sdk/pq/src/index.ts +0 -166
  163. package/sdk/pq/tsconfig.json +0 -14
  164. package/sdk/pq/vitest.config.ts +0 -7
  165. package/sdk/python/README.md +0 -18
  166. package/sdk/python/protocol_7h3/__init__.py +0 -46
  167. package/sdk/python/protocol_7h3/encryption.py +0 -252
  168. package/sdk/python/protocol_7h3/http.py +0 -212
  169. package/sdk/python/protocol_7h3/keys.py +0 -149
  170. package/sdk/python/protocol_7h3/pq.py +0 -244
  171. package/sdk/python/protocol_7h3/protocol.py +0 -525
  172. package/sdk/python/protocol_7h3/queue.py +0 -118
  173. package/sdk/python/protocol_7h3/replay.py +0 -98
  174. package/sdk/python/protocol_7h3/webhook.py +0 -116
  175. package/sdk/python/pyproject.toml +0 -40
  176. package/sdk/python/tests/test_conformance.py +0 -110
  177. package/sdk/python/tests/test_encryption.py +0 -206
  178. package/sdk/python/tests/test_http.py +0 -305
  179. package/sdk/python/tests/test_keys.py +0 -417
  180. package/sdk/python/tests/test_queue.py +0 -120
  181. package/sdk/python/tests/test_webhook.py +0 -345
  182. package/sdk/rust/Cargo.lock +0 -371
  183. package/sdk/rust/Cargo.toml +0 -25
  184. package/sdk/rust/README.md +0 -31
  185. package/sdk/rust/fuzz/Cargo.toml +0 -29
  186. package/sdk/rust/fuzz/fuzz_targets/fuzz_canonicalize.rs +0 -46
  187. package/sdk/rust/fuzz/fuzz_targets/fuzz_decode.rs +0 -11
  188. package/sdk/rust/src/bin/aip_mcp_gateway.rs +0 -59
  189. package/sdk/rust/src/http.rs +0 -145
  190. package/sdk/rust/src/keys.rs +0 -161
  191. package/sdk/rust/src/lib.rs +0 -688
  192. package/sdk/rust/src/queue.rs +0 -79
  193. package/sdk/rust/src/webhook.rs +0 -86
  194. package/sdk/rust/tests/conformance.rs +0 -148
  195. package/sdk/rust/tests/gateway.rs +0 -130
  196. package/sdk/rust/tests/http_webhook_queue.rs +0 -201
  197. package/sdk/rust/tests/keys.rs +0 -189
  198. package/sdk/threshold/index.d.ts +0 -68
  199. package/sdk/threshold/index.d.ts.map +0 -1
  200. package/sdk/threshold/index.js +0 -254
  201. package/sdk/threshold/package-lock.json +0 -1361
  202. package/sdk/threshold/package.json +0 -39
  203. package/sdk/threshold/src/index.d.ts +0 -68
  204. package/sdk/threshold/src/index.d.ts.map +0 -1
  205. package/sdk/threshold/src/index.js +0 -254
  206. package/sdk/threshold/src/index.test.ts +0 -238
  207. package/sdk/threshold/src/index.ts +0 -355
  208. package/sdk/threshold/tsconfig.json +0 -19
  209. package/sdk/threshold/vitest.config.ts +0 -12
  210. package/src/agentAdapter.test.ts +0 -48
  211. package/src/agentAdapter.ts +0 -56
  212. package/src/auditLog.test.ts +0 -145
  213. package/src/auditLog.ts +0 -147
  214. package/src/capability.test.ts +0 -504
  215. package/src/capability.ts +0 -380
  216. package/src/cborCodec.test.ts +0 -263
  217. package/src/cborCodec.ts +0 -339
  218. package/src/conformance.test.ts +0 -136
  219. package/src/conformanceVectors.ts +0 -99
  220. package/src/encryption.test.ts +0 -206
  221. package/src/encryption.ts +0 -245
  222. package/src/envelopeCbor.ts +0 -140
  223. package/src/frameworkAdapters.test.ts +0 -290
  224. package/src/frameworkAdapters.ts +0 -261
  225. package/src/gateway.test.ts +0 -343
  226. package/src/gateway.ts +0 -246
  227. package/src/grpcBinding.test.ts +0 -211
  228. package/src/grpcBinding.ts +0 -103
  229. package/src/httpBinding.test.ts +0 -376
  230. package/src/httpBinding.ts +0 -189
  231. package/src/index.ts +0 -39
  232. package/src/keyInfra.test.ts +0 -278
  233. package/src/keyInfra.ts +0 -228
  234. package/src/keyRegistry.ts +0 -59
  235. package/src/keyRotation.test.ts +0 -78
  236. package/src/keyRotation.ts +0 -72
  237. package/src/mcpGateway.test.ts +0 -129
  238. package/src/mcpGateway.ts +0 -250
  239. package/src/mcpTransports.test.ts +0 -92
  240. package/src/mcpTransports.ts +0 -169
  241. package/src/mcpWrapper.test.ts +0 -179
  242. package/src/mcpWrapper.ts +0 -206
  243. package/src/otel.ts +0 -136
  244. package/src/policyEnforcer.test.ts +0 -99
  245. package/src/policyEnforcer.ts +0 -169
  246. package/src/policyTelemetryFeedback.test.ts +0 -25
  247. package/src/policyTelemetryFeedback.ts +0 -38
  248. package/src/protocol.bench.ts +0 -37
  249. package/src/protocol.d.ts.map +0 -1
  250. package/src/protocol.js +0 -294
  251. package/src/protocol.test.ts +0 -155
  252. package/src/protocol.ts +0 -414
  253. package/src/protocolAgent.test.ts +0 -105
  254. package/src/protocolAgent.ts +0 -169
  255. package/src/protocolBinary.test.ts +0 -165
  256. package/src/protocolBinary.ts +0 -312
  257. package/src/protocolCapabilities.ts +0 -70
  258. package/src/protocolFuzz.advanced.test.ts +0 -235
  259. package/src/protocolFuzz.test.ts +0 -111
  260. package/src/protocolNegative.test.ts +0 -97
  261. package/src/protocolReplay.test.ts +0 -71
  262. package/src/protocolReplay.ts +0 -194
  263. package/src/protocolTransport.test.ts +0 -556
  264. package/src/protocolTransport.ts +0 -483
  265. package/src/queueBinding.test.ts +0 -130
  266. package/src/queueBinding.ts +0 -102
  267. package/src/rateLimiter.test.ts +0 -96
  268. package/src/rateLimiter.ts +0 -46
  269. package/src/redisIntegration.test.ts +0 -134
  270. package/src/replayStores.test.ts +0 -273
  271. package/src/replayStores.ts +0 -215
  272. package/src/revocation.test.ts +0 -98
  273. package/src/revocation.ts +0 -0
  274. package/src/routePolicy.test.ts +0 -87
  275. package/src/routePolicy.ts +0 -72
  276. package/src/runtimePolicy.test.ts +0 -49
  277. package/src/runtimePolicy.ts +0 -81
  278. package/src/runtimePolicyManager.test.ts +0 -29
  279. package/src/runtimePolicyManager.ts +0 -50
  280. package/src/runtimePolicyPresets.ts +0 -43
  281. package/src/signedResponse.test.ts +0 -111
  282. package/src/signedResponse.ts +0 -83
  283. package/src/stream.test.ts +0 -254
  284. package/src/stream.ts +0 -417
  285. package/src/telemetry.test.ts +0 -251
  286. package/src/telemetry.ts +0 -299
  287. package/src/webhookBinding.test.ts +0 -144
  288. package/src/webhookBinding.ts +0 -115
  289. package/src/wsBinding.test.ts +0 -221
  290. package/src/wsBinding.ts +0 -200
  291. package/tsconfig.json +0 -15
  292. package/tsconfig.lib.json +0 -23
  293. package/vite.lib.config.ts +0 -16
  294. package/vitest.config.ts +0 -11
@@ -1,39 +0,0 @@
1
- {
2
- "name": "@7h3/protocol-threshold",
3
- "version": "0.5.0",
4
- "description": "7h3 Protocol — M-of-N threshold signatures via BLS12-381",
5
- "type": "module",
6
- "main": "./index.js",
7
- "types": "./index.d.ts",
8
- "exports": {
9
- ".": {
10
- "types": "./index.d.ts",
11
- "import": "./index.js"
12
- }
13
- },
14
- "dependencies": {
15
- "@noble/curves": "^1.4.0"
16
- },
17
- "peerDependencies": {
18
- "@7h3/protocol": "^0.5.0"
19
- },
20
- "devDependencies": {
21
- "@types/node": "^26.1.0",
22
- "typescript": "~6.0.3",
23
- "vitest": "^4.1.8"
24
- },
25
- "scripts": {
26
- "test": "vitest run src/index.test.ts",
27
- "build": "tsc"
28
- },
29
- "license": "MIT",
30
- "keywords": [
31
- "7h3",
32
- "protocol",
33
- "threshold",
34
- "bls",
35
- "bls12-381",
36
- "m-of-n",
37
- "multisig"
38
- ]
39
- }
@@ -1,68 +0,0 @@
1
- export interface ProtocolHeader {
2
- version: string;
3
- messageId: string;
4
- timestampMs: number;
5
- ttlMs: number;
6
- sender: string;
7
- recipient?: string;
8
- nonce: string;
9
- }
10
- export interface ProtocolBody {
11
- intent: string;
12
- content: string;
13
- capability?: string;
14
- correlationId?: string;
15
- }
16
- export interface ProtocolEnvelope {
17
- header: ProtocolHeader;
18
- body: ProtocolBody;
19
- }
20
- export interface BlsKeyPair {
21
- publicKey: string;
22
- privateKey: string;
23
- }
24
- export interface ThresholdConfig {
25
- m: number;
26
- n: number;
27
- }
28
- export interface ThresholdSignature {
29
- alg: 'BLS-G2-2';
30
- keyId: string;
31
- value: string;
32
- signerIds: string[];
33
- threshold: ThresholdConfig;
34
- }
35
- export interface ThresholdEnvelope extends ProtocolEnvelope {
36
- thresholdSignature: ThresholdSignature;
37
- }
38
- /**
39
- * Canonical serialization of a protocol envelope for signing.
40
- * Must match the canonical format in @7h3/protocol.
41
- */
42
- export declare function canonicalizeEnvelopeForBls(envelope: ProtocolEnvelope): string;
43
- export declare function generateBlsKeyPair(): BlsKeyPair;
44
- export declare function signEnvelopeBls(envelope: ProtocolEnvelope, privateKeyBase64Url: string, signerId: string): Promise<{
45
- signerId: string;
46
- partialSig: string;
47
- canonicalHash: string;
48
- }>;
49
- export declare function aggregateSignatures(partialSigs: Array<{
50
- signerId: string;
51
- partialSig: string;
52
- }>, publicKeys: Record<string, string>, // signerId → BLS public key (base64url)
53
- envelope: ProtocolEnvelope, config: ThresholdConfig): Promise<ThresholdEnvelope>;
54
- export declare function verifyThresholdEnvelope(envelope: ThresholdEnvelope, participantPublicKeys: Record<string, string>, config: ThresholdConfig): Promise<boolean>;
55
- /**
56
- * Split a BLS private key into N shares using Shamir's Secret Sharing.
57
- * Any M shares can reconstruct the original key.
58
- * Returns N shares as base64url strings.
59
- * Share format: 1 byte index (1-based) || 32 bytes value
60
- */
61
- export declare function splitPrivateKey(privateKeyBase64Url: string, m: number, n: number): string[];
62
- /**
63
- * Reconstruct a BLS private key from M or more shares using Lagrange interpolation.
64
- * @param shares - array of share strings (base64url, at least m of them)
65
- * @param m - minimum number of shares required (used for validation only)
66
- */
67
- export declare function reconstructPrivateKey(shares: string[], m: number): string;
68
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,KAAK,EAAE,MAAM,CAAA;CACd;AAED,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,cAAc,CAAA;IACtB,IAAI,EAAE,YAAY,CAAA;CACnB;AAID,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,WAAW,eAAe;IAC9B,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;CACV;AAED,MAAM,WAAW,kBAAkB;IACjC,GAAG,EAAE,UAAU,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,MAAM,EAAE,CAAA;IACnB,SAAS,EAAE,eAAe,CAAA;CAC3B;AAED,MAAM,WAAW,iBAAkB,SAAQ,gBAAgB;IACzD,kBAAkB,EAAE,kBAAkB,CAAA;CACvC;AAuBD;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,gBAAgB,GAAG,MAAM,CAyB7E;AAID,wBAAgB,kBAAkB,IAAI,UAAU,CAO/C;AAID,wBAAsB,eAAe,CACnC,QAAQ,EAAE,gBAAgB,EAC1B,mBAAmB,EAAE,MAAM,EAC3B,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,MAAM,CAAA;CAAE,CAAC,CAW1E;AAID,wBAAsB,mBAAmB,CACvC,WAAW,EAAE,KAAK,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC,EAC5D,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAG,wCAAwC;AAC7E,QAAQ,EAAE,gBAAgB,EAC1B,MAAM,EAAE,eAAe,GACtB,OAAO,CAAC,iBAAiB,CAAC,CAkC5B;AAID,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,iBAAiB,EAC3B,qBAAqB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC7C,MAAM,EAAE,eAAe,GACtB,OAAO,CAAC,OAAO,CAAC,CA2BlB;AA+DD;;;;;GAKG;AACH,wBAAgB,eAAe,CAC7B,mBAAmB,EAAE,MAAM,EAC3B,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,GACR,MAAM,EAAE,CA6BV;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAqCzE"}
@@ -1,254 +0,0 @@
1
- import { bls12_381 } from '@noble/curves/bls12-381';
2
- // ─── Helpers ─────────────────────────────────────────────────────────────────
3
- function toBase64Url(bytes) {
4
- const base64 = Buffer.from(bytes).toString('base64');
5
- return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
6
- }
7
- function fromBase64Url(value) {
8
- const padded = value
9
- .replace(/-/g, '+')
10
- .replace(/_/g, '/')
11
- .padEnd(Math.ceil(value.length / 4) * 4, '=');
12
- return new Uint8Array(Buffer.from(padded, 'base64'));
13
- }
14
- async function sha256(data) {
15
- const encoder = new TextEncoder();
16
- const buf = await crypto.subtle.digest('SHA-256', encoder.encode(data));
17
- return new Uint8Array(buf);
18
- }
19
- /**
20
- * Canonical serialization of a protocol envelope for signing.
21
- * Must match the canonical format in @7h3/protocol.
22
- */
23
- export function canonicalizeEnvelopeForBls(envelope) {
24
- const h = envelope.header;
25
- const b = envelope.body;
26
- const headerParts = [
27
- `"messageId":${JSON.stringify(h.messageId)}`,
28
- `"nonce":${JSON.stringify(h.nonce)}`,
29
- ];
30
- if (h.recipient !== undefined) {
31
- headerParts.push(`"recipient":${JSON.stringify(h.recipient)}`);
32
- }
33
- headerParts.push(`"sender":${JSON.stringify(h.sender)}`);
34
- headerParts.push(`"timestampMs":${h.timestampMs}`);
35
- headerParts.push(`"ttlMs":${h.ttlMs}`);
36
- headerParts.push(`"version":${JSON.stringify(h.version)}`);
37
- const headerStr = `{${headerParts.join(',')}}`;
38
- const bodyParts = [];
39
- if (b.capability !== undefined)
40
- bodyParts.push(`"capability":${JSON.stringify(b.capability)}`);
41
- bodyParts.push(`"content":${JSON.stringify(b.content)}`);
42
- if (b.correlationId !== undefined)
43
- bodyParts.push(`"correlationId":${JSON.stringify(b.correlationId)}`);
44
- bodyParts.push(`"intent":${JSON.stringify(b.intent)}`);
45
- const bodyStr = `{${bodyParts.join(',')}}`;
46
- return `{"body":${bodyStr},"header":${headerStr}}`;
47
- }
48
- // ─── Key Generation ──────────────────────────────────────────────────────────
49
- export function generateBlsKeyPair() {
50
- const privateKeyBytes = bls12_381.utils.randomPrivateKey();
51
- const publicKeyBytes = bls12_381.getPublicKey(privateKeyBytes);
52
- return {
53
- publicKey: toBase64Url(publicKeyBytes),
54
- privateKey: toBase64Url(privateKeyBytes),
55
- };
56
- }
57
- // ─── Partial Signing ─────────────────────────────────────────────────────────
58
- export async function signEnvelopeBls(envelope, privateKeyBase64Url, signerId) {
59
- const canonical = canonicalizeEnvelopeForBls(envelope);
60
- const msgHash = await sha256(canonical);
61
- const privateKeyBytes = fromBase64Url(privateKeyBase64Url);
62
- // BLS sign: signature is a G2 point (96 bytes)
63
- const sigBytes = bls12_381.sign(msgHash, privateKeyBytes);
64
- return {
65
- signerId,
66
- partialSig: toBase64Url(sigBytes),
67
- canonicalHash: toBase64Url(msgHash),
68
- };
69
- }
70
- // ─── Aggregation ─────────────────────────────────────────────────────────────
71
- export async function aggregateSignatures(partialSigs, publicKeys, // signerId → BLS public key (base64url)
72
- envelope, config) {
73
- if (partialSigs.length < config.m) {
74
- throw new Error(`Threshold not met: need ${config.m} signatures, got ${partialSigs.length}`);
75
- }
76
- // Use exactly m signatures (first m)
77
- const selected = partialSigs.slice(0, config.m);
78
- const signerIds = selected.map((s) => s.signerId);
79
- const sigBytesArr = selected.map((s) => fromBase64Url(s.partialSig));
80
- const pubKeyBytesArr = signerIds.map((id) => {
81
- const pk = publicKeys[id];
82
- if (!pk)
83
- throw new Error(`Missing public key for signer: ${id}`);
84
- return fromBase64Url(pk);
85
- });
86
- const aggregatedSig = bls12_381.aggregateSignatures(sigBytesArr);
87
- const aggregatedPubKey = bls12_381.aggregatePublicKeys(pubKeyBytesArr);
88
- // Fingerprint: first 16 bytes of aggregated pubkey as base64url
89
- const keyId = toBase64Url(aggregatedPubKey.slice(0, 16));
90
- return {
91
- ...envelope,
92
- thresholdSignature: {
93
- alg: 'BLS-G2-2',
94
- keyId,
95
- value: toBase64Url(aggregatedSig),
96
- signerIds,
97
- threshold: config,
98
- },
99
- };
100
- }
101
- // ─── Verification ────────────────────────────────────────────────────────────
102
- export async function verifyThresholdEnvelope(envelope, participantPublicKeys, config) {
103
- try {
104
- const { thresholdSignature } = envelope;
105
- if (!thresholdSignature)
106
- return false;
107
- if (thresholdSignature.alg !== 'BLS-G2-2')
108
- return false;
109
- if (thresholdSignature.signerIds.length < config.m)
110
- return false;
111
- const canonical = canonicalizeEnvelopeForBls({
112
- header: envelope.header,
113
- body: envelope.body,
114
- });
115
- const msgHash = await sha256(canonical);
116
- const signerIds = thresholdSignature.signerIds;
117
- const pubKeyBytesArr = signerIds.map((id) => {
118
- const pk = participantPublicKeys[id];
119
- if (!pk)
120
- throw new Error(`Missing public key for signer: ${id}`);
121
- return fromBase64Url(pk);
122
- });
123
- const aggregatedPubKey = bls12_381.aggregatePublicKeys(pubKeyBytesArr);
124
- const sigBytes = fromBase64Url(thresholdSignature.value);
125
- return bls12_381.verify(sigBytes, msgHash, aggregatedPubKey);
126
- }
127
- catch {
128
- return false;
129
- }
130
- }
131
- // ─── Shamir Secret Sharing ───────────────────────────────────────────────────
132
- // Operates over the BLS12-381 scalar field (Fr), prime order r.
133
- // BLS12-381 scalar field order r
134
- const FIELD_ORDER = BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001');
135
- function fieldMod(n) {
136
- return ((n % FIELD_ORDER) + FIELD_ORDER) % FIELD_ORDER;
137
- }
138
- function fieldAdd(a, b) {
139
- return fieldMod(a + b);
140
- }
141
- function fieldMul(a, b) {
142
- return fieldMod(a * b);
143
- }
144
- // Modular inverse via Fermat's little theorem (field is prime order)
145
- function fieldInv(a) {
146
- if (a === 0n)
147
- throw new Error('Cannot invert zero');
148
- return fieldPow(a, FIELD_ORDER - 2n);
149
- }
150
- function fieldPow(base, exp) {
151
- let result = 1n;
152
- base = fieldMod(base);
153
- while (exp > 0n) {
154
- if (exp & 1n)
155
- result = fieldMul(result, base);
156
- base = fieldMul(base, base);
157
- exp >>= 1n;
158
- }
159
- return result;
160
- }
161
- function bigintToBytes32(n) {
162
- const hex = n.toString(16).padStart(64, '0');
163
- const bytes = new Uint8Array(32);
164
- for (let i = 0; i < 32; i++) {
165
- bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
166
- }
167
- return bytes;
168
- }
169
- function bytes32ToBigint(bytes) {
170
- let hex = '';
171
- for (const b of bytes) {
172
- hex += b.toString(16).padStart(2, '0');
173
- }
174
- return BigInt('0x' + hex);
175
- }
176
- // Generate a cryptographically random field element
177
- function randomFieldElement() {
178
- const bytes = new Uint8Array(32);
179
- crypto.getRandomValues(bytes);
180
- return fieldMod(bytes32ToBigint(bytes));
181
- }
182
- /**
183
- * Split a BLS private key into N shares using Shamir's Secret Sharing.
184
- * Any M shares can reconstruct the original key.
185
- * Returns N shares as base64url strings.
186
- * Share format: 1 byte index (1-based) || 32 bytes value
187
- */
188
- export function splitPrivateKey(privateKeyBase64Url, m, n) {
189
- if (m < 2 || m > n)
190
- throw new Error(`Invalid threshold: m=${m}, n=${n}`);
191
- const secretBytes = fromBase64Url(privateKeyBase64Url);
192
- const secret = fieldMod(bytes32ToBigint(secretBytes));
193
- // Build polynomial: f(x) = secret + a1*x + a2*x^2 + ... + a_{m-1}*x^{m-1}
194
- const coefficients = [secret];
195
- for (let i = 1; i < m; i++) {
196
- coefficients.push(randomFieldElement());
197
- }
198
- // Evaluate at x = 1..n
199
- const shares = [];
200
- for (let x = 1; x <= n; x++) {
201
- let y = 0n;
202
- let xPow = 1n;
203
- for (const coeff of coefficients) {
204
- y = fieldAdd(y, fieldMul(coeff, xPow));
205
- xPow = fieldMul(xPow, BigInt(x));
206
- }
207
- // Encode share: index byte + 32-byte value
208
- const shareBytes = new Uint8Array(33);
209
- shareBytes[0] = x;
210
- shareBytes.set(bigintToBytes32(y), 1);
211
- shares.push(toBase64Url(shareBytes));
212
- }
213
- return shares;
214
- }
215
- /**
216
- * Reconstruct a BLS private key from M or more shares using Lagrange interpolation.
217
- * @param shares - array of share strings (base64url, at least m of them)
218
- * @param m - minimum number of shares required (used for validation only)
219
- */
220
- export function reconstructPrivateKey(shares, m) {
221
- if (shares.length < m) {
222
- throw new Error(`Need at least ${m} shares, got ${shares.length}`);
223
- }
224
- // Decode shares — take exactly m
225
- const decoded = shares.slice(0, m).map((s) => {
226
- const bytes = fromBase64Url(s);
227
- if (bytes.length !== 33)
228
- throw new Error('Invalid share format');
229
- const x = BigInt(bytes[0]);
230
- const y = bytes32ToBigint(bytes.slice(1));
231
- return { x, y };
232
- });
233
- // Lagrange interpolation at x=0 to recover secret
234
- let secret = 0n;
235
- for (let i = 0; i < decoded.length; i++) {
236
- const xi = decoded[i].x;
237
- const yi = decoded[i].y;
238
- // Compute Lagrange basis polynomial l_i(0)
239
- let num = 1n;
240
- let den = 1n;
241
- for (let j = 0; j < decoded.length; j++) {
242
- if (i === j)
243
- continue;
244
- const xj = decoded[j].x;
245
- // num *= (0 - xj) = -xj
246
- num = fieldMul(num, fieldMod(-xj));
247
- // den *= (xi - xj)
248
- den = fieldMul(den, fieldMod(xi - xj));
249
- }
250
- const lagrangeBasis = fieldMul(num, fieldInv(den));
251
- secret = fieldAdd(secret, fieldMul(yi, lagrangeBasis));
252
- }
253
- return toBase64Url(bigintToBytes32(secret));
254
- }
@@ -1,238 +0,0 @@
1
- import { describe, it, expect } from 'vitest'
2
- import {
3
- generateBlsKeyPair,
4
- signEnvelopeBls,
5
- aggregateSignatures,
6
- verifyThresholdEnvelope,
7
- splitPrivateKey,
8
- reconstructPrivateKey,
9
- type ThresholdConfig,
10
- type ThresholdEnvelope,
11
- type ProtocolEnvelope,
12
- } from './index.js'
13
-
14
- // ─── Helpers ─────────────────────────────────────────────────────────────────
15
-
16
- function makeEnvelope(): Omit<ProtocolEnvelope, never> {
17
- return {
18
- header: {
19
- version: '7h3/0.1',
20
- messageId: 'msg-test-001',
21
- timestampMs: Date.now(),
22
- ttlMs: 60_000,
23
- sender: 'agent-alpha',
24
- recipient: 'agent-beta',
25
- nonce: 'nonce-abc',
26
- },
27
- body: {
28
- intent: 'TASK',
29
- content: 'approve-transaction-001',
30
- },
31
- }
32
- }
33
-
34
- function fromBase64Url(value: string): Uint8Array {
35
- const padded = value
36
- .replace(/-/g, '+')
37
- .replace(/_/g, '/')
38
- .padEnd(Math.ceil(value.length / 4) * 4, '=')
39
- return new Uint8Array(Buffer.from(padded, 'base64'))
40
- }
41
-
42
- // ─── Tests ───────────────────────────────────────────────────────────────────
43
-
44
- describe('generateBlsKeyPair', () => {
45
- it('returns a 48-byte G1 public key and 32-byte private key (base64url)', () => {
46
- const kp = generateBlsKeyPair()
47
- expect(kp.publicKey).toBeTypeOf('string')
48
- expect(kp.privateKey).toBeTypeOf('string')
49
-
50
- const pubBytes = fromBase64Url(kp.publicKey)
51
- const privBytes = fromBase64Url(kp.privateKey)
52
- expect(pubBytes.length).toBe(48)
53
- expect(privBytes.length).toBe(32)
54
- })
55
- })
56
-
57
- describe('3-of-5 threshold round trip', () => {
58
- it('aggregates 3 signatures and verifies successfully', async () => {
59
- const config: ThresholdConfig = { m: 3, n: 5 }
60
- const envelope = makeEnvelope()
61
-
62
- // Generate 5 keypairs
63
- const keypairs: Array<{ id: string; publicKey: string; privateKey: string }> = []
64
- for (let i = 0; i < 5; i++) {
65
- const kp = generateBlsKeyPair()
66
- keypairs.push({ id: `agent-${i}`, ...kp })
67
- }
68
-
69
- const publicKeys: Record<string, string> = {}
70
- for (const kp of keypairs) publicKeys[kp.id] = kp.publicKey
71
-
72
- // Sign with first 3 agents
73
- const partialSigs = await Promise.all(
74
- keypairs.slice(0, 3).map((kp) =>
75
- signEnvelopeBls(envelope, kp.privateKey, kp.id),
76
- ),
77
- )
78
-
79
- const thresholdEnvelope = await aggregateSignatures(
80
- partialSigs,
81
- publicKeys,
82
- envelope,
83
- config,
84
- )
85
-
86
- expect(thresholdEnvelope.thresholdSignature.alg).toBe('BLS-G2-2')
87
- expect(thresholdEnvelope.thresholdSignature.signerIds).toHaveLength(3)
88
-
89
- const valid = await verifyThresholdEnvelope(thresholdEnvelope, publicKeys, config)
90
- expect(valid).toBe(true)
91
- })
92
-
93
- it('throws when only 2 partial sigs are provided for 3-of-5', async () => {
94
- const config: ThresholdConfig = { m: 3, n: 5 }
95
- const envelope = makeEnvelope()
96
-
97
- const keypairs = [generateBlsKeyPair(), generateBlsKeyPair()]
98
- const ids = ['a0', 'a1']
99
- const publicKeys: Record<string, string> = {}
100
- ids.forEach((id, i) => { publicKeys[id] = keypairs[i].publicKey })
101
-
102
- const partialSigs = await Promise.all(
103
- keypairs.map((kp, i) => signEnvelopeBls(envelope, kp.privateKey, ids[i])),
104
- )
105
-
106
- await expect(
107
- aggregateSignatures(partialSigs, publicKeys, envelope, config),
108
- ).rejects.toThrow('Threshold not met')
109
- })
110
- })
111
-
112
- describe('verifyThresholdEnvelope', () => {
113
- async function build3of5() {
114
- const config: ThresholdConfig = { m: 3, n: 5 }
115
- const envelope = makeEnvelope()
116
- const keypairs = Array.from({ length: 5 }, (_, i) => ({
117
- id: `p${i}`,
118
- ...generateBlsKeyPair(),
119
- }))
120
- const publicKeys: Record<string, string> = {}
121
- for (const kp of keypairs) publicKeys[kp.id] = kp.publicKey
122
-
123
- const partialSigs = await Promise.all(
124
- keypairs.slice(0, 3).map((kp) =>
125
- signEnvelopeBls(envelope, kp.privateKey, kp.id),
126
- ),
127
- )
128
-
129
- const thresholdEnvelope = await aggregateSignatures(
130
- partialSigs,
131
- publicKeys,
132
- envelope,
133
- config,
134
- )
135
-
136
- return { thresholdEnvelope, publicKeys, config }
137
- }
138
-
139
- it('valid 3-of-5 envelope returns true', async () => {
140
- const { thresholdEnvelope, publicKeys, config } = await build3of5()
141
- const result = await verifyThresholdEnvelope(thresholdEnvelope, publicKeys, config)
142
- expect(result).toBe(true)
143
- })
144
-
145
- it('tampered envelope body returns false', async () => {
146
- const { thresholdEnvelope, publicKeys, config } = await build3of5()
147
- const tampered: ThresholdEnvelope = {
148
- ...thresholdEnvelope,
149
- body: { ...thresholdEnvelope.body, content: 'tampered-content-99999' },
150
- }
151
- const result = await verifyThresholdEnvelope(tampered, publicKeys, config)
152
- expect(result).toBe(false)
153
- })
154
-
155
- it('wrong public keys returns false', async () => {
156
- const { thresholdEnvelope, config } = await build3of5()
157
- // Replace all public keys with fresh unrelated ones
158
- const wrongKeys: Record<string, string> = {}
159
- for (const id of thresholdEnvelope.thresholdSignature.signerIds) {
160
- wrongKeys[id] = generateBlsKeyPair().publicKey
161
- }
162
- const result = await verifyThresholdEnvelope(thresholdEnvelope, wrongKeys, config)
163
- expect(result).toBe(false)
164
- })
165
- })
166
-
167
- describe('Shamir Secret Sharing', () => {
168
- it('splitPrivateKey(key, 3, 5) returns 5 shares', () => {
169
- const kp = generateBlsKeyPair()
170
- const shares = splitPrivateKey(kp.privateKey, 3, 5)
171
- expect(shares).toHaveLength(5)
172
- for (const s of shares) {
173
- expect(s).toBeTypeOf('string')
174
- expect(fromBase64Url(s).length).toBe(33) // 1 index byte + 32 value bytes
175
- }
176
- })
177
-
178
- it('reconstructPrivateKey with any 3 shares recovers original key', () => {
179
- const kp = generateBlsKeyPair()
180
- const shares = splitPrivateKey(kp.privateKey, 3, 5)
181
-
182
- // Try shares [0,1,2]
183
- const rec1 = reconstructPrivateKey([shares[0], shares[1], shares[2]], 3)
184
- expect(rec1).toBe(kp.privateKey)
185
-
186
- // Try shares [1,3,4]
187
- const rec2 = reconstructPrivateKey([shares[1], shares[3], shares[4]], 3)
188
- expect(rec2).toBe(kp.privateKey)
189
- })
190
-
191
- it('different 3-share combinations produce the same key', () => {
192
- const kp = generateBlsKeyPair()
193
- const shares = splitPrivateKey(kp.privateKey, 3, 5)
194
-
195
- const rec1 = reconstructPrivateKey([shares[0], shares[2], shares[4]], 3)
196
- const rec2 = reconstructPrivateKey([shares[1], shares[2], shares[3]], 3)
197
- expect(rec1).toBe(rec2)
198
- })
199
-
200
- it('only 2 shares cannot reconstruct the correct key', () => {
201
- const kp = generateBlsKeyPair()
202
- const shares = splitPrivateKey(kp.privateKey, 3, 5)
203
-
204
- // With only 2 shares we underdetermine the polynomial — result will be wrong
205
- const wrong = reconstructPrivateKey([shares[0], shares[1]], 2)
206
- expect(wrong).not.toBe(kp.privateKey)
207
- })
208
- })
209
-
210
- describe('2-of-2 simple multisig round trip', () => {
211
- it('both agents sign, aggregate, verify — passes', async () => {
212
- const config: ThresholdConfig = { m: 2, n: 2 }
213
- const envelope = makeEnvelope()
214
-
215
- const kp1 = { id: 'alice', ...generateBlsKeyPair() }
216
- const kp2 = { id: 'bob', ...generateBlsKeyPair() }
217
- const publicKeys: Record<string, string> = {
218
- alice: kp1.publicKey,
219
- bob: kp2.publicKey,
220
- }
221
-
222
- const partialSigs = await Promise.all([
223
- signEnvelopeBls(envelope, kp1.privateKey, kp1.id),
224
- signEnvelopeBls(envelope, kp2.privateKey, kp2.id),
225
- ])
226
-
227
- const thresholdEnvelope = await aggregateSignatures(
228
- partialSigs,
229
- publicKeys,
230
- envelope,
231
- config,
232
- )
233
-
234
- expect(thresholdEnvelope.thresholdSignature.threshold).toEqual(config)
235
- const valid = await verifyThresholdEnvelope(thresholdEnvelope, publicKeys, config)
236
- expect(valid).toBe(true)
237
- })
238
- })