starkshield 1.0.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/README.md +101 -0
- package/bin/circom +0 -0
- package/bin/cli.js +36 -0
- package/bin/index.js +13 -0
- package/circomlib/.vscode/launch.json +63 -0
- package/circomlib/README.md +18 -0
- package/circomlib/circuits/README.md +830 -0
- package/circomlib/circuits/aliascheck.circom +33 -0
- package/circomlib/circuits/babyjub.circom +107 -0
- package/circomlib/circuits/binsub.circom +74 -0
- package/circomlib/circuits/binsum.circom +101 -0
- package/circomlib/circuits/bitify.circom +106 -0
- package/circomlib/circuits/comparators.circom +141 -0
- package/circomlib/circuits/compconstant.circom +74 -0
- package/circomlib/circuits/eddsa.circom +139 -0
- package/circomlib/circuits/eddsamimc.circom +124 -0
- package/circomlib/circuits/eddsamimcsponge.circom +124 -0
- package/circomlib/circuits/eddsaposeidon.circom +123 -0
- package/circomlib/circuits/escalarmul.circom +166 -0
- package/circomlib/circuits/escalarmulany.circom +197 -0
- package/circomlib/circuits/escalarmulfix.circom +299 -0
- package/circomlib/circuits/escalarmulw4table.circom +52 -0
- package/circomlib/circuits/gates.circom +96 -0
- package/circomlib/circuits/mimc.circom +156 -0
- package/circomlib/circuits/mimcsponge.circom +293 -0
- package/circomlib/circuits/montgomery.circom +142 -0
- package/circomlib/circuits/multiplexer.circom +115 -0
- package/circomlib/circuits/mux1.circom +48 -0
- package/circomlib/circuits/mux2.circom +63 -0
- package/circomlib/circuits/mux3.circom +75 -0
- package/circomlib/circuits/mux4.circom +119 -0
- package/circomlib/circuits/pedersen.circom +257 -0
- package/circomlib/circuits/pedersen_old.circom +68 -0
- package/circomlib/circuits/pointbits.circom +164 -0
- package/circomlib/circuits/poseidon.circom +208 -0
- package/circomlib/circuits/poseidon_constants.circom +24959 -0
- package/circomlib/circuits/poseidon_constants_old.circom +252 -0
- package/circomlib/circuits/poseidon_old.circom +97 -0
- package/circomlib/circuits/sha256/ch.circom +47 -0
- package/circomlib/circuits/sha256/constants.circom +53 -0
- package/circomlib/circuits/sha256/main.circom +35 -0
- package/circomlib/circuits/sha256/maj.circom +45 -0
- package/circomlib/circuits/sha256/rotate.circom +28 -0
- package/circomlib/circuits/sha256/sha256.circom +81 -0
- package/circomlib/circuits/sha256/sha256_2.circom +91 -0
- package/circomlib/circuits/sha256/sha256compression.circom +166 -0
- package/circomlib/circuits/sha256/sha256compression_function.circom +112 -0
- package/circomlib/circuits/sha256/shift.circom +33 -0
- package/circomlib/circuits/sha256/sigma.circom +77 -0
- package/circomlib/circuits/sha256/sigmaplus.circom +50 -0
- package/circomlib/circuits/sha256/t1.circom +58 -0
- package/circomlib/circuits/sha256/t2.circom +51 -0
- package/circomlib/circuits/sha256/xor3.circom +45 -0
- package/circomlib/circuits/sign.circom +36 -0
- package/circomlib/circuits/smt/smthash_mimc.circom +58 -0
- package/circomlib/circuits/smt/smthash_poseidon.circom +57 -0
- package/circomlib/circuits/smt/smtlevins.circom +103 -0
- package/circomlib/circuits/smt/smtprocessor.circom +261 -0
- package/circomlib/circuits/smt/smtprocessorlevel.circom +95 -0
- package/circomlib/circuits/smt/smtprocessorsm.circom +165 -0
- package/circomlib/circuits/smt/smtverifier.circom +138 -0
- package/circomlib/circuits/smt/smtverifierlevel.circom +71 -0
- package/circomlib/circuits/smt/smtverifiersm.circom +106 -0
- package/circomlib/circuits/switcher.circom +42 -0
- package/circomlib/doc/root_transfer.monopic +0 -0
- package/circomlib/doc/smt_diagram_0.monopic +0 -0
- package/circomlib/doc/smt_diagram_1.monopic +0 -0
- package/circomlib/doc/smt_hash.monopic +0 -0
- package/circomlib/doc/smt_levins.monopic +0 -0
- package/circomlib/doc/smt_sm.monopic +0 -0
- package/circomlib/doc/smt_verifier_sm.monopic +0 -0
- package/circomlib/doc/voting.monopic +0 -0
- package/circomlib/doc/window.monopic +0 -0
- package/circomlib/doc/window_chain.monopic +0 -0
- package/circomlib/index.js +2 -0
- package/circomlib/package.json +33 -0
- package/circomlib/test/aliascheck.js +77 -0
- package/circomlib/test/babyjub.js +118 -0
- package/circomlib/test/binsub.js +52 -0
- package/circomlib/test/binsum.js +38 -0
- package/circomlib/test/circuits/aliascheck_test.circom +4 -0
- package/circomlib/test/circuits/babyadd_tester.circom +4 -0
- package/circomlib/test/circuits/babycheck_test.circom +4 -0
- package/circomlib/test/circuits/babypbk_test.circom +4 -0
- package/circomlib/test/circuits/binsub_test.circom +33 -0
- package/circomlib/test/circuits/constants_test.circom +20 -0
- package/circomlib/test/circuits/eddsa_test.circom +5 -0
- package/circomlib/test/circuits/eddsamimc_test.circom +5 -0
- package/circomlib/test/circuits/eddsaposeidon_test.circom +5 -0
- package/circomlib/test/circuits/edwards2montgomery.circom +5 -0
- package/circomlib/test/circuits/escalarmul_min_test.circom +27 -0
- package/circomlib/test/circuits/escalarmul_test.circom +33 -0
- package/circomlib/test/circuits/escalarmul_test_min.circom +28 -0
- package/circomlib/test/circuits/escalarmulany_test.circom +30 -0
- package/circomlib/test/circuits/escalarmulfix_test.circom +31 -0
- package/circomlib/test/circuits/escalarmulw4table.circom +20 -0
- package/circomlib/test/circuits/escalarmulw4table_test.circom +19 -0
- package/circomlib/test/circuits/escalarmulw4table_test3.circom +19 -0
- package/circomlib/test/circuits/greatereqthan.circom +5 -0
- package/circomlib/test/circuits/greaterthan.circom +5 -0
- package/circomlib/test/circuits/isequal.circom +5 -0
- package/circomlib/test/circuits/iszero.circom +5 -0
- package/circomlib/test/circuits/lesseqthan.circom +5 -0
- package/circomlib/test/circuits/lessthan.circom +5 -0
- package/circomlib/test/circuits/mimc_sponge_hash_test.circom +5 -0
- package/circomlib/test/circuits/mimc_sponge_test.circom +5 -0
- package/circomlib/test/circuits/mimc_test.circom +5 -0
- package/circomlib/test/circuits/montgomery2edwards.circom +5 -0
- package/circomlib/test/circuits/montgomeryadd.circom +5 -0
- package/circomlib/test/circuits/montgomerydouble.circom +5 -0
- package/circomlib/test/circuits/mux1_1.circom +33 -0
- package/circomlib/test/circuits/mux2_1.circom +37 -0
- package/circomlib/test/circuits/mux3_1.circom +41 -0
- package/circomlib/test/circuits/mux4_1.circom +56 -0
- package/circomlib/test/circuits/pedersen2_test.circom +34 -0
- package/circomlib/test/circuits/pedersen_test.circom +31 -0
- package/circomlib/test/circuits/pointbits_loopback.circom +25 -0
- package/circomlib/test/circuits/poseidon3_test.circom +5 -0
- package/circomlib/test/circuits/poseidon6_test.circom +5 -0
- package/circomlib/test/circuits/poseidonex_test.circom +5 -0
- package/circomlib/test/circuits/sha256_2_test.circom +17 -0
- package/circomlib/test/circuits/sha256_test448.circom +5 -0
- package/circomlib/test/circuits/sha256_test512.circom +5 -0
- package/circomlib/test/circuits/sign_test.circom +5 -0
- package/circomlib/test/circuits/smtprocessor10_test.circom +5 -0
- package/circomlib/test/circuits/smtverifier10_test.circom +5 -0
- package/circomlib/test/circuits/sum_test.circom +33 -0
- package/circomlib/test/comparators.js +187 -0
- package/circomlib/test/eddsa.js +75 -0
- package/circomlib/test/eddsamimc.js +102 -0
- package/circomlib/test/eddsaposeidon.js +103 -0
- package/circomlib/test/escalarmul.js +121 -0
- package/circomlib/test/escalarmulany.js +51 -0
- package/circomlib/test/escalarmulfix.js +95 -0
- package/circomlib/test/helpers/printsignal.js +22 -0
- package/circomlib/test/helpers/sha256.js +178 -0
- package/circomlib/test/mimccircuit.js +27 -0
- package/circomlib/test/mimcspongecircuit.js +47 -0
- package/circomlib/test/montgomery.js +101 -0
- package/circomlib/test/multiplexer.js +101 -0
- package/circomlib/test/pedersen.js +83 -0
- package/circomlib/test/pedersen2.js +56 -0
- package/circomlib/test/point2bits.js +30 -0
- package/circomlib/test/poseidoncircuit.js +80 -0
- package/circomlib/test/sha256.js +118 -0
- package/circomlib/test/sign.js +82 -0
- package/circomlib/test/smtprocessor.js +219 -0
- package/circomlib/test/smtverifier.js +141 -0
- package/lib/compile.js +82 -0
- package/lib/deploy.js +391 -0
- package/lib/test.js +47 -0
- package/lib/verify.js +153 -0
- package/package.json +43 -0
- package/pot12_0000.ptau +0 -0
- package/pot12_0001.ptau +0 -0
- package/ptau/pot12_final.ptau +0 -0
package/lib/verify.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs-extra');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const snarkjs = require('snarkjs');
|
|
6
|
+
|
|
7
|
+
const SEPOLIA_RPC = 'https://rpc.starknet-testnet.lava.build';
|
|
8
|
+
|
|
9
|
+
function convertProof(proof, publicSignals, CurveId) {
|
|
10
|
+
const B = (s) => BigInt(s);
|
|
11
|
+
return {
|
|
12
|
+
a: { x: B(proof.pi_a[0]), y: B(proof.pi_a[1]), curveId: CurveId.BN254 },
|
|
13
|
+
b: {
|
|
14
|
+
x: [B(proof.pi_b[0][0]), B(proof.pi_b[0][1])],
|
|
15
|
+
y: [B(proof.pi_b[1][0]), B(proof.pi_b[1][1])],
|
|
16
|
+
curveId: CurveId.BN254,
|
|
17
|
+
},
|
|
18
|
+
c: { x: B(proof.pi_c[0]), y: B(proof.pi_c[1]), curveId: CurveId.BN254 },
|
|
19
|
+
publicInputs: publicSignals.map(B),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function convertVk(vk, CurveId) {
|
|
24
|
+
const B = (s) => BigInt(s);
|
|
25
|
+
return {
|
|
26
|
+
alpha: { x: B(vk.vk_alpha_1[0]), y: B(vk.vk_alpha_1[1]), curveId: CurveId.BN254 },
|
|
27
|
+
beta: {
|
|
28
|
+
x: [B(vk.vk_beta_2[0][0]), B(vk.vk_beta_2[0][1])],
|
|
29
|
+
y: [B(vk.vk_beta_2[1][0]), B(vk.vk_beta_2[1][1])],
|
|
30
|
+
curveId: CurveId.BN254,
|
|
31
|
+
},
|
|
32
|
+
gamma: {
|
|
33
|
+
x: [B(vk.vk_gamma_2[0][0]), B(vk.vk_gamma_2[0][1])],
|
|
34
|
+
y: [B(vk.vk_gamma_2[1][0]), B(vk.vk_gamma_2[1][1])],
|
|
35
|
+
curveId: CurveId.BN254,
|
|
36
|
+
},
|
|
37
|
+
delta: {
|
|
38
|
+
x: [B(vk.vk_delta_2[0][0]), B(vk.vk_delta_2[0][1])],
|
|
39
|
+
y: [B(vk.vk_delta_2[1][0]), B(vk.vk_delta_2[1][1])],
|
|
40
|
+
curveId: CurveId.BN254,
|
|
41
|
+
},
|
|
42
|
+
ic: vk.IC.map(p => ({ x: B(p[0]), y: B(p[1]), curveId: CurveId.BN254 })),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Generate a Groth16 ZK proof from the given inputs and verify it on-chain.
|
|
48
|
+
*
|
|
49
|
+
* Usage:
|
|
50
|
+
* const { verifyProof } = require("starkshield");
|
|
51
|
+
* const result = await verifyProof({ input: { a: 3, b: 11 } }, "./simple");
|
|
52
|
+
* console.log(result ? "✅ Valid proof" : "❌ Invalid proof");
|
|
53
|
+
*
|
|
54
|
+
* @param {{ input: Record<string, any> }} options
|
|
55
|
+
* @param {string} folderPath - Path to the circuit output folder (e.g. "./simple")
|
|
56
|
+
* @returns {Promise<boolean>}
|
|
57
|
+
*/
|
|
58
|
+
async function verifyProof({ input }, folderPath) {
|
|
59
|
+
|
|
60
|
+
// ── 1. Load deployment.json ─────────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
const absFolder = path.resolve(folderPath);
|
|
63
|
+
const deploymentPath = path.join(absFolder, 'deployment.json');
|
|
64
|
+
|
|
65
|
+
if (!fs.existsSync(deploymentPath)) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`deployment.json not found at: ${deploymentPath}\n` +
|
|
68
|
+
'Run "npx starkshield deploy <folder> <privateKey> <accountAddress>" first.',
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const { contractAddress, abi, paths, network } = fs.readJsonSync(deploymentPath);
|
|
73
|
+
|
|
74
|
+
if (!contractAddress) throw new Error('deployment.json missing contractAddress. Re-run deploy.');
|
|
75
|
+
if (!paths?.wasm || !paths?.zkey || !paths?.verificationKey) {
|
|
76
|
+
throw new Error('deployment.json missing paths. Re-run deploy.');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
for (const [key, filePath] of Object.entries(paths)) {
|
|
80
|
+
if (!fs.existsSync(filePath)) {
|
|
81
|
+
throw new Error(`Circuit artefact not found (${key}): ${filePath}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ── 2. Generate proof with snarkjs ──────────────────────────────────────────
|
|
86
|
+
|
|
87
|
+
console.log('🔐 Generating ZK proof ...');
|
|
88
|
+
|
|
89
|
+
const { proof: snarkjsProof, publicSignals } = await snarkjs.groth16.fullProve(
|
|
90
|
+
input,
|
|
91
|
+
paths.wasm,
|
|
92
|
+
paths.zkey,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
console.log('✅ Proof generated. Public signals:', publicSignals);
|
|
96
|
+
|
|
97
|
+
// ── 3. Build Garaga calldata ────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
console.log('🔄 Converting proof to Garaga calldata ...');
|
|
100
|
+
|
|
101
|
+
const { init, getGroth16CallData, CurveId } = require('garaga');
|
|
102
|
+
await init();
|
|
103
|
+
|
|
104
|
+
const rawVk = fs.readJsonSync(paths.verificationKey);
|
|
105
|
+
const proof = convertProof(snarkjsProof, publicSignals, CurveId);
|
|
106
|
+
const vk = convertVk(rawVk, CurveId);
|
|
107
|
+
|
|
108
|
+
// Garaga returns bigint[] where [0] is already the Span length prefix.
|
|
109
|
+
const calldataHex = getGroth16CallData(proof, vk, CurveId.BN254)
|
|
110
|
+
.map(v => '0x' + v.toString(16));
|
|
111
|
+
|
|
112
|
+
console.log(`✅ Calldata prepared (${calldataHex.length} felt252 values).`);
|
|
113
|
+
|
|
114
|
+
// ── 4. Call the verifier contract on Starknet ───────────────────────────────
|
|
115
|
+
//
|
|
116
|
+
// ABI shows the function is inside an interface, not at top level.
|
|
117
|
+
// We use RPC directly via provider.callContract to avoid starknet.js
|
|
118
|
+
// ABI parsing issues with nested interfaces and Result return types.
|
|
119
|
+
//
|
|
120
|
+
// The function returns Result<Span<u256>, felt252>:
|
|
121
|
+
// Ok → proof is valid (variant index 0)
|
|
122
|
+
// Err → proof is invalid (variant index 1)
|
|
123
|
+
|
|
124
|
+
console.log(`\n📡 Calling verify_groth16_proof_bn254 on ${network} ...`);
|
|
125
|
+
console.log(` Contract: ${contractAddress}`);
|
|
126
|
+
|
|
127
|
+
const { RpcProvider } = require('starknet');
|
|
128
|
+
const provider = new RpcProvider({ nodeUrl: SEPOLIA_RPC });
|
|
129
|
+
|
|
130
|
+
let response;
|
|
131
|
+
try {
|
|
132
|
+
response = await provider.callContract({
|
|
133
|
+
contractAddress,
|
|
134
|
+
entrypoint: 'verify_groth16_proof_bn254',
|
|
135
|
+
calldata: calldataHex,
|
|
136
|
+
});
|
|
137
|
+
} catch (err) {
|
|
138
|
+
throw new Error(`On-chain call failed: ${err.message}`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
console.log(' Raw response:', response);
|
|
142
|
+
|
|
143
|
+
// Result<Span<u256>, felt252> encoding:
|
|
144
|
+
// response[0] = 0x0 → Ok (proof valid)
|
|
145
|
+
// response[0] = 0x1 → Err (proof invalid)
|
|
146
|
+
const variant = BigInt(response[0]);
|
|
147
|
+
const verified = variant === 0n;
|
|
148
|
+
|
|
149
|
+
console.log(verified ? '\n✅ Proof is VALID on-chain!' : '\n❌ Proof is INVALID on-chain.');
|
|
150
|
+
return verified;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
module.exports = { verifyProof };
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "starkshield",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A zero-knowledge proof SDK for compiling Circom circuits and deploying Solidity verifiers to Starknet.",
|
|
5
|
+
"main": "./bin/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"starkshield": "./bin/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"zk",
|
|
14
|
+
"circom",
|
|
15
|
+
"snarkjs",
|
|
16
|
+
"starknet",
|
|
17
|
+
"solidity",
|
|
18
|
+
"zkp"
|
|
19
|
+
],
|
|
20
|
+
"author": "Jatin Sahijwani",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"type": "commonjs",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"circom": "^0.5.37",
|
|
25
|
+
"circomlib": "^2.0.5",
|
|
26
|
+
"commander": "^14.0.0",
|
|
27
|
+
"fs-extra": "^11.3.0",
|
|
28
|
+
"garaga": "^1.0.1",
|
|
29
|
+
"snarkjs": "^0.7.5",
|
|
30
|
+
"solc": "^0.8.29",
|
|
31
|
+
"starknet": "^6.24.1",
|
|
32
|
+
"web3": "^1.10.0"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"bin",
|
|
36
|
+
"lib",
|
|
37
|
+
"circomlib",
|
|
38
|
+
"ptau",
|
|
39
|
+
"pot12_0000.ptau",
|
|
40
|
+
"pot12_0001.ptau",
|
|
41
|
+
"README.md"
|
|
42
|
+
]
|
|
43
|
+
}
|
package/pot12_0000.ptau
ADDED
|
Binary file
|
package/pot12_0001.ptau
ADDED
|
Binary file
|
|
Binary file
|