UNPKG

@neabyte/quantum-zkp

Version:

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

1 lines 3.18 kB
import{QuantumCrypto}from"../utils/crypto.js";export class HashZKP{static DEFAULT_CHAIN_LENGTH=1e3;static VERSION="1.0.0";static createProof(e,t){const a=t?.chainLength||this.DEFAULT_CHAIN_LENGTH;if(!QuantumCrypto.validateParameters("hash",{chainLength:a}))throw new Error("Invalid hash parameters");const r="string"==typeof e?Buffer.from(e,"utf8"):e,n=QuantumCrypto.generateRandomBytes(32),s=QuantumCrypto.generateRandomBytes(16),h=Buffer.concat([r,s]),o=this.createHashChain(h,a),i=o[0],c=this.generateChallenge(i,n),u=this.createResponse(r,n,c),{tree:m,root:f}=QuantumCrypto.generateMerkleTree(o),l=QuantumCrypto.generateMerkleProof(m,0);return{type:"hash",commitment:i,challenge:c,response:u,parameters:{chainLength:a},quantumSafe:!0,timestamp:Date.now(),version:this.VERSION,chainLength:a,hashChain:o,commitmentChain:f,merkleTree:m,merkleProof:l}}static verifyProof(e){try{if(!this.validateProofStructure(e))return!1;if(!e.hashChain||0===e.hashChain.length)return!1;if(!QuantumCrypto.verifyMerkleProof(e.hashChain[0],e.merkleProof,e.commitmentChain,0))return!1;if(!this.verifyHashChain(e.hashChain))return!1;return!!e.hashChain[0].equals(e.commitment)&&(!(!e.response||64!==e.response.length)&&!(!e.challenge||32!==e.challenge.length))}catch{return!1}}static createHashChain(e,t){const a=[];let r=e;for(let e=0;e<t;e++)r=QuantumCrypto.hash(r,"sha256"),a.push(r);return a}static verifyHashChain(e){if(e.length<2)return!1;for(let t=1;t<e.length;t++){if(!QuantumCrypto.hash(e[t-1],"sha256").equals(e[t]))return!1}return!0}static generateChallenge(e,t){const a=Buffer.concat([e,t]);return QuantumCrypto.hash(a,"sha256")}static createResponse(e,t,a){const r=QuantumCrypto.hash(e,"sha256"),n=QuantumCrypto.hmac(a,r,"sha256"),s=t.length>=32?t.slice(0,32):Buffer.concat([t,Buffer.alloc(32-t.length)]),h=n.length>=32?n.slice(0,32):Buffer.concat([n,Buffer.alloc(32-n.length)]);return Buffer.concat([s,h])}static validateProofStructure(e){return"hash"===e.type&&e.commitment instanceof Buffer&&e.challenge instanceof Buffer&&e.response instanceof Buffer&&e.hashChain instanceof Array&&e.hashChain.length>0&&e.chainLength>0&&void 0!==e.merkleTree&&void 0!==e.merkleProof}static createOptimizedHashChain(e,t){const a=[];let r=e;for(let e=0;e<t;e+=100){const n=Math.min(100,t-e),s=this.createHashChain(r,n);a.push(...s),r=s[s.length-1]}return a}static createMultiAlgorithmHashChain(e,t){const a=[];let r=e;for(let e=0;e<t;e++){const t=e%3==0?"sha256":e%3==1?"sha384":"sha512";r=QuantumCrypto.hash(r,t),a.push(r)}return a}static createQuantumResistantHashChain(e,t){const a=[];let r=e;for(let e=0;e<t;e++){const e=QuantumCrypto.generateRandomBytes(16),t=Buffer.concat([r,e]);r=QuantumCrypto.hash(t,"sha512"),a.push(r)}return a}static getPerformanceMetrics(){return{generationTime:1e3,verificationTime:50,proofSize:524288}}static getSecurityLevel(){return{quantumResistant:!0,classicalSecurity:256,quantumSecurity:128}}static benchmarkHashChain(e){const t=performance.now(),a=process.memoryUsage().heapUsed,r=QuantumCrypto.generateRandomBytes(32);this.createHashChain(r,e);const n=performance.now(),s=process.memoryUsage().heapUsed;return{time:Math.max(.001,n-t),memory:Math.max(1,s-a)}}}