UNPKG

@neabyte/quantum-zkp

Version:

Educational quantum-resistant zero-knowledge proof library for learning and prototyping

1 lines 5.66 kB
import{LatticeZKP}from"../algorithms/lattice.js";import{HashZKP}from"../algorithms/hash.js";import{MultivariateZKP}from"../algorithms/multivariate.js";import{HybridZKP}from"../algorithms/hybrid.js";import{QuantumCrypto}from"../utils/crypto.js";export class QuantumZKP{static VERSION="1.0.0";static LEARNING_USE=["learning","prototyping"];static EDUCATIONAL_LIMITATIONS=["educational implementation","not production-ready"];static DEFAULT_CONFIG={defaultAlgorithm:"hash",algorithms:{lattice:{name:"lattice",securityLevel:{quantumResistant:!0,classicalSecurity:256,quantumSecurity:128,recommendedUse:this.LEARNING_USE},performanceProfile:{generationTime:2e3,verificationTime:150,proofSize:1048576,memoryUsage:1048576},recommendedFor:["learning lattice cryptography","understanding LWE concepts"],limitations:this.EDUCATIONAL_LIMITATIONS},hash:{name:"hash",securityLevel:{quantumResistant:!0,classicalSecurity:256,quantumSecurity:128,recommendedUse:this.LEARNING_USE},performanceProfile:{generationTime:1e3,verificationTime:50,proofSize:524288,memoryUsage:524288},recommendedFor:["learning hash-based cryptography","understanding hash chains"],limitations:this.EDUCATIONAL_LIMITATIONS},multivariate:{name:"multivariate",securityLevel:{quantumResistant:!0,classicalSecurity:256,quantumSecurity:128,recommendedUse:this.LEARNING_USE},performanceProfile:{generationTime:3e3,verificationTime:200,proofSize:2097152,memoryUsage:2097152},recommendedFor:["learning polynomial cryptography","understanding multivariate systems"],limitations:this.EDUCATIONAL_LIMITATIONS},hybrid:{name:"hybrid",securityLevel:{quantumResistant:!0,classicalSecurity:512,quantumSecurity:256,recommendedUse:this.LEARNING_USE},performanceProfile:{generationTime:6e3,verificationTime:400,proofSize:4194304,memoryUsage:4194304},recommendedFor:["learning multi-algorithm cryptography","understanding defense-in-depth"],limitations:this.EDUCATIONAL_LIMITATIONS}},batchSize:10,enableCaching:!0,enableParallelProcessing:!0,securityLevel:"standard"};config;constructor(e){this.config={...QuantumZKP.DEFAULT_CONFIG,...e}}createProof(e,t=this.config.defaultAlgorithm,r){switch(t){case"lattice":return LatticeZKP.createProof(e,r);case"hash":return HashZKP.createProof(e,r);case"multivariate":return MultivariateZKP.createProof(e,r);case"hybrid":return HybridZKP.createProof(e,r);default:throw new Error(`Unsupported algorithm: ${t}`)}}verifyProof(e){const t=performance.now();let r,a=!1;try{switch(e.type){case"lattice":a=LatticeZKP.verifyProof(e);break;case"hash":a=HashZKP.verifyProof(e);break;case"multivariate":a=MultivariateZKP.verifyProof(e);break;case"hybrid":a=HybridZKP.verifyProof(e);break;default:r=`Unknown proof type: ${e.type}`,a=!1}}catch(e){r=e instanceof Error?e.message:"Unknown verification error",a=!1}const i=performance.now()-t,o={isValid:a,algorithm:e.type,verificationTime:Math.max(i,.001)};return r&&(o.error=r),o}createThresholdProof(e,t=3,r=this.config.defaultAlgorithm){if(t<2)throw new Error("Number of parties must be at least 2");const a=this.shareSecret(e,t),i=[];for(const e of a){const t=this.createProof(e,r);i.push(t)}const o=this.createReconstructionKey(a);return{shares:a,proofs:i,parties:t,algorithm:r,quantumSafe:!0,threshold:Math.ceil(t/2),reconstructionKey:o}}batchCreateProofs(e,t=this.config.defaultAlgorithm,r){const a={parallel:!0,batchSize:this.config.batchSize,...r},i=[];if(a.parallel)for(let r=0;r<e.length;r+=a.batchSize){const o=e.slice(r,r+a.batchSize).map(e=>this.createProof(e,t));if(i.push(...o),a.progressCallback){const t=Math.min((r+a.batchSize)/e.length,1);a.progressCallback(t)}}else for(const r of e){const e=this.createProof(r,t);i.push(e)}return i}getPerformanceMetrics(e){switch(e){case"lattice":return{...LatticeZKP.getPerformanceMetrics(),memoryUsage:1048576};case"hash":return{...HashZKP.getPerformanceMetrics(),memoryUsage:524288};case"multivariate":return{...MultivariateZKP.getPerformanceMetrics(),memoryUsage:2097152};case"hybrid":return{...HybridZKP.getPerformanceMetrics(),memoryUsage:4194304};default:throw new Error(`Unsupported algorithm: ${e}`)}}getSecurityLevel(e){switch(e){case"lattice":return{...LatticeZKP.getSecurityLevel(),recommendedUse:["learning","prototyping"]};case"hash":return{...HashZKP.getSecurityLevel(),recommendedUse:["learning","prototyping"]};case"multivariate":return{...MultivariateZKP.getSecurityLevel(),recommendedUse:["learning","prototyping"]};case"hybrid":return{...HybridZKP.getSecurityLevel(),recommendedUse:["learning","prototyping"]};default:throw new Error(`Unsupported algorithm: ${e}`)}}benchmarkAllAlgorithms(){const e=["lattice","hash","multivariate","hybrid"],t=[];for(const r of e){const e=performance.now(),a=process.memoryUsage().heapUsed,i=Buffer.from("benchmark-test-secret");this.createProof(i,r);const o=performance.now(),s=process.memoryUsage().heapUsed,n=this.getPerformanceMetrics(r);t.push({algorithm:r,operationsPerSecond:1e3/Math.max(n.generationTime,.001),averageProofSize:n.proofSize,averageGenerationTime:n.generationTime,averageVerificationTime:n.verificationTime,memoryUsage:s-a,cpuUsage:Math.max((o-e)/1e3,.001)})}return t}getAlgorithmConfig(e){const t=this.config.algorithms[e];if(!t)throw new Error(`Unsupported algorithm: ${e}`);return t}getVersion(){return QuantumZKP.VERSION}shareSecret(e,t){const r="string"==typeof e?Buffer.from(e,"utf8"):e,a=[];for(let e=0;e<t;e++){const e=QuantumCrypto.hash(r,"sha256");a.push(e)}return a}createReconstructionKey(e){const t=Buffer.concat(e);return QuantumCrypto.hash(t,"sha256")}getSupportedAlgorithms(){return["lattice","hash","multivariate","hybrid"]}isQuantumResistant(e){return this.getSecurityLevel(e).quantumResistant}}