From bfc1d1b9da6a150891119195248be2373bdbc673 Mon Sep 17 00:00:00 2001 From: vmidyllic <74898029+vmidyllic@users.noreply.github.com> Date: Mon, 31 Jul 2023 18:14:38 +0300 Subject: [PATCH 1/2] add FSCircuitStore --- src/index.ts | 1 - src/loaders/index.ts | 1 - src/loaders/key.ts | 36 -------- src/proof/prover.ts | 24 ++++-- src/storage/entities/circuitData.ts | 6 +- src/storage/fs/circuits-storage.ts | 129 ++++++++++++++++++++++++++++ src/storage/fs/index.ts | 1 + src/storage/index.ts | 1 + tests/handlers/auth.test.ts | 43 +++------- tests/proofs/mtp-onchain.test.ts | 30 +------ tests/proofs/mtp.test.ts | 27 +----- tests/proofs/sig-onchain.test.ts | 34 +------- tests/proofs/sig.test.ts | 42 ++------- 13 files changed, 178 insertions(+), 197 deletions(-) delete mode 100644 src/loaders/index.ts delete mode 100644 src/loaders/key.ts create mode 100644 src/storage/fs/circuits-storage.ts create mode 100644 src/storage/fs/index.ts diff --git a/src/index.ts b/src/index.ts index 0d89107b..10dfd706 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,6 @@ export * from './schema-processor'; export * from './proof'; export * from './iden3comm'; export * from './circuits'; -export * from './loaders'; export * from './iden3comm/handlers'; export * from './utils'; import * as core from '@iden3/js-iden3-core'; diff --git a/src/loaders/index.ts b/src/loaders/index.ts deleted file mode 100644 index 0a33c489..00000000 --- a/src/loaders/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './key'; diff --git a/src/loaders/key.ts b/src/loaders/key.ts deleted file mode 100644 index ddc94b01..00000000 --- a/src/loaders/key.ts +++ /dev/null @@ -1,36 +0,0 @@ -import * as fs from 'fs'; - -/** - * Loader interface to load from different sources - * - * @public - * @interface IKeyLoader - */ -export interface IKeyLoader { - /** - * loads file as a byte array - * - * @param {string} path - path to file - * @returns `Promise` - */ - load(path: string): Promise; -} -const isBrowser = new Function('try {return this===window;}catch(e){ return false;}'); - -/** - * Loader from file system - * - * @public - * @class FSKeyLoader - * @implements implements IKeyLoader interface - */ -export class FSKeyLoader implements IKeyLoader { - constructor(public readonly dir: string) {} - public async load(path: string): Promise { - if (isBrowser()) { - throw new Error('can not use fs loader in the browser'); - } - const data = await fs.readFileSync(`${this.dir}/${path}`); - return new Uint8Array(data); - } -} diff --git a/src/proof/prover.ts b/src/proof/prover.ts index 8c43a11b..e283dc0d 100644 --- a/src/proof/prover.ts +++ b/src/proof/prover.ts @@ -49,13 +49,16 @@ export class NativeProver implements IZKProver { * @param {string} circuitId - circuit id for proof verification * @returns `Promise` */ - async verify(zkp: ZKProof, circuitName: CircuitId): Promise { + async verify(zkp: ZKProof, circuitId: CircuitId): Promise { try { - const verKey: Uint8Array = (await this._circuitStorage.loadCircuitData(circuitName)) - .verificationKey; + const circuitData = await this._circuitStorage.loadCircuitData(circuitId); + + if (!circuitData.verificationKey) { + throw new Error(`verification file doesn't exist for circuit ${circuitId}`); + } await snarkjs.groth16.verify( - JSON.parse(byteDecoder.decode(verKey)), + JSON.parse(byteDecoder.decode(circuitData.verificationKey)), zkp.pub_signals, zkp.proof ); @@ -79,17 +82,20 @@ export class NativeProver implements IZKProver { */ async generate(inputs: Uint8Array, circuitId: CircuitId): Promise { const circuitData = await this._circuitStorage.loadCircuitData(circuitId); - const wasm: Uint8Array = circuitData.wasm; + if (!circuitData.wasm) { + throw new Error(`wasm file doesn't exist for circuit ${circuitId}`); + } - const witnessCalculator = await witnessBuilder(wasm); + const witnessCalculator = await witnessBuilder(circuitData.wasm); const parsedData = JSON.parse(byteDecoder.decode(inputs)); const wtnsBytes: Uint8Array = await witnessCalculator.calculateWTNSBin(parsedData, 0); - const provingKey = circuitData.provingKey; - - const { proof, publicSignals } = await snarkjs.groth16.prove(provingKey, wtnsBytes); + if (!circuitData.provingKey) { + throw new Error(`proving file doesn't exist for circuit ${circuitId}`); + } + const { proof, publicSignals } = await snarkjs.groth16.prove(circuitData.provingKey, wtnsBytes); // we need to terminate curve manually await this.terminateCurve(); diff --git a/src/storage/entities/circuitData.ts b/src/storage/entities/circuitData.ts index a64885c8..8a0d6f0e 100644 --- a/src/storage/entities/circuitData.ts +++ b/src/storage/entities/circuitData.ts @@ -1,7 +1,7 @@ /** Circuit data that includes id, wasm file, verification key and proving key */ export type CircuitData = { circuitId: string; - wasm: Uint8Array; - verificationKey: Uint8Array; - provingKey: Uint8Array; + wasm: Uint8Array | null; + verificationKey: Uint8Array | null; + provingKey: Uint8Array | null; }; diff --git a/src/storage/fs/circuits-storage.ts b/src/storage/fs/circuits-storage.ts new file mode 100644 index 00000000..997ea118 --- /dev/null +++ b/src/storage/fs/circuits-storage.ts @@ -0,0 +1,129 @@ +import { CircuitId } from '../../circuits'; +import { CircuitData } from '../entities/circuitData'; +import { ICircuitStorage } from '../interfaces/circuits'; +import fs from 'fs'; + +/** + * Options for FSCircuitStorage, + * Path to the circuit file is constructed from `${this._dirname}/${circuitId}/${filename}`, + * by default values for keys are: + * - verification key : verification_key.json + * - proving key : circuit_final.zkey + * - wasm file : circuit.wasm + * you can customize filename by passing the corresponding option. + * dirname is mandatory. + * hierarchical structure for files is mandatory + * e.g. --circuits + * -----circuitId + * ---------file + * @public + * @interface FSCircuitStorageOptions + */ +export interface FSCircuitStorageOptions { + dirname: string; + verificationFileName?: string; + provingFileName?: string; + wasmFileName?: string; +} +/** + * Implementation of ICircuitStorage to store keys data in file system + * + * @public + * @class FSCircuitStorage + * @implements implements ICircuitStorage interface + */ +export class FSCircuitStorage implements ICircuitStorage { + private readonly _defaultVerificationKeyPath = 'verification_key.json'; + private readonly _defaultProvingKeyPath = 'circuit_final.zkey'; + private readonly _defaultWasmFilePath = 'circuit.wasm'; + + /** + * Creates an instance of FSCircuitStorage. + * @param {string} opts - options to read / save files + */ + constructor(private readonly opts: FSCircuitStorageOptions) {} + + /** + * loads circuit data by id from file storage + * {@inheritdoc ICircuitStorage.loadCircuitData} + * @param {CircuitId} circuitId - id of the circuit + * @returns `Promise` + */ + async loadCircuitData(circuitId: CircuitId): Promise { + const verificationKey = await this.loadCircuitFile( + circuitId, + this.opts.verificationFileName ?? this._defaultVerificationKeyPath + ); + const provingKey = await this.loadCircuitFile( + circuitId, + this.opts.provingFileName ?? this._defaultProvingKeyPath + ); + const wasm = await this.loadCircuitFile( + circuitId, + this.opts.wasmFileName ?? this._defaultWasmFilePath + ); + + return { + circuitId, + wasm, + provingKey, + verificationKey + }; + } + + private async loadCircuitFile( + circuitId: CircuitId, + filename: string + ): Promise { + const keyPath = `${this.opts.dirname}/${circuitId}/${filename}`; + if (fs.existsSync(keyPath)) { + const keyData = fs.readFileSync(keyPath); + return new Uint8Array(keyData); + } + return null; + } + private async writeCircuitFile( + circuitId: CircuitId, + filename: string, + file: Uint8Array, + encoding?: BufferEncoding + ): Promise { + const dirPath = `${this.opts.dirname}/${circuitId}`; + const keyPath = `${dirPath}/${filename}`; + fs.mkdirSync(dirPath, { recursive: true }); + fs.writeFileSync(keyPath, file, encoding); + } + + /** + * {@inheritdoc ICircuitStorage.loadCircuitData} + * saves circuit data for circuit id to the file storage + * @param {CircuitId} circuitId - id of the circuit + * @param {CircuitData} circuitData - circuit keys + * @returns `Promise` + */ + async saveCircuitData(circuitId: CircuitId, circuitData: CircuitData): Promise { + if (circuitData.verificationKey) { + await this.writeCircuitFile( + circuitId, + this.opts.verificationFileName ?? this._defaultVerificationKeyPath, + circuitData.verificationKey, + 'utf-8' + ); + } + + if (circuitData.provingKey) { + await this.writeCircuitFile( + circuitId, + this.opts.provingFileName ?? this._defaultProvingKeyPath, + circuitData.provingKey + ); + } + if (circuitData.wasm) { + await this.writeCircuitFile( + circuitId, + this.opts.wasmFileName ?? this._defaultWasmFilePath, + circuitData.wasm + ); + } + } +} diff --git a/src/storage/fs/index.ts b/src/storage/fs/index.ts new file mode 100644 index 00000000..fedfa783 --- /dev/null +++ b/src/storage/fs/index.ts @@ -0,0 +1 @@ +export * from './circuits-storage'; diff --git a/src/storage/index.ts b/src/storage/index.ts index b30a254d..f8738d49 100644 --- a/src/storage/index.ts +++ b/src/storage/index.ts @@ -6,3 +6,4 @@ export * from './memory'; export * from './local-storage'; export * from './indexed-db'; export * from './shared'; +export * from './fs'; diff --git a/tests/handlers/auth.test.ts b/tests/handlers/auth.test.ts index 0511326c..bce57c47 100644 --- a/tests/handlers/auth.test.ts +++ b/tests/handlers/auth.test.ts @@ -4,8 +4,8 @@ import { IdentityStorage } from '../../src/storage/shared/identity-storage'; import { PlainPacker } from '../../src/iden3comm/packers/plain'; import { AuthHandler, - CircuitStorage, CredentialStorage, + FSCircuitStorage, IAuthHandler, IdentityWallet, byteEncoder @@ -17,7 +17,6 @@ import { InMemoryDataSource, InMemoryMerkleTreeStorage } from '../../src/storage import { CredentialRequest, CredentialWallet } from '../../src/credentials'; import { ProofService } from '../../src/proof'; import { CircuitId } from '../../src/circuits'; -import { FSKeyLoader } from '../../src/loaders'; import { CredentialStatusType, VerifiableConstants, W3CCredential } from '../../src/verifiable'; import { RootInfo, StateProof } from '../../src/storage/entities/state'; import path from 'path'; @@ -99,6 +98,10 @@ describe('auth', () => { const verificationFn = new VerificationHandlerFunc(stateVerificationFn); const mapKey = proving.provingMethodGroth16AuthV2Instance.methodAlg.toString(); + + if (!circuitData.verificationKey) { + throw new Error(`verification key doesn't exist for ${circuitData.circuitId}`); + } const verificationParamMap: Map = new Map([ [ mapKey, @@ -109,6 +112,12 @@ describe('auth', () => { ] ]); + if (!circuitData.provingKey) { + throw new Error(`proving doesn't exist for ${circuitData.circuitId}`); + } + if (!circuitData.wasm) { + throw new Error(`wasm file doesn't exist for ${circuitData.circuitId}`); + } const provingParamMap: Map = new Map(); provingParamMap.set(mapKey, { dataPreparer: authInputsHandler, @@ -138,34 +147,8 @@ describe('auth', () => { mt: new InMemoryMerkleTreeStorage(40), states: mockStateStorage // new EthStateStorage(defaultEthConnectionConfig) }; - - const circuitStorage = new CircuitStorage(new InMemoryDataSource()); - - const loader = new FSKeyLoader(path.join(__dirname, '../proofs/testdata')); - - await circuitStorage.saveCircuitData(CircuitId.AuthV2, { - circuitId: CircuitId.AuthV2.toString(), - wasm: await loader.load(`${CircuitId.AuthV2.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.AuthV2.toString()}/circuit_final.zkey`), - verificationKey: await loader.load(`${CircuitId.AuthV2.toString()}/verification_key.json`) - }); - - await circuitStorage.saveCircuitData(CircuitId.AtomicQuerySigV2, { - circuitId: CircuitId.AtomicQuerySigV2.toString(), - wasm: await loader.load(`${CircuitId.AtomicQuerySigV2.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.AtomicQuerySigV2.toString()}/circuit_final.zkey`), - verificationKey: await loader.load( - `${CircuitId.AtomicQuerySigV2.toString()}/verification_key.json` - ) - }); - - await circuitStorage.saveCircuitData(CircuitId.StateTransition, { - circuitId: CircuitId.StateTransition.toString(), - wasm: await loader.load(`${CircuitId.StateTransition.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.StateTransition.toString()}/circuit_final.zkey`), - verificationKey: await loader.load( - `${CircuitId.AtomicQueryMTPV2.toString()}/verification_key.json` - ) + const circuitStorage = new FSCircuitStorage({ + dirname: path.join(__dirname, '../proofs/testdata') }); const resolvers = new CredentialStatusResolverRegistry(); diff --git a/tests/proofs/mtp-onchain.test.ts b/tests/proofs/mtp-onchain.test.ts index 0bde57ef..11b1af72 100644 --- a/tests/proofs/mtp-onchain.test.ts +++ b/tests/proofs/mtp-onchain.test.ts @@ -1,7 +1,7 @@ /* eslint-disable no-console */ import { - CircuitStorage, CredentialStorage, + FSCircuitStorage, Identity, IdentityStorage, IdentityWallet, @@ -20,14 +20,12 @@ import { } from '../../src/credentials'; import { ProofService } from '../../src/proof'; import { CircuitId } from '../../src/circuits'; -import { FSKeyLoader } from '../../src/loaders'; import { ethers } from 'ethers'; import { EthStateStorage } from '../../src/storage/blockchain/state'; import { RootInfo, StateProof } from '../../src/storage/entities/state'; import path from 'path'; import { CredentialStatusType, W3CCredential } from '../../src/verifiable'; import { ZeroKnowledgeProofRequest } from '../../src/iden3comm'; -import { CircuitData } from '../../src/storage/entities/circuitData'; import { Blockchain, DidMethod, NetworkId } from '@iden3/js-iden3-core'; import { expect } from 'chai'; @@ -95,29 +93,8 @@ describe('mtp onchain proofs', () => { states: mockStateStorage }; - const circuitStorage = new CircuitStorage(new InMemoryDataSource()); - - // todo: change this loader - const loader = new FSKeyLoader(path.join(__dirname, './testdata')); - - await circuitStorage.saveCircuitData(CircuitId.AtomicQueryMTPV2OnChain, { - circuitId: CircuitId.AtomicQueryMTPV2OnChain, - wasm: await loader.load(`${CircuitId.AtomicQueryMTPV2OnChain.toString()}/circuit.wasm`), - provingKey: await loader.load( - `${CircuitId.AtomicQueryMTPV2OnChain.toString()}/circuit_final.zkey` - ), - verificationKey: await loader.load( - `${CircuitId.AtomicQueryMTPV2OnChain.toString()}/verification_key.json` - ) - }); - - await circuitStorage.saveCircuitData(CircuitId.StateTransition, { - circuitId: CircuitId.StateTransition, - wasm: await loader.load(`${CircuitId.StateTransition.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.StateTransition.toString()}/circuit_final.zkey`), - verificationKey: await loader.load( - `${CircuitId.AtomicQueryMTPV2OnChain.toString()}/verification_key.json` - ) + const circuitStorage = new FSCircuitStorage({ + dirname: path.join(__dirname, './testdata') }); /* @@ -130,6 +107,7 @@ describe('mtp onchain proofs', () => { dataStorage.states = ethStorage; */ + const resolvers = new CredentialStatusResolverRegistry(); resolvers.register( CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, diff --git a/tests/proofs/mtp.test.ts b/tests/proofs/mtp.test.ts index 85d4a19b..422e28f3 100644 --- a/tests/proofs/mtp.test.ts +++ b/tests/proofs/mtp.test.ts @@ -1,7 +1,7 @@ /* eslint-disable no-console */ import { - CircuitStorage, CredentialStorage, + FSCircuitStorage, Identity, IdentityStorage, IdentityWallet, @@ -15,14 +15,12 @@ import { InMemoryDataSource, InMemoryMerkleTreeStorage } from '../../src/storage import { CredentialRequest, CredentialWallet } from '../../src/credentials'; import { ProofService } from '../../src/proof'; import { CircuitId } from '../../src/circuits'; -import { FSKeyLoader } from '../../src/loaders'; import { ethers } from 'ethers'; import { EthStateStorage } from '../../src/storage/blockchain/state'; import { RootInfo, StateProof } from '../../src/storage/entities/state'; import path from 'path'; import { CredentialStatusType, W3CCredential } from '../../src/verifiable'; import { ZeroKnowledgeProofRequest } from '../../src/iden3comm'; -import { CircuitData } from '../../src/storage/entities/circuitData'; import { Blockchain, DidMethod, NetworkId } from '@iden3/js-iden3-core'; import { expect } from 'chai'; import { CredentialStatusResolverRegistry } from '../../src/credentials'; @@ -92,27 +90,8 @@ describe('mtp proofs', () => { states: mockStateStorage }; - const circuitStorage = new CircuitStorage(new InMemoryDataSource()); - - // todo: change this loader - const loader = new FSKeyLoader(path.join(__dirname, './testdata')); - - await circuitStorage.saveCircuitData(CircuitId.AtomicQueryMTPV2, { - circuitId: CircuitId.AtomicQueryMTPV2, - wasm: await loader.load(`${CircuitId.AtomicQueryMTPV2.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.AtomicQueryMTPV2.toString()}/circuit_final.zkey`), - verificationKey: await loader.load( - `${CircuitId.AtomicQueryMTPV2.toString()}/verification_key.json` - ) - }); - - await circuitStorage.saveCircuitData(CircuitId.StateTransition, { - circuitId: CircuitId.StateTransition, - wasm: await loader.load(`${CircuitId.StateTransition.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.StateTransition.toString()}/circuit_final.zkey`), - verificationKey: await loader.load( - `${CircuitId.AtomicQueryMTPV2.toString()}/verification_key.json` - ) + const circuitStorage = new FSCircuitStorage({ + dirname: path.join(__dirname, './testdata') }); /* diff --git a/tests/proofs/sig-onchain.test.ts b/tests/proofs/sig-onchain.test.ts index 70fa99b2..b86e2a0f 100644 --- a/tests/proofs/sig-onchain.test.ts +++ b/tests/proofs/sig-onchain.test.ts @@ -1,7 +1,7 @@ /* eslint-disable no-console */ import { - CircuitStorage, CredentialStorage, + FSCircuitStorage, Identity, IdentityStorage, IdentityWallet, @@ -19,13 +19,11 @@ import { } from '../../src/credentials'; import { ProofService } from '../../src/proof'; import { CircuitId } from '../../src/circuits'; -import { FSKeyLoader } from '../../src/loaders'; import { CredentialStatusType, VerifiableConstants, W3CCredential } from '../../src/verifiable'; import { RootInfo, StateProof } from '../../src/storage/entities/state'; import path from 'path'; import { byteEncoder } from '../../src'; import { ZeroKnowledgeProofRequest } from '../../src/iden3comm'; -import { CircuitData } from '../../src/storage/entities/circuitData'; import { Blockchain, DidMethod, NetworkId } from '@iden3/js-iden3-core'; import { expect } from 'chai'; @@ -84,36 +82,10 @@ describe('sig onchain proofs', () => { states: mockStateStorage }; - const circuitStorage = new CircuitStorage(new InMemoryDataSource()); - - const loader = new FSKeyLoader(path.join(__dirname, './testdata')); - - await circuitStorage.saveCircuitData(CircuitId.AuthV2, { - circuitId: CircuitId.AuthV2, - wasm: await loader.load(`${CircuitId.AuthV2.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.AuthV2.toString()}/circuit_final.zkey`), - verificationKey: await loader.load(`${CircuitId.AuthV2.toString()}/verification_key.json`) + const circuitStorage = new FSCircuitStorage({ + dirname: path.join(__dirname, './testdata') }); - await circuitStorage.saveCircuitData(CircuitId.AtomicQuerySigV2OnChain, { - circuitId: CircuitId.AtomicQuerySigV2OnChain, - wasm: await loader.load(`${CircuitId.AtomicQuerySigV2OnChain.toString()}/circuit.wasm`), - provingKey: await loader.load( - `${CircuitId.AtomicQuerySigV2OnChain.toString()}/circuit_final.zkey` - ), - verificationKey: await loader.load( - `${CircuitId.AtomicQuerySigV2OnChain.toString()}/verification_key.json` - ) - }); - - await circuitStorage.saveCircuitData(CircuitId.StateTransition, { - circuitId: CircuitId.StateTransition, - wasm: await loader.load(`${CircuitId.StateTransition.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.StateTransition.toString()}/circuit_final.zkey`), - verificationKey: await loader.load( - `${CircuitId.AtomicQueryMTPV2.toString()}/verification_key.json` - ) - }); const resolvers = new CredentialStatusResolverRegistry(); resolvers.register( CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, diff --git a/tests/proofs/sig.test.ts b/tests/proofs/sig.test.ts index 66a1ee3d..24ae6e98 100644 --- a/tests/proofs/sig.test.ts +++ b/tests/proofs/sig.test.ts @@ -1,7 +1,7 @@ /* eslint-disable no-console */ import { - CircuitStorage, CredentialStorage, + FSCircuitStorage, Identity, IdentityStorage, IdentityWallet, @@ -9,18 +9,16 @@ import { } from '../../src'; import { BjjProvider, KMS, KmsKeyType } from '../../src/kms'; import { InMemoryPrivateKeyStore } from '../../src/kms/store'; -import { IDataStorage, IStateStorage } from '../../src/storage/interfaces'; +import { ICircuitStorage, IDataStorage, IStateStorage } from '../../src/storage/interfaces'; import { InMemoryDataSource, InMemoryMerkleTreeStorage } from '../../src/storage/memory'; import { CredentialRequest, CredentialWallet } from '../../src/credentials'; import { ProofService } from '../../src/proof'; import { CircuitId } from '../../src/circuits'; -import { FSKeyLoader } from '../../src/loaders'; import { CredentialStatusType, VerifiableConstants, W3CCredential } from '../../src/verifiable'; import { RootInfo, StateProof } from '../../src/storage/entities/state'; import path from 'path'; import { byteEncoder } from '../../src'; import { ZeroKnowledgeProofRequest } from '../../src/iden3comm'; -import { CircuitData } from '../../src/storage/entities/circuitData'; import { Blockchain, DID, DidMethod, NetworkId } from '@iden3/js-iden3-core'; import { expect } from 'chai'; import { CredentialStatusResolverRegistry } from '../../src/credentials'; @@ -37,7 +35,7 @@ describe('sig proofs', () => { let userDID: DID; let issuerDID: DID; - let circuitStorage: CircuitStorage; + let circuitStorage: ICircuitStorage; const mockStateStorage: IStateStorage = { getLatestStateById: async () => { @@ -70,38 +68,10 @@ describe('sig proofs', () => { } }; - before(async () => { - circuitStorage = new CircuitStorage(new InMemoryDataSource()); - - const loader = new FSKeyLoader(path.join(__dirname, './testdata')); - - await circuitStorage.saveCircuitData(CircuitId.AuthV2, { - circuitId: CircuitId.AuthV2, - wasm: await loader.load(`${CircuitId.AuthV2.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.AuthV2.toString()}/circuit_final.zkey`), - verificationKey: await loader.load(`${CircuitId.AuthV2.toString()}/verification_key.json`) - }); - - await circuitStorage.saveCircuitData(CircuitId.AtomicQuerySigV2, { - circuitId: CircuitId.AtomicQuerySigV2, - wasm: await loader.load(`${CircuitId.AtomicQuerySigV2.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.AtomicQuerySigV2.toString()}/circuit_final.zkey`), - verificationKey: await loader.load( - `${CircuitId.AtomicQuerySigV2.toString()}/verification_key.json` - ) - }); - - await circuitStorage.saveCircuitData(CircuitId.StateTransition, { - circuitId: CircuitId.StateTransition, - wasm: await loader.load(`${CircuitId.StateTransition.toString()}/circuit.wasm`), - provingKey: await loader.load(`${CircuitId.StateTransition.toString()}/circuit_final.zkey`), - verificationKey: await loader.load( - `${CircuitId.AtomicQueryMTPV2.toString()}/verification_key.json` - ) - }); - }); - beforeEach(async () => { + circuitStorage = new FSCircuitStorage({ + dirname: path.join(__dirname, './testdata') + }); const memoryKeyStore = new InMemoryPrivateKeyStore(); const bjjProvider = new BjjProvider(KmsKeyType.BabyJubJub, memoryKeyStore); const kms = new KMS(); From 57144cc1ee3327547eda726a087d536d12e57e3e Mon Sep 17 00:00:00 2001 From: vmidyllic <74898029+vmidyllic@users.noreply.github.com> Date: Mon, 31 Jul 2023 18:22:22 +0300 Subject: [PATCH 2/2] fix --- src/storage/fs/circuits-storage.ts | 41 ++++++++++-------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/src/storage/fs/circuits-storage.ts b/src/storage/fs/circuits-storage.ts index 997ea118..557b12f1 100644 --- a/src/storage/fs/circuits-storage.ts +++ b/src/storage/fs/circuits-storage.ts @@ -33,15 +33,19 @@ export interface FSCircuitStorageOptions { * @implements implements ICircuitStorage interface */ export class FSCircuitStorage implements ICircuitStorage { - private readonly _defaultVerificationKeyPath = 'verification_key.json'; - private readonly _defaultProvingKeyPath = 'circuit_final.zkey'; - private readonly _defaultWasmFilePath = 'circuit.wasm'; + private readonly _verificationKeyPath: string = 'verification_key.json'; + private readonly _provingKeyPath: string = 'circuit_final.zkey'; + private readonly _wasmFilePath: string = 'circuit.wasm'; /** * Creates an instance of FSCircuitStorage. * @param {string} opts - options to read / save files */ - constructor(private readonly opts: FSCircuitStorageOptions) {} + constructor(private readonly opts: FSCircuitStorageOptions) { + this._verificationKeyPath = this.opts.verificationFileName ?? this._verificationKeyPath; + this._provingKeyPath = this.opts.provingFileName ?? this._provingKeyPath; + this._wasmFilePath = this.opts.wasmFileName ?? this._wasmFilePath; + } /** * loads circuit data by id from file storage @@ -50,18 +54,9 @@ export class FSCircuitStorage implements ICircuitStorage { * @returns `Promise` */ async loadCircuitData(circuitId: CircuitId): Promise { - const verificationKey = await this.loadCircuitFile( - circuitId, - this.opts.verificationFileName ?? this._defaultVerificationKeyPath - ); - const provingKey = await this.loadCircuitFile( - circuitId, - this.opts.provingFileName ?? this._defaultProvingKeyPath - ); - const wasm = await this.loadCircuitFile( - circuitId, - this.opts.wasmFileName ?? this._defaultWasmFilePath - ); + const verificationKey = await this.loadCircuitFile(circuitId, this._verificationKeyPath); + const provingKey = await this.loadCircuitFile(circuitId, this._provingKeyPath); + const wasm = await this.loadCircuitFile(circuitId, this._wasmFilePath); return { circuitId, @@ -105,25 +100,17 @@ export class FSCircuitStorage implements ICircuitStorage { if (circuitData.verificationKey) { await this.writeCircuitFile( circuitId, - this.opts.verificationFileName ?? this._defaultVerificationKeyPath, + this._verificationKeyPath, circuitData.verificationKey, 'utf-8' ); } if (circuitData.provingKey) { - await this.writeCircuitFile( - circuitId, - this.opts.provingFileName ?? this._defaultProvingKeyPath, - circuitData.provingKey - ); + await this.writeCircuitFile(circuitId, this._provingKeyPath, circuitData.provingKey); } if (circuitData.wasm) { - await this.writeCircuitFile( - circuitId, - this.opts.wasmFileName ?? this._defaultWasmFilePath, - circuitData.wasm - ); + await this.writeCircuitFile(circuitId, this._wasmFilePath, circuitData.wasm); } } }