@t3foundry/tasra-sdk 0.3.1-next.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1067 -28
- package/README.md +15 -0
- package/dist/app/approvals.d.ts +66 -4
- package/dist/app/approvals.js +153 -16
- package/dist/app/authorization.d.ts +1 -1
- package/dist/app/authorization.js +1 -1
- package/dist/app/browser.d.ts +63 -0
- package/dist/app/browser.js +242 -0
- package/dist/app/client.d.ts +114 -13
- package/dist/app/client.js +302 -76
- package/dist/app/identity.js +21 -9
- package/dist/app/index.d.ts +28 -3
- package/dist/app/index.js +16 -1
- package/dist/app/memory-store.d.ts +7 -0
- package/dist/app/memory-store.js +55 -0
- package/dist/app/node.d.ts +34 -6
- package/dist/app/node.js +387 -64
- package/dist/app/paid-allowance.d.ts +325 -0
- package/dist/app/paid-allowance.js +1290 -0
- package/dist/app/paid-approval.d.ts +122 -0
- package/dist/app/paid-approval.js +379 -0
- package/dist/app/paid-journal.d.ts +51 -0
- package/dist/app/paid-journal.js +117 -0
- package/dist/app/paid-keys.d.ts +12 -0
- package/dist/app/paid-keys.js +20 -0
- package/dist/app/paid-operation.d.ts +821 -0
- package/dist/app/paid-operation.js +1720 -0
- package/dist/app/paid-reservation.d.ts +157 -0
- package/dist/app/paid-reservation.js +323 -0
- package/dist/app/paid-session.d.ts +147 -0
- package/dist/app/paid-session.js +578 -0
- package/dist/app/payer-queue.d.ts +11 -0
- package/dist/app/payer-queue.js +27 -0
- package/dist/app/policy-bootstrap.d.ts +342 -0
- package/dist/app/policy-bootstrap.js +365 -0
- package/dist/app/policy-operation.d.ts +47 -0
- package/dist/app/policy-operation.js +101 -0
- package/dist/app/ready-slot.d.ts +12 -0
- package/dist/app/ready-slot.js +5 -2
- package/dist/app/rotation-lock.d.ts +38 -0
- package/dist/app/rotation-lock.js +42 -0
- package/dist/app/session-decrypt.d.ts +135 -0
- package/dist/app/session-decrypt.js +335 -0
- package/dist/app/session-grants.d.ts +349 -0
- package/dist/app/session-grants.js +811 -0
- package/dist/app/signing-intents.d.ts +78 -0
- package/dist/app/signing-intents.js +95 -0
- package/dist/app/store-commit.d.ts +63 -0
- package/dist/app/store-commit.js +126 -0
- package/dist/app/store-errors.d.ts +8 -0
- package/dist/app/store-errors.js +11 -0
- package/dist/app/tasra-client.d.ts +2 -0
- package/dist/app/tasra-client.js +1 -1
- package/dist/app/wallet.js +4 -1
- package/dist/auth/holderProof.d.ts +3 -0
- package/dist/auth/holderProof.js +8 -7
- package/dist/auth/verifier.d.ts +4 -2
- package/dist/auth/verifier.js +5 -2
- package/dist/chain/abis/index.d.ts +6178 -4101
- package/dist/chain/abis/index.js +3 -0
- package/dist/chain/abis/keyRegistry.d.ts +256 -0
- package/dist/chain/abis/keyRegistry.js +333 -0
- package/dist/chain/abis/prepaidFaultOracle.d.ts +321 -0
- package/dist/chain/abis/prepaidFaultOracle.js +413 -0
- package/dist/chain/abis/settlement.d.ts +2203 -703
- package/dist/chain/abis/settlement.js +2685 -764
- package/dist/chain/allowance.d.ts +358 -0
- package/dist/chain/allowance.js +800 -0
- package/dist/chain/billing.d.ts +85 -0
- package/dist/chain/billing.js +197 -0
- package/dist/chain/chainId.d.ts +13 -0
- package/dist/chain/chainId.js +26 -0
- package/dist/chain/client.d.ts +15 -1
- package/dist/chain/client.js +57 -1
- package/dist/chain/commitmentRecovery.d.ts +51 -0
- package/dist/chain/commitmentRecovery.js +38 -0
- package/dist/chain/committeeClient.d.ts +17 -10
- package/dist/chain/committeeClient.js +64 -16
- package/dist/chain/committeeDraw.d.ts +12 -0
- package/dist/chain/committeeDraw.js +56 -0
- package/dist/chain/custodyHealth.d.ts +212 -0
- package/dist/chain/custodyHealth.js +365 -0
- package/dist/chain/deployments.js +2 -0
- package/dist/chain/events.js +2 -0
- package/dist/chain/headFollower.d.ts +77 -0
- package/dist/chain/headFollower.js +219 -0
- package/dist/chain/headPrefetch.d.ts +23 -0
- package/dist/chain/headPrefetch.js +67 -0
- package/dist/chain/index.d.ts +50 -8
- package/dist/chain/index.js +28 -3
- package/dist/chain/keeperExecution.d.ts +44 -0
- package/dist/chain/keeperExecution.js +127 -0
- package/dist/chain/keeperRecovery.d.ts +78 -0
- package/dist/chain/keeperRecovery.js +91 -0
- package/dist/chain/node.d.ts +24 -1
- package/dist/chain/node.js +88 -43
- package/dist/chain/offchain.d.ts +2 -0
- package/dist/chain/paidAgentTransport.d.ts +38 -0
- package/dist/chain/paidAgentTransport.js +1 -0
- package/dist/chain/paidEpoch.d.ts +66 -0
- package/dist/chain/paidEpoch.js +139 -0
- package/dist/chain/paidPacing.d.ts +81 -0
- package/dist/chain/paidPacing.js +136 -0
- package/dist/chain/pinnedReads.d.ts +49 -0
- package/dist/chain/pinnedReads.js +218 -0
- package/dist/chain/prepaid.d.ts +383 -0
- package/dist/chain/prepaid.js +1046 -0
- package/dist/chain/prepaidEscape.d.ts +274 -0
- package/dist/chain/prepaidEscape.js +432 -0
- package/dist/chain/prepaidFault.d.ts +94 -0
- package/dist/chain/prepaidFault.js +148 -0
- package/dist/chain/receipt.d.ts +24 -0
- package/dist/chain/receipt.js +82 -0
- package/dist/chain/registeredAgent.d.ts +68 -3
- package/dist/chain/registeredAgent.js +143 -42
- package/dist/chain/registeredOperation.d.ts +10 -1
- package/dist/chain/registeredOperation.js +18 -3
- package/dist/chain/registeredPaidAgent.d.ts +254 -0
- package/dist/chain/registeredPaidAgent.js +889 -0
- package/dist/chain/registeredRelay.d.ts +31 -1
- package/dist/chain/registeredRelay.js +164 -43
- package/dist/chain/relayVoucher.d.ts +64 -0
- package/dist/chain/relayVoucher.js +69 -0
- package/dist/chain/reservation.d.ts +239 -0
- package/dist/chain/reservation.js +746 -0
- package/dist/chain/reshareRoster.d.ts +64 -0
- package/dist/chain/reshareRoster.js +85 -0
- package/dist/chain/revertReason.d.ts +8 -0
- package/dist/chain/revertReason.js +2 -1
- package/dist/chain/rotationLock.d.ts +130 -0
- package/dist/chain/rotationLock.js +168 -0
- package/dist/chain/serviceRequestPolicy.d.ts +47 -0
- package/dist/chain/serviceRequestPolicy.js +86 -0
- package/dist/chain/services.d.ts +7 -0
- package/dist/chain/services.js +44 -4
- package/dist/chain/slotClient.js +11 -2
- package/dist/chain/slotHandoff.d.ts +81 -0
- package/dist/chain/slotHandoff.js +166 -0
- package/dist/chain/slotSeed.d.ts +47 -5
- package/dist/chain/slotSeed.js +173 -42
- package/dist/chain/verifierSnapshot.d.ts +36 -0
- package/dist/chain/verifierSnapshot.js +197 -0
- package/dist/chain/write.d.ts +111 -21
- package/dist/chain/write.js +378 -146
- package/dist/committee/blsSharing.d.ts +53 -0
- package/dist/committee/blsSharing.js +90 -0
- package/dist/committee/client.d.ts +62 -23
- package/dist/committee/client.js +288 -72
- package/dist/committee/dual-operation.d.ts +165 -0
- package/dist/committee/dual-operation.js +229 -0
- package/dist/committee/dual-sign.d.ts +26 -1
- package/dist/committee/dual-sign.js +85 -7
- package/dist/committee/ecdsa.d.ts +38 -0
- package/dist/committee/ecdsa.js +87 -15
- package/dist/committee/evidence.d.ts +30 -0
- package/dist/committee/evidence.js +65 -0
- package/dist/committee/execution.d.ts +414 -0
- package/dist/committee/execution.js +446 -0
- package/dist/committee/extraction.js +33 -26
- package/dist/committee/index.d.ts +30 -7
- package/dist/committee/index.js +18 -6
- package/dist/committee/keeperExecution.d.ts +37 -0
- package/dist/committee/keeperExecution.js +141 -0
- package/dist/committee/oauth.d.ts +14 -4
- package/dist/committee/oauth.js +17 -5
- package/dist/committee/payment.d.ts +369 -0
- package/dist/committee/payment.js +339 -0
- package/dist/committee/recovery.d.ts +136 -0
- package/dist/committee/recovery.js +204 -0
- package/dist/committee/recoveryJson.d.ts +2 -0
- package/dist/committee/recoveryJson.js +87 -0
- package/dist/committee/request.d.ts +63 -27
- package/dist/committee/request.js +77 -66
- package/dist/committee/session-grant.d.ts +320 -0
- package/dist/committee/session-grant.js +397 -0
- package/dist/committee/status-refresh.d.ts +257 -0
- package/dist/committee/status-refresh.js +619 -0
- package/dist/committee/token.d.ts +163 -15
- package/dist/committee/token.js +247 -10
- package/dist/crypto/bn254.d.ts +70 -0
- package/dist/crypto/bn254.js +222 -0
- package/dist/crypto/ibe.d.ts +32 -0
- package/dist/crypto/ibe.js +56 -8
- package/dist/crypto/index.d.ts +1 -1
- package/dist/crypto/index.js +1 -1
- package/dist/crypto/kem.d.ts +31 -0
- package/dist/crypto/kem.js +113 -7
- package/dist/crypto/recipient.d.ts +75 -0
- package/dist/crypto/recipient.js +107 -0
- package/dist/decryption/client.d.ts +10 -16
- package/dist/decryption/client.js +8 -76
- package/dist/decryption/index.d.ts +2 -0
- package/dist/decryption/index.js +2 -0
- package/dist/decryption/sealed.d.ts +72 -0
- package/dist/decryption/sealed.js +147 -0
- package/dist/decryption/session.d.ts +115 -0
- package/dist/decryption/session.js +358 -0
- package/dist/errors.d.ts +406 -1
- package/dist/errors.js +400 -5
- package/dist/index.d.ts +9 -2
- package/dist/index.js +6 -1
- package/dist/internal/bounded.d.ts +66 -0
- package/dist/internal/bounded.js +126 -0
- package/dist/internal/network.d.ts +4 -0
- package/dist/internal/network.js +40 -0
- package/dist/internal/retryAfter.d.ts +8 -0
- package/dist/internal/retryAfter.js +28 -0
- package/dist/internal/sleep.d.ts +6 -0
- package/dist/internal/sleep.js +15 -0
- package/dist/internal/typedData.d.ts +13 -0
- package/dist/internal/typedData.js +77 -0
- package/dist/internal/viemErrors.d.ts +26 -0
- package/dist/internal/viemErrors.js +49 -0
- package/dist/oid4vp/binding.d.ts +27 -1
- package/dist/oid4vp/binding.js +36 -1
- package/dist/oid4vp/jose.d.ts +4 -2
- package/dist/oid4vp/jose.js +39 -7
- package/dist/oid4vp/request-object.d.ts +61 -4
- package/dist/oid4vp/request-object.js +140 -5
- package/dist/oid4vp/verifier-agent.d.ts +18 -8
- package/dist/oid4vp/verifier-agent.js +48 -6
- package/dist/oid4vp/wallet.d.ts +2 -4
- package/dist/oid4vp/wallet.js +1 -0
- package/dist/retry.d.ts +78 -0
- package/dist/retry.js +137 -0
- package/dist/signing/frost.d.ts +1 -1
- package/dist/verifier-agent/index.d.ts +41 -5
- package/dist/verifier-agent/index.js +124 -9
- package/dist/verifier-agent/payment.d.ts +7 -0
- package/dist/verifier-agent/payment.js +17 -0
- package/docs/api.md +8 -2
- package/docs/application-api.md +25 -2
- package/docs/chain.md +155 -9
- package/docs/consumer-migrations.md +61 -0
- package/docs/encrypted-notes.md +3 -2
- package/docs/encryption.md +4 -2
- package/docs/errors.md +14 -1
- package/docs/flows.md +2 -2
- package/docs/funding.md +902 -0
- package/docs/native-approvals.md +63 -0
- package/docs/reference/README.md +9 -8
- package/docs/reference/app-browser.md +232 -0
- package/docs/reference/app-node.md +38 -8
- package/docs/reference/app.md +4621 -805
- package/docs/reference/chain-node.md +74 -16
- package/docs/reference/chain.md +6740 -1592
- package/docs/reference/committee.md +5630 -987
- package/docs/reference/main.md +1439 -140
- package/docs/reference/oid4vp.md +297 -144
- package/docs/reference/verifier-agent.md +77 -35
- package/docs/signing.md +6 -0
- package/examples/encrypted-notes.ts +21 -6
- package/examples/getting-started.ts +2 -7
- package/examples/live-config.ts +24 -17
- package/package.json +17 -5
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
// Threshold BLS on BN254: the accountant authorization root (ADR-0090 debit vouchers).
|
|
2
|
+
//
|
|
3
|
+
// Keys in G2, signatures in G1, `sigma = s * H(digest)`, partials Lagrange-combined at zero: the
|
|
4
|
+
// scheme of `keykeeper-bls::bn254::sign`. Every field and group operation is @noble/curves'
|
|
5
|
+
// (`bn254`, `bn254_Fr`); nothing here does modular arithmetic by hand.
|
|
6
|
+
//
|
|
7
|
+
// Two encodings are cross-language contracts, not standards, and both are pinned by vectors:
|
|
8
|
+
// - hash-to-G1 is try-and-increment over `keccak256(digest || uint256(i))`, `x mod p`, with the
|
|
9
|
+
// square root `(y^2)^((p+1)/4)` (a library sqrt may return the OTHER root, which still lies on
|
|
10
|
+
// the curve and would only disagree with the Solidity verifier);
|
|
11
|
+
// - points are the bare EVM encodings: G1 `x || y`, G2 imaginary part first `x_c1 || x_c0 || y_c1 || y_c0`.
|
|
12
|
+
//
|
|
13
|
+
// ~100-bit security; never reuse for user data (KEM/IBE stay BLS12-381).
|
|
14
|
+
import { bn254, bn254_Fr } from '@noble/curves/bn254';
|
|
15
|
+
import { keccak_256 } from '@noble/hashes/sha3';
|
|
16
|
+
import { bytesToNumberBE, numberToBytesBE } from '@noble/curves/utils.js';
|
|
17
|
+
import { bytesToHex } from '@noble/hashes/utils';
|
|
18
|
+
import { BoundedCache } from '../internal/bounded.js';
|
|
19
|
+
const Fp = bn254.fields.Fp, Fp2 = bn254.fields.Fp2, Fp12 = bn254.fields.Fp12;
|
|
20
|
+
const G1 = bn254.G1.ProjectivePoint, G2 = bn254.G2.ProjectivePoint;
|
|
21
|
+
/** Matches the Solidity loop bound: both implementations fail on the same inputs. */
|
|
22
|
+
const MAX_HASH_ATTEMPTS = 256;
|
|
23
|
+
const SQRT_EXPONENT = (Fp.ORDER + 1n) / 4n;
|
|
24
|
+
/** A BN254 encoding or verification failure; `reason` names the operand. */
|
|
25
|
+
export class Bn254Error extends Error {
|
|
26
|
+
constructor(message) { super(message); this.name = 'Bn254Error'; }
|
|
27
|
+
}
|
|
28
|
+
function fq(bytes) {
|
|
29
|
+
const value = bytesToNumberBE(bytes);
|
|
30
|
+
// Canonical only: a value >= p would be a different encoding of a reduced point, which the
|
|
31
|
+
// precompile refuses. Decoding must not reduce.
|
|
32
|
+
if (value >= Fp.ORDER)
|
|
33
|
+
throw new Error('non-canonical BN254 field element');
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Map a 32-byte digest to G1 exactly as the Solidity verifier and the Rust signer do.
|
|
38
|
+
* @param digest - 32-byte message digest.
|
|
39
|
+
*/
|
|
40
|
+
export function hashToG1(digest) {
|
|
41
|
+
if (digest.length !== 32)
|
|
42
|
+
throw new Bn254Error('hash-to-G1 takes a 32-byte digest');
|
|
43
|
+
const preimage = new Uint8Array(64);
|
|
44
|
+
preimage.set(digest);
|
|
45
|
+
for (let i = 0; i < MAX_HASH_ATTEMPTS; i++) {
|
|
46
|
+
preimage.set(numberToBytesBE(BigInt(i), 32), 32);
|
|
47
|
+
const x = Fp.create(bytesToNumberBE(keccak_256(preimage)));
|
|
48
|
+
const y2 = Fp.add(Fp.mul(Fp.sqr(x), x), 3n);
|
|
49
|
+
const y = Fp.pow(y2, SQRT_EXPONENT);
|
|
50
|
+
if (Fp.eql(Fp.sqr(y), y2))
|
|
51
|
+
return G1.fromAffine({ x, y });
|
|
52
|
+
}
|
|
53
|
+
throw new Bn254Error(`hash-to-G1 did not converge in ${MAX_HASH_ATTEMPTS} attempts`);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Decode a 64-byte `x || y` G1 point; all-zero is infinity (the EVM spelling), returned as ZERO.
|
|
57
|
+
* @param bytes - 64-byte EVM encoding.
|
|
58
|
+
*/
|
|
59
|
+
export function decodeG1(bytes) {
|
|
60
|
+
if (bytes.length !== 64)
|
|
61
|
+
throw new Bn254Error('G1 point must be 64 bytes');
|
|
62
|
+
if (bytes.every(b => b === 0))
|
|
63
|
+
return G1.ZERO;
|
|
64
|
+
try {
|
|
65
|
+
const point = G1.fromAffine({ x: fq(bytes.subarray(0, 32)), y: fq(bytes.subarray(32)) });
|
|
66
|
+
point.assertValidity(); // cofactor 1: on-curve is in the subgroup
|
|
67
|
+
return point;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
throw new Bn254Error('malformed G1 point');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Encode a G1 point as `x || y` (all-zero for infinity).
|
|
75
|
+
* @param point - G1 point.
|
|
76
|
+
*/
|
|
77
|
+
export function encodeG1(point) {
|
|
78
|
+
if (point.equals(G1.ZERO))
|
|
79
|
+
return new Uint8Array(64);
|
|
80
|
+
const { x, y } = point.toAffine();
|
|
81
|
+
const out = new Uint8Array(64);
|
|
82
|
+
out.set(numberToBytesBE(x, 32));
|
|
83
|
+
out.set(numberToBytesBE(y, 32), 32);
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Decode a 128-byte G2 point (imaginary part first), subgroup-checked: BN254's G2 has a large
|
|
88
|
+
* cofactor, so on-curve alone is not enough. All-zero is infinity.
|
|
89
|
+
* @param bytes - 128-byte EVM encoding.
|
|
90
|
+
*/
|
|
91
|
+
export function decodeG2(bytes) {
|
|
92
|
+
if (bytes.length !== 128)
|
|
93
|
+
throw new Bn254Error('G2 point must be 128 bytes');
|
|
94
|
+
if (bytes.every(b => b === 0))
|
|
95
|
+
return G2.ZERO;
|
|
96
|
+
try {
|
|
97
|
+
const limb = (i) => fq(bytes.subarray(i * 32, (i + 1) * 32));
|
|
98
|
+
const point = G2.fromAffine({ x: Fp2.create({ c0: limb(1), c1: limb(0) }), y: Fp2.create({ c0: limb(3), c1: limb(2) }) });
|
|
99
|
+
point.assertValidity();
|
|
100
|
+
return point;
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
throw new Bn254Error('malformed or out-of-subgroup G2 point');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Encode a G2 point (imaginary part first; all-zero for infinity).
|
|
108
|
+
* @param point - G2 point.
|
|
109
|
+
*/
|
|
110
|
+
export function encodeG2(point) {
|
|
111
|
+
if (point.equals(G2.ZERO))
|
|
112
|
+
return new Uint8Array(128);
|
|
113
|
+
const { x, y } = point.toAffine();
|
|
114
|
+
const out = new Uint8Array(128);
|
|
115
|
+
[x.c1, x.c0, y.c1, y.c0].forEach((limb, i) => out.set(numberToBytesBE(limb, 32), i * 32));
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
// Keys a signature is checked against (the group key `beacon.mpk()`, a member's verifying share)
|
|
119
|
+
// are few and static per sharing, and each costs a subgroup-checked decode plus the pairing's
|
|
120
|
+
// line precomputation (noble memoizes those per point OBJECT). A bounded set of decoded keys is
|
|
121
|
+
// kept, so both are paid once per key instead of once per check. Only keys that decoded and
|
|
122
|
+
// passed the subgroup check are stored (a throwing decode stores nothing); the cached value is a
|
|
123
|
+
// pure function of the bytes, an immutable public point.
|
|
124
|
+
const verifiedKeys = new BoundedCache(64);
|
|
125
|
+
function decodeKeyG2(bytes) {
|
|
126
|
+
if (bytes.length !== 128)
|
|
127
|
+
throw new Bn254Error('G2 point must be 128 bytes');
|
|
128
|
+
return verifiedKeys.getOrCompute(bytesToHex(bytes), () => decodeG2(bytes));
|
|
129
|
+
}
|
|
130
|
+
/** `e(a, b) * e(c, d) == 1`, the shape precompile 0x08 computes. */
|
|
131
|
+
function pairingProductIsOne(a, b, c, d) {
|
|
132
|
+
return Fp12.eql(bn254.pairingBatch([{ g1: a, g2: b }, { g1: c, g2: d }]), Fp12.ONE);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Verify a 64-byte signature over `digest` against a 128-byte G2 key: `e(-sigma, g2) * e(H(m), key) == 1`.
|
|
136
|
+
* Infinity on either side is refused (it would satisfy the equation trivially).
|
|
137
|
+
* @param key - Group public key or a member's verifying share.
|
|
138
|
+
* @param digest - 32-byte digest.
|
|
139
|
+
* @param signature - 64-byte G1 signature or partial.
|
|
140
|
+
*/
|
|
141
|
+
export function verifyBn254(key, digest, signature) {
|
|
142
|
+
let sigma, pk;
|
|
143
|
+
try {
|
|
144
|
+
sigma = decodeG1(signature);
|
|
145
|
+
pk = decodeKeyG2(key);
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
if (sigma.equals(G1.ZERO) || pk.equals(G2.ZERO))
|
|
151
|
+
return false;
|
|
152
|
+
return pairingProductIsOne(sigma.negate(), G2.BASE, hashToG1(digest), pk);
|
|
153
|
+
}
|
|
154
|
+
function lagrangeAtZero(id, ids) {
|
|
155
|
+
let num = bn254_Fr.ONE, den = bn254_Fr.ONE;
|
|
156
|
+
for (const other of ids) {
|
|
157
|
+
if (other === id)
|
|
158
|
+
continue;
|
|
159
|
+
num = bn254_Fr.mul(num, other);
|
|
160
|
+
den = bn254_Fr.mul(den, bn254_Fr.sub(other, id));
|
|
161
|
+
}
|
|
162
|
+
return bn254_Fr.div(num, den);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Lagrange-interpolate exactly `threshold` partials at zero, WITHOUT any verification: the result
|
|
166
|
+
* is a signature only if the caller then verifies it against the group key (one pairing check,
|
|
167
|
+
* where {@link combineBn254} spends one per partial as well). Undefined when the identifiers are
|
|
168
|
+
* invalid or repeated, too few partials are given, or a partial is not a valid G1 point.
|
|
169
|
+
* @param partials - Distinct members' partials (the first `threshold` are used).
|
|
170
|
+
* @param threshold - k.
|
|
171
|
+
*/
|
|
172
|
+
export function interpolateBn254(partials, threshold) {
|
|
173
|
+
if (!Number.isSafeInteger(threshold) || threshold < 1 || partials.length < threshold)
|
|
174
|
+
return undefined;
|
|
175
|
+
const used = partials.slice(0, threshold);
|
|
176
|
+
if (used.some(p => !Number.isSafeInteger(p.identifier) || p.identifier < 1 || p.identifier > 65535) || new Set(used.map(p => p.identifier)).size !== used.length)
|
|
177
|
+
return undefined;
|
|
178
|
+
const xs = used.map(p => BigInt(p.identifier));
|
|
179
|
+
try {
|
|
180
|
+
let acc = G1.ZERO;
|
|
181
|
+
// Every input is public (members' published partials, their identifiers), so the variable-time
|
|
182
|
+
// multiplication leaks nothing; the result is still only trusted after the group-key check.
|
|
183
|
+
for (const p of used)
|
|
184
|
+
acc = acc.add(decodeG1(p.partial).multiplyUnsafe(lagrangeAtZero(BigInt(p.identifier), xs)));
|
|
185
|
+
return encodeG1(acc);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Lagrange-combine exactly `threshold` partials (each pre-verified against its own verifying share,
|
|
193
|
+
* so a byzantine member is named, not merely detected) and verify the aggregate against the group
|
|
194
|
+
* key: a below-threshold or mismatched set interpolates to the wrong point and is caught there.
|
|
195
|
+
* @param groupKey - 128-byte group public key.
|
|
196
|
+
* @param digest - 32-byte digest.
|
|
197
|
+
* @param partials - Distinct members' partials.
|
|
198
|
+
* @param threshold - k; exactly k partials are combined.
|
|
199
|
+
*/
|
|
200
|
+
export function combineBn254(groupKey, digest, partials, threshold) {
|
|
201
|
+
if (!Number.isSafeInteger(threshold) || threshold < 1 || partials.length < threshold)
|
|
202
|
+
throw new Bn254Error(`need ${threshold} partials, have ${partials.length}`);
|
|
203
|
+
const ids = new Set();
|
|
204
|
+
for (const p of partials) {
|
|
205
|
+
if (!Number.isSafeInteger(p.identifier) || p.identifier < 1 || p.identifier > 65535)
|
|
206
|
+
throw new Bn254Error(`invalid identifier ${p.identifier}`);
|
|
207
|
+
if (ids.has(p.identifier))
|
|
208
|
+
throw new Bn254Error(`duplicate identifier ${p.identifier}`);
|
|
209
|
+
ids.add(p.identifier);
|
|
210
|
+
if (!verifyBn254(p.verifyingShare, digest, p.partial))
|
|
211
|
+
throw new Bn254Error(`partial of member ${p.identifier} does not verify against its share`);
|
|
212
|
+
}
|
|
213
|
+
const used = partials.slice(0, threshold), xs = used.map(p => BigInt(p.identifier));
|
|
214
|
+
let acc = G1.ZERO;
|
|
215
|
+
// Public partials and identifiers (as in interpolateBn254): variable-time is safe.
|
|
216
|
+
for (const p of used)
|
|
217
|
+
acc = acc.add(decodeG1(p.partial).multiplyUnsafe(lagrangeAtZero(BigInt(p.identifier), xs)));
|
|
218
|
+
const signature = encodeG1(acc);
|
|
219
|
+
if (!verifyBn254(groupKey, digest, signature))
|
|
220
|
+
throw new Bn254Error('combined signature does not verify against the group key');
|
|
221
|
+
return signature;
|
|
222
|
+
}
|
package/dist/crypto/ibe.d.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
declare const G1: {
|
|
2
|
+
Point: import("@noble/curves/abstract/weierstrass").WeierstrassPointCons<bigint>;
|
|
3
|
+
} & import("@noble/curves/abstract/hash-to-curve").H2CHasherBase<bigint> & {
|
|
4
|
+
encodeToCurve: import("@noble/curves/abstract/hash-to-curve").H2CMethod<bigint>;
|
|
5
|
+
mapToCurve: import("@noble/curves/abstract/hash-to-curve").MapMethod<bigint>;
|
|
6
|
+
defaults: import("@noble/curves/abstract/hash-to-curve").H2COpts & {
|
|
7
|
+
encodeDST?: import("@noble/curves/abstract/hash-to-curve").UnicodeOrBytes;
|
|
8
|
+
};
|
|
9
|
+
} & import("@noble/curves/abstract/weierstrass").CurvePointsRes<bigint>;
|
|
1
10
|
/** Domain tag for the KDF/nonce derivations (mirrors `IBE_DOMAIN`). */
|
|
2
11
|
export declare const IBE_DOMAIN: Uint8Array<ArrayBuffer>;
|
|
3
12
|
/** RFC 9380 DST for identity hashing (mirrors `IBE_HASH_DST`). */
|
|
@@ -20,6 +29,12 @@ export interface IbeDecryptionShare {
|
|
|
20
29
|
}
|
|
21
30
|
/** A node's dual-group verifying share - only the 96-byte G2 half is needed here. */
|
|
22
31
|
export type IbeVerifyingShares = ReadonlyMap<number, Uint8Array>;
|
|
32
|
+
type G1Point = InstanceType<typeof G1.ProjectivePoint>;
|
|
33
|
+
/**
|
|
34
|
+
* @internal Hash an identity to its G1 point Q_ID once, for several {@link ibeVerifyShareAt} checks.
|
|
35
|
+
* @param identity - Exact bytes of the requested identity.
|
|
36
|
+
*/
|
|
37
|
+
export declare function ibeIdentityPoint(identity: Uint8Array): G1Point;
|
|
23
38
|
/** `Q_ID` as 48 compressed bytes - pins the hash-to-curve suite + DST cross-language. */
|
|
24
39
|
export declare function ibeHashIdentityToG1(identity: Uint8Array): Uint8Array;
|
|
25
40
|
/**
|
|
@@ -52,6 +67,14 @@ export declare function ibeEncryptWithScalar(mpkBytes: Uint8Array, identity: Uin
|
|
|
52
67
|
* @param share - Identity-key share to verify.
|
|
53
68
|
*/
|
|
54
69
|
export declare function ibeVerifyShare(verifyingShares: IbeVerifyingShares, identity: Uint8Array, share: IbeDecryptionShare): void;
|
|
70
|
+
/**
|
|
71
|
+
* @internal {@link ibeVerifyShare} with Q_ID already computed by {@link ibeIdentityPoint}: the same
|
|
72
|
+
* check and error, without re-hashing the identity for every share of one operation.
|
|
73
|
+
* @param verifyingShare - Authenticated 96-byte G2 verifying share for this identifier.
|
|
74
|
+
* @param qId - Q_ID of the identity the share must belong to.
|
|
75
|
+
* @param share - The share to check.
|
|
76
|
+
*/
|
|
77
|
+
export declare function ibeVerifyShareAt(verifyingShare: Uint8Array, qId: G1Point, share: IbeDecryptionShare): void;
|
|
55
78
|
/**
|
|
56
79
|
* Lagrange-combine k verified partials into the identity key `sk_ID = msk * Q_ID`
|
|
57
80
|
* (48-byte compressed G1).
|
|
@@ -66,6 +89,14 @@ export declare function ibeVerifyShare(verifyingShares: IbeVerifyingShares, iden
|
|
|
66
89
|
* @param identity - Exact bytes of the requested identity.
|
|
67
90
|
*/
|
|
68
91
|
export declare function ibeCombineExtract(verifyingShares: IbeVerifyingShares, shares: IbeDecryptionShare[], identity: Uint8Array): Uint8Array;
|
|
92
|
+
/**
|
|
93
|
+
* @internal Lagrange-combine distinct partials that THIS caller already pairing-verified with
|
|
94
|
+
* {@link ibeVerifyShare} against authenticated verifying shares. Never use it for shares from
|
|
95
|
+
* another caller: {@link ibeCombineExtract} verifies them.
|
|
96
|
+
*
|
|
97
|
+
* @param shares - Distinct, already verified identity-key shares sufficient for the slot threshold.
|
|
98
|
+
*/
|
|
99
|
+
export declare function combineVerifiedIbeShares(shares: IbeDecryptionShare[]): Uint8Array;
|
|
69
100
|
/**
|
|
70
101
|
* Combine k extraction partials and AEAD-decrypt `ct` - mirrors
|
|
71
102
|
* `bls::ibe::combine_decrypt` (verify every share to Lagrange-combine to `T = e(sk_ID,U)`
|
|
@@ -86,3 +117,4 @@ export declare function ibeCombineDecrypt(verifyingShares: IbeVerifyingShares, s
|
|
|
86
117
|
* @param identity - Identity bytes used during encryption.
|
|
87
118
|
*/
|
|
88
119
|
export declare function ibeDecryptWithKey(skIdBytes: Uint8Array, ct: IbeCiphertext, identity: Uint8Array): Uint8Array;
|
|
120
|
+
export {};
|
package/dist/crypto/ibe.js
CHANGED
|
@@ -30,7 +30,8 @@
|
|
|
30
30
|
import { chacha20poly1305 } from '@noble/ciphers/chacha';
|
|
31
31
|
import { bls12_381 } from '@noble/curves/bls12-381';
|
|
32
32
|
import { sha256 } from '@noble/hashes/sha256';
|
|
33
|
-
import { randomBytes } from '@noble/hashes/utils';
|
|
33
|
+
import { bytesToHex, randomBytes } from '@noble/hashes/utils';
|
|
34
|
+
import { BoundedCache } from '../internal/bounded.js';
|
|
34
35
|
const { Fr, Fp12 } = bls12_381.fields;
|
|
35
36
|
const G1 = bls12_381.G1;
|
|
36
37
|
const G2 = bls12_381.G2;
|
|
@@ -52,6 +53,21 @@ function decodeG1(bytes, what) {
|
|
|
52
53
|
throw new Error(`${what}: point at infinity`);
|
|
53
54
|
return p;
|
|
54
55
|
}
|
|
56
|
+
// Verifying shares and group keys are static per slot epoch; decoding one (with its subgroup
|
|
57
|
+
// check) costs about as much as a hash to G1, so a bounded set of decoded public points is kept.
|
|
58
|
+
const trustedG2 = new BoundedCache(256);
|
|
59
|
+
function decodeTrustedG2(bytes, what) {
|
|
60
|
+
if (bytes.length !== 96)
|
|
61
|
+
throw new Error(`${what}: expected 96 bytes, got ${bytes.length}`);
|
|
62
|
+
return trustedG2.getOrCompute(bytesToHex(bytes), () => decodeG2(bytes, what));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @internal Hash an identity to its G1 point Q_ID once, for several {@link ibeVerifyShareAt} checks.
|
|
66
|
+
* @param identity - Exact bytes of the requested identity.
|
|
67
|
+
*/
|
|
68
|
+
export function ibeIdentityPoint(identity) {
|
|
69
|
+
return hashToG1(identity);
|
|
70
|
+
}
|
|
55
71
|
function decodeG2(bytes, what) {
|
|
56
72
|
if (bytes.length !== 96)
|
|
57
73
|
throw new Error(`${what}: expected 96 bytes, got ${bytes.length}`);
|
|
@@ -186,12 +202,26 @@ export function ibeVerifyShare(verifyingShares, identity, share) {
|
|
|
186
202
|
if (!vsBytes)
|
|
187
203
|
throw new Error(`ibeVerifyShare: unknown identifier ${share.identifier}`);
|
|
188
204
|
const dI = decodeG1(share.value, `share ${share.identifier}`);
|
|
189
|
-
const yI =
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
205
|
+
const yI = decodeTrustedG2(vsBytes, `verifying share ${share.identifier}`);
|
|
206
|
+
verifyShareEquation(dI, yI, hashToG1(identity), share.identifier);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @internal {@link ibeVerifyShare} with Q_ID already computed by {@link ibeIdentityPoint}: the same
|
|
210
|
+
* check and error, without re-hashing the identity for every share of one operation.
|
|
211
|
+
* @param verifyingShare - Authenticated 96-byte G2 verifying share for this identifier.
|
|
212
|
+
* @param qId - Q_ID of the identity the share must belong to.
|
|
213
|
+
* @param share - The share to check.
|
|
214
|
+
*/
|
|
215
|
+
export function ibeVerifyShareAt(verifyingShare, qId, share) {
|
|
216
|
+
const dI = decodeG1(share.value, `share ${share.identifier}`);
|
|
217
|
+
const yI = decodeTrustedG2(verifyingShare, `verifying share ${share.identifier}`);
|
|
218
|
+
verifyShareEquation(dI, yI, qId, share.identifier);
|
|
219
|
+
}
|
|
220
|
+
function verifyShareEquation(dI, yI, qId, identifier) {
|
|
221
|
+
// e(Dᵢ, g2) == e(Q_ID, Yᵢ) ⟺ e(Dᵢ, g2) · e(−Q_ID, Yᵢ) == 1 (bilinearity): the same exact
|
|
222
|
+
// equation with one shared final exponentiation instead of two.
|
|
223
|
+
if (!Fp12.eql(bls12_381.pairingBatch([{ g1: dI, g2: G2.ProjectivePoint.BASE }, { g1: qId.negate(), g2: yI }]), Fp12.ONE)) {
|
|
224
|
+
throw new Error(`ibeVerifyShare: invalid share from identifier ${identifier}`);
|
|
195
225
|
}
|
|
196
226
|
}
|
|
197
227
|
/**
|
|
@@ -216,11 +246,29 @@ export function ibeCombineExtract(verifyingShares, shares, identity) {
|
|
|
216
246
|
}
|
|
217
247
|
for (const s of shares)
|
|
218
248
|
ibeVerifyShare(verifyingShares, identity, s);
|
|
249
|
+
return combineVerifiedIbeShares(shares);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @internal Lagrange-combine distinct partials that THIS caller already pairing-verified with
|
|
253
|
+
* {@link ibeVerifyShare} against authenticated verifying shares. Never use it for shares from
|
|
254
|
+
* another caller: {@link ibeCombineExtract} verifies them.
|
|
255
|
+
*
|
|
256
|
+
* @param shares - Distinct, already verified identity-key shares sufficient for the slot threshold.
|
|
257
|
+
*/
|
|
258
|
+
export function combineVerifiedIbeShares(shares) {
|
|
259
|
+
if (shares.length === 0)
|
|
260
|
+
throw new Error('ibeCombineExtract: no shares');
|
|
261
|
+
const ids = shares.map(s => s.identifier);
|
|
262
|
+
if (new Set(ids).size !== ids.length) {
|
|
263
|
+
throw new Error('ibeCombineExtract: duplicate identifier');
|
|
264
|
+
}
|
|
219
265
|
const xs = ids.map(BigInt);
|
|
220
266
|
let skId = G1.ProjectivePoint.ZERO;
|
|
221
267
|
for (const s of shares) {
|
|
222
268
|
const lambda = lagrangeAtZero(BigInt(s.identifier), xs);
|
|
223
|
-
|
|
269
|
+
// λ is public (identifiers only): the variable-time ladder leaks nothing about it and skips the
|
|
270
|
+
// per-point wNAF table the constant-time multiply builds.
|
|
271
|
+
skId = skId.add(decodeG1(s.value, `share ${s.identifier}`).multiplyUnsafe(lambda));
|
|
224
272
|
}
|
|
225
273
|
return skId.toRawBytes(true);
|
|
226
274
|
}
|
package/dist/crypto/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { encryptEnvelope, toBytes, fromBytes, MAX_PLAINTEXT_LEN } from './envelope.js';
|
|
2
2
|
export type { GroupEnvelope } from './envelope.js';
|
|
3
|
-
export { decryptWithMasterKey, combineDecryptShares, verifyDecryptShare } from './kem.js';
|
|
3
|
+
export { decryptWithMasterKey, combineDecryptShares, verifyDecryptShare, verifyDecryptSharesBatch, decodeKemU } from './kem.js';
|
|
4
4
|
export type { DecryptShare, Ciphertext } from './kem.js';
|
|
5
5
|
export { ibeEncrypt, ibeVerifyShare, ibeCombineExtract, ibeCombineDecrypt, ibeDecryptWithKey, IBE_DOMAIN, IBE_HASH_DST, } from './ibe.js';
|
|
6
6
|
export type { IbeCiphertext, IbeDecryptionShare, IbeVerifyingShares } from './ibe.js';
|
package/dist/crypto/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Pure cryptographic primitives - NO network I/O. BLS KEM + envelope wire format,
|
|
2
2
|
// FROST-Ed25519 signature aggregation, [KK] envelope detection, hex helpers.
|
|
3
3
|
export { encryptEnvelope, toBytes, fromBytes, MAX_PLAINTEXT_LEN } from './envelope.js';
|
|
4
|
-
export { decryptWithMasterKey, combineDecryptShares, verifyDecryptShare } from './kem.js';
|
|
4
|
+
export { decryptWithMasterKey, combineDecryptShares, verifyDecryptShare, verifyDecryptSharesBatch, decodeKemU } from './kem.js';
|
|
5
5
|
// BF-IBE: real identity-bound encryption (pairing-level), not the KEM above.
|
|
6
6
|
export { ibeEncrypt, ibeVerifyShare, ibeCombineExtract, ibeCombineDecrypt, ibeDecryptWithKey, IBE_DOMAIN, IBE_HASH_DST, } from './ibe.js';
|
|
7
7
|
export { buildTasraText, parseTasraPost, isTasraPost } from './detect.js';
|
package/dist/crypto/kem.d.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
declare const G2: {
|
|
2
|
+
Point: import("@noble/curves/abstract/weierstrass").WeierstrassPointCons<import("@noble/curves/abstract/tower").Fp2>;
|
|
3
|
+
} & import("@noble/curves/abstract/hash-to-curve").H2CHasherBase<import("@noble/curves/abstract/tower").Fp2> & {
|
|
4
|
+
encodeToCurve: import("@noble/curves/abstract/hash-to-curve").H2CMethod<import("@noble/curves/abstract/tower").Fp2>;
|
|
5
|
+
mapToCurve: import("@noble/curves/abstract/hash-to-curve").MapMethod<import("@noble/curves/abstract/tower").Fp2>;
|
|
6
|
+
defaults: import("@noble/curves/abstract/hash-to-curve").H2COpts & {
|
|
7
|
+
encodeDST?: import("@noble/curves/abstract/hash-to-curve").UnicodeOrBytes;
|
|
8
|
+
};
|
|
9
|
+
} & import("@noble/curves/abstract/weierstrass").CurvePointsRes<import("@noble/curves/abstract/tower").Fp2>;
|
|
1
10
|
export declare function leToScalar(leBytes: Uint8Array): bigint;
|
|
2
11
|
export declare function scalarToLe(n: bigint): Uint8Array;
|
|
3
12
|
/** BLS group encryption ciphertext containing an ephemeral key, nonce and authenticated payload. */
|
|
@@ -42,6 +51,27 @@ export interface DecryptShare {
|
|
|
42
51
|
* @param u - 96-byte compressed ephemeral G2 key from the ciphertext.
|
|
43
52
|
*/
|
|
44
53
|
export declare function verifyDecryptShare(share: DecryptShare, u: Uint8Array): boolean;
|
|
54
|
+
type G2Point = InstanceType<typeof G2.ProjectivePoint>;
|
|
55
|
+
/**
|
|
56
|
+
* @internal Decode a ciphertext's U once for every share check of one operation. Undefined when
|
|
57
|
+
* it is malformed, which makes every {@link verifyDecryptShareAt} check fail, as before.
|
|
58
|
+
* @param u - 96-byte compressed ephemeral G2 key from the ciphertext.
|
|
59
|
+
*/
|
|
60
|
+
export declare function decodeKemU(u: Uint8Array): G2Point | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* @internal {@link verifyDecryptShare} with U already decoded by {@link decodeKemU}.
|
|
63
|
+
* @param share - Partial decryption and its public verifying share.
|
|
64
|
+
* @param U - Decoded ephemeral key, or undefined when it did not decode.
|
|
65
|
+
*/
|
|
66
|
+
export declare function verifyDecryptShareAt(share: DecryptShare, U: G2Point | undefined): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Verify many partial decryptions of ONE ciphertext with one multi-pairing. Returns, per share,
|
|
69
|
+
* exactly what {@link verifyDecryptShareAt} returns for it: a share that does not decode is
|
|
70
|
+
* false, and a failed combination re-checks each share individually.
|
|
71
|
+
* @param shares - Partial decryptions with their verifying shares.
|
|
72
|
+
* @param U - Ciphertext ephemeral key decoded by {@link decodeKemU}; undefined fails every share.
|
|
73
|
+
*/
|
|
74
|
+
export declare function verifyDecryptSharesBatch(shares: DecryptShare[], U: G2Point | undefined): boolean[];
|
|
45
75
|
/**
|
|
46
76
|
* Interpolate distinct partial decryptions and authenticate the plaintext. Enable verify to check each share before combining; the caller supplies a sufficient threshold.
|
|
47
77
|
*
|
|
@@ -53,3 +83,4 @@ export declare function verifyDecryptShare(share: DecryptShare, u: Uint8Array):
|
|
|
53
83
|
export declare function combineDecryptShares(shares: DecryptShare[], ct: Ciphertext, identity: Uint8Array, opts?: {
|
|
54
84
|
verify?: boolean;
|
|
55
85
|
}): Uint8Array;
|
|
86
|
+
export {};
|
package/dist/crypto/kem.js
CHANGED
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
import { chacha20poly1305 } from '@noble/ciphers/chacha';
|
|
22
22
|
import { bls12_381 } from '@noble/curves/bls12-381';
|
|
23
23
|
import { sha256 } from '@noble/hashes/sha256';
|
|
24
|
-
import { randomBytes } from '@noble/hashes/utils';
|
|
24
|
+
import { bytesToHex, randomBytes } from '@noble/hashes/utils';
|
|
25
|
+
import { BoundedCache } from '../internal/bounded.js';
|
|
25
26
|
import { KEM_DOMAIN } from './constants.js';
|
|
26
27
|
const { Fr } = bls12_381.fields;
|
|
27
28
|
const G1 = bls12_381.G1;
|
|
@@ -184,18 +185,121 @@ export function decryptWithMasterKey(mskBytes, ct, identity) {
|
|
|
184
185
|
* @param u - 96-byte compressed ephemeral G2 key from the ciphertext.
|
|
185
186
|
*/
|
|
186
187
|
export function verifyDecryptShare(share, u) {
|
|
188
|
+
return verifyDecryptShareAt(share, decodeKemU(u));
|
|
189
|
+
}
|
|
190
|
+
// Verifying shares are static per slot epoch; keep a bounded set of decoded public points.
|
|
191
|
+
const verifyingG1 = new BoundedCache(256);
|
|
192
|
+
// −g1, the fixed generator side of every share equation.
|
|
193
|
+
const NEG_G1 = G1.ProjectivePoint.BASE.negate();
|
|
194
|
+
/**
|
|
195
|
+
* @internal Decode a ciphertext's U once for every share check of one operation. Undefined when
|
|
196
|
+
* it is malformed, which makes every {@link verifyDecryptShareAt} check fail, as before.
|
|
197
|
+
* @param u - 96-byte compressed ephemeral G2 key from the ciphertext.
|
|
198
|
+
*/
|
|
199
|
+
export function decodeKemU(u) {
|
|
200
|
+
try {
|
|
201
|
+
return G2.ProjectivePoint.fromHex(u);
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* @internal {@link verifyDecryptShare} with U already decoded by {@link decodeKemU}.
|
|
209
|
+
* @param share - Partial decryption and its public verifying share.
|
|
210
|
+
* @param U - Decoded ephemeral key, or undefined when it did not decode.
|
|
211
|
+
*/
|
|
212
|
+
export function verifyDecryptShareAt(share, U) {
|
|
187
213
|
try {
|
|
188
214
|
if (!share.verifyingShare || share.verifyingShare.length !== 144)
|
|
189
215
|
return false;
|
|
190
|
-
const
|
|
191
|
-
const
|
|
216
|
+
const g1 = share.verifyingShare.subarray(96, 144);
|
|
217
|
+
const yiG1 = verifyingG1.getOrCompute(bytesToHex(g1), () => G1.ProjectivePoint.fromHex(g1));
|
|
218
|
+
if (!U)
|
|
219
|
+
return false;
|
|
192
220
|
const di = G2.ProjectivePoint.fromHex(share.decryptionShare);
|
|
193
|
-
|
|
221
|
+
// e(Yᵢ, U) == e(g1, Dᵢ) ⟺ e(Yᵢ, U) · e(−g1, Dᵢ) == 1, since e(−g1, Dᵢ) = e(g1, Dᵢ)⁻¹ by
|
|
222
|
+
// bilinearity. The SAME equation, exactly (no randomness): one Miller-loop product and ONE
|
|
223
|
+
// final exponentiation instead of two. Identity points throw in both forms (→ false).
|
|
224
|
+
return Fp12.eql(bls12_381.pairingBatch([{ g1: yiG1, g2: U }, { g1: NEG_G1, g2: di }]), Fp12.ONE);
|
|
194
225
|
}
|
|
195
226
|
catch {
|
|
196
227
|
return false;
|
|
197
228
|
}
|
|
198
229
|
}
|
|
230
|
+
// Batch verification (ADR-0090 D9): a random linear combination of the pairing equations,
|
|
231
|
+
// e(Σ rᵢ·Yᵢ, U) == e(g1, Σ rᵢ·Dᵢ), checked as one multi-pairing. A valid batch holds every
|
|
232
|
+
// share; a failed one falls back to the per-share check, so the verdicts are EXACTLY the
|
|
233
|
+
// per-share ones (an invalid share escapes the combination only with probability ≤ 2⁻¹²⁸,
|
|
234
|
+
// the rᵢ being independent uniform 128-bit scalars the sender cannot predict).
|
|
235
|
+
function randomBatchScalar() {
|
|
236
|
+
while (true) {
|
|
237
|
+
let n = 0n;
|
|
238
|
+
for (const b of randomBytes(16))
|
|
239
|
+
n = (n << 8n) | BigInt(b);
|
|
240
|
+
if (n !== 0n)
|
|
241
|
+
return n;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Verify many partial decryptions of ONE ciphertext with one multi-pairing. Returns, per share,
|
|
246
|
+
* exactly what {@link verifyDecryptShareAt} returns for it: a share that does not decode is
|
|
247
|
+
* false, and a failed combination re-checks each share individually.
|
|
248
|
+
* @param shares - Partial decryptions with their verifying shares.
|
|
249
|
+
* @param U - Ciphertext ephemeral key decoded by {@link decodeKemU}; undefined fails every share.
|
|
250
|
+
*/
|
|
251
|
+
export function verifyDecryptSharesBatch(shares, U) {
|
|
252
|
+
const verdict = shares.map(() => false);
|
|
253
|
+
if (!U || shares.length === 0)
|
|
254
|
+
return verdict;
|
|
255
|
+
const decoded = [];
|
|
256
|
+
shares.forEach((share, index) => {
|
|
257
|
+
try {
|
|
258
|
+
if (!share.verifyingShare || share.verifyingShare.length !== 144)
|
|
259
|
+
return;
|
|
260
|
+
const g1 = share.verifyingShare.subarray(96, 144);
|
|
261
|
+
const y = verifyingG1.getOrCompute(bytesToHex(g1), () => G1.ProjectivePoint.fromHex(g1));
|
|
262
|
+
decoded.push({ index, y, d: G2.ProjectivePoint.fromHex(share.decryptionShare) });
|
|
263
|
+
}
|
|
264
|
+
catch { /* undecodable: false, exactly as the per-share check */ }
|
|
265
|
+
});
|
|
266
|
+
if (decoded.length === 0)
|
|
267
|
+
return verdict;
|
|
268
|
+
if (decoded.length === 1) {
|
|
269
|
+
verdict[decoded[0].index] = verifyDecryptShareAt(shares[decoded[0].index], U);
|
|
270
|
+
return verdict;
|
|
271
|
+
}
|
|
272
|
+
let a = G1.ProjectivePoint.ZERO, b = G2.ProjectivePoint.ZERO;
|
|
273
|
+
for (const { y, d } of decoded) {
|
|
274
|
+
const r = randomBatchScalar();
|
|
275
|
+
a = a.add(y.multiply(r));
|
|
276
|
+
b = b.add(d.multiply(r));
|
|
277
|
+
}
|
|
278
|
+
let holds;
|
|
279
|
+
try {
|
|
280
|
+
// e(A, U) · e(-g1, B) == 1, one final exponentiation for the whole batch.
|
|
281
|
+
holds = Fp12.eql(bls12_381.pairingBatch([{ g1: a, g2: U }, { g1: NEG_G1, g2: b }]), Fp12.ONE);
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
holds = false;
|
|
285
|
+
}
|
|
286
|
+
if (holds)
|
|
287
|
+
for (const { index } of decoded)
|
|
288
|
+
verdict[index] = true;
|
|
289
|
+
else
|
|
290
|
+
for (const { index } of decoded)
|
|
291
|
+
verdict[index] = verifyDecryptShareAt(shares[index], U);
|
|
292
|
+
return verdict;
|
|
293
|
+
}
|
|
294
|
+
// Lagrange coefficients at 0 per signer set: a chat combines the same k keepers again and again.
|
|
295
|
+
const lagrangeSets = new BoundedCache(256);
|
|
296
|
+
function lagrangeFor(ids) {
|
|
297
|
+
const sorted = [...ids].sort((x, y) => x - y);
|
|
298
|
+
return lagrangeSets.getOrCompute(sorted.join(','), () => {
|
|
299
|
+
const xs = sorted.map(id => BigInt(id));
|
|
300
|
+
return new Map(sorted.map(id => [id, lagrangeAtZero(BigInt(id), xs)]));
|
|
301
|
+
});
|
|
302
|
+
}
|
|
199
303
|
// Combine k partial decryption shares into the plaintext. With `verify`, each
|
|
200
304
|
// share is pairing-checked first (identifiable abort) and a bad share throws
|
|
201
305
|
// naming its id. Mirrors the reference combine_decrypt.
|
|
@@ -223,13 +327,15 @@ export function combineDecryptShares(shares, ct, identity, opts = {}) {
|
|
|
223
327
|
}
|
|
224
328
|
}
|
|
225
329
|
}
|
|
226
|
-
const
|
|
330
|
+
const lambdas = lagrangeFor(shares.map(s => s.id));
|
|
227
331
|
let dPoint = G2.ProjectivePoint.ZERO;
|
|
228
332
|
for (const s of shares) {
|
|
229
333
|
const di = G2.ProjectivePoint.fromHex(s.decryptionShare);
|
|
230
|
-
const lambda =
|
|
334
|
+
const lambda = lambdas.get(s.id);
|
|
335
|
+
// λ is public (a function of the public identifiers only), so the variable-time ladder leaks
|
|
336
|
+
// nothing about it; the constant-time multiply built a full wNAF table for this one-off point.
|
|
231
337
|
if (lambda !== 0n)
|
|
232
|
-
dPoint = dPoint.add(di.
|
|
338
|
+
dPoint = dPoint.add(di.multiplyUnsafe(lambda));
|
|
233
339
|
}
|
|
234
340
|
const aeadKey = deriveKey(identity, dPoint.toRawBytes(true)); // compress(S)
|
|
235
341
|
try {
|