@solarity/zkit 0.3.0-rc.1 → 0.3.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/README.md +42 -25
- package/dist/constants.d.ts +2 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +5 -0
- package/dist/constants.js.map +1 -0
- package/dist/core/CircuitZKit.d.ts.map +1 -1
- package/dist/core/CircuitZKit.js +2 -1
- package/dist/core/CircuitZKit.js.map +1 -1
- package/dist/core/protocols/Groth16Implementer.d.ts.map +1 -1
- package/dist/core/protocols/Groth16Implementer.js +7 -2
- package/dist/core/protocols/Groth16Implementer.js.map +1 -1
- package/dist/core/protocols/PlonkImplementer.d.ts.map +1 -1
- package/dist/core/protocols/PlonkImplementer.js +7 -2
- package/dist/core/protocols/PlonkImplementer.js.map +1 -1
- package/dist/core/templates/verifier_groth16.sol.ejs +3 -3
- package/dist/core/templates/verifier_groth16.vy.ejs +3 -0
- package/dist/core/templates/verifier_plonk.sol.ejs +5 -5
- package/dist/core/templates/verifier_plonk.vy.ejs +3 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +32 -0
- package/dist/utils.js.map +1 -0
- package/package.json +2 -2
- package/src/constants.ts +1 -0
- package/src/core/CircuitZKit.ts +3 -1
- package/src/core/protocols/Groth16Implementer.ts +12 -2
- package/src/core/protocols/PlonkImplementer.ts +12 -2
- package/src/core/templates/verifier_groth16.sol.ejs +3 -3
- package/src/core/templates/verifier_groth16.vy.ejs +3 -0
- package/src/core/templates/verifier_plonk.sol.ejs +5 -5
- package/src/core/templates/verifier_plonk.vy.ejs +3 -0
- package/src/utils.ts +6 -0
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
**A zero knowledge kit that helps you interact with Circom circuits.**
|
|
7
7
|
|
|
8
8
|
- Generate and verify ZK proofs with a single line of code.
|
|
9
|
+
- Leverage `groth16` and `plonk` proving systems.
|
|
9
10
|
- Render optimized Solidity | Vyper verifiers.
|
|
10
11
|
- Build and work with ZK witnesses.
|
|
11
12
|
|
|
@@ -26,76 +27,92 @@ npm install --save-dev @solarity/zkit
|
|
|
26
27
|
|
|
27
28
|
`CircuitZKit` is a user-friendly interface for interacting with circom circuits.
|
|
28
29
|
|
|
29
|
-
To create a CircuitZKit object it is necessary to pass a config:
|
|
30
|
+
To create a `CircuitZKit` object it is necessary to pass a circuit config and a `ProtocolImplementer` instance:
|
|
30
31
|
|
|
31
32
|
```typescript
|
|
32
|
-
|
|
33
|
+
const config = {
|
|
33
34
|
circuitName: string;
|
|
34
35
|
circuitArtifactsPath: string;
|
|
35
36
|
verifierDirPath: string;
|
|
36
|
-
provingSystem?: VerifierProvingSystem;
|
|
37
37
|
};
|
|
38
|
+
|
|
39
|
+
const implementer = new Groth16Implementer() | new PlonkImplementer();
|
|
40
|
+
|
|
41
|
+
const circuit = new CircuitZKit<"groth16" | "plonk">(config, implementer);
|
|
38
42
|
```
|
|
39
43
|
|
|
40
|
-
|
|
44
|
+
The `config` contains all the information required to work with the circuit, namely:
|
|
41
45
|
|
|
42
|
-
- `circuitName` - Name of the circuit file without extension
|
|
43
|
-
- `circuitArtifactsPath` - Full path to compilation artifacts for the desired circuit
|
|
44
|
-
- `verifierDirPath` - Full path to the directory where Solidity | Vyper verifier file will be generated
|
|
45
|
-
- `provingSystem` - The proving system that will be used to generate the verifier contract. Right now only `groth16` is supported
|
|
46
|
+
- `circuitName` - Name of the circuit file without extension.
|
|
47
|
+
- `circuitArtifactsPath` - Full path to compilation artifacts for the desired circuit.
|
|
48
|
+
- `verifierDirPath` - Full path to the directory where Solidity | Vyper verifier file will be generated.
|
|
46
49
|
|
|
47
|
-
|
|
50
|
+
The `implementer` is the instance of a certain proving system. Currently `groth16` and `plonk` systems are supported.
|
|
48
51
|
|
|
49
|
-
|
|
52
|
+
#### API reference
|
|
50
53
|
|
|
51
|
-
|
|
52
|
-
const templateContent = CircuitZKit.getTemplate("groth16", "sol");
|
|
53
|
-
```
|
|
54
|
+
---
|
|
54
55
|
|
|
55
|
-
|
|
56
|
+
- **`async createVerifier("sol" | "vy")`**
|
|
56
57
|
|
|
57
58
|
Creates a Solidity | Vyper verifier contract on `verifierDirPath` path, which was specified in the config.
|
|
58
59
|
|
|
59
60
|
```typescript
|
|
60
|
-
await
|
|
61
|
+
await circuit.createVerifier("sol");
|
|
61
62
|
```
|
|
62
63
|
|
|
63
|
-
|
|
64
|
+
- **`async calculateWitness(inputs) -> bigint[]`**
|
|
64
65
|
|
|
65
66
|
Calculates a witness in the `tmp` directory and returns its json representation.
|
|
66
67
|
|
|
67
68
|
```typescript
|
|
68
69
|
/// witness = [1n, 200n, 20n, 10n]
|
|
69
|
-
const witness = await
|
|
70
|
+
const witness = await circuit.calculateWitness({ a: 10, b: 20 });
|
|
70
71
|
```
|
|
71
72
|
|
|
72
|
-
|
|
73
|
+
- **`async generateProof(inputs) -> proof`**
|
|
73
74
|
|
|
74
75
|
Generates a proof for the given inputs.
|
|
75
76
|
|
|
76
77
|
```typescript
|
|
77
78
|
/// { proof: { pi_a, pi_b, pi_c, protocol, curve }, publicSignals: [6] }
|
|
78
|
-
const proof = await
|
|
79
|
+
const proof = await circuit.generateProof({ a: 2, b: 3 });
|
|
79
80
|
```
|
|
80
81
|
|
|
81
|
-
|
|
82
|
+
- **`async verifyProof(proof) -> bool`**
|
|
82
83
|
|
|
83
84
|
Verifies the proof.
|
|
84
85
|
|
|
85
86
|
```typescript
|
|
86
87
|
/// true
|
|
87
|
-
const isValidProof = await
|
|
88
|
+
const isValidProof = await circuit.verifyProof(proof);
|
|
88
89
|
```
|
|
89
90
|
|
|
90
|
-
|
|
91
|
+
- **`async generateCalldata(proof) -> calldata`**
|
|
91
92
|
|
|
92
93
|
Generates calldata by proof for the Solidity | Vyper verifier's `verifyProof()` method.
|
|
93
94
|
|
|
94
95
|
```typescript
|
|
95
96
|
/// You can use this calldata to call the verifier contract
|
|
96
|
-
const calldata = await
|
|
97
|
+
const calldata = await circuit.generateCalldata(proof);
|
|
97
98
|
```
|
|
98
99
|
|
|
99
|
-
|
|
100
|
+
- **`getCircuitName() -> string`**
|
|
101
|
+
|
|
102
|
+
Returns the name of the circuit from the config.
|
|
103
|
+
|
|
104
|
+
- **`getVerifierName() -> string`**
|
|
105
|
+
|
|
106
|
+
Returns the name of the verifier in the following form:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
<Circuit name><Proving system>Verifier
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- **`getProvingSystemType() -> "groth16" | "plonk"`**
|
|
113
|
+
|
|
114
|
+
Returns the current proving system in use.
|
|
115
|
+
|
|
116
|
+
- **`getVerifierTemplate() -> string`**
|
|
100
117
|
|
|
101
|
-
|
|
118
|
+
Returns the full `ejs` verifier template as a `string`.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,gBAAgB,GAAG,OAAO,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CircuitZKit.d.ts","sourceRoot":"","sources":["../../src/core/CircuitZKit.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AACnG,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAExH;;GAEG;AACH,qBAAa,WAAW,CAAC,IAAI,SAAS,iBAAiB;IAEnD,OAAO,CAAC,QAAQ,CAAC,OA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,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;IAIpG;;;;OAIG;IACI,cAAc,IAAI,MAAM;IAI/B;;;;OAIG;IACI,eAAe,IAAI,MAAM;IAIhC;;;;OAIG;IACI,oBAAoB,IAAI,iBAAiB;IAIhD;;;;OAIG;IACI,mBAAmB,CAAC,iBAAiB,EAAE,oBAAoB,GAAG,MAAM;IAI3E;;;;;OAKG;IACI,wBAAwB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM;IAUpE;;;;;OAKG;IACI,oBAAoB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM;CAgCjE"}
|
package/dist/core/CircuitZKit.js
CHANGED
|
@@ -63,7 +63,8 @@ class CircuitZKit {
|
|
|
63
63
|
const wtnsFile = path_1.default.join(tmpDir, `${this.getCircuitName()}.wtns`);
|
|
64
64
|
const wasmFile = this.mustGetArtifactsFilePath("wasm");
|
|
65
65
|
await snarkjs.wtns.calculate(inputs, wasmFile, wtnsFile);
|
|
66
|
-
|
|
66
|
+
const wtnsJson = await snarkjs.wtns.exportJson(wtnsFile);
|
|
67
|
+
return wtnsJson;
|
|
67
68
|
}
|
|
68
69
|
/**
|
|
69
70
|
* Generates a proof for the given inputs.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CircuitZKit.js","sourceRoot":"","sources":["../../src/core/CircuitZKit.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,uCAAyB;AACzB,iDAAmC;AAMnC;;GAEG;AACH,MAAa,WAAW;IAEH;IACA;IAFnB,YACmB,OAA0B,EAC1B,YAAwC;QADxC,YAAO,GAAP,OAAO,CAAmB;QAC1B,iBAAY,GAAZ,YAAY,CAA4B;IACxD,CAAC;IAEJ;;OAEG;IACI,KAAK,CAAC,cAAc,CAAC,iBAAuC;QAC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
|
|
|
1
|
-
{"version":3,"file":"Groth16Implementer.d.ts","sourceRoot":"","sources":["../../../src/core/protocols/Groth16Implementer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"Groth16Implementer.d.ts","sourceRoot":"","sources":["../../../src/core/protocols/Groth16Implementer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAI/F,qBAAa,kBAAmB,SAAQ,2BAA2B,CAAC,SAAS,CAAC;IAC/D,aAAa,CAAC,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAQvG,WAAW,CAAC,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAU9E,gBAAgB,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,eAAe,CAAC;IAM3E,oBAAoB,IAAI,iBAAiB;CAGjD"}
|
|
@@ -30,13 +30,18 @@ exports.Groth16Implementer = void 0;
|
|
|
30
30
|
const fs_1 = __importDefault(require("fs"));
|
|
31
31
|
const snarkjs = __importStar(require("snarkjs"));
|
|
32
32
|
const AbstractImplementer_1 = require("./AbstractImplementer");
|
|
33
|
+
const utils_1 = require("../../utils");
|
|
33
34
|
class Groth16Implementer extends AbstractImplementer_1.AbstractProtocolImplementer {
|
|
34
35
|
async generateProof(inputs, zKeyFilePath, wasmFilePath) {
|
|
35
|
-
|
|
36
|
+
const fullProof = await snarkjs.groth16.fullProve(inputs, wasmFilePath, zKeyFilePath);
|
|
37
|
+
await (0, utils_1.terminateCurve)();
|
|
38
|
+
return fullProof;
|
|
36
39
|
}
|
|
37
40
|
async verifyProof(proof, vKeyFilePath) {
|
|
38
41
|
const verifier = JSON.parse(fs_1.default.readFileSync(vKeyFilePath).toString());
|
|
39
|
-
|
|
42
|
+
const proofVerification = await snarkjs.groth16.verify(verifier, proof.publicSignals, proof.proof);
|
|
43
|
+
await (0, utils_1.terminateCurve)();
|
|
44
|
+
return proofVerification;
|
|
40
45
|
}
|
|
41
46
|
async generateCalldata(proof) {
|
|
42
47
|
const calldata = await snarkjs.groth16.exportSolidityCallData(proof.proof, proof.publicSignals);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Groth16Implementer.js","sourceRoot":"","sources":["../../../src/core/protocols/Groth16Implementer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,iDAAmC;AAEnC,+DAAoE;AAKpE,MAAa,kBAAmB,SAAQ,iDAAsC;IACrE,KAAK,CAAC,aAAa,CAAC,MAAe,EAAE,YAAoB,EAAE,YAAoB;QACpF,
|
|
1
|
+
{"version":3,"file":"Groth16Implementer.js","sourceRoot":"","sources":["../../../src/core/protocols/Groth16Implementer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,iDAAmC;AAEnC,+DAAoE;AAKpE,uCAA6C;AAE7C,MAAa,kBAAmB,SAAQ,iDAAsC;IACrE,KAAK,CAAC,aAAa,CAAC,MAAe,EAAE,YAAoB,EAAE,YAAoB;QACpF,MAAM,SAAS,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;QAEtF,MAAM,IAAA,sBAAc,GAAE,CAAC;QAEvB,OAAO,SAA+B,CAAC;IACzC,CAAC;IAEM,KAAK,CAAC,WAAW,CAAC,KAAyB,EAAE,YAAoB;QACtE,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,YAAE,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;QAEtE,MAAM,iBAAiB,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;QAEnG,MAAM,IAAA,sBAAc,GAAE,CAAC;QAEvB,OAAO,iBAAiB,CAAC;IAC3B,CAAC;IAEM,KAAK,CAAC,gBAAgB,CAAC,KAAyB;QACrD,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,sBAAsB,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC;QAEhG,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,QAAQ,GAAG,CAAoB,CAAC;IACxD,CAAC;IAEM,oBAAoB;QACzB,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AA5BD,gDA4BC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlonkImplementer.d.ts","sourceRoot":"","sources":["../../../src/core/protocols/PlonkImplementer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"PlonkImplementer.d.ts","sourceRoot":"","sources":["../../../src/core/protocols/PlonkImplementer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAI3F,qBAAa,gBAAiB,SAAQ,2BAA2B,CAAC,OAAO,CAAC;IAC3D,aAAa,CAAC,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAQrG,WAAW,CAAC,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAU5E,gBAAgB,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,aAAa,CAAC;IASvE,oBAAoB,IAAI,iBAAiB;CAGjD"}
|
|
@@ -30,13 +30,18 @@ exports.PlonkImplementer = void 0;
|
|
|
30
30
|
const fs_1 = __importDefault(require("fs"));
|
|
31
31
|
const snarkjs = __importStar(require("snarkjs"));
|
|
32
32
|
const AbstractImplementer_1 = require("./AbstractImplementer");
|
|
33
|
+
const utils_1 = require("../../utils");
|
|
33
34
|
class PlonkImplementer extends AbstractImplementer_1.AbstractProtocolImplementer {
|
|
34
35
|
async generateProof(inputs, zKeyFilePath, wasmFilePath) {
|
|
35
|
-
|
|
36
|
+
const fullProof = await snarkjs.plonk.fullProve(inputs, wasmFilePath, zKeyFilePath);
|
|
37
|
+
await (0, utils_1.terminateCurve)();
|
|
38
|
+
return fullProof;
|
|
36
39
|
}
|
|
37
40
|
async verifyProof(proof, vKeyFilePath) {
|
|
38
41
|
const verifier = JSON.parse(fs_1.default.readFileSync(vKeyFilePath).toString());
|
|
39
|
-
|
|
42
|
+
const proofVerification = await snarkjs.plonk.verify(verifier, proof.publicSignals, proof.proof);
|
|
43
|
+
await (0, utils_1.terminateCurve)();
|
|
44
|
+
return proofVerification;
|
|
40
45
|
}
|
|
41
46
|
async generateCalldata(proof) {
|
|
42
47
|
const calldata = await snarkjs.plonk.exportSolidityCallData(proof.proof, proof.publicSignals);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlonkImplementer.js","sourceRoot":"","sources":["../../../src/core/protocols/PlonkImplementer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,iDAAmC;AAEnC,+DAAoE;AAKpE,MAAa,gBAAiB,SAAQ,iDAAoC;IACjE,KAAK,CAAC,aAAa,CAAC,MAAe,EAAE,YAAoB,EAAE,YAAoB;QACpF,
|
|
1
|
+
{"version":3,"file":"PlonkImplementer.js","sourceRoot":"","sources":["../../../src/core/protocols/PlonkImplementer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,iDAAmC;AAEnC,+DAAoE;AAKpE,uCAA6C;AAE7C,MAAa,gBAAiB,SAAQ,iDAAoC;IACjE,KAAK,CAAC,aAAa,CAAC,MAAe,EAAE,YAAoB,EAAE,YAAoB;QACpF,MAAM,SAAS,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;QAEpF,MAAM,IAAA,sBAAc,GAAE,CAAC;QAEvB,OAAO,SAA6B,CAAC;IACvC,CAAC;IAEM,KAAK,CAAC,WAAW,CAAC,KAAuB,EAAE,YAAoB;QACpE,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,YAAE,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;QAEtE,MAAM,iBAAiB,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;QAEjG,MAAM,IAAA,sBAAc,GAAE,CAAC;QAEvB,OAAO,iBAAiB,CAAC;IAC3B,CAAC;IAEM,KAAK,CAAC,gBAAgB,CAAC,KAAuB;QACnD,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,sBAAsB,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC;QAC9F,MAAM,gBAAgB,GAAW,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAE3D,OAAO,IAAI,CAAC,KAAK,CACf,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,CAC/E,CAAC;IACrB,CAAC;IAEM,oBAAoB;QACzB,OAAO,OAAO,CAAC;IACjB,CAAC;CACF;AA/BD,4CA+BC"}
|
|
@@ -67,7 +67,7 @@ contract <%=verifier_id%> {
|
|
|
67
67
|
mstore(add(pointer_, 32), y_)
|
|
68
68
|
mstore(add(pointer_, 64), s_)
|
|
69
69
|
|
|
70
|
-
res_ := staticcall(
|
|
70
|
+
res_ := staticcall(6000, 7, pointer_, 96, pointer_, 64) // ecMul
|
|
71
71
|
res_ := and(res_, gt(returndatasize(), 0)) // check that multiplication succeeded
|
|
72
72
|
|
|
73
73
|
if iszero(res_) {
|
|
@@ -77,7 +77,7 @@ contract <%=verifier_id%> {
|
|
|
77
77
|
mstore(add(pointer_, 64), mload(pR_))
|
|
78
78
|
mstore(add(pointer_, 96), mload(add(pR_, 32)))
|
|
79
79
|
|
|
80
|
-
res_ := staticcall(
|
|
80
|
+
res_ := staticcall(150, 6, pointer_, 128, pR_, 64) // ecAdd
|
|
81
81
|
res_ := and(res_, gt(returndatasize(), 0)) // check that addition succeeded
|
|
82
82
|
}
|
|
83
83
|
|
|
@@ -136,7 +136,7 @@ contract <%=verifier_id%> {
|
|
|
136
136
|
mstore(add(pPairing_, 704), DELTA_Y1)
|
|
137
137
|
mstore(add(pPairing_, 736), DELTA_Y2)
|
|
138
138
|
|
|
139
|
-
res_ := staticcall(
|
|
139
|
+
res_ := staticcall(181000, 8, pPairing_, 768, pPairing_, 32) // ecPairing
|
|
140
140
|
res_ := and(res_, mload(pPairing_)) // check that pairing succeeded
|
|
141
141
|
}
|
|
142
142
|
|
|
@@ -53,6 +53,7 @@ def _g1MulAdd(pR: uint256[2], pP: uint256[2], s: uint256) -> (bool, uint256[2]):
|
|
|
53
53
|
success, response = raw_call(
|
|
54
54
|
EC_MUL_PRECOMPILED_ADDRESS,
|
|
55
55
|
abi_encode(pP, s),
|
|
56
|
+
gas=6000,
|
|
56
57
|
max_outsize=64,
|
|
57
58
|
is_static_call=True,
|
|
58
59
|
revert_on_failure=False
|
|
@@ -68,6 +69,7 @@ def _g1MulAdd(pR: uint256[2], pP: uint256[2], s: uint256) -> (bool, uint256[2]):
|
|
|
68
69
|
success, response = raw_call(
|
|
69
70
|
EC_ADD_PRECOMPILED_ADDRESS,
|
|
70
71
|
abi_encode(pR, pS),
|
|
72
|
+
gas=150,
|
|
71
73
|
max_outsize=64,
|
|
72
74
|
is_static_call=True,
|
|
73
75
|
revert_on_failure=False
|
|
@@ -107,6 +109,7 @@ def _checkPairing(pA: uint256[2], pB: uint256[2][2], pC: uint256[2], pubSignals:
|
|
|
107
109
|
pC,
|
|
108
110
|
DELTA_X1, DELTA_X2, DELTA_Y1, DELTA_Y2
|
|
109
111
|
),
|
|
112
|
+
gas=181000,
|
|
110
113
|
max_outsize=32,
|
|
111
114
|
is_static_call=True,
|
|
112
115
|
revert_on_failure=False
|
|
@@ -415,7 +415,7 @@ contract <%=verifier_id%> {
|
|
|
415
415
|
mstore(add(mIn,64), mload(pP_))
|
|
416
416
|
mstore(add(mIn,96), mload(add(pP_, 32)))
|
|
417
417
|
|
|
418
|
-
res_ := staticcall(
|
|
418
|
+
res_ := staticcall(150, 6, mIn, 128, pR_, 64)
|
|
419
419
|
}
|
|
420
420
|
|
|
421
421
|
function g1_mulAccC(pR_, x_, y_, s_) -> res_ {
|
|
@@ -424,7 +424,7 @@ contract <%=verifier_id%> {
|
|
|
424
424
|
mstore(add(mIn,32), y_)
|
|
425
425
|
mstore(add(mIn,64), s_)
|
|
426
426
|
|
|
427
|
-
res_ := staticcall(
|
|
427
|
+
res_ := staticcall(6000, 7, mIn, 96, mIn, 64)
|
|
428
428
|
|
|
429
429
|
if iszero(res_) {
|
|
430
430
|
leave
|
|
@@ -433,7 +433,7 @@ contract <%=verifier_id%> {
|
|
|
433
433
|
mstore(add(mIn,64), mload(pR_))
|
|
434
434
|
mstore(add(mIn,96), mload(add(pR_, 32)))
|
|
435
435
|
|
|
436
|
-
res_ := staticcall(
|
|
436
|
+
res_ := staticcall(150, 6, mIn, 128, pR_, 64)
|
|
437
437
|
}
|
|
438
438
|
|
|
439
439
|
function g1_mulSetC(pR_, x_, y_, s_) -> res_ {
|
|
@@ -442,7 +442,7 @@ contract <%=verifier_id%> {
|
|
|
442
442
|
mstore(add(mIn,32), y_)
|
|
443
443
|
mstore(add(mIn,64), s_)
|
|
444
444
|
|
|
445
|
-
res_ := staticcall(
|
|
445
|
+
res_ := staticcall(6000, 7, mIn, 96, pR_, 64)
|
|
446
446
|
}
|
|
447
447
|
|
|
448
448
|
function g1_mulSet(pR_, pP_, s_) -> res_ {
|
|
@@ -748,7 +748,7 @@ contract <%=verifier_id%> {
|
|
|
748
748
|
mstore(add(mIn,320), G2_Y2)
|
|
749
749
|
mstore(add(mIn,352), G2_Y1)
|
|
750
750
|
|
|
751
|
-
if iszero(staticcall(
|
|
751
|
+
if iszero(staticcall(113000, 8, mIn, 384, mIn, 0x20)) {
|
|
752
752
|
leave
|
|
753
753
|
}
|
|
754
754
|
|
|
@@ -149,6 +149,7 @@ def _ecadd(a: uint256[2], b: uint256[2]) -> (bool, uint256[2]):
|
|
|
149
149
|
success, response = raw_call(
|
|
150
150
|
EC_ADD_PRECOMPILED_ADDRESS,
|
|
151
151
|
abi_encode(a, b),
|
|
152
|
+
gas=150,
|
|
152
153
|
max_outsize=64,
|
|
153
154
|
is_static_call=True,
|
|
154
155
|
revert_on_failure=False
|
|
@@ -171,6 +172,7 @@ def _ecmul(p: uint256[2], s: uint256) -> (bool, uint256[2]):
|
|
|
171
172
|
success, response = raw_call(
|
|
172
173
|
EC_MUL_PRECOMPILED_ADDRESS,
|
|
173
174
|
abi_encode(p, s),
|
|
175
|
+
gas=6000,
|
|
174
176
|
max_outsize=64,
|
|
175
177
|
is_static_call=True,
|
|
176
178
|
revert_on_failure=False
|
|
@@ -639,6 +641,7 @@ def _checkPairing(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> bool:
|
|
|
639
641
|
success, response = raw_call(
|
|
640
642
|
EC_PAIRING_PRECOMPILED_ADDRESS,
|
|
641
643
|
abi_encode(mIn),
|
|
644
|
+
gas=113000,
|
|
642
645
|
max_outsize=32,
|
|
643
646
|
is_static_call=True,
|
|
644
647
|
revert_on_failure=False
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,wBAAsB,cAAc,kBAEnC"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.terminateCurve = terminateCurve;
|
|
27
|
+
const snarkjs = __importStar(require("snarkjs"));
|
|
28
|
+
const constants_1 = require("./constants");
|
|
29
|
+
async function terminateCurve() {
|
|
30
|
+
await (await snarkjs.curves.getCurveFromName(constants_1.BN128_CURVE_NAME)).terminate();
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAGA,wCAEC;AALD,iDAAmC;AACnC,2CAA+C;AAExC,KAAK,UAAU,cAAc;IAClC,MAAM,CAAC,MAAO,OAAe,CAAC,MAAM,CAAC,gBAAgB,CAAC,4BAAgB,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;AACvF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solarity/zkit",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Distributed Lab",
|
|
6
6
|
"readme": "README.md",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"ejs": "3.1.10",
|
|
42
|
-
"snarkjs": "0.7.
|
|
42
|
+
"snarkjs": "0.7.5"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@nomicfoundation/hardhat-ethers": "3.0.5",
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const BN128_CURVE_NAME = "bn128";
|
package/src/core/CircuitZKit.ts
CHANGED
|
@@ -47,7 +47,9 @@ export class CircuitZKit<Type extends ProvingSystemType> {
|
|
|
47
47
|
|
|
48
48
|
await snarkjs.wtns.calculate(inputs, wasmFile, wtnsFile);
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
const wtnsJson = await snarkjs.wtns.exportJson(wtnsFile);
|
|
51
|
+
|
|
52
|
+
return wtnsJson as bigint[];
|
|
51
53
|
}
|
|
52
54
|
|
|
53
55
|
/**
|
|
@@ -6,15 +6,25 @@ import { AbstractProtocolImplementer } from "./AbstractImplementer";
|
|
|
6
6
|
import { Signals } from "../../types/proof-utils";
|
|
7
7
|
import { Groth16ProofStruct, ProvingSystemType, Groth16Calldata } from "../../types/protocols";
|
|
8
8
|
|
|
9
|
+
import { terminateCurve } from "../../utils";
|
|
10
|
+
|
|
9
11
|
export class Groth16Implementer extends AbstractProtocolImplementer<"groth16"> {
|
|
10
12
|
public async generateProof(inputs: Signals, zKeyFilePath: string, wasmFilePath: string): Promise<Groth16ProofStruct> {
|
|
11
|
-
|
|
13
|
+
const fullProof = await snarkjs.groth16.fullProve(inputs, wasmFilePath, zKeyFilePath);
|
|
14
|
+
|
|
15
|
+
await terminateCurve();
|
|
16
|
+
|
|
17
|
+
return fullProof as Groth16ProofStruct;
|
|
12
18
|
}
|
|
13
19
|
|
|
14
20
|
public async verifyProof(proof: Groth16ProofStruct, vKeyFilePath: string): Promise<boolean> {
|
|
15
21
|
const verifier = JSON.parse(fs.readFileSync(vKeyFilePath).toString());
|
|
16
22
|
|
|
17
|
-
|
|
23
|
+
const proofVerification = await snarkjs.groth16.verify(verifier, proof.publicSignals, proof.proof);
|
|
24
|
+
|
|
25
|
+
await terminateCurve();
|
|
26
|
+
|
|
27
|
+
return proofVerification;
|
|
18
28
|
}
|
|
19
29
|
|
|
20
30
|
public async generateCalldata(proof: Groth16ProofStruct): Promise<Groth16Calldata> {
|
|
@@ -6,15 +6,25 @@ import { AbstractProtocolImplementer } from "./AbstractImplementer";
|
|
|
6
6
|
import { Signals } from "../../types/proof-utils";
|
|
7
7
|
import { PlonkCalldata, PlonkProofStruct, ProvingSystemType } from "../../types/protocols";
|
|
8
8
|
|
|
9
|
+
import { terminateCurve } from "../../utils";
|
|
10
|
+
|
|
9
11
|
export class PlonkImplementer extends AbstractProtocolImplementer<"plonk"> {
|
|
10
12
|
public async generateProof(inputs: Signals, zKeyFilePath: string, wasmFilePath: string): Promise<PlonkProofStruct> {
|
|
11
|
-
|
|
13
|
+
const fullProof = await snarkjs.plonk.fullProve(inputs, wasmFilePath, zKeyFilePath);
|
|
14
|
+
|
|
15
|
+
await terminateCurve();
|
|
16
|
+
|
|
17
|
+
return fullProof as PlonkProofStruct;
|
|
12
18
|
}
|
|
13
19
|
|
|
14
20
|
public async verifyProof(proof: PlonkProofStruct, vKeyFilePath: string): Promise<boolean> {
|
|
15
21
|
const verifier = JSON.parse(fs.readFileSync(vKeyFilePath).toString());
|
|
16
22
|
|
|
17
|
-
|
|
23
|
+
const proofVerification = await snarkjs.plonk.verify(verifier, proof.publicSignals, proof.proof);
|
|
24
|
+
|
|
25
|
+
await terminateCurve();
|
|
26
|
+
|
|
27
|
+
return proofVerification;
|
|
18
28
|
}
|
|
19
29
|
|
|
20
30
|
public async generateCalldata(proof: PlonkProofStruct): Promise<PlonkCalldata> {
|
|
@@ -67,7 +67,7 @@ contract <%=verifier_id%> {
|
|
|
67
67
|
mstore(add(pointer_, 32), y_)
|
|
68
68
|
mstore(add(pointer_, 64), s_)
|
|
69
69
|
|
|
70
|
-
res_ := staticcall(
|
|
70
|
+
res_ := staticcall(6000, 7, pointer_, 96, pointer_, 64) // ecMul
|
|
71
71
|
res_ := and(res_, gt(returndatasize(), 0)) // check that multiplication succeeded
|
|
72
72
|
|
|
73
73
|
if iszero(res_) {
|
|
@@ -77,7 +77,7 @@ contract <%=verifier_id%> {
|
|
|
77
77
|
mstore(add(pointer_, 64), mload(pR_))
|
|
78
78
|
mstore(add(pointer_, 96), mload(add(pR_, 32)))
|
|
79
79
|
|
|
80
|
-
res_ := staticcall(
|
|
80
|
+
res_ := staticcall(150, 6, pointer_, 128, pR_, 64) // ecAdd
|
|
81
81
|
res_ := and(res_, gt(returndatasize(), 0)) // check that addition succeeded
|
|
82
82
|
}
|
|
83
83
|
|
|
@@ -136,7 +136,7 @@ contract <%=verifier_id%> {
|
|
|
136
136
|
mstore(add(pPairing_, 704), DELTA_Y1)
|
|
137
137
|
mstore(add(pPairing_, 736), DELTA_Y2)
|
|
138
138
|
|
|
139
|
-
res_ := staticcall(
|
|
139
|
+
res_ := staticcall(181000, 8, pPairing_, 768, pPairing_, 32) // ecPairing
|
|
140
140
|
res_ := and(res_, mload(pPairing_)) // check that pairing succeeded
|
|
141
141
|
}
|
|
142
142
|
|
|
@@ -53,6 +53,7 @@ def _g1MulAdd(pR: uint256[2], pP: uint256[2], s: uint256) -> (bool, uint256[2]):
|
|
|
53
53
|
success, response = raw_call(
|
|
54
54
|
EC_MUL_PRECOMPILED_ADDRESS,
|
|
55
55
|
abi_encode(pP, s),
|
|
56
|
+
gas=6000,
|
|
56
57
|
max_outsize=64,
|
|
57
58
|
is_static_call=True,
|
|
58
59
|
revert_on_failure=False
|
|
@@ -68,6 +69,7 @@ def _g1MulAdd(pR: uint256[2], pP: uint256[2], s: uint256) -> (bool, uint256[2]):
|
|
|
68
69
|
success, response = raw_call(
|
|
69
70
|
EC_ADD_PRECOMPILED_ADDRESS,
|
|
70
71
|
abi_encode(pR, pS),
|
|
72
|
+
gas=150,
|
|
71
73
|
max_outsize=64,
|
|
72
74
|
is_static_call=True,
|
|
73
75
|
revert_on_failure=False
|
|
@@ -107,6 +109,7 @@ def _checkPairing(pA: uint256[2], pB: uint256[2][2], pC: uint256[2], pubSignals:
|
|
|
107
109
|
pC,
|
|
108
110
|
DELTA_X1, DELTA_X2, DELTA_Y1, DELTA_Y2
|
|
109
111
|
),
|
|
112
|
+
gas=181000,
|
|
110
113
|
max_outsize=32,
|
|
111
114
|
is_static_call=True,
|
|
112
115
|
revert_on_failure=False
|
|
@@ -415,7 +415,7 @@ contract <%=verifier_id%> {
|
|
|
415
415
|
mstore(add(mIn,64), mload(pP_))
|
|
416
416
|
mstore(add(mIn,96), mload(add(pP_, 32)))
|
|
417
417
|
|
|
418
|
-
res_ := staticcall(
|
|
418
|
+
res_ := staticcall(150, 6, mIn, 128, pR_, 64)
|
|
419
419
|
}
|
|
420
420
|
|
|
421
421
|
function g1_mulAccC(pR_, x_, y_, s_) -> res_ {
|
|
@@ -424,7 +424,7 @@ contract <%=verifier_id%> {
|
|
|
424
424
|
mstore(add(mIn,32), y_)
|
|
425
425
|
mstore(add(mIn,64), s_)
|
|
426
426
|
|
|
427
|
-
res_ := staticcall(
|
|
427
|
+
res_ := staticcall(6000, 7, mIn, 96, mIn, 64)
|
|
428
428
|
|
|
429
429
|
if iszero(res_) {
|
|
430
430
|
leave
|
|
@@ -433,7 +433,7 @@ contract <%=verifier_id%> {
|
|
|
433
433
|
mstore(add(mIn,64), mload(pR_))
|
|
434
434
|
mstore(add(mIn,96), mload(add(pR_, 32)))
|
|
435
435
|
|
|
436
|
-
res_ := staticcall(
|
|
436
|
+
res_ := staticcall(150, 6, mIn, 128, pR_, 64)
|
|
437
437
|
}
|
|
438
438
|
|
|
439
439
|
function g1_mulSetC(pR_, x_, y_, s_) -> res_ {
|
|
@@ -442,7 +442,7 @@ contract <%=verifier_id%> {
|
|
|
442
442
|
mstore(add(mIn,32), y_)
|
|
443
443
|
mstore(add(mIn,64), s_)
|
|
444
444
|
|
|
445
|
-
res_ := staticcall(
|
|
445
|
+
res_ := staticcall(6000, 7, mIn, 96, pR_, 64)
|
|
446
446
|
}
|
|
447
447
|
|
|
448
448
|
function g1_mulSet(pR_, pP_, s_) -> res_ {
|
|
@@ -748,7 +748,7 @@ contract <%=verifier_id%> {
|
|
|
748
748
|
mstore(add(mIn,320), G2_Y2)
|
|
749
749
|
mstore(add(mIn,352), G2_Y1)
|
|
750
750
|
|
|
751
|
-
if iszero(staticcall(
|
|
751
|
+
if iszero(staticcall(113000, 8, mIn, 384, mIn, 0x20)) {
|
|
752
752
|
leave
|
|
753
753
|
}
|
|
754
754
|
|
|
@@ -149,6 +149,7 @@ def _ecadd(a: uint256[2], b: uint256[2]) -> (bool, uint256[2]):
|
|
|
149
149
|
success, response = raw_call(
|
|
150
150
|
EC_ADD_PRECOMPILED_ADDRESS,
|
|
151
151
|
abi_encode(a, b),
|
|
152
|
+
gas=150,
|
|
152
153
|
max_outsize=64,
|
|
153
154
|
is_static_call=True,
|
|
154
155
|
revert_on_failure=False
|
|
@@ -171,6 +172,7 @@ def _ecmul(p: uint256[2], s: uint256) -> (bool, uint256[2]):
|
|
|
171
172
|
success, response = raw_call(
|
|
172
173
|
EC_MUL_PRECOMPILED_ADDRESS,
|
|
173
174
|
abi_encode(p, s),
|
|
175
|
+
gas=6000,
|
|
174
176
|
max_outsize=64,
|
|
175
177
|
is_static_call=True,
|
|
176
178
|
revert_on_failure=False
|
|
@@ -639,6 +641,7 @@ def _checkPairing(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> bool:
|
|
|
639
641
|
success, response = raw_call(
|
|
640
642
|
EC_PAIRING_PRECOMPILED_ADDRESS,
|
|
641
643
|
abi_encode(mIn),
|
|
644
|
+
gas=113000,
|
|
642
645
|
max_outsize=32,
|
|
643
646
|
is_static_call=True,
|
|
644
647
|
revert_on_failure=False
|