@reclaimprotocol/attestor-core 5.0.1-beta.7 → 5.0.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/browser/resources/attestor-browser.min.mjs +4512 -0
- package/lib/client/tunnels/make-rpc-tls-tunnel.d.ts +1 -1
- package/lib/external-rpc/index.js +10399 -3
- package/lib/index.js +8323 -10
- package/lib/server/utils/proxy-session.d.ts +1 -1
- package/lib/types/general.d.ts +0 -1
- package/lib/utils/generics.d.ts +1 -6
- package/lib/utils/index.d.ts +0 -1
- package/package.json +8 -7
- package/lib/avs/abis/avsDirectoryABI.js +0 -343
- package/lib/avs/abis/delegationABI.js +0 -4
- package/lib/avs/abis/registryABI.js +0 -728
- package/lib/avs/client/create-claim-on-avs.js +0 -168
- package/lib/avs/config.js +0 -26
- package/lib/avs/contracts/ReclaimServiceManager.js +0 -0
- package/lib/avs/contracts/common.js +0 -0
- package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +0 -1183
- package/lib/avs/contracts/factories/index.js +0 -4
- package/lib/avs/contracts/index.js +0 -6
- package/lib/avs/types/index.js +0 -0
- package/lib/avs/utils/contracts.js +0 -53
- package/lib/avs/utils/register.js +0 -74
- package/lib/avs/utils/tasks.js +0 -48
- package/lib/browser/avs/abis/avsDirectoryABI.d.ts +0 -60
- package/lib/browser/avs/abis/avsDirectoryABI.js +0 -343
- package/lib/browser/avs/abis/delegationABI.d.ts +0 -126
- package/lib/browser/avs/abis/delegationABI.js +0 -4
- package/lib/browser/avs/abis/registryABI.d.ts +0 -136
- package/lib/browser/avs/abis/registryABI.js +0 -728
- package/lib/browser/avs/client/create-claim-on-avs.d.ts +0 -12
- package/lib/browser/avs/client/create-claim-on-avs.js +0 -168
- package/lib/browser/avs/config.d.ts +0 -7
- package/lib/browser/avs/config.js +0 -26
- package/lib/browser/avs/contracts/ReclaimServiceManager.d.ts +0 -601
- package/lib/browser/avs/contracts/ReclaimServiceManager.js +0 -0
- package/lib/browser/avs/contracts/common.d.ts +0 -50
- package/lib/browser/avs/contracts/common.js +0 -0
- package/lib/browser/avs/contracts/factories/ReclaimServiceManager__factory.d.ts +0 -890
- package/lib/browser/avs/contracts/factories/ReclaimServiceManager__factory.js +0 -1183
- package/lib/browser/avs/contracts/factories/index.d.ts +0 -1
- package/lib/browser/avs/contracts/factories/index.js +0 -4
- package/lib/browser/avs/contracts/index.d.ts +0 -3
- package/lib/browser/avs/contracts/index.js +0 -6
- package/lib/browser/avs/types/index.d.ts +0 -55
- package/lib/browser/avs/types/index.js +0 -0
- package/lib/browser/avs/utils/contracts.d.ts +0 -21
- package/lib/browser/avs/utils/contracts.js +0 -53
- package/lib/browser/avs/utils/register.d.ts +0 -27
- package/lib/browser/avs/utils/register.js +0 -74
- package/lib/browser/avs/utils/tasks.d.ts +0 -22
- package/lib/browser/avs/utils/tasks.js +0 -48
- package/lib/browser/client/create-claim.d.ts +0 -5
- package/lib/browser/client/create-claim.js +0 -461
- package/lib/browser/client/index.d.ts +0 -3
- package/lib/browser/client/index.js +0 -3
- package/lib/browser/client/tunnels/make-rpc-tcp-tunnel.d.ts +0 -16
- package/lib/browser/client/tunnels/make-rpc-tcp-tunnel.js +0 -53
- package/lib/browser/client/tunnels/make-rpc-tls-tunnel.d.ts +0 -26
- package/lib/browser/client/tunnels/make-rpc-tls-tunnel.js +0 -127
- package/lib/browser/client/utils/attestor-pool.d.ts +0 -8
- package/lib/browser/client/utils/attestor-pool.js +0 -24
- package/lib/browser/client/utils/client-socket.d.ts +0 -11
- package/lib/browser/client/utils/client-socket.js +0 -120
- package/lib/browser/client/utils/message-handler.d.ts +0 -4
- package/lib/browser/client/utils/message-handler.js +0 -97
- package/lib/browser/config/index.d.ts +0 -31
- package/lib/browser/config/index.js +0 -62
- package/lib/browser/external-rpc/benchmark.d.ts +0 -1
- package/lib/browser/external-rpc/benchmark.js +0 -82
- package/lib/browser/external-rpc/event-bus.d.ts +0 -7
- package/lib/browser/external-rpc/event-bus.js +0 -17
- package/lib/browser/external-rpc/global.d.js +0 -0
- package/lib/browser/external-rpc/handle-incoming-msg.d.ts +0 -2
- package/lib/browser/external-rpc/handle-incoming-msg.js +0 -241
- package/lib/browser/external-rpc/index.d.ts +0 -3
- package/lib/browser/external-rpc/index.js +0 -3
- package/lib/browser/external-rpc/jsc-polyfills/1.d.ts +0 -14
- package/lib/browser/external-rpc/jsc-polyfills/1.js +0 -80
- package/lib/browser/external-rpc/jsc-polyfills/2.d.ts +0 -1
- package/lib/browser/external-rpc/jsc-polyfills/2.js +0 -15
- package/lib/browser/external-rpc/jsc-polyfills/event.d.ts +0 -10
- package/lib/browser/external-rpc/jsc-polyfills/event.js +0 -19
- package/lib/browser/external-rpc/jsc-polyfills/index.d.ts +0 -2
- package/lib/browser/external-rpc/jsc-polyfills/index.js +0 -2
- package/lib/browser/external-rpc/jsc-polyfills/ws.d.ts +0 -21
- package/lib/browser/external-rpc/jsc-polyfills/ws.js +0 -83
- package/lib/browser/external-rpc/setup-browser.d.ts +0 -6
- package/lib/browser/external-rpc/setup-browser.js +0 -33
- package/lib/browser/external-rpc/setup-jsc.d.ts +0 -24
- package/lib/browser/external-rpc/setup-jsc.js +0 -22
- package/lib/browser/external-rpc/types.d.ts +0 -213
- package/lib/browser/external-rpc/types.js +0 -0
- package/lib/browser/external-rpc/utils.d.ts +0 -20
- package/lib/browser/external-rpc/utils.js +0 -100
- package/lib/browser/external-rpc/zk.d.ts +0 -14
- package/lib/browser/external-rpc/zk.js +0 -58
- package/lib/browser/index.browser.js +0 -13
- package/lib/browser/index.d.ts +0 -9
- package/lib/browser/index.js +0 -13
- package/lib/browser/mechain/abis/governanceABI.d.ts +0 -50
- package/lib/browser/mechain/abis/governanceABI.js +0 -461
- package/lib/browser/mechain/abis/taskABI.d.ts +0 -157
- package/lib/browser/mechain/abis/taskABI.js +0 -512
- package/lib/browser/mechain/client/create-claim-on-mechain.d.ts +0 -10
- package/lib/browser/mechain/client/create-claim-on-mechain.js +0 -33
- package/lib/browser/mechain/client/index.d.ts +0 -1
- package/lib/browser/mechain/client/index.js +0 -1
- package/lib/browser/mechain/constants/index.d.ts +0 -3
- package/lib/browser/mechain/constants/index.js +0 -8
- package/lib/browser/mechain/index.d.ts +0 -2
- package/lib/browser/mechain/index.js +0 -2
- package/lib/browser/mechain/types/index.d.ts +0 -23
- package/lib/browser/mechain/types/index.js +0 -0
- package/lib/browser/proto/api.d.ts +0 -651
- package/lib/browser/proto/api.js +0 -4250
- package/lib/browser/proto/tee-bundle.d.ts +0 -156
- package/lib/browser/proto/tee-bundle.js +0 -1296
- package/lib/browser/providers/http/index.d.ts +0 -18
- package/lib/browser/providers/http/index.js +0 -640
- package/lib/browser/providers/http/patch-parse5-tree.d.ts +0 -6
- package/lib/browser/providers/http/patch-parse5-tree.js +0 -34
- package/lib/browser/providers/http/utils.d.ts +0 -77
- package/lib/browser/providers/http/utils.js +0 -283
- package/lib/browser/providers/index.d.ts +0 -4
- package/lib/browser/providers/index.js +0 -7
- package/lib/browser/types/bgp.d.ts +0 -11
- package/lib/browser/types/bgp.js +0 -0
- package/lib/browser/types/claims.d.ts +0 -70
- package/lib/browser/types/claims.js +0 -0
- package/lib/browser/types/client.d.ts +0 -163
- package/lib/browser/types/client.js +0 -0
- package/lib/browser/types/general.d.ts +0 -77
- package/lib/browser/types/general.js +0 -0
- package/lib/browser/types/handlers.d.ts +0 -10
- package/lib/browser/types/handlers.js +0 -0
- package/lib/browser/types/index.d.ts +0 -10
- package/lib/browser/types/index.js +0 -10
- package/lib/browser/types/providers.d.ts +0 -161
- package/lib/browser/types/providers.gen.d.ts +0 -443
- package/lib/browser/types/providers.gen.js +0 -16
- package/lib/browser/types/providers.js +0 -0
- package/lib/browser/types/rpc.d.ts +0 -35
- package/lib/browser/types/rpc.js +0 -0
- package/lib/browser/types/signatures.d.ts +0 -28
- package/lib/browser/types/signatures.js +0 -0
- package/lib/browser/types/tunnel.d.ts +0 -18
- package/lib/browser/types/tunnel.js +0 -0
- package/lib/browser/types/zk.d.ts +0 -38
- package/lib/browser/types/zk.js +0 -0
- package/lib/browser/utils/auth.d.ts +0 -8
- package/lib/browser/utils/auth.js +0 -71
- package/lib/browser/utils/b64-json.d.ts +0 -2
- package/lib/browser/utils/b64-json.js +0 -17
- package/lib/browser/utils/claims.d.ts +0 -33
- package/lib/browser/utils/claims.js +0 -89
- package/lib/browser/utils/env.d.ts +0 -3
- package/lib/browser/utils/env.js +0 -19
- package/lib/browser/utils/error.d.ts +0 -26
- package/lib/browser/utils/error.js +0 -54
- package/lib/browser/utils/generics.d.ts +0 -119
- package/lib/browser/utils/generics.js +0 -272
- package/lib/browser/utils/http-parser.d.ts +0 -59
- package/lib/browser/utils/http-parser.js +0 -201
- package/lib/browser/utils/index.browser.js +0 -13
- package/lib/browser/utils/index.d.ts +0 -13
- package/lib/browser/utils/index.js +0 -13
- package/lib/browser/utils/logger.browser.js +0 -88
- package/lib/browser/utils/logger.d.ts +0 -14
- package/lib/browser/utils/logger.js +0 -88
- package/lib/browser/utils/prepare-packets.d.ts +0 -16
- package/lib/browser/utils/prepare-packets.js +0 -69
- package/lib/browser/utils/redactions.d.ts +0 -73
- package/lib/browser/utils/redactions.js +0 -135
- package/lib/browser/utils/retries.d.ts +0 -12
- package/lib/browser/utils/retries.js +0 -26
- package/lib/browser/utils/signatures/eth.d.ts +0 -2
- package/lib/browser/utils/signatures/eth.js +0 -31
- package/lib/browser/utils/signatures/index.d.ts +0 -5
- package/lib/browser/utils/signatures/index.js +0 -12
- package/lib/browser/utils/socket-base.d.ts +0 -23
- package/lib/browser/utils/socket-base.js +0 -96
- package/lib/browser/utils/tls-imports.d.ts +0 -1
- package/lib/browser/utils/tls-imports.js +0 -34
- package/lib/browser/utils/tls.d.ts +0 -2
- package/lib/browser/utils/tls.js +0 -58
- package/lib/browser/utils/ws.d.ts +0 -7
- package/lib/browser/utils/ws.js +0 -22
- package/lib/browser/utils/zk.d.ts +0 -71
- package/lib/browser/utils/zk.js +0 -625
- package/lib/client/create-claim.js +0 -461
- package/lib/client/index.js +0 -3
- package/lib/client/tunnels/make-rpc-tcp-tunnel.js +0 -53
- package/lib/client/tunnels/make-rpc-tls-tunnel.js +0 -127
- package/lib/client/utils/attestor-pool.js +0 -24
- package/lib/client/utils/client-socket.js +0 -120
- package/lib/client/utils/message-handler.js +0 -97
- package/lib/config/index.js +0 -62
- package/lib/external-rpc/benchmark.js +0 -82
- package/lib/external-rpc/event-bus.js +0 -17
- package/lib/external-rpc/global.d.js +0 -0
- package/lib/external-rpc/handle-incoming-msg.js +0 -241
- package/lib/external-rpc/jsc-polyfills/1.js +0 -80
- package/lib/external-rpc/jsc-polyfills/2.js +0 -15
- package/lib/external-rpc/jsc-polyfills/event.js +0 -19
- package/lib/external-rpc/jsc-polyfills/index.js +0 -2
- package/lib/external-rpc/jsc-polyfills/ws.js +0 -83
- package/lib/external-rpc/setup-browser.js +0 -33
- package/lib/external-rpc/setup-jsc.js +0 -22
- package/lib/external-rpc/types.js +0 -0
- package/lib/external-rpc/utils.js +0 -100
- package/lib/external-rpc/zk.js +0 -58
- package/lib/index.browser.d.ts +0 -9
- package/lib/mechain/abis/governanceABI.js +0 -461
- package/lib/mechain/abis/taskABI.js +0 -512
- package/lib/mechain/client/create-claim-on-mechain.js +0 -33
- package/lib/mechain/client/index.js +0 -1
- package/lib/mechain/constants/index.js +0 -8
- package/lib/mechain/index.js +0 -2
- package/lib/mechain/types/index.js +0 -0
- package/lib/proto/api.js +0 -4250
- package/lib/proto/tee-bundle.js +0 -1296
- package/lib/providers/http/index.js +0 -640
- package/lib/providers/http/patch-parse5-tree.js +0 -34
- package/lib/providers/http/utils.js +0 -283
- package/lib/providers/index.js +0 -7
- package/lib/scripts/check-avs-registration.js +0 -28
- package/lib/scripts/fallbacks/crypto.js +0 -4
- package/lib/scripts/fallbacks/empty.js +0 -4
- package/lib/scripts/fallbacks/re2.js +0 -7
- package/lib/scripts/fallbacks/snarkjs.js +0 -10
- package/lib/scripts/fallbacks/stwo.js +0 -159
- package/lib/scripts/generate-provider-types.js +0 -101
- package/lib/scripts/generate-receipt.js +0 -101
- package/lib/scripts/generate-toprf-keys.js +0 -24
- package/lib/scripts/jsc-cli-rpc.js +0 -35
- package/lib/scripts/register-avs-operator.js +0 -3
- package/lib/scripts/start-server.js +0 -11
- package/lib/scripts/update-avs-metadata.js +0 -20
- package/lib/scripts/utils.js +0 -10
- package/lib/scripts/whitelist-operator.js +0 -16
- package/lib/server/create-server.js +0 -105
- package/lib/server/handlers/claimTeeBundle.js +0 -232
- package/lib/server/handlers/claimTunnel.js +0 -80
- package/lib/server/handlers/completeClaimOnChain.js +0 -29
- package/lib/server/handlers/createClaimOnChain.js +0 -32
- package/lib/server/handlers/createTaskOnMechain.js +0 -57
- package/lib/server/handlers/createTunnel.js +0 -98
- package/lib/server/handlers/disconnectTunnel.js +0 -8
- package/lib/server/handlers/fetchCertificateBytes.js +0 -57
- package/lib/server/handlers/index.js +0 -25
- package/lib/server/handlers/init.js +0 -33
- package/lib/server/handlers/toprf.js +0 -19
- package/lib/server/index.js +0 -4
- package/lib/server/socket.js +0 -112
- package/lib/server/tunnels/make-tcp-tunnel.js +0 -202
- package/lib/server/utils/apm.js +0 -29
- package/lib/server/utils/assert-valid-claim-request.js +0 -354
- package/lib/server/utils/config-env.js +0 -4
- package/lib/server/utils/dns.js +0 -24
- package/lib/server/utils/gcp-attestation.js +0 -237
- package/lib/server/utils/generics.js +0 -45
- package/lib/server/utils/iso.js +0 -259
- package/lib/server/utils/keep-alive.js +0 -38
- package/lib/server/utils/nitro-attestation.js +0 -249
- package/lib/server/utils/oprf-raw.js +0 -61
- package/lib/server/utils/process-handshake.js +0 -233
- package/lib/server/utils/proxy-session.js +0 -4
- package/lib/server/utils/tee-oprf-mpc-verification.js +0 -86
- package/lib/server/utils/tee-oprf-verification.js +0 -151
- package/lib/server/utils/tee-transcript-reconstruction.js +0 -140
- package/lib/server/utils/tee-verification.js +0 -358
- package/lib/server/utils/validation.js +0 -45
- package/lib/types/bgp.js +0 -0
- package/lib/types/claims.js +0 -0
- package/lib/types/client.js +0 -0
- package/lib/types/general.js +0 -0
- package/lib/types/handlers.js +0 -0
- package/lib/types/index.js +0 -10
- package/lib/types/providers.gen.js +0 -16
- package/lib/types/providers.js +0 -0
- package/lib/types/rpc.js +0 -0
- package/lib/types/signatures.js +0 -0
- package/lib/types/tunnel.js +0 -0
- package/lib/types/zk.js +0 -0
- package/lib/utils/auth.js +0 -71
- package/lib/utils/b64-json.js +0 -17
- package/lib/utils/bgp-listener.js +0 -123
- package/lib/utils/claims.js +0 -89
- package/lib/utils/env.js +0 -19
- package/lib/utils/error.js +0 -54
- package/lib/utils/generics.js +0 -272
- package/lib/utils/http-parser.js +0 -201
- package/lib/utils/index.browser.d.ts +0 -13
- package/lib/utils/index.js +0 -14
- package/lib/utils/logger.browser.d.ts +0 -14
- package/lib/utils/logger.js +0 -82
- package/lib/utils/prepare-packets.js +0 -69
- package/lib/utils/redactions.js +0 -135
- package/lib/utils/retries.js +0 -26
- package/lib/utils/signatures/eth.js +0 -31
- package/lib/utils/signatures/index.js +0 -12
- package/lib/utils/socket-base.js +0 -96
- package/lib/utils/tls-imports.d.ts +0 -1
- package/lib/utils/tls-imports.js +0 -34
- package/lib/utils/tls.js +0 -58
- package/lib/utils/ws.js +0 -22
- package/lib/utils/zk.js +0 -625
package/lib/utils/zk.js
DELETED
|
@@ -1,625 +0,0 @@
|
|
|
1
|
-
import { concatenateUint8Arrays, crypto, generateIV } from "../utils/tls-imports.js";
|
|
2
|
-
import {
|
|
3
|
-
ceilToBlockSizeMultiple,
|
|
4
|
-
CONFIG as ZK_CONFIG,
|
|
5
|
-
generateProof,
|
|
6
|
-
getBlockSizeBytes,
|
|
7
|
-
makeLocalFileFetch,
|
|
8
|
-
makeRemoteFileFetch,
|
|
9
|
-
verifyProof
|
|
10
|
-
} from "@reclaimprotocol/zk-symmetric-crypto";
|
|
11
|
-
import { makeGnarkOPRFOperator, makeGnarkZkOperator } from "@reclaimprotocol/zk-symmetric-crypto/gnark";
|
|
12
|
-
import { makeSnarkJsZKOperator } from "@reclaimprotocol/zk-symmetric-crypto/snarkjs";
|
|
13
|
-
import { makeStwoZkOperator } from "@reclaimprotocol/zk-symmetric-crypto/stwo";
|
|
14
|
-
import PQueue from "p-queue";
|
|
15
|
-
import {
|
|
16
|
-
DEFAULT_REMOTE_FILE_FETCH_BASE_URL,
|
|
17
|
-
DEFAULT_ZK_CONCURRENCY,
|
|
18
|
-
TOPRF_DOMAIN_SEPARATOR
|
|
19
|
-
} from "../config/index.js";
|
|
20
|
-
import { ZKProofEngine } from "../proto/api.js";
|
|
21
|
-
import { detectEnvironment, getEnvVariable } from "../utils/env.js";
|
|
22
|
-
import { AttestorError } from "../utils/error.js";
|
|
23
|
-
import {
|
|
24
|
-
getPureCiphertext,
|
|
25
|
-
getRecordIV,
|
|
26
|
-
getZkAlgorithmForCipherSuite,
|
|
27
|
-
isTls13Suite,
|
|
28
|
-
strToUint8Array
|
|
29
|
-
} from "../utils/generics.js";
|
|
30
|
-
import { logger as LOGGER } from "../utils/logger.js";
|
|
31
|
-
import { binaryHashToStr, isFullyRedacted, isRedactionCongruent, REDACTION_CHAR_CODE } from "../utils/redactions.js";
|
|
32
|
-
const ZK_CONCURRENCY = +(getEnvVariable("ZK_CONCURRENCY") || DEFAULT_ZK_CONCURRENCY);
|
|
33
|
-
async function makeZkProofGenerator({
|
|
34
|
-
zkOperators,
|
|
35
|
-
oprfOperators,
|
|
36
|
-
logger = LOGGER,
|
|
37
|
-
zkProofConcurrency = ZK_CONCURRENCY,
|
|
38
|
-
cipherSuite,
|
|
39
|
-
zkEngine = "snarkjs"
|
|
40
|
-
}) {
|
|
41
|
-
const zkQueue = new PQueue({ concurrency: zkProofConcurrency, autoStart: true });
|
|
42
|
-
const packetsToProve = [];
|
|
43
|
-
logger = logger.child({ module: "zk", zkEngine });
|
|
44
|
-
let zkProofsToGen = 0;
|
|
45
|
-
return {
|
|
46
|
-
/**
|
|
47
|
-
* Adds the given packet to the list of packets to
|
|
48
|
-
* generate ZK proofs for.
|
|
49
|
-
*
|
|
50
|
-
* Call `generateProofs()` to finally generate the proofs
|
|
51
|
-
*/
|
|
52
|
-
async addPacketToProve(packet, {
|
|
53
|
-
redactedPlaintext,
|
|
54
|
-
toprfs = [],
|
|
55
|
-
overshotToprfFromPrevBlock
|
|
56
|
-
}, onGeneratedProofs, getNextPacket) {
|
|
57
|
-
if (packet.type === "plaintext") {
|
|
58
|
-
throw new Error("Cannot generate proof for plaintext");
|
|
59
|
-
}
|
|
60
|
-
const alg = getZkAlgorithmForCipherSuite(cipherSuite);
|
|
61
|
-
const chunkSizeBytes = getChunkSizeBytes(alg);
|
|
62
|
-
const key = await crypto.exportKey(packet.encKey);
|
|
63
|
-
const iv = packet.iv;
|
|
64
|
-
const ciphertext = getPureCiphertext(packet.ciphertext, cipherSuite);
|
|
65
|
-
if (overshotToprfFromPrevBlock) {
|
|
66
|
-
redactedPlaintext.set(
|
|
67
|
-
new Uint8Array(overshotToprfFromPrevBlock.length).fill(REDACTION_CHAR_CODE)
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
const trueCiphertextLength = isTls13Suite(cipherSuite) ? ciphertext.length - 1 : ciphertext.length;
|
|
71
|
-
const packetToProve = {
|
|
72
|
-
onGeneratedProofs,
|
|
73
|
-
algorithm: alg,
|
|
74
|
-
proofsToGenerate: [],
|
|
75
|
-
toprfsToGenerate: [],
|
|
76
|
-
iv: packet.fixedIv
|
|
77
|
-
};
|
|
78
|
-
for (const toprf of toprfs) {
|
|
79
|
-
const toprfDistFromEnd = trueCiphertextLength - (toprf.dataLocation.fromIndex + toprf.dataLocation.length);
|
|
80
|
-
if (toprfDistFromEnd < 0) {
|
|
81
|
-
const nextPacket = getNextPacket();
|
|
82
|
-
if (nextPacket?.type !== "ciphertext") {
|
|
83
|
-
throw new AttestorError(
|
|
84
|
-
"ERROR_INTERNAL",
|
|
85
|
-
"TOPRF data overshoots ciphertext length, but no next ciphertext packet found"
|
|
86
|
-
);
|
|
87
|
-
}
|
|
88
|
-
if (nextPacket.encKey !== packet.encKey) {
|
|
89
|
-
throw new AttestorError(
|
|
90
|
-
"ERROR_INTERNAL",
|
|
91
|
-
"TOPRF data overshoots ciphertext length, but next packet has different encryption key"
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
const nextCiphertext = nextPacket.ciphertext.slice(0, Math.abs(toprfDistFromEnd));
|
|
95
|
-
const iv2 = nextPacket.iv;
|
|
96
|
-
toprf.overshoot = {
|
|
97
|
-
ciphertext: nextCiphertext,
|
|
98
|
-
iv: iv2,
|
|
99
|
-
recordNumber: nextPacket.recordNumber
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
const fromIndex = getIdealOffsetForToprfBlock(alg, toprf);
|
|
103
|
-
const toIndex = Math.min(fromIndex + chunkSizeBytes, ciphertext.length);
|
|
104
|
-
const slice = { fromIndex, toIndex };
|
|
105
|
-
packetToProve.toprfsToGenerate.push(getTOPRFProofGenerationParamsForSlice({
|
|
106
|
-
key,
|
|
107
|
-
iv,
|
|
108
|
-
ciphertext,
|
|
109
|
-
slice,
|
|
110
|
-
toprf: {
|
|
111
|
-
...toprf,
|
|
112
|
-
dataLocation: {
|
|
113
|
-
...toprf.dataLocation,
|
|
114
|
-
fromIndex: toprf.dataLocation.fromIndex - fromIndex
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}));
|
|
118
|
-
zkProofsToGen += 1;
|
|
119
|
-
const pktToIndex = Math.min(
|
|
120
|
-
trueCiphertextLength,
|
|
121
|
-
toprf.dataLocation.fromIndex + toprf.dataLocation.length
|
|
122
|
-
);
|
|
123
|
-
const pktFromIndex = toprf.dataLocation.fromIndex;
|
|
124
|
-
for (let i = pktFromIndex; i < pktToIndex; i++) {
|
|
125
|
-
redactedPlaintext[i] = REDACTION_CHAR_CODE;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
for (let i = 0; i < ciphertext.length; i += chunkSizeBytes) {
|
|
129
|
-
const slice = {
|
|
130
|
-
fromIndex: i,
|
|
131
|
-
toIndex: Math.min(i + chunkSizeBytes, ciphertext.length)
|
|
132
|
-
};
|
|
133
|
-
const proofParams = getProofGenerationParamsForSlice(
|
|
134
|
-
{ key, iv, ciphertext, redactedPlaintext, slice }
|
|
135
|
-
);
|
|
136
|
-
if (!proofParams) {
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
packetToProve.proofsToGenerate.push(proofParams);
|
|
140
|
-
zkProofsToGen += 1;
|
|
141
|
-
}
|
|
142
|
-
packetsToProve.push(packetToProve);
|
|
143
|
-
},
|
|
144
|
-
getTotalChunksToProve() {
|
|
145
|
-
return zkProofsToGen;
|
|
146
|
-
},
|
|
147
|
-
async generateProofs(onChunkDone) {
|
|
148
|
-
if (!packetsToProve.length) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
const start = Date.now();
|
|
152
|
-
const tasks = [];
|
|
153
|
-
for (const {
|
|
154
|
-
onGeneratedProofs,
|
|
155
|
-
algorithm,
|
|
156
|
-
proofsToGenerate,
|
|
157
|
-
toprfsToGenerate
|
|
158
|
-
} of packetsToProve) {
|
|
159
|
-
const proofs = [];
|
|
160
|
-
const toprfs = [];
|
|
161
|
-
let proofsLeft = proofsToGenerate.length + toprfsToGenerate.length;
|
|
162
|
-
for (const proofToGen of proofsToGenerate) {
|
|
163
|
-
tasks.push(zkQueue.add(async () => {
|
|
164
|
-
const proof = await generateZkProofForChunk(algorithm, proofToGen);
|
|
165
|
-
onChunkDone?.();
|
|
166
|
-
proofs.push(proof);
|
|
167
|
-
proofsLeft -= 1;
|
|
168
|
-
if (proofsLeft === 0) {
|
|
169
|
-
onGeneratedProofs(proofs, toprfs);
|
|
170
|
-
}
|
|
171
|
-
}, { throwOnTimeout: true }));
|
|
172
|
-
}
|
|
173
|
-
for (const toprfToGen of toprfsToGenerate) {
|
|
174
|
-
tasks.push(zkQueue.add(async () => {
|
|
175
|
-
const toprf = await generateOprfProofForChunk(algorithm, toprfToGen);
|
|
176
|
-
onChunkDone?.();
|
|
177
|
-
toprfs.push(toprf);
|
|
178
|
-
proofsLeft -= 1;
|
|
179
|
-
if (proofsLeft === 0) {
|
|
180
|
-
onGeneratedProofs(proofs, toprfs);
|
|
181
|
-
}
|
|
182
|
-
}, { throwOnTimeout: true }));
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
await Promise.all(tasks);
|
|
186
|
-
logger?.info(
|
|
187
|
-
{ durationMs: Date.now() - start, zkProofsToGen },
|
|
188
|
-
"generated ZK proofs"
|
|
189
|
-
);
|
|
190
|
-
packetsToProve.splice(0, packetsToProve.length);
|
|
191
|
-
zkProofsToGen = 0;
|
|
192
|
-
const alg = getZkAlgorithmForCipherSuite(cipherSuite);
|
|
193
|
-
const zkOperator = await getZkOperatorForAlgorithm(alg);
|
|
194
|
-
zkOperator.release?.();
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
async function generateZkProofForChunk(algorithm, {
|
|
198
|
-
startIdx,
|
|
199
|
-
redactedPlaintext,
|
|
200
|
-
privateInput,
|
|
201
|
-
publicInput
|
|
202
|
-
}) {
|
|
203
|
-
const operator = getZkOperatorForAlgorithm(algorithm);
|
|
204
|
-
const proof = await generateProof(
|
|
205
|
-
{ algorithm, privateInput, publicInput, operator, logger }
|
|
206
|
-
);
|
|
207
|
-
logger?.debug({ startIdx }, "generated proof for chunk");
|
|
208
|
-
return {
|
|
209
|
-
proofData: typeof proof.proofData === "string" ? strToUint8Array(proof.proofData) : proof.proofData,
|
|
210
|
-
decryptedRedactedCiphertext: proof.plaintext || new Uint8Array(),
|
|
211
|
-
redactedPlaintext,
|
|
212
|
-
startIdx
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
async function generateOprfProofForChunk(algorithm, { startIdx, privateInput, publicInput, toprf }) {
|
|
216
|
-
const operator = getOprfOperatorForAlgorithm(algorithm);
|
|
217
|
-
const toprfLocations = [];
|
|
218
|
-
if (toprf?.overshoot) {
|
|
219
|
-
const { dataLocation, overshoot: { ciphertext } } = toprf;
|
|
220
|
-
toprfLocations.push(
|
|
221
|
-
{
|
|
222
|
-
pos: dataLocation.fromIndex,
|
|
223
|
-
len: dataLocation.length - ciphertext.length
|
|
224
|
-
},
|
|
225
|
-
{
|
|
226
|
-
pos: ceilToBlockSizeMultiple(
|
|
227
|
-
dataLocation.fromIndex + dataLocation.length,
|
|
228
|
-
algorithm
|
|
229
|
-
),
|
|
230
|
-
len: ciphertext.length
|
|
231
|
-
}
|
|
232
|
-
);
|
|
233
|
-
} else if (toprf) {
|
|
234
|
-
toprfLocations.push({
|
|
235
|
-
pos: toprf.dataLocation.fromIndex,
|
|
236
|
-
len: toprf.dataLocation.length
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
const proof = await generateProof(
|
|
240
|
-
{
|
|
241
|
-
algorithm,
|
|
242
|
-
privateInput,
|
|
243
|
-
publicInput,
|
|
244
|
-
operator,
|
|
245
|
-
logger,
|
|
246
|
-
...toprf ? {
|
|
247
|
-
toprf: {
|
|
248
|
-
locations: toprfLocations,
|
|
249
|
-
output: toprf.nullifier,
|
|
250
|
-
responses: toprf.responses,
|
|
251
|
-
domainSeparator: TOPRF_DOMAIN_SEPARATOR
|
|
252
|
-
},
|
|
253
|
-
mask: toprf.mask
|
|
254
|
-
} : {}
|
|
255
|
-
}
|
|
256
|
-
);
|
|
257
|
-
logger?.debug({ toprfLocations }, "generated TOPRF proof for chunk");
|
|
258
|
-
return {
|
|
259
|
-
startIdx,
|
|
260
|
-
proofData: typeof proof.proofData === "string" ? strToUint8Array(proof.proofData) : proof.proofData,
|
|
261
|
-
payload: toprf
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
function getZkOperatorForAlgorithm(algorithm) {
|
|
265
|
-
return zkOperators?.[algorithm] || makeDefaultZkOperator(algorithm, zkEngine, logger);
|
|
266
|
-
}
|
|
267
|
-
function getOprfOperatorForAlgorithm(algorithm) {
|
|
268
|
-
return oprfOperators?.[algorithm] || makeDefaultOPRFOperator(algorithm, zkEngine, logger);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
async function verifyZkPacket({
|
|
272
|
-
cipherSuite,
|
|
273
|
-
ciphertext,
|
|
274
|
-
zkReveal,
|
|
275
|
-
zkOperators,
|
|
276
|
-
oprfOperators,
|
|
277
|
-
logger = LOGGER,
|
|
278
|
-
zkEngine = "snarkjs",
|
|
279
|
-
iv,
|
|
280
|
-
recordNumber,
|
|
281
|
-
toprfOvershotNullifier,
|
|
282
|
-
getNextPacket
|
|
283
|
-
}) {
|
|
284
|
-
const { proofs, toprfs, oprfRawMarkers } = zkReveal;
|
|
285
|
-
const algorithm = getZkAlgorithmForCipherSuite(cipherSuite);
|
|
286
|
-
const recordIV = getRecordIV(ciphertext, cipherSuite);
|
|
287
|
-
ciphertext = new Uint8Array(getPureCiphertext(ciphertext, cipherSuite));
|
|
288
|
-
const realRedactedPlaintext = new Uint8Array(ciphertext.length).fill(REDACTION_CHAR_CODE);
|
|
289
|
-
const replacements = await Promise.all(toprfs.map(async (toprf, i) => {
|
|
290
|
-
try {
|
|
291
|
-
return await verifyToprfProofPacket(toprf);
|
|
292
|
-
} catch (e) {
|
|
293
|
-
e.message += ` (TOPRF proof ${i}, from ${toprf.payload?.dataLocation?.fromIndex}, record ${recordNumber})`;
|
|
294
|
-
throw e;
|
|
295
|
-
}
|
|
296
|
-
}));
|
|
297
|
-
await Promise.all(proofs.map(async (proof, i) => {
|
|
298
|
-
try {
|
|
299
|
-
await verifyZkProofPacket(proof);
|
|
300
|
-
} catch (e) {
|
|
301
|
-
e.message += ` (ZK proof ${i}, startIdx ${proof.startIdx}, record ${recordNumber})`;
|
|
302
|
-
throw e;
|
|
303
|
-
}
|
|
304
|
-
}));
|
|
305
|
-
for (const { set, startIdx } of replacements) {
|
|
306
|
-
realRedactedPlaintext.set(set, startIdx);
|
|
307
|
-
}
|
|
308
|
-
if (toprfOvershotNullifier) {
|
|
309
|
-
realRedactedPlaintext.set(toprfOvershotNullifier);
|
|
310
|
-
}
|
|
311
|
-
return { redactedPlaintext: realRedactedPlaintext, oprfRawMarkers };
|
|
312
|
-
async function verifyZkProofPacket({
|
|
313
|
-
proofData,
|
|
314
|
-
decryptedRedactedCiphertext,
|
|
315
|
-
redactedPlaintext,
|
|
316
|
-
startIdx
|
|
317
|
-
}) {
|
|
318
|
-
const ciphertextChunkEnd = startIdx + redactedPlaintext.length;
|
|
319
|
-
const ciphertextChunk = ciphertext.slice(startIdx, ciphertextChunkEnd);
|
|
320
|
-
for (let i = 0; i < ciphertextChunk.length; i++) {
|
|
321
|
-
if (redactedPlaintext[i] === REDACTION_CHAR_CODE) {
|
|
322
|
-
ciphertextChunk[i] = REDACTION_CHAR_CODE;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
let nonce = concatenateUint8Arrays([iv, recordIV]);
|
|
326
|
-
if (!recordIV.length) {
|
|
327
|
-
nonce = generateIV(nonce, recordNumber);
|
|
328
|
-
}
|
|
329
|
-
const ciphertextInput = {
|
|
330
|
-
ciphertext: ciphertextChunk,
|
|
331
|
-
iv: nonce,
|
|
332
|
-
offsetBytes: startIdx
|
|
333
|
-
};
|
|
334
|
-
if (!isRedactionCongruent(redactedPlaintext, decryptedRedactedCiphertext)) {
|
|
335
|
-
throw new Error("redacted ciphertext not congruent");
|
|
336
|
-
}
|
|
337
|
-
await verifyProof(
|
|
338
|
-
{
|
|
339
|
-
proof: {
|
|
340
|
-
algorithm,
|
|
341
|
-
proofData,
|
|
342
|
-
plaintext: decryptedRedactedCiphertext
|
|
343
|
-
},
|
|
344
|
-
publicInput: ciphertextInput,
|
|
345
|
-
logger,
|
|
346
|
-
operator: getZkOperator()
|
|
347
|
-
}
|
|
348
|
-
);
|
|
349
|
-
logger?.debug(
|
|
350
|
-
{ startIdx, endIdx: startIdx + redactedPlaintext.length },
|
|
351
|
-
"verified proof"
|
|
352
|
-
);
|
|
353
|
-
realRedactedPlaintext.set(redactedPlaintext, startIdx);
|
|
354
|
-
}
|
|
355
|
-
async function verifyToprfProofPacket({ startIdx, proofData, payload: toprf }) {
|
|
356
|
-
if (!toprf?.dataLocation || !toprf.responses || !toprf.nullifier) {
|
|
357
|
-
throw new Error("invalid TOPRF proof payload");
|
|
358
|
-
}
|
|
359
|
-
const { dataLocation, nullifier } = toprf;
|
|
360
|
-
const ciphertextChunkEnd = Math.min(ciphertext.length, getChunkSizeBytes(algorithm) + startIdx);
|
|
361
|
-
const isLastChunk = ciphertextChunkEnd >= ciphertext.length;
|
|
362
|
-
const ciphertextChunk = ciphertext.slice(startIdx, ciphertextChunkEnd);
|
|
363
|
-
let nonce = concatenateUint8Arrays([iv, recordIV]);
|
|
364
|
-
if (!recordIV.length) {
|
|
365
|
-
nonce = generateIV(nonce, recordNumber);
|
|
366
|
-
}
|
|
367
|
-
const ciphertextInput = {
|
|
368
|
-
ciphertext: ciphertextChunk,
|
|
369
|
-
iv: nonce,
|
|
370
|
-
offsetBytes: startIdx
|
|
371
|
-
};
|
|
372
|
-
let pubInput = ciphertextInput;
|
|
373
|
-
const nulliferStr = binaryHashToStr(nullifier, dataLocation.length);
|
|
374
|
-
const locations = [];
|
|
375
|
-
const toprfEndIdx = dataLocation.fromIndex + dataLocation.length;
|
|
376
|
-
const trueCiphLen = isLastChunk && isTls13Suite(cipherSuite) ? ciphertextChunk.length - 1 : ciphertextChunk.length;
|
|
377
|
-
const overshoot = toprfEndIdx - trueCiphLen;
|
|
378
|
-
if (overshoot > 0) {
|
|
379
|
-
const nextPkt = getNextPacket(
|
|
380
|
-
strToUint8Array(nulliferStr.slice(dataLocation.length - overshoot))
|
|
381
|
-
);
|
|
382
|
-
if (!nextPkt) {
|
|
383
|
-
throw new Error("OPRF data overshot, but no next packet found");
|
|
384
|
-
}
|
|
385
|
-
const nextRecordIV = getRecordIV(ciphertext, cipherSuite);
|
|
386
|
-
let nextNonce = concatenateUint8Arrays([iv, nextRecordIV]);
|
|
387
|
-
if (!nextRecordIV.length) {
|
|
388
|
-
nextNonce = generateIV(nextNonce, recordNumber + 1);
|
|
389
|
-
}
|
|
390
|
-
pubInput = [
|
|
391
|
-
ciphertextInput,
|
|
392
|
-
{
|
|
393
|
-
ciphertext: nextPkt.slice(0, overshoot),
|
|
394
|
-
iv: nextNonce,
|
|
395
|
-
offsetBytes: 0
|
|
396
|
-
}
|
|
397
|
-
];
|
|
398
|
-
locations.push(
|
|
399
|
-
{
|
|
400
|
-
pos: dataLocation.fromIndex,
|
|
401
|
-
len: dataLocation.length - overshoot
|
|
402
|
-
},
|
|
403
|
-
{
|
|
404
|
-
pos: ceilToBlockSizeMultiple(
|
|
405
|
-
dataLocation.fromIndex + dataLocation.length,
|
|
406
|
-
algorithm
|
|
407
|
-
),
|
|
408
|
-
len: overshoot
|
|
409
|
-
}
|
|
410
|
-
);
|
|
411
|
-
} else {
|
|
412
|
-
locations.push({
|
|
413
|
-
pos: dataLocation.fromIndex,
|
|
414
|
-
len: dataLocation.length
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
await verifyProof(
|
|
418
|
-
{
|
|
419
|
-
proof: { algorithm, proofData, plaintext: void 0 },
|
|
420
|
-
publicInput: pubInput,
|
|
421
|
-
logger,
|
|
422
|
-
operator: getOprfOperator(),
|
|
423
|
-
toprf: {
|
|
424
|
-
locations,
|
|
425
|
-
domainSeparator: TOPRF_DOMAIN_SEPARATOR,
|
|
426
|
-
output: nullifier,
|
|
427
|
-
responses: toprf.responses
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
);
|
|
431
|
-
logger?.debug({ locations }, "verified TOPRF proof");
|
|
432
|
-
return {
|
|
433
|
-
set: strToUint8Array(
|
|
434
|
-
nulliferStr.slice(0, locations[0].len)
|
|
435
|
-
),
|
|
436
|
-
startIdx: locations[0].pos + startIdx
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
function getZkOperator() {
|
|
440
|
-
return zkOperators?.[algorithm] || makeDefaultZkOperator(algorithm, zkEngine, logger);
|
|
441
|
-
}
|
|
442
|
-
function getOprfOperator() {
|
|
443
|
-
return oprfOperators?.[algorithm] || makeDefaultOPRFOperator(algorithm, zkEngine, logger);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
function getChunkSizeBytes(alg) {
|
|
447
|
-
const { chunkSize, bitsPerWord } = ZK_CONFIG[alg];
|
|
448
|
-
return chunkSize * bitsPerWord / 8;
|
|
449
|
-
}
|
|
450
|
-
const zkEngines = {};
|
|
451
|
-
const oprfEngines = {};
|
|
452
|
-
const operatorMakers = {
|
|
453
|
-
"snarkjs": makeSnarkJsZKOperator,
|
|
454
|
-
"gnark": makeGnarkZkOperator,
|
|
455
|
-
"stwo": makeStwoZkOperator
|
|
456
|
-
};
|
|
457
|
-
const OPRF_OPERATOR_MAKERS = {
|
|
458
|
-
"gnark": makeGnarkOPRFOperator
|
|
459
|
-
};
|
|
460
|
-
function makeDefaultZkOperator(algorithm, zkEngine, logger) {
|
|
461
|
-
let zkOperators = zkEngines[zkEngine];
|
|
462
|
-
if (!zkOperators) {
|
|
463
|
-
zkEngines[zkEngine] = {};
|
|
464
|
-
zkOperators = zkEngines[zkEngine];
|
|
465
|
-
}
|
|
466
|
-
if (!zkOperators[algorithm]) {
|
|
467
|
-
const opType = getOperatorType();
|
|
468
|
-
const zkBaseUrl = opType === "remote" ? getZkResourcesBaseUrl() : void 0;
|
|
469
|
-
logger?.info({ type: opType, algorithm, zkBaseUrl }, "fetching zk operator");
|
|
470
|
-
const fetcher = opType === "local" ? makeLocalFileFetch() : makeRemoteFileFetch({ baseUrl: zkBaseUrl, logger });
|
|
471
|
-
const maker = operatorMakers[zkEngine];
|
|
472
|
-
if (!maker) {
|
|
473
|
-
throw new Error(`No ZK operator maker for ${zkEngine}`);
|
|
474
|
-
}
|
|
475
|
-
zkOperators[algorithm] = maker({ algorithm, fetcher });
|
|
476
|
-
}
|
|
477
|
-
return zkOperators[algorithm];
|
|
478
|
-
}
|
|
479
|
-
function getOperatorType() {
|
|
480
|
-
const envop = getEnvVariable("ZK_OPERATOR_TYPE");
|
|
481
|
-
if (envop === "local" || envop === "remote") {
|
|
482
|
-
return envop;
|
|
483
|
-
}
|
|
484
|
-
return detectEnvironment() === "node" ? "local" : "remote";
|
|
485
|
-
}
|
|
486
|
-
function makeDefaultOPRFOperator(algorithm, zkEngine, logger) {
|
|
487
|
-
let operators = oprfEngines[zkEngine];
|
|
488
|
-
if (!operators) {
|
|
489
|
-
oprfEngines[zkEngine] = {};
|
|
490
|
-
operators = oprfEngines[zkEngine];
|
|
491
|
-
}
|
|
492
|
-
if (!operators[algorithm]) {
|
|
493
|
-
const type = getOperatorType();
|
|
494
|
-
const zkBaseUrl = type === "remote" ? getZkResourcesBaseUrl() : void 0;
|
|
495
|
-
logger?.info({ type, algorithm, zkBaseUrl }, "fetching oprf operator");
|
|
496
|
-
const fetcher = type === "local" ? makeLocalFileFetch() : makeRemoteFileFetch({ baseUrl: zkBaseUrl, logger });
|
|
497
|
-
const maker = OPRF_OPERATOR_MAKERS[zkEngine];
|
|
498
|
-
if (!maker) {
|
|
499
|
-
throw new Error(`No OPRF operator maker for ${zkEngine}`);
|
|
500
|
-
}
|
|
501
|
-
operators[algorithm] = maker({ algorithm, fetcher });
|
|
502
|
-
}
|
|
503
|
-
return operators[algorithm];
|
|
504
|
-
}
|
|
505
|
-
function getEngineString(engine) {
|
|
506
|
-
if (engine === ZKProofEngine.ZK_ENGINE_GNARK) {
|
|
507
|
-
return "gnark";
|
|
508
|
-
}
|
|
509
|
-
if (engine === ZKProofEngine.ZK_ENGINE_SNARKJS) {
|
|
510
|
-
return "snarkjs";
|
|
511
|
-
}
|
|
512
|
-
if (engine === ZKProofEngine.ZK_ENGINE_STWO) {
|
|
513
|
-
return "stwo";
|
|
514
|
-
}
|
|
515
|
-
throw new Error(`Unknown ZK engine: ${engine}`);
|
|
516
|
-
}
|
|
517
|
-
function getEngineProto(engine) {
|
|
518
|
-
if (engine === "gnark") {
|
|
519
|
-
return ZKProofEngine.ZK_ENGINE_GNARK;
|
|
520
|
-
}
|
|
521
|
-
if (engine === "snarkjs") {
|
|
522
|
-
return ZKProofEngine.ZK_ENGINE_SNARKJS;
|
|
523
|
-
}
|
|
524
|
-
if (engine === "stwo") {
|
|
525
|
-
return ZKProofEngine.ZK_ENGINE_STWO;
|
|
526
|
-
}
|
|
527
|
-
throw new Error(`Unknown ZK engine: ${engine}`);
|
|
528
|
-
}
|
|
529
|
-
function getProofGenerationParamsForSlice({
|
|
530
|
-
key,
|
|
531
|
-
iv,
|
|
532
|
-
ciphertext,
|
|
533
|
-
redactedPlaintext,
|
|
534
|
-
slice: { fromIndex, toIndex }
|
|
535
|
-
}) {
|
|
536
|
-
const ciphertextChunk = ciphertext.slice(fromIndex, toIndex);
|
|
537
|
-
const plaintextChunk = redactedPlaintext.slice(fromIndex, toIndex);
|
|
538
|
-
if (isFullyRedacted(plaintextChunk)) {
|
|
539
|
-
return;
|
|
540
|
-
}
|
|
541
|
-
for (let i = 0; i < ciphertextChunk.length; i++) {
|
|
542
|
-
if (plaintextChunk[i] === REDACTION_CHAR_CODE) {
|
|
543
|
-
ciphertextChunk[i] = REDACTION_CHAR_CODE;
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
return {
|
|
547
|
-
startIdx: fromIndex,
|
|
548
|
-
redactedPlaintext: plaintextChunk,
|
|
549
|
-
privateInput: { key },
|
|
550
|
-
publicInput: { ciphertext: ciphertextChunk, iv, offsetBytes: fromIndex }
|
|
551
|
-
};
|
|
552
|
-
}
|
|
553
|
-
function getTOPRFProofGenerationParamsForSlice({
|
|
554
|
-
key,
|
|
555
|
-
iv,
|
|
556
|
-
ciphertext,
|
|
557
|
-
slice: { fromIndex, toIndex },
|
|
558
|
-
toprf
|
|
559
|
-
}) {
|
|
560
|
-
const ciphertextChunk = ciphertext.slice(fromIndex, toIndex);
|
|
561
|
-
if (toprf?.overshoot) {
|
|
562
|
-
const {
|
|
563
|
-
overshoot: { ciphertext: overshootCiphertext, iv: overshootIv }
|
|
564
|
-
} = toprf;
|
|
565
|
-
return {
|
|
566
|
-
privateInput: { key },
|
|
567
|
-
publicInput: [
|
|
568
|
-
{
|
|
569
|
-
ciphertext: ciphertextChunk,
|
|
570
|
-
iv,
|
|
571
|
-
offsetBytes: fromIndex
|
|
572
|
-
},
|
|
573
|
-
{ ciphertext: overshootCiphertext, iv: overshootIv }
|
|
574
|
-
],
|
|
575
|
-
toprf,
|
|
576
|
-
startIdx: fromIndex
|
|
577
|
-
};
|
|
578
|
-
}
|
|
579
|
-
return {
|
|
580
|
-
privateInput: { key },
|
|
581
|
-
publicInput: { ciphertext: ciphertextChunk, iv, offsetBytes: fromIndex },
|
|
582
|
-
toprf,
|
|
583
|
-
startIdx: fromIndex
|
|
584
|
-
};
|
|
585
|
-
}
|
|
586
|
-
function getIdealOffsetForToprfBlock(alg, { dataLocation, overshoot }) {
|
|
587
|
-
const chunkSizeBytes = getChunkSizeBytes(alg);
|
|
588
|
-
const blockSizeBytes = getBlockSizeBytes(alg);
|
|
589
|
-
const offsetChunks = Math.floor(dataLocation.fromIndex / chunkSizeBytes);
|
|
590
|
-
const endOffsetChunks = Math.floor((dataLocation.fromIndex + dataLocation.length) / chunkSizeBytes);
|
|
591
|
-
if (endOffsetChunks === offsetChunks) {
|
|
592
|
-
const start = offsetChunks * chunkSizeBytes;
|
|
593
|
-
if (overshoot) {
|
|
594
|
-
const overshootBlocks = Math.ceil(overshoot.ciphertext.length / blockSizeBytes);
|
|
595
|
-
return start + overshootBlocks * blockSizeBytes;
|
|
596
|
-
}
|
|
597
|
-
return start;
|
|
598
|
-
}
|
|
599
|
-
const offsetBytes = Math.floor(dataLocation.fromIndex / blockSizeBytes) * blockSizeBytes;
|
|
600
|
-
const endOffsetBytes = Math.ceil((dataLocation.fromIndex + dataLocation.length) / blockSizeBytes);
|
|
601
|
-
if (endOffsetBytes - offsetBytes > chunkSizeBytes) {
|
|
602
|
-
throw new AttestorError(
|
|
603
|
-
"ERROR_BAD_REQUEST",
|
|
604
|
-
"OPRF data cannot fit into a single chunk"
|
|
605
|
-
);
|
|
606
|
-
}
|
|
607
|
-
return offsetBytes;
|
|
608
|
-
}
|
|
609
|
-
function getZkResourcesBaseUrl() {
|
|
610
|
-
if (typeof ATTESTOR_BASE_URL !== "string") {
|
|
611
|
-
return DEFAULT_REMOTE_FILE_FETCH_BASE_URL;
|
|
612
|
-
}
|
|
613
|
-
return new URL(
|
|
614
|
-
DEFAULT_REMOTE_FILE_FETCH_BASE_URL,
|
|
615
|
-
ATTESTOR_BASE_URL
|
|
616
|
-
).toString();
|
|
617
|
-
}
|
|
618
|
-
export {
|
|
619
|
-
getEngineProto,
|
|
620
|
-
getEngineString,
|
|
621
|
-
makeDefaultOPRFOperator,
|
|
622
|
-
makeDefaultZkOperator,
|
|
623
|
-
makeZkProofGenerator,
|
|
624
|
-
verifyZkPacket
|
|
625
|
-
};
|