@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.
- package/README.md +6 -1317
- package/agentAdapter.d.ts +26 -0
- package/auditLog.d.ts +54 -0
- package/capability.d.ts +67 -0
- package/cborCodec.d.ts +34 -0
- package/conformanceVectors.d.ts +20 -0
- package/encryption.d.ts +85 -0
- package/envelopeCbor.d.ts +33 -0
- package/frameworkAdapters.d.ts +72 -0
- package/gateway.d.ts +53 -0
- package/grpcBinding.d.ts +29 -0
- package/httpBinding.d.ts +50 -0
- package/index.d.ts +39 -0
- package/index.js +3662 -0
- package/keyInfra.d.ts +64 -0
- package/keyRegistry.d.ts +9 -0
- package/keyRotation.d.ts +20 -0
- package/mcpGateway.d.ts +37 -0
- package/mcpTransports.d.ts +62 -0
- package/mcpWrapper.d.ts +83 -0
- package/otel.d.ts +65 -0
- package/package.json +13 -121
- package/policyEnforcer.d.ts +50 -0
- package/policyTelemetryFeedback.d.ts +11 -0
- package/{src/protocol.d.ts → protocol.d.ts} +1 -2
- package/protocolAgent.d.ts +58 -0
- package/protocolBinary.d.ts +8 -0
- package/protocolCapabilities.d.ts +24 -0
- package/protocolReplay.d.ts +35 -0
- package/protocolTransport.d.ts +73 -0
- package/queueBinding.d.ts +43 -0
- package/rateLimiter.d.ts +18 -0
- package/{src/redisClient.ts → redisClient.d.ts} +25 -75
- package/replayStores.d.ts +98 -0
- package/revocation.d.ts +71 -0
- package/routePolicy.d.ts +30 -0
- package/runtimePolicy.d.ts +24 -0
- package/runtimePolicyManager.d.ts +15 -0
- package/runtimePolicyPresets.d.ts +11 -0
- package/signedResponse.d.ts +21 -0
- package/stream.d.ts +59 -0
- package/telemetry.d.ts +79 -0
- package/webhookBinding.d.ts +30 -0
- package/wsBinding.d.ts +52 -0
- package/.dockerignore +0 -19
- package/.github/dependabot.yml +0 -32
- package/.github/workflows/ci.yml +0 -31
- package/.github/workflows/publish.yml +0 -59
- package/.github/workflows/scorecard.yml +0 -37
- package/7h3.example.yaml +0 -125
- package/CHANGELOG.md +0 -152
- package/CONTRIBUTING.md +0 -82
- package/Dockerfile +0 -73
- package/GOVERNANCE.md +0 -62
- package/SECURITY.md +0 -70
- package/bench-results/replay-cache-full-1777891033256.json +0 -10
- package/bench-results/replay-cache-full-1777896317488.json +0 -10
- package/bench-results/replay-cache-full-1777900993184.json +0 -10
- package/bench-results/replay-cache-full-1777901019285.json +0 -10
- package/bench-results/replay-cache-quick-1777870170126.json +0 -10
- package/bench-results/signature-profiles-quick-1775875160079.json +0 -85
- package/bench-results/signature-profiles-quick-1775983539716.json +0 -85
- package/bench-results/signature-profiles-quick-1776237913190.json +0 -85
- package/bench-results/wire-codecs-full-1777891019803.json +0 -93
- package/bench-results/wire-codecs-full-1777896260964.json +0 -93
- package/bench-results/wire-codecs-full-1777901004247.json +0 -93
- package/bench-results/wire-codecs-quick-1775972879056.json +0 -93
- package/bench-results/wire-codecs-quick-1775983541111.json +0 -93
- package/bench-results/wire-codecs-quick-1776237914299.json +0 -93
- package/bench-results/wire-codecs-quick-1777841285236.json +0 -93
- package/bench-results/wire-codecs-quick-1777841321772.json +0 -93
- package/bench-results/wire-codecs-quick-1777841330408.json +0 -93
- package/bench-results/wire-codecs-quick-1777852886082.json +0 -93
- package/bench-results/wire-codecs-quick-1777852988773.json +0 -93
- package/bench-results/wire-codecs-quick-1777870188095.json +0 -93
- package/bench-results/wire-codecs-quick-1777870263918.json +0 -93
- package/bench-results/wire-codecs-quick-1777870455034.json +0 -93
- package/bench-results/wire-codecs-quick-1778816163081.json +0 -93
- package/bench-results/wire-codecs-quick-1778843936130.json +0 -93
- package/bin/7h3.ts +0 -406
- package/conformance/7h3_v0_1.json +0 -77
- package/conformance/7h3_v0_1_binary.json +0 -20
- package/conformance/aip_v0_1_binary.json +0 -20
- package/docker-compose.yaml +0 -77
- package/docs/ADOPTION_PLAN.md +0 -120
- package/docs/AGENTS.md +0 -77
- package/docs/AIP_RFC_v0.1.md +0 -97
- package/docs/AI_DECISION_CARD.md +0 -122
- package/docs/AI_RUNTIME_POLICY.json +0 -126
- package/docs/AI_RUNTIME_POLICY.yaml +0 -110
- package/docs/BACKPRESSURE_TUNING.md +0 -65
- package/docs/BENCHMARK_CLAIM_MATRIX.md +0 -42
- package/docs/BENCHMARK_REPORT_TEMPLATE.md +0 -169
- package/docs/BINARY_CODEC_BENCH.md +0 -23
- package/docs/CLEAN_CLONE_RUNBOOK.md +0 -36
- package/docs/CLOCK_SKEW_POLICY.md +0 -30
- package/docs/DISTRIBUTED_REPLAY.md +0 -142
- package/docs/FUZZ_CAMPAIGN.md +0 -121
- package/docs/GATEWAY.md +0 -195
- package/docs/KEY_MANAGEMENT_POLICY.md +0 -53
- package/docs/KEY_REVOCATION.md +0 -69
- package/docs/MCP_WRAPPER.md +0 -159
- package/docs/MIGRATION_GUIDE.md +0 -40
- package/docs/OPERATORS.md +0 -184
- package/docs/PERF_REGRESSION_POLICY.md +0 -34
- package/docs/PROJECT_EXAMINATION_2026-05-31.md +0 -219
- package/docs/RELEASE_BENCHMARK_REPORT_2026-05-15.md +0 -135
- package/docs/RELEASE_GATE.md +0 -25
- package/docs/RELEASE_NOTES_v0.1.0.md +0 -54
- package/docs/SECURITY_REVIEW_2026-06-05.md +0 -165
- package/docs/TELEMETRY.md +0 -41
- package/docs/THREAT_MODEL.md +0 -89
- package/docs/VERSIONING_POLICY.md +0 -30
- package/docs/assets/banner-github.png +0 -0
- package/docs/assets/banner.png +0 -0
- package/docs/assets/banner.svg +0 -123
- package/eslint.config.js +0 -15
- package/fuzz/ts/harness-decode.ts +0 -136
- package/fuzz/ts/harness-verify.ts +0 -121
- package/fuzz/ts/run.ts +0 -35
- package/mcp-server/README.md +0 -38
- package/mcp-server/package-lock.json +0 -1187
- package/mcp-server/package.json +0 -35
- package/mcp-server/src/index.ts +0 -236
- package/mcp-server/tsconfig.json +0 -14
- package/scripts/aip-framework-quickstart.ts +0 -110
- package/scripts/aip-mcp-gateway.ts +0 -38
- package/scripts/aip-mcp-wrap-demo.ts +0 -72
- package/scripts/aip-quickstart.ts +0 -60
- package/scripts/bench-diff.ts +0 -118
- package/scripts/bench-protocol-e2e.ts +0 -937
- package/scripts/bench-protocol-openloop.ts +0 -1397
- package/scripts/bench-replay-cache.ts +0 -76
- package/scripts/bench-signature-profiles.ts +0 -180
- package/scripts/bench-wire-codecs.ts +0 -161
- package/scripts/build-binary-conformance.ts +0 -36
- package/scripts/build-release-dashboard.ts +0 -175
- package/scripts/canary-rollout.ts +0 -38
- package/scripts/mcpGatewayCli.test.ts +0 -116
- package/scripts/prepare-aip-package.ts +0 -88
- package/scripts/regen-conformance-sigs.ts +0 -18
- package/scripts/release-gate.ts +0 -19
- package/scripts/validate-runtime-policy.ts +0 -18
- package/sdk/browser/index.test.ts +0 -162
- package/sdk/browser/index.ts +0 -257
- package/sdk/browser/package.json +0 -13
- package/sdk/go/cbor.go +0 -551
- package/sdk/go/cbor_test.go +0 -232
- package/sdk/go/encryption.go +0 -280
- package/sdk/go/encryption_test.go +0 -318
- package/sdk/go/go.mod +0 -7
- package/sdk/go/go.sum +0 -4
- package/sdk/go/http.go +0 -135
- package/sdk/go/protocol.go +0 -324
- package/sdk/go/protocol_test.go +0 -334
- package/sdk/go/replay.go +0 -121
- package/sdk/go/replay_test.go +0 -149
- package/sdk/go/webhook.go +0 -136
- package/sdk/pq/package-lock.json +0 -1358
- package/sdk/pq/package.json +0 -42
- package/sdk/pq/src/index.test.ts +0 -143
- package/sdk/pq/src/index.ts +0 -166
- package/sdk/pq/tsconfig.json +0 -14
- package/sdk/pq/vitest.config.ts +0 -7
- package/sdk/python/README.md +0 -18
- package/sdk/python/protocol_7h3/__init__.py +0 -46
- package/sdk/python/protocol_7h3/encryption.py +0 -252
- package/sdk/python/protocol_7h3/http.py +0 -212
- package/sdk/python/protocol_7h3/keys.py +0 -149
- package/sdk/python/protocol_7h3/pq.py +0 -244
- package/sdk/python/protocol_7h3/protocol.py +0 -525
- package/sdk/python/protocol_7h3/queue.py +0 -118
- package/sdk/python/protocol_7h3/replay.py +0 -98
- package/sdk/python/protocol_7h3/webhook.py +0 -116
- package/sdk/python/pyproject.toml +0 -40
- package/sdk/python/tests/test_conformance.py +0 -110
- package/sdk/python/tests/test_encryption.py +0 -206
- package/sdk/python/tests/test_http.py +0 -305
- package/sdk/python/tests/test_keys.py +0 -417
- package/sdk/python/tests/test_queue.py +0 -120
- package/sdk/python/tests/test_webhook.py +0 -345
- package/sdk/rust/Cargo.lock +0 -371
- package/sdk/rust/Cargo.toml +0 -25
- package/sdk/rust/README.md +0 -31
- package/sdk/rust/fuzz/Cargo.toml +0 -29
- package/sdk/rust/fuzz/fuzz_targets/fuzz_canonicalize.rs +0 -46
- package/sdk/rust/fuzz/fuzz_targets/fuzz_decode.rs +0 -11
- package/sdk/rust/src/bin/aip_mcp_gateway.rs +0 -59
- package/sdk/rust/src/http.rs +0 -145
- package/sdk/rust/src/keys.rs +0 -161
- package/sdk/rust/src/lib.rs +0 -688
- package/sdk/rust/src/queue.rs +0 -79
- package/sdk/rust/src/webhook.rs +0 -86
- package/sdk/rust/tests/conformance.rs +0 -148
- package/sdk/rust/tests/gateway.rs +0 -130
- package/sdk/rust/tests/http_webhook_queue.rs +0 -201
- package/sdk/rust/tests/keys.rs +0 -189
- package/sdk/threshold/index.d.ts +0 -68
- package/sdk/threshold/index.d.ts.map +0 -1
- package/sdk/threshold/index.js +0 -254
- package/sdk/threshold/package-lock.json +0 -1361
- package/sdk/threshold/package.json +0 -39
- package/sdk/threshold/src/index.d.ts +0 -68
- package/sdk/threshold/src/index.d.ts.map +0 -1
- package/sdk/threshold/src/index.js +0 -254
- package/sdk/threshold/src/index.test.ts +0 -238
- package/sdk/threshold/src/index.ts +0 -355
- package/sdk/threshold/tsconfig.json +0 -19
- package/sdk/threshold/vitest.config.ts +0 -12
- package/src/agentAdapter.test.ts +0 -48
- package/src/agentAdapter.ts +0 -56
- package/src/auditLog.test.ts +0 -145
- package/src/auditLog.ts +0 -147
- package/src/capability.test.ts +0 -504
- package/src/capability.ts +0 -380
- package/src/cborCodec.test.ts +0 -263
- package/src/cborCodec.ts +0 -339
- package/src/conformance.test.ts +0 -136
- package/src/conformanceVectors.ts +0 -99
- package/src/encryption.test.ts +0 -206
- package/src/encryption.ts +0 -245
- package/src/envelopeCbor.ts +0 -140
- package/src/frameworkAdapters.test.ts +0 -290
- package/src/frameworkAdapters.ts +0 -261
- package/src/gateway.test.ts +0 -343
- package/src/gateway.ts +0 -246
- package/src/grpcBinding.test.ts +0 -211
- package/src/grpcBinding.ts +0 -103
- package/src/httpBinding.test.ts +0 -376
- package/src/httpBinding.ts +0 -189
- package/src/index.ts +0 -39
- package/src/keyInfra.test.ts +0 -278
- package/src/keyInfra.ts +0 -228
- package/src/keyRegistry.ts +0 -59
- package/src/keyRotation.test.ts +0 -78
- package/src/keyRotation.ts +0 -72
- package/src/mcpGateway.test.ts +0 -129
- package/src/mcpGateway.ts +0 -250
- package/src/mcpTransports.test.ts +0 -92
- package/src/mcpTransports.ts +0 -169
- package/src/mcpWrapper.test.ts +0 -179
- package/src/mcpWrapper.ts +0 -206
- package/src/otel.ts +0 -136
- package/src/policyEnforcer.test.ts +0 -99
- package/src/policyEnforcer.ts +0 -169
- package/src/policyTelemetryFeedback.test.ts +0 -25
- package/src/policyTelemetryFeedback.ts +0 -38
- package/src/protocol.bench.ts +0 -37
- package/src/protocol.d.ts.map +0 -1
- package/src/protocol.js +0 -294
- package/src/protocol.test.ts +0 -155
- package/src/protocol.ts +0 -414
- package/src/protocolAgent.test.ts +0 -105
- package/src/protocolAgent.ts +0 -169
- package/src/protocolBinary.test.ts +0 -165
- package/src/protocolBinary.ts +0 -312
- package/src/protocolCapabilities.ts +0 -70
- package/src/protocolFuzz.advanced.test.ts +0 -235
- package/src/protocolFuzz.test.ts +0 -111
- package/src/protocolNegative.test.ts +0 -97
- package/src/protocolReplay.test.ts +0 -71
- package/src/protocolReplay.ts +0 -194
- package/src/protocolTransport.test.ts +0 -556
- package/src/protocolTransport.ts +0 -483
- package/src/queueBinding.test.ts +0 -130
- package/src/queueBinding.ts +0 -102
- package/src/rateLimiter.test.ts +0 -96
- package/src/rateLimiter.ts +0 -46
- package/src/redisIntegration.test.ts +0 -134
- package/src/replayStores.test.ts +0 -273
- package/src/replayStores.ts +0 -215
- package/src/revocation.test.ts +0 -98
- package/src/revocation.ts +0 -0
- package/src/routePolicy.test.ts +0 -87
- package/src/routePolicy.ts +0 -72
- package/src/runtimePolicy.test.ts +0 -49
- package/src/runtimePolicy.ts +0 -81
- package/src/runtimePolicyManager.test.ts +0 -29
- package/src/runtimePolicyManager.ts +0 -50
- package/src/runtimePolicyPresets.ts +0 -43
- package/src/signedResponse.test.ts +0 -111
- package/src/signedResponse.ts +0 -83
- package/src/stream.test.ts +0 -254
- package/src/stream.ts +0 -417
- package/src/telemetry.test.ts +0 -251
- package/src/telemetry.ts +0 -299
- package/src/webhookBinding.test.ts +0 -144
- package/src/webhookBinding.ts +0 -115
- package/src/wsBinding.test.ts +0 -221
- package/src/wsBinding.ts +0 -200
- package/tsconfig.json +0 -15
- package/tsconfig.lib.json +0 -23
- package/vite.lib.config.ts +0 -16
- package/vitest.config.ts +0 -11
package/sdk/rust/tests/keys.rs
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
use protocol_7h3::keys::{
|
|
2
|
-
KeyEntry, KeyRotationManager, ManagedKeyPair, RevocationRegistry, WellKnownKeysDocument,
|
|
3
|
-
};
|
|
4
|
-
use std::time::{SystemTime, UNIX_EPOCH};
|
|
5
|
-
|
|
6
|
-
fn now_ms() -> u64 {
|
|
7
|
-
SystemTime::now()
|
|
8
|
-
.duration_since(UNIX_EPOCH)
|
|
9
|
-
.unwrap_or_default()
|
|
10
|
-
.as_millis() as u64
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
fn make_key_entry(id: &str, revoked: Option<bool>, expires: Option<u64>) -> KeyEntry {
|
|
14
|
-
KeyEntry {
|
|
15
|
-
id: id.to_string(),
|
|
16
|
-
algorithm: "Ed25519".to_string(),
|
|
17
|
-
public_key: "dGVzdC1wdWJsaWMta2V5".to_string(),
|
|
18
|
-
created: now_ms() - 1000,
|
|
19
|
-
expires,
|
|
20
|
-
revoked,
|
|
21
|
-
revoked_at: None,
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// 1. WellKnownKeysDocument round-trip JSON serialization
|
|
26
|
-
#[test]
|
|
27
|
-
fn test_well_known_document_json_round_trip() {
|
|
28
|
-
let entry = make_key_entry("key-1", None, None);
|
|
29
|
-
let doc = WellKnownKeysDocument::new(vec![entry]);
|
|
30
|
-
|
|
31
|
-
let json = doc.to_json().expect("serialization should succeed");
|
|
32
|
-
let restored = WellKnownKeysDocument::from_json(&json).expect("deserialization should succeed");
|
|
33
|
-
|
|
34
|
-
assert_eq!(restored.version, "7h3/0.1");
|
|
35
|
-
assert_eq!(restored.keys.len(), 1);
|
|
36
|
-
assert_eq!(restored.keys[0].id, "key-1");
|
|
37
|
-
assert_eq!(restored.keys[0].algorithm, "Ed25519");
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// 2. get_active_key finds non-revoked keys
|
|
41
|
-
#[test]
|
|
42
|
-
fn test_get_active_key_finds_active() {
|
|
43
|
-
let entry = make_key_entry("key-active", None, None);
|
|
44
|
-
let doc = WellKnownKeysDocument::new(vec![entry]);
|
|
45
|
-
|
|
46
|
-
let found = doc.get_active_key("key-active");
|
|
47
|
-
assert!(found.is_some(), "should find an active key");
|
|
48
|
-
assert_eq!(found.unwrap().id, "key-active");
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// 3. get_active_key ignores revoked keys
|
|
52
|
-
#[test]
|
|
53
|
-
fn test_get_active_key_ignores_revoked() {
|
|
54
|
-
let revoked = make_key_entry("key-revoked", Some(true), None);
|
|
55
|
-
let doc = WellKnownKeysDocument::new(vec![revoked]);
|
|
56
|
-
|
|
57
|
-
let found = doc.get_active_key("key-revoked");
|
|
58
|
-
assert!(found.is_none(), "should NOT find a revoked key");
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 3b. get_active_key ignores expired keys
|
|
62
|
-
#[test]
|
|
63
|
-
fn test_get_active_key_ignores_expired() {
|
|
64
|
-
// expires 10 seconds in the past
|
|
65
|
-
let expired = make_key_entry("key-expired", None, Some(now_ms() - 10_000));
|
|
66
|
-
let doc = WellKnownKeysDocument::new(vec![expired]);
|
|
67
|
-
|
|
68
|
-
let found = doc.get_active_key("key-expired");
|
|
69
|
-
assert!(found.is_none(), "should NOT find an expired key");
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// 4. KeyRotationManager: add_key + get_current_key
|
|
73
|
-
#[test]
|
|
74
|
-
fn test_rotation_manager_add_and_get_current() {
|
|
75
|
-
let mgr = KeyRotationManager::new(86_400_000, 3_600_000);
|
|
76
|
-
|
|
77
|
-
let pair = ManagedKeyPair {
|
|
78
|
-
id: "rot-key-1".to_string(),
|
|
79
|
-
public_key: "pub1".to_string(),
|
|
80
|
-
private_key: "priv1".to_string(),
|
|
81
|
-
created: now_ms(),
|
|
82
|
-
expires_at: None,
|
|
83
|
-
};
|
|
84
|
-
mgr.add_key(pair);
|
|
85
|
-
|
|
86
|
-
let current = mgr.get_current_key();
|
|
87
|
-
assert!(current.is_some(), "should return current key");
|
|
88
|
-
assert_eq!(current.unwrap().id, "rot-key-1");
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// 4b. get_current_key returns the most recent non-expired key
|
|
92
|
-
#[test]
|
|
93
|
-
fn test_rotation_manager_returns_most_recent() {
|
|
94
|
-
let mgr = KeyRotationManager::new(86_400_000, 3_600_000);
|
|
95
|
-
let base = now_ms();
|
|
96
|
-
|
|
97
|
-
mgr.add_key(ManagedKeyPair {
|
|
98
|
-
id: "older".to_string(),
|
|
99
|
-
public_key: "pub-older".to_string(),
|
|
100
|
-
private_key: "priv-older".to_string(),
|
|
101
|
-
created: base - 5000,
|
|
102
|
-
expires_at: None,
|
|
103
|
-
});
|
|
104
|
-
mgr.add_key(ManagedKeyPair {
|
|
105
|
-
id: "newer".to_string(),
|
|
106
|
-
public_key: "pub-newer".to_string(),
|
|
107
|
-
private_key: "priv-newer".to_string(),
|
|
108
|
-
created: base,
|
|
109
|
-
expires_at: None,
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
let current = mgr.get_current_key().expect("should have a current key");
|
|
113
|
-
assert_eq!(current.id, "newer");
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// 4c. get_current_key excludes expired keys
|
|
117
|
-
#[test]
|
|
118
|
-
fn test_rotation_manager_excludes_expired() {
|
|
119
|
-
let mgr = KeyRotationManager::new(86_400_000, 3_600_000);
|
|
120
|
-
|
|
121
|
-
mgr.add_key(ManagedKeyPair {
|
|
122
|
-
id: "exp-key".to_string(),
|
|
123
|
-
public_key: "pub-exp".to_string(),
|
|
124
|
-
private_key: "priv-exp".to_string(),
|
|
125
|
-
created: now_ms() - 2000,
|
|
126
|
-
expires_at: Some(now_ms() - 1000), // already expired
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
let current = mgr.get_current_key();
|
|
130
|
-
assert!(current.is_none(), "expired key should not be returned as current");
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// 5. KeyRotationManager: get_well_known_document structure
|
|
134
|
-
#[test]
|
|
135
|
-
fn test_rotation_manager_well_known_document() {
|
|
136
|
-
let mgr = KeyRotationManager::new(86_400_000, 3_600_000);
|
|
137
|
-
|
|
138
|
-
mgr.add_key(ManagedKeyPair {
|
|
139
|
-
id: "doc-key-1".to_string(),
|
|
140
|
-
public_key: "pub-doc".to_string(),
|
|
141
|
-
private_key: "priv-doc".to_string(),
|
|
142
|
-
created: now_ms(),
|
|
143
|
-
expires_at: None,
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
let doc = mgr.get_well_known_document();
|
|
147
|
-
assert_eq!(doc.version, "7h3/0.1");
|
|
148
|
-
assert_eq!(doc.keys.len(), 1);
|
|
149
|
-
assert_eq!(doc.keys[0].id, "doc-key-1");
|
|
150
|
-
assert_eq!(doc.keys[0].algorithm, "Ed25519");
|
|
151
|
-
assert_eq!(doc.keys[0].public_key, "pub-doc");
|
|
152
|
-
// non-expired key should not be marked revoked
|
|
153
|
-
assert!(doc.keys[0].revoked.is_none() || !doc.keys[0].revoked.unwrap());
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// 6. RevocationRegistry: revoke + is_revoked
|
|
157
|
-
#[test]
|
|
158
|
-
fn test_revocation_registry_revoke_and_check() {
|
|
159
|
-
let registry = RevocationRegistry::new();
|
|
160
|
-
|
|
161
|
-
assert!(!registry.is_revoked("key-x"), "should not be revoked initially");
|
|
162
|
-
registry.revoke("key-x", Some("compromised"));
|
|
163
|
-
assert!(registry.is_revoked("key-x"), "should be revoked after revoke()");
|
|
164
|
-
assert!(!registry.is_revoked("key-y"), "unrelated key should not be revoked");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// 7. RevocationRegistry: get_list contains correct fields
|
|
168
|
-
#[test]
|
|
169
|
-
fn test_revocation_registry_get_list() {
|
|
170
|
-
let registry = RevocationRegistry::new();
|
|
171
|
-
registry.revoke("key-a", Some("expired"));
|
|
172
|
-
registry.revoke("key-b", None);
|
|
173
|
-
|
|
174
|
-
let list = registry.get_list();
|
|
175
|
-
|
|
176
|
-
assert_eq!(list["version"], "7h3/0.1");
|
|
177
|
-
let revoked_keys = list["revokedKeys"].as_array().expect("revokedKeys should be array");
|
|
178
|
-
assert_eq!(revoked_keys.len(), 2);
|
|
179
|
-
|
|
180
|
-
// Find key-a in the list
|
|
181
|
-
let key_a = revoked_keys.iter().find(|e| e["id"] == "key-a").expect("key-a should be in list");
|
|
182
|
-
assert!(key_a["revokedAt"].as_u64().unwrap() > 0, "revokedAt should be a positive timestamp");
|
|
183
|
-
assert_eq!(key_a["reason"], "expired");
|
|
184
|
-
|
|
185
|
-
// Find key-b in the list — no reason
|
|
186
|
-
let key_b = revoked_keys.iter().find(|e| e["id"] == "key-b").expect("key-b should be in list");
|
|
187
|
-
assert!(key_b["revokedAt"].as_u64().unwrap() > 0);
|
|
188
|
-
assert!(key_b.get("reason").is_none() || key_b["reason"].is_null());
|
|
189
|
-
}
|
package/sdk/threshold/index.d.ts
DELETED
|
@@ -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":["src/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"}
|
package/sdk/threshold/index.js
DELETED
|
@@ -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
|
-
}
|