@caatinga/zk 3.0.1 → 3.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caatinga/zk",
3
- "version": "3.0.1",
3
+ "version": "3.1.1",
4
4
  "description": "Zero-knowledge proof serialization bridge for Stellar/Soroban",
5
5
  "type": "module",
6
6
  "engines": {
@@ -27,7 +27,7 @@
27
27
  "LICENSE"
28
28
  ],
29
29
  "dependencies": {
30
- "@caatinga/core": "^3.0.1"
30
+ "@caatinga/core": "^3.1.1"
31
31
  },
32
32
  "devDependencies": {
33
33
  "tsup": "^8.3.5",
@@ -1,130 +0,0 @@
1
- // src/serialization/bigint-helpers.ts
2
- function decimalToLe32(decimal) {
3
- let value = BigInt(decimal);
4
- const out = new Uint8Array(32);
5
- for (let i = 0; i < 32; i++) {
6
- out[i] = Number(value & 0xffn);
7
- value >>= 8n;
8
- }
9
- return out;
10
- }
11
- function decimalToBe48(decimal) {
12
- let value = BigInt(decimal);
13
- const out = new Uint8Array(48);
14
- for (let i = 47; i >= 0; i--) {
15
- out[i] = Number(value & 0xffn);
16
- value >>= 8n;
17
- }
18
- return out;
19
- }
20
-
21
- // src/serialization/serialize-proof.ts
22
- function serializeProof(proof) {
23
- if (proof.curve !== "bls12381") {
24
- throw new Error(`Expected curve bls12381, got ${proof.curve}`);
25
- }
26
- return {
27
- a: {
28
- x: decimalToBe48(proof.pi_a[0]),
29
- y: decimalToBe48(proof.pi_a[1])
30
- },
31
- b: {
32
- x: [decimalToBe48(proof.pi_b[0][1]), decimalToBe48(proof.pi_b[0][0])],
33
- y: [decimalToBe48(proof.pi_b[1][1]), decimalToBe48(proof.pi_b[1][0])]
34
- },
35
- c: {
36
- x: decimalToBe48(proof.pi_c[0]),
37
- y: decimalToBe48(proof.pi_c[1])
38
- }
39
- };
40
- }
41
-
42
- // src/serialization/serialize-vk.ts
43
- function g2FromSnarkjsExport(rows) {
44
- return g2FromSnarkjs([rows[0], rows[1]]);
45
- }
46
- function g1FromSnarkjs(p) {
47
- return {
48
- x: decimalToBe48(p[0]),
49
- y: decimalToBe48(p[1])
50
- };
51
- }
52
- function g2FromSnarkjs(p) {
53
- return {
54
- x: [decimalToBe48(p[0][1]), decimalToBe48(p[0][0])],
55
- y: [decimalToBe48(p[1][1]), decimalToBe48(p[1][0])]
56
- };
57
- }
58
- function serializeVk(vk) {
59
- if (vk.curve !== "bls12381") {
60
- throw new Error(`Expected curve bls12381, got ${vk.curve}`);
61
- }
62
- const ic = vk.vk_ic ?? vk.IC;
63
- if (!ic) {
64
- throw new Error("Verification key is missing vk_ic/IC entries.");
65
- }
66
- return {
67
- alpha: g1FromSnarkjs(vk.vk_alpha_1),
68
- beta: g2FromSnarkjsExport(vk.vk_beta_2),
69
- gamma: g2FromSnarkjsExport(vk.vk_gamma_2),
70
- delta: g2FromSnarkjsExport(vk.vk_delta_2),
71
- ic: ic.map(g1FromSnarkjs)
72
- };
73
- }
74
-
75
- // src/serialization/serialize-public-signals.ts
76
- function serializePublicSignals(signals) {
77
- return signals.map((s) => decimalToLe32(s));
78
- }
79
-
80
- // src/serialization/curve-bytes.ts
81
- function concatG1Bytes(point) {
82
- const bytes = new Uint8Array(point.x.length + point.y.length);
83
- bytes.set(point.x, 0);
84
- bytes.set(point.y, point.x.length);
85
- return bytes;
86
- }
87
- function concatG2Bytes(point) {
88
- const bytes = new Uint8Array(
89
- point.x[0].length + point.x[1].length + point.y[0].length + point.y[1].length
90
- );
91
- let offset = 0;
92
- for (const chunk of [point.x[0], point.x[1], point.y[0], point.y[1]]) {
93
- bytes.set(chunk, offset);
94
- offset += chunk.length;
95
- }
96
- return bytes;
97
- }
98
- function concatG1Hex(point) {
99
- return bytesToHex(concatG1Bytes(point));
100
- }
101
- function concatG2Hex(point) {
102
- return bytesToHex(concatG2Bytes(point));
103
- }
104
- function bytesToHex(bytes) {
105
- return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
106
- }
107
-
108
- // src/errors/ZkError.ts
109
- var ZkError = class extends Error {
110
- constructor(message, code, hint) {
111
- super(message);
112
- this.code = code;
113
- this.hint = hint;
114
- this.name = "ZkError";
115
- }
116
- code;
117
- hint;
118
- };
119
-
120
- export {
121
- serializeProof,
122
- serializeVk,
123
- serializePublicSignals,
124
- concatG1Bytes,
125
- concatG2Bytes,
126
- concatG1Hex,
127
- concatG2Hex,
128
- ZkError
129
- };
130
- //# sourceMappingURL=chunk-TOEC4W3C.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/serialization/bigint-helpers.ts","../src/serialization/serialize-proof.ts","../src/serialization/serialize-vk.ts","../src/serialization/serialize-public-signals.ts","../src/serialization/curve-bytes.ts","../src/errors/ZkError.ts"],"sourcesContent":["/**\n * Convert a decimal string to a little-endian unsigned 32-byte array.\n */\nexport function decimalToLe32(decimal: string): Uint8Array {\n let value = BigInt(decimal);\n const out = new Uint8Array(32);\n for (let i = 0; i < 32; i++) {\n out[i] = Number(value & 0xffn);\n value >>= 8n;\n }\n return out;\n}\n\n/**\n * Convert a decimal string to a big-endian unsigned 48-byte array.\n */\nexport function decimalToBe48(decimal: string): Uint8Array {\n let value = BigInt(decimal);\n const out = new Uint8Array(48);\n for (let i = 47; i >= 0; i--) {\n out[i] = Number(value & 0xffn);\n value >>= 8n;\n }\n return out;\n}\n","import { decimalToBe48 } from \"./bigint-helpers.js\";\n\nexport type SerializedG1 = {\n x: Uint8Array;\n y: Uint8Array;\n};\n\nexport type SerializedG2 = {\n x: [Uint8Array, Uint8Array];\n y: [Uint8Array, Uint8Array];\n};\n\nexport type SerializedProof = {\n a: SerializedG1;\n b: SerializedG2;\n c: SerializedG1;\n};\n\nexport type SnarkjsProof = {\n pi_a: [string, string, string];\n pi_b: [[string, string], [string, string]];\n pi_c: [string, string, string];\n protocol: string;\n curve: string;\n};\n\nexport function serializeProof(proof: SnarkjsProof): SerializedProof {\n if (proof.curve !== \"bls12381\") {\n throw new Error(`Expected curve bls12381, got ${proof.curve}`);\n }\n\n return {\n a: {\n x: decimalToBe48(proof.pi_a[0]),\n y: decimalToBe48(proof.pi_a[1]),\n },\n b: {\n x: [decimalToBe48(proof.pi_b[0][1]), decimalToBe48(proof.pi_b[0][0])],\n y: [decimalToBe48(proof.pi_b[1][1]), decimalToBe48(proof.pi_b[1][0])],\n },\n c: {\n x: decimalToBe48(proof.pi_c[0]),\n y: decimalToBe48(proof.pi_c[1]),\n },\n };\n}\n","import { decimalToBe48 } from \"./bigint-helpers.js\";\nimport type { SerializedG1, SerializedG2 } from \"./serialize-proof.js\";\n\nexport type SerializedVk = {\n alpha: SerializedG1;\n beta: SerializedG2;\n gamma: SerializedG2;\n delta: SerializedG2;\n ic: SerializedG1[];\n};\n\nexport type SnarkjsVk = {\n protocol: string;\n curve: string;\n vk_alpha_1: [string, string, string];\n vk_beta_2: [[string, string], [string, string], ...string[][]];\n vk_gamma_2: [[string, string], [string, string], ...string[][]];\n vk_delta_2: [[string, string], [string, string], ...string[][]];\n vk_ic?: Array<[string, string, string]>;\n IC?: Array<[string, string, string]>;\n};\n\nfunction g2FromSnarkjsExport(rows: [[string, string], [string, string], ...string[][]]): SerializedG2 {\n return g2FromSnarkjs([rows[0]!, rows[1]!]);\n}\n\nfunction g1FromSnarkjs(p: [string, string, string]): SerializedG1 {\n return {\n x: decimalToBe48(p[0]),\n y: decimalToBe48(p[1]),\n };\n}\n\nfunction g2FromSnarkjs(p: [[string, string], [string, string]]): SerializedG2 {\n return {\n x: [decimalToBe48(p[0][1]), decimalToBe48(p[0][0])],\n y: [decimalToBe48(p[1][1]), decimalToBe48(p[1][0])],\n };\n}\n\nexport function serializeVk(vk: SnarkjsVk): SerializedVk {\n if (vk.curve !== \"bls12381\") {\n throw new Error(`Expected curve bls12381, got ${vk.curve}`);\n }\n\n const ic = vk.vk_ic ?? vk.IC;\n if (!ic) {\n throw new Error(\"Verification key is missing vk_ic/IC entries.\");\n }\n\n return {\n alpha: g1FromSnarkjs(vk.vk_alpha_1),\n beta: g2FromSnarkjsExport(vk.vk_beta_2),\n gamma: g2FromSnarkjsExport(vk.vk_gamma_2),\n delta: g2FromSnarkjsExport(vk.vk_delta_2),\n ic: ic.map(g1FromSnarkjs),\n };\n}\n","import { decimalToLe32 } from \"./bigint-helpers.js\";\n\nexport function serializePublicSignals(signals: string[]): Uint8Array[] {\n return signals.map((s) => decimalToLe32(s));\n}\n","import type { SerializedG1, SerializedG2 } from \"./serialize-proof.js\";\n\nexport function concatG1Bytes(point: SerializedG1): Uint8Array {\n const bytes = new Uint8Array(point.x.length + point.y.length);\n bytes.set(point.x, 0);\n bytes.set(point.y, point.x.length);\n return bytes;\n}\n\nexport function concatG2Bytes(point: SerializedG2): Uint8Array {\n const bytes = new Uint8Array(\n point.x[0].length + point.x[1].length + point.y[0].length + point.y[1].length\n );\n let offset = 0;\n for (const chunk of [point.x[0], point.x[1], point.y[0], point.y[1]]) {\n bytes.set(chunk, offset);\n offset += chunk.length;\n }\n return bytes;\n}\n\nexport function concatG1Hex(point: SerializedG1): string {\n return bytesToHex(concatG1Bytes(point));\n}\n\nexport function concatG2Hex(point: SerializedG2): string {\n return bytesToHex(concatG2Bytes(point));\n}\n\nfunction bytesToHex(bytes: Uint8Array): string {\n return Array.from(bytes, (byte) => byte.toString(16).padStart(2, \"0\")).join(\"\");\n}\n","export class ZkError extends Error {\n constructor(\n message: string,\n public readonly code: string,\n public readonly hint?: string\n ) {\n super(message);\n this.name = \"ZkError\";\n }\n}\n"],"mappings":";AAGO,SAAS,cAAc,SAA6B;AACzD,MAAI,QAAQ,OAAO,OAAO;AAC1B,QAAM,MAAM,IAAI,WAAW,EAAE;AAC7B,WAAS,IAAI,GAAG,IAAI,IAAI,KAAK;AAC3B,QAAI,CAAC,IAAI,OAAO,QAAQ,KAAK;AAC7B,cAAU;AAAA,EACZ;AACA,SAAO;AACT;AAKO,SAAS,cAAc,SAA6B;AACzD,MAAI,QAAQ,OAAO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