@reclaimprotocol/attestor-core 5.0.1-beta.1 → 5.0.1-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/lib/avs/abis/avsDirectoryABI.d.ts +60 -0
  2. package/lib/avs/abis/avsDirectoryABI.js +343 -0
  3. package/lib/avs/abis/delegationABI.d.ts +126 -0
  4. package/lib/avs/abis/delegationABI.js +4 -0
  5. package/lib/avs/abis/registryABI.d.ts +136 -0
  6. package/lib/avs/abis/registryABI.js +728 -0
  7. package/lib/avs/client/create-claim-on-avs.d.ts +12 -0
  8. package/lib/avs/client/create-claim-on-avs.js +168 -0
  9. package/lib/avs/config.d.ts +7 -0
  10. package/lib/avs/config.js +26 -0
  11. package/lib/avs/contracts/ReclaimServiceManager.d.ts +601 -0
  12. package/lib/avs/contracts/ReclaimServiceManager.js +0 -0
  13. package/lib/avs/contracts/common.d.ts +50 -0
  14. package/lib/avs/contracts/common.js +0 -0
  15. package/lib/avs/contracts/factories/ReclaimServiceManager__factory.d.ts +890 -0
  16. package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +1183 -0
  17. package/lib/avs/contracts/factories/index.d.ts +1 -0
  18. package/lib/avs/contracts/factories/index.js +4 -0
  19. package/lib/avs/contracts/index.d.ts +3 -0
  20. package/lib/avs/contracts/index.js +6 -0
  21. package/lib/avs/types/index.d.ts +55 -0
  22. package/lib/avs/types/index.js +0 -0
  23. package/lib/avs/utils/contracts.d.ts +21 -0
  24. package/lib/avs/utils/contracts.js +53 -0
  25. package/lib/avs/utils/register.d.ts +27 -0
  26. package/lib/avs/utils/register.js +74 -0
  27. package/lib/avs/utils/tasks.d.ts +22 -0
  28. package/lib/avs/utils/tasks.js +48 -0
  29. package/lib/client/create-claim.d.ts +5 -0
  30. package/lib/client/create-claim.js +461 -0
  31. package/lib/client/index.d.ts +3 -0
  32. package/lib/client/index.js +3 -0
  33. package/lib/client/tunnels/make-rpc-tcp-tunnel.d.ts +16 -0
  34. package/lib/client/tunnels/make-rpc-tcp-tunnel.js +53 -0
  35. package/lib/client/tunnels/make-rpc-tls-tunnel.d.ts +26 -0
  36. package/lib/client/tunnels/make-rpc-tls-tunnel.js +127 -0
  37. package/lib/client/utils/attestor-pool.d.ts +8 -0
  38. package/lib/client/utils/attestor-pool.js +24 -0
  39. package/lib/client/utils/client-socket.d.ts +11 -0
  40. package/lib/client/utils/client-socket.js +120 -0
  41. package/lib/client/utils/message-handler.d.ts +4 -0
  42. package/lib/client/utils/message-handler.js +97 -0
  43. package/lib/config/index.d.ts +31 -0
  44. package/lib/config/index.js +62 -0
  45. package/lib/external-rpc/benchmark.d.ts +1 -0
  46. package/lib/external-rpc/benchmark.js +82 -0
  47. package/lib/external-rpc/event-bus.d.ts +7 -0
  48. package/lib/external-rpc/event-bus.js +17 -0
  49. package/lib/external-rpc/global.d.js +0 -0
  50. package/lib/external-rpc/handle-incoming-msg.d.ts +2 -0
  51. package/lib/external-rpc/handle-incoming-msg.js +241 -0
  52. package/lib/external-rpc/index.d.ts +3 -0
  53. package/lib/external-rpc/index.js +3 -0
  54. package/lib/external-rpc/jsc-polyfills/1.d.ts +14 -0
  55. package/lib/external-rpc/jsc-polyfills/1.js +80 -0
  56. package/lib/external-rpc/jsc-polyfills/2.d.ts +1 -0
  57. package/lib/external-rpc/jsc-polyfills/2.js +15 -0
  58. package/lib/external-rpc/jsc-polyfills/event.d.ts +10 -0
  59. package/lib/external-rpc/jsc-polyfills/event.js +19 -0
  60. package/lib/external-rpc/jsc-polyfills/index.d.ts +2 -0
  61. package/lib/external-rpc/jsc-polyfills/index.js +2 -0
  62. package/lib/external-rpc/jsc-polyfills/ws.d.ts +21 -0
  63. package/lib/external-rpc/jsc-polyfills/ws.js +83 -0
  64. package/lib/external-rpc/setup-browser.d.ts +6 -0
  65. package/lib/external-rpc/setup-browser.js +33 -0
  66. package/lib/external-rpc/setup-jsc.d.ts +24 -0
  67. package/lib/external-rpc/setup-jsc.js +22 -0
  68. package/lib/external-rpc/types.d.ts +213 -0
  69. package/lib/external-rpc/types.js +0 -0
  70. package/lib/external-rpc/utils.d.ts +20 -0
  71. package/lib/external-rpc/utils.js +100 -0
  72. package/lib/external-rpc/zk.d.ts +14 -0
  73. package/lib/external-rpc/zk.js +58 -0
  74. package/lib/index.d.ts +9 -0
  75. package/lib/index.js +13 -0
  76. package/lib/mechain/abis/governanceABI.d.ts +50 -0
  77. package/lib/mechain/abis/governanceABI.js +461 -0
  78. package/lib/mechain/abis/taskABI.d.ts +157 -0
  79. package/lib/mechain/abis/taskABI.js +512 -0
  80. package/lib/mechain/client/create-claim-on-mechain.d.ts +10 -0
  81. package/lib/mechain/client/create-claim-on-mechain.js +33 -0
  82. package/lib/mechain/client/index.d.ts +1 -0
  83. package/lib/mechain/client/index.js +1 -0
  84. package/lib/mechain/constants/index.d.ts +3 -0
  85. package/lib/mechain/constants/index.js +8 -0
  86. package/lib/mechain/index.d.ts +2 -0
  87. package/lib/mechain/index.js +2 -0
  88. package/lib/mechain/types/index.d.ts +23 -0
  89. package/lib/mechain/types/index.js +0 -0
  90. package/lib/proto/api.d.ts +651 -0
  91. package/lib/proto/api.js +4250 -0
  92. package/lib/proto/tee-bundle.d.ts +156 -0
  93. package/lib/proto/tee-bundle.js +1296 -0
  94. package/lib/providers/http/index.d.ts +18 -0
  95. package/lib/providers/http/index.js +640 -0
  96. package/lib/providers/http/patch-parse5-tree.d.ts +6 -0
  97. package/lib/providers/http/patch-parse5-tree.js +34 -0
  98. package/lib/providers/http/utils.d.ts +77 -0
  99. package/lib/providers/http/utils.js +283 -0
  100. package/lib/providers/index.d.ts +4 -0
  101. package/lib/providers/index.js +7 -0
  102. package/lib/scripts/build-browser.d.ts +1 -0
  103. package/lib/scripts/build-jsc.d.ts +1 -0
  104. package/lib/scripts/build-lib.d.ts +1 -0
  105. package/lib/scripts/check-avs-registration.d.ts +1 -0
  106. package/lib/scripts/check-avs-registration.js +28 -0
  107. package/lib/scripts/fallbacks/crypto.d.ts +1 -0
  108. package/lib/scripts/fallbacks/crypto.js +4 -0
  109. package/lib/scripts/fallbacks/empty.d.ts +3 -0
  110. package/lib/scripts/fallbacks/empty.js +4 -0
  111. package/lib/scripts/fallbacks/re2.d.ts +1 -0
  112. package/lib/scripts/fallbacks/re2.js +7 -0
  113. package/lib/scripts/fallbacks/snarkjs.d.ts +1 -0
  114. package/lib/scripts/fallbacks/snarkjs.js +10 -0
  115. package/lib/scripts/fallbacks/stwo.d.ts +6 -0
  116. package/lib/scripts/fallbacks/stwo.js +159 -0
  117. package/lib/scripts/generate-provider-types.d.ts +5 -0
  118. package/lib/scripts/generate-provider-types.js +101 -0
  119. package/lib/scripts/generate-receipt.d.ts +9 -0
  120. package/lib/scripts/generate-receipt.js +101 -0
  121. package/lib/scripts/generate-toprf-keys.d.ts +1 -0
  122. package/lib/scripts/generate-toprf-keys.js +24 -0
  123. package/lib/scripts/jsc-cli-rpc.d.ts +1 -0
  124. package/lib/scripts/jsc-cli-rpc.js +35 -0
  125. package/lib/scripts/register-avs-operator.d.ts +1 -0
  126. package/lib/scripts/register-avs-operator.js +3 -0
  127. package/lib/scripts/start-server.d.ts +1 -0
  128. package/lib/scripts/start-server.js +11 -0
  129. package/lib/scripts/update-avs-metadata.d.ts +1 -0
  130. package/lib/scripts/update-avs-metadata.js +20 -0
  131. package/lib/scripts/utils.d.ts +1 -0
  132. package/lib/scripts/utils.js +10 -0
  133. package/lib/scripts/whitelist-operator.d.ts +1 -0
  134. package/lib/scripts/whitelist-operator.js +16 -0
  135. package/lib/server/create-server.d.ts +8 -0
  136. package/lib/server/create-server.js +105 -0
  137. package/lib/server/handlers/claimTeeBundle.d.ts +6 -0
  138. package/lib/server/handlers/claimTeeBundle.js +232 -0
  139. package/lib/server/handlers/claimTunnel.d.ts +2 -0
  140. package/lib/server/handlers/claimTunnel.js +80 -0
  141. package/lib/server/handlers/completeClaimOnChain.d.ts +2 -0
  142. package/lib/server/handlers/completeClaimOnChain.js +29 -0
  143. package/lib/server/handlers/createClaimOnChain.d.ts +2 -0
  144. package/lib/server/handlers/createClaimOnChain.js +32 -0
  145. package/lib/server/handlers/createTaskOnMechain.d.ts +2 -0
  146. package/lib/server/handlers/createTaskOnMechain.js +57 -0
  147. package/lib/server/handlers/createTunnel.d.ts +2 -0
  148. package/lib/server/handlers/createTunnel.js +98 -0
  149. package/lib/server/handlers/disconnectTunnel.d.ts +2 -0
  150. package/lib/server/handlers/disconnectTunnel.js +8 -0
  151. package/lib/server/handlers/fetchCertificateBytes.d.ts +2 -0
  152. package/lib/server/handlers/fetchCertificateBytes.js +57 -0
  153. package/lib/server/handlers/index.d.ts +4 -0
  154. package/lib/server/handlers/index.js +25 -0
  155. package/lib/server/handlers/init.d.ts +2 -0
  156. package/lib/server/handlers/init.js +33 -0
  157. package/lib/server/handlers/toprf.d.ts +2 -0
  158. package/lib/server/handlers/toprf.js +19 -0
  159. package/lib/server/index.d.ts +4 -0
  160. package/lib/server/index.js +4 -0
  161. package/lib/server/socket.d.ts +13 -0
  162. package/lib/server/socket.js +112 -0
  163. package/lib/server/tunnels/make-tcp-tunnel.d.ts +22 -0
  164. package/lib/server/tunnels/make-tcp-tunnel.js +202 -0
  165. package/lib/server/utils/apm.d.ts +11 -0
  166. package/lib/server/utils/apm.js +29 -0
  167. package/lib/server/utils/assert-valid-claim-request.d.ts +31 -0
  168. package/lib/server/utils/assert-valid-claim-request.js +354 -0
  169. package/lib/server/utils/config-env.d.ts +1 -0
  170. package/lib/server/utils/config-env.js +4 -0
  171. package/lib/server/utils/dns.d.ts +1 -0
  172. package/lib/server/utils/dns.js +24 -0
  173. package/lib/server/utils/gcp-attestation.d.ts +17 -0
  174. package/lib/server/utils/gcp-attestation.js +237 -0
  175. package/lib/server/utils/generics.d.ts +22 -0
  176. package/lib/server/utils/generics.js +45 -0
  177. package/lib/server/utils/iso.d.ts +1 -0
  178. package/lib/server/utils/iso.js +259 -0
  179. package/lib/server/utils/keep-alive.d.ts +7 -0
  180. package/lib/server/utils/keep-alive.js +38 -0
  181. package/lib/server/utils/nitro-attestation.d.ts +33 -0
  182. package/lib/server/utils/nitro-attestation.js +249 -0
  183. package/lib/server/utils/oprf-raw.d.ts +21 -0
  184. package/lib/server/utils/oprf-raw.js +61 -0
  185. package/lib/server/utils/process-handshake.d.ts +13 -0
  186. package/lib/server/utils/process-handshake.js +233 -0
  187. package/lib/server/utils/proxy-session.d.ts +1 -0
  188. package/lib/server/utils/proxy-session.js +6 -0
  189. package/lib/server/utils/tee-oprf-mpc-verification.d.ts +16 -0
  190. package/lib/server/utils/tee-oprf-mpc-verification.js +86 -0
  191. package/lib/server/utils/tee-oprf-verification.d.ts +24 -0
  192. package/lib/server/utils/tee-oprf-verification.js +151 -0
  193. package/lib/server/utils/tee-transcript-reconstruction.d.ts +24 -0
  194. package/lib/server/utils/tee-transcript-reconstruction.js +140 -0
  195. package/lib/server/utils/tee-verification.d.ts +28 -0
  196. package/lib/server/utils/tee-verification.js +358 -0
  197. package/lib/server/utils/validation.d.ts +2 -0
  198. package/lib/server/utils/validation.js +45 -0
  199. package/lib/types/bgp.d.ts +11 -0
  200. package/lib/types/bgp.js +0 -0
  201. package/lib/types/claims.d.ts +70 -0
  202. package/lib/types/claims.js +0 -0
  203. package/lib/types/client.d.ts +163 -0
  204. package/lib/types/client.js +0 -0
  205. package/lib/types/general.d.ts +76 -0
  206. package/lib/types/general.js +0 -0
  207. package/lib/types/handlers.d.ts +10 -0
  208. package/lib/types/handlers.js +0 -0
  209. package/lib/types/index.d.ts +10 -0
  210. package/lib/types/index.js +10 -0
  211. package/lib/types/providers.d.ts +161 -0
  212. package/lib/types/providers.gen.d.ts +443 -0
  213. package/lib/types/providers.gen.js +16 -0
  214. package/lib/types/providers.js +0 -0
  215. package/lib/types/rpc.d.ts +35 -0
  216. package/lib/types/rpc.js +0 -0
  217. package/lib/types/signatures.d.ts +28 -0
  218. package/lib/types/signatures.js +0 -0
  219. package/lib/types/tunnel.d.ts +18 -0
  220. package/lib/types/tunnel.js +0 -0
  221. package/lib/types/zk.d.ts +38 -0
  222. package/lib/types/zk.js +0 -0
  223. package/lib/utils/auth.d.ts +8 -0
  224. package/lib/utils/auth.js +71 -0
  225. package/lib/utils/b64-json.d.ts +2 -0
  226. package/lib/utils/b64-json.js +17 -0
  227. package/lib/utils/bgp-listener.d.ts +7 -0
  228. package/lib/utils/bgp-listener.js +123 -0
  229. package/lib/utils/claims.d.ts +33 -0
  230. package/lib/utils/claims.js +89 -0
  231. package/lib/utils/env.d.ts +3 -0
  232. package/lib/utils/env.js +19 -0
  233. package/lib/utils/error.d.ts +26 -0
  234. package/lib/utils/error.js +54 -0
  235. package/lib/utils/generics.d.ts +114 -0
  236. package/lib/utils/generics.js +268 -0
  237. package/lib/utils/http-parser.d.ts +59 -0
  238. package/lib/utils/http-parser.js +201 -0
  239. package/lib/utils/index.d.ts +13 -0
  240. package/lib/utils/index.js +13 -0
  241. package/lib/utils/logger.d.ts +13 -0
  242. package/lib/utils/logger.js +82 -0
  243. package/lib/utils/prepare-packets.d.ts +16 -0
  244. package/lib/utils/prepare-packets.js +69 -0
  245. package/lib/utils/redactions.d.ts +73 -0
  246. package/lib/utils/redactions.js +135 -0
  247. package/lib/utils/retries.d.ts +12 -0
  248. package/lib/utils/retries.js +26 -0
  249. package/lib/utils/signatures/eth.d.ts +2 -0
  250. package/lib/utils/signatures/eth.js +31 -0
  251. package/lib/utils/signatures/index.d.ts +5 -0
  252. package/lib/utils/signatures/index.js +12 -0
  253. package/lib/utils/socket-base.d.ts +23 -0
  254. package/lib/utils/socket-base.js +96 -0
  255. package/lib/utils/tls.d.ts +2 -0
  256. package/lib/utils/tls.js +58 -0
  257. package/lib/utils/ws.d.ts +7 -0
  258. package/lib/utils/ws.js +22 -0
  259. package/lib/utils/zk.d.ts +71 -0
  260. package/lib/utils/zk.js +625 -0
  261. package/package.json +2 -2
@@ -0,0 +1,625 @@
1
+ import { concatenateUint8Arrays, crypto, generateIV } from "@reclaimprotocol/tls";
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reclaimprotocol/attestor-core",
3
- "version": "5.0.1-beta.1",
3
+ "version": "5.0.1-beta.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "imports": {
@@ -17,7 +17,7 @@
17
17
  }
18
18
  },
19
19
  "scripts": {
20
- "build": "tsc -p tsconfig.build.json",
20
+ "build": "npm run run:tsc -- src/scripts/build-lib.ts && tsc -p tsconfig.build.json",
21
21
  "build:browser": "sh ./src/scripts/build-browser.sh",
22
22
  "run:tsc": "node --experimental-strip-types",
23
23
  "start": "npm run run:tsc -- src/scripts/start-server.ts",