@solarity/zkit 0.1.0 → 0.2.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 CHANGED
@@ -3,12 +3,11 @@
3
3
 
4
4
  # ZKit - Circom Zero Knowledge Kit
5
5
 
6
- **A zero knowledge kit that helps you develop circuits using Circom.**
6
+ **A zero knowledge kit that helps you interact with Circom circuits.**
7
7
 
8
- - Compile and interact with circuits without snarkjs hassle.
9
8
  - Generate and verify ZK proofs with a single line of code.
10
9
  - Render optimized Solidity verifiers.
11
- - Forget about native dependencies - everything is in TypeScript.
10
+ - Build and work with ZK witnesses.
12
11
 
13
12
  ## Installation
14
13
 
@@ -20,67 +19,42 @@ npm install --save-dev @solarity/zkit
20
19
 
21
20
  ## Usage
22
21
 
23
- ### CircomZKit
24
-
25
- ZKit is a configless package, which means you don't need to provide any configuration to use it:
26
-
27
- ```typescript
28
- import { CircomZKit } from "@solarity/zkit";
29
-
30
- async function main() {
31
- const zkit = new CircomZKit();
32
-
33
- const multiplier = zkit.getCircuit("Multiplier");
34
-
35
- // Generates artifacts in the "./zkit-artifacts" directory
36
- await multiplier.compile();
37
-
38
- // Generates ZK proof
39
- const proof = await multiplier.generateProof({ a: 2, b: 3});
40
- }
41
- ```
42
-
43
- By default, ZKit will look for the circuit file in the `./circuits` directory. However, you can change this by providing a custom one:
44
-
45
- ```typescript
46
- new CircomZKit({ circuitsDir: "./my-circuits" });
47
- ```
48
-
49
- To generate zkey, the power-of-tau file is required. ZKit automatically downloads those files from [Hermes](https://hermez.s3-eu-west-1.amazonaws.com/) to the `${HOME}/.zkit/.ptau` directory, so you don't need to re-download them every time you start a new project.
50
-
51
- You can also provide a custom path to the directory where the power-of-tau files are stored:
22
+ > [!IMPORTANT]
23
+ > The kit is not meant to be used directly as its fitness relies heavily on the environment, Circom compilation artifacts management, processing of remappings, etc. Consider using [hardhat-zkit](https://github.com/dl-solarity/hardhat-zkit) which is a complete, developer-friendly package.
52
24
 
53
- ```typescript
54
- new CircomZKit({ ptauDir: "./my-ptau" });
55
- ```
25
+ ### CircuitZKit
56
26
 
57
- > [!IMPORTANT]
58
- > Note that all the files in the `ptauDir` directory must have the `powers-of-tau-{x}.ptau` name format, where `{x}` is a maximum degree (2<sup>x</sup>) of constraints a `ptau` supports.
27
+ `CircuitZKit` is a user-friendly interface for interacting with circom circuits.
59
28
 
60
- ZKit may also ask you for the permission to download the power-of-tau files. You can enable this by toggling off the `allowDownload` option:
29
+ To create a CircuitZKit object it is necessary to pass a config:
61
30
 
62
31
  ```typescript
63
- new CircomZKit({ allowDownload: false });
32
+ CircuitZKitConfig = {
33
+ circuitName: string;
34
+ circuitArtifactsPath: string;
35
+ verifierDirPath: string;
36
+ templateType?: VerifierTemplateType;
37
+ };
64
38
  ```
65
39
 
66
- ### CircuitZKit
67
-
68
- Once you created a `CircuitZKit` instance using the `getCircuit` method, you can manage the underlying circuit using the following methods:
40
+ This config contains all the information required to work with the circuit, namely:
69
41
 
70
- > [!NOTE]
71
- > You should first compile the circuit before creating verifiers or generating proofs.
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 verifier file will be generated
45
+ - `templateType` - The type of template that will be used to generate the Solidity verifier contract. Right now only `groth16` is supported
72
46
 
73
- #### compile()
47
+ #### getTemplate()
74
48
 
75
- Compiles the circuit and generates the artifacts in the `./zkit-artifacts` or in the provided `artifactsDir` directory. The default output is `r1cs`, `zkey` and `vkey` files.
49
+ Static `CircuitZKit` function that returns the contents of a template file by the passed type.
76
50
 
77
51
  ```typescript
78
- await multiplier.compile();
52
+ const templateContent = CircuitZKit.getTemplate("groth16");
79
53
  ```
80
54
 
81
55
  #### createVerifier()
82
56
 
83
- Creates Solidity verifier contract in the `./contracts/verifiers` or in the provided `verifiersDir` directory.
57
+ Creates a Solidity verifier contract on `verifierDirPath` path, which was specified in the config.
84
58
 
85
59
  ```typescript
86
60
  await multiplier.createVerifier();
@@ -92,7 +66,7 @@ Generates a proof for the given inputs.
92
66
 
93
67
  ```typescript
94
68
  /// { proof: { pi_a, pi_b, pi_c, protocol, curve }, publicSignals: [6] }
95
- const proof = await multiplier.generateProof({ a: 2, b: 3});
69
+ const proof = await multiplier.generateProof({ a: 2, b: 3 });
96
70
  ```
97
71
 
98
72
  #### verifyProof()
@@ -116,5 +90,3 @@ const calldata = await multiplier.generateCalldata(proof);
116
90
  ## Known limitations
117
91
 
118
92
  - Currently, ZKit supports only the Groth16 proving system.
119
- - Zkey generation doesn't allow additional contributions.
120
- - The `compile` method may cause [issues](https://github.com/iden3/snarkjs/issues/494).
@@ -1,3 +1,4 @@
1
+ import { TemplateType } from "../types/types";
1
2
  declare const Context: any;
2
3
  export type ManagerZKitConfig = {
3
4
  circuitsDir: string;
@@ -12,6 +13,8 @@ export type CompileOptions = {
12
13
  json: boolean;
13
14
  c: boolean;
14
15
  quiet: boolean;
16
+ setup: TemplateType;
17
+ contributions: number;
15
18
  };
16
19
  export declare const defaultCompileOptions: CompileOptions;
17
20
  export type ManagerZKitPrivateConfig = ManagerZKitConfig & {
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config/config.ts"],"names":[],"mappings":"AAAA,QAAA,MAAQ,OAAO,KAAuC,CAAC;AAEvD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,OAAO,CAAC,iBAAiB,CAK5D,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,GAAG,EAAE,OAAO,CAAC;IACb,IAAI,EAAE,OAAO,CAAC;IACd,CAAC,EAAE,OAAO,CAAC;IACX,KAAK,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,cAKnC,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAAG,iBAAiB,GAAG;IACzD,QAAQ,EAAE,OAAO,OAAO,CAAC;IACzB,SAAS,EAAE;QACT,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;CACH,CAAC"}
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,QAAA,MAAQ,OAAO,KAAuC,CAAC;AAEvD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,OAAO,CAAC,iBAAiB,CAK5D,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,GAAG,EAAE,OAAO,CAAC;IACb,IAAI,EAAE,OAAO,CAAC;IACd,CAAC,EAAE,OAAO,CAAC;IACX,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,EAAE,YAAY,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,cAOnC,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAAG,iBAAiB,GAAG;IACzD,QAAQ,EAAE,OAAO,OAAO,CAAC;IACzB,SAAS,EAAE;QACT,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;CACH,CAAC"}
@@ -13,5 +13,7 @@ exports.defaultCompileOptions = {
13
13
  json: false,
14
14
  c: false,
15
15
  quiet: false,
16
+ setup: "groth16",
17
+ contributions: 5,
16
18
  };
17
19
  //# sourceMappingURL=config.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config/config.ts"],"names":[],"mappings":";;;AAAA,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAAC;AAU1C,QAAA,qBAAqB,GAA+B;IAC/D,WAAW,EAAE,UAAU;IACvB,YAAY,EAAE,gBAAgB;IAC9B,YAAY,EAAE,qBAAqB;IACnC,aAAa,EAAE,IAAI;CACpB,CAAC;AASW,QAAA,qBAAqB,GAAmB;IACnD,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,KAAK;IACX,CAAC,EAAE,KAAK;IACR,KAAK,EAAE,KAAK;CACb,CAAC"}
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config/config.ts"],"names":[],"mappings":";;;AAEA,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAAC;AAU1C,QAAA,qBAAqB,GAA+B;IAC/D,WAAW,EAAE,UAAU;IACvB,YAAY,EAAE,gBAAgB;IAC9B,YAAY,EAAE,qBAAqB;IACnC,aAAa,EAAE,IAAI;CACpB,CAAC;AAWW,QAAA,qBAAqB,GAAmB;IACnD,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,KAAK;IACX,CAAC,EAAE,KAAK;IACR,KAAK,EAAE,KAAK;IACZ,KAAK,EAAE,SAAS;IAChB,aAAa,EAAE,CAAC;CACjB,CAAC"}
@@ -1,32 +1,19 @@
1
- import { CompileOptions } from "../config/config";
2
- import { ManagerZKit } from "./ManagerZKit";
3
- import { Calldata, Inputs, ProofStruct } from "../types/types";
1
+ import { ArtifactsFileType, Calldata, CircuitZKitConfig, Inputs, ProofStruct, VerifierTemplateType } from "../types/circuit-zkit";
4
2
  /**
5
3
  * `CircuitZKit` represents a single circuit and provides a high-level API to work with it.
6
- *
7
- * @dev This class is not meant to be used directly. Use the `CircomZKit` to create its instance.
8
4
  */
9
5
  export declare class CircuitZKit {
10
- private readonly _circuit;
11
- private readonly _manager;
6
+ private readonly _config;
7
+ constructor(_config: CircuitZKitConfig);
12
8
  /**
13
- * Creates a new instance of `CircuitZKit`.
9
+ * Returns the Solidity verifier template for the specified proving system.
14
10
  *
15
- * @param {string} _circuit - The path to the circuit.
16
- * @param {ManagerZKit} _manager - The manager that maintains the global state.
11
+ * @param {VerifierTemplateType} templateType - The template type.
12
+ * @returns {string} The Solidity verifier template.
17
13
  */
18
- constructor(_circuit: string, _manager: ManagerZKit);
14
+ static getTemplate(templateType: VerifierTemplateType): string;
19
15
  /**
20
- * Compiles the circuit and generates the artifacts.
21
- *
22
- * @dev If compilation fails, the latest valid artifacts will be preserved.
23
- * @dev Doesn't show the compilation error if `quiet` is set to `true`.
24
- *
25
- * @param {Partial<CompileOptions>} [options=defaultCompileOptions] - Compilation options.
26
- */
27
- compile(options?: Partial<CompileOptions>): Promise<void>;
28
- /**
29
- * Creates a verifier contract.
16
+ * Creates a Solidity verifier contract.
30
17
  */
31
18
  createVerifier(): Promise<void>;
32
19
  /**
@@ -58,89 +45,36 @@ export declare class CircuitZKit {
58
45
  */
59
46
  generateCalldata(proof: ProofStruct): Promise<Calldata>;
60
47
  /**
61
- * Returns the circuit ID. The circuit ID is the name of the circuit file without the extension.
62
- *
63
- * @returns {string} The circuit ID.
64
- */
65
- getCircuitId(): string;
66
- /**
67
- * Returns the verifier ID. The verifier ID is the name of the circuit file without the extension, suffixed with "Verifier".
68
- *
69
- * @returns {string} The verifier ID.
70
- */
71
- getVerifierId(): string;
72
- /**
73
- * Generates zero-knowledge key for the circuit.
74
- *
75
- * @param {string} outDir - The directory to save the generated key.
76
- * @todo This method may cause issues https://github.com/iden3/snarkjs/issues/494
77
- */
78
- private _generateZKey;
79
- /**
80
- * Generates verification key for the circuit.
48
+ * Returns the circuit name. The circuit name is the name of the circuit file without the extension.
81
49
  *
82
- * @param {string} outDir - The directory to save the generated key.
50
+ * @returns {string} The circuit name.
83
51
  */
84
- private _generateVKey;
52
+ getCircuitName(): string;
85
53
  /**
86
- * Returns the arguments to compile the circuit.
54
+ * Returns the verifier name. The verifier name is the name of the circuit file without the extension, suffixed with "Verifier".
87
55
  *
88
- * @param {CompileOptions} options - Compilation options.
89
- * @param {string} outDir - The directory to save the compiled artifacts.
90
- * @returns {string[]} The arguments to compile the circuit.
56
+ * @returns {string} The verifier name.
91
57
  */
92
- private _getCompileArgs;
58
+ getVerifierName(): string;
93
59
  /**
94
- * Compiles the circuit.
60
+ * Returns the type of verifier template that was stored in the config
95
61
  *
96
- * @param {CompileOptions} options - Compilation options.
97
- * @param {string} outDir - The directory to save the compiled artifacts.
62
+ * @returns {VerifierTemplateType} The verifier template type.
98
63
  */
99
- private _compile;
64
+ getTemplateType(): VerifierTemplateType;
100
65
  /**
101
- * Returns the number of constraints in the circuit. This value is used to fetch the correct `ptau` file.
66
+ * Returns the path to the file of the given type inside artifacts directory. Throws an error if the file doesn't exist.
102
67
  *
103
- * @param {string} outDir - The directory where the compiled artifacts are saved.
104
- * @returns {Promise<number>} The number of constraints in the circuit.
105
- */
106
- _getConstraints(outDir: string): Promise<number>;
107
- /**
108
- * Returns the path to the file of the given type.
109
- *
110
- * @param {FileType} fileType - The type of the file.
111
- * @param {string | undefined} temp - The temporary directory to use.
68
+ * @param {ArtifactsFileType} fileType - The type of the file.
112
69
  * @returns {string} The path to the file.
113
70
  */
114
- private _getFile;
115
- /**
116
- * Returns the path to the directory of the given type.
117
- *
118
- * @param {DirType} dirType - The type of the directory.
119
- * @returns {string} The path to the directory.
120
- */
121
- private _getDir;
71
+ mustGetArtifactsFilePath(fileType: ArtifactsFileType): string;
122
72
  /**
123
- * Returns the path to the file of the given type. Throws an error if the file doesn't exist.
73
+ * Returns the path to the file of the given type inside artifacts directory.
124
74
  *
125
- * @param {FileType} fileType - The type of the file.
126
- * @param {string | undefined} temp - The temporary directory to use.
75
+ * @param {ArtifactsFileType} fileType - The type of the file.
127
76
  * @returns {string} The path to the file.
128
77
  */
129
- private _mustGetFile;
130
- /**
131
- * Moves the files from the temporary directory to the output directory.
132
- *
133
- * @param {string} tempDir - The temporary directory.
134
- * @param {string} outDir - The output directory.
135
- */
136
- private _moveFromTempDirToOutDir;
137
- /**
138
- * Returns a new instance of `CircomRunner`. The `CircomRunner` is used to compile the circuit.
139
- *
140
- * @param {string[]} args - The arguments to run the `circom` compiler.
141
- * @param {boolean} quiet - Whether to suppress the compilation error.
142
- * @returns {typeof CircomRunner} The `CircomRunner` instance.
143
- */
144
- private _getCircomRunner;
78
+ getArtifactsFilePath(fileType: ArtifactsFileType): string;
145
79
  }
146
80
  //# sourceMappingURL=CircuitZKit.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"CircuitZKit.d.ts","sourceRoot":"","sources":["../../src/core/CircuitZKit.ts"],"names":[],"mappings":"AAKA,OAAO,EAAyB,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAqB,MAAM,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAKlF;;;;GAIG;AACH,qBAAa,WAAW;IAQpB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAR3B;;;;;OAKG;gBAEgB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,WAAW;IAGxC;;;;;;;OAOG;IACU,OAAO,CAAC,OAAO,GAAE,OAAO,CAAC,cAAc,CAAyB,GAAG,OAAO,CAAC,IAAI,CAAC;IAqB7F;;OAEG;IACU,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;IA0B5C;;;;;;;;OAQG;IACU,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAOhE;;;;;;;;OAQG;IACU,WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC;IAQ9D;;;;;;OAMG;IACU,gBAAgB,CAAC,KAAK,EAAE,WAAW,GAAG,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