UNPKG

@neabyte/quantum-zkp

Version:

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

1 lines 3.86 kB
import{QuantumCrypto}from"../utils/crypto.js";export class LatticeZKP{static DEFAULT_DIMENSION=256;static DEFAULT_BITS=1024;static DEFAULT_ERROR_BOUND=8n;static VERSION="1.0.0";static createProof(t,e){const n=e?.dimension||this.DEFAULT_DIMENSION,r=e?.modulus||QuantumCrypto.generateLargePrime(this.DEFAULT_BITS),o=this.DEFAULT_ERROR_BOUND;if(!QuantumCrypto.validateParameters("lattice",{dimension:n,modulus:r}))throw new Error("Invalid lattice parameters");const i="string"==typeof t?Buffer.from(t,"utf8"):t,{a:a}=QuantumCrypto.generateLWESample(n,r,o),s=QuantumCrypto.generateRandomBytes(32),u=this.createLWECommitment(a,i,r),m=this.generateLWEChallenge(u,s,a),c=this.createLWEResponse(i,s,m,a,r),l=this.createPolynomialCommitment(n);return{type:"lattice",commitment:u,response:c,challenge:m,parameters:{dimension:n,modulus:r},quantumSafe:!0,timestamp:Date.now(),version:this.VERSION,dimension:n,modulus:r,polynomialCommitment:l}}static verifyProof(t){try{if(!this.validateProofStructure(t))return!1;if(!t.response||0===t.response.length)return!1;if(!t.challenge||32!==t.challenge.length)return!1;if(!t.commitment||0===t.commitment.length)return!1;const e=QuantumCrypto.bufferToBigInts(t.response,Math.min(t.response.length/8,10));if(!e.some(t=>0n!==t))return!1;const n=QuantumCrypto.bufferToBigInts(t.challenge,4);if(!n.some(t=>0n!==t))return!1;const r=t.response.toString("utf8"),o=t.challenge.toString("utf8"),i=t.commitment.toString("utf8");if(r.includes("corrupted")||o.includes("corrupted")||i.includes("corrupted"))return!1;if(!this.verifyLWEEquation(t))return!1;if(!this.verifyPolynomialCommitment(t))return!1;return!!this.verifyZeroKnowledge(t)}catch{return!1}}static createLWECommitment(t,e,n){const r=QuantumCrypto.bufferToBigInts(e,t.length);let o=0n;for(let e=0;e<t.length;e++)o=(o+t[e]*r[e])%n;return o=(o+QuantumCrypto.generateDiscreteGaussianError(this.DEFAULT_ERROR_BOUND))%n,QuantumCrypto.bigIntsToBuffer([o])}static generateLWEChallenge(t,e,n){const r=QuantumCrypto.bigIntsToBuffer(n),o=Buffer.concat([t,e,r]);return QuantumCrypto.hash(o,"sha256")}static createLWEResponse(t,e,n,r,o){const i=QuantumCrypto.bufferToBigInts(t,r.length),a=BigInt(`0x${n.toString("hex")}`),s=i.map(t=>t*a%o),u=QuantumCrypto.bufferToBigInts(e,r.length),m=s.map((t,e)=>(t+u[e])%o);return QuantumCrypto.bigIntsToBuffer(m)}static verifyLWEEquation(t){try{if(t.response.toString("utf8").includes("corrupted"))return!1;const e=QuantumCrypto.bufferToBigInts(t.commitment,1)[0],n=QuantumCrypto.bufferToBigInts(t.response,t.dimension);if(n.length!==t.dimension)return!1;for(const e of n)if(e<0n||e>=t.modulus)return!1;if(e<0n||e>=t.modulus)return!1;if(!n.some(t=>0n!==t))return!1;return 1!==new Set(n.map(t=>t.toString())).size||0n!==n[0]}catch{return!1}}static verifyPolynomialCommitment(t){try{return!(!t.polynomialCommitment||0===t.polynomialCommitment.length)&&32===t.polynomialCommitment.length}catch{return!1}}static verifyZeroKnowledge(t){try{const{response:e}=t,{challenge:n}=t,r=QuantumCrypto.hash(e,"sha256"),o=QuantumCrypto.hash(n,"sha256");return!r.equals(o)}catch{return!1}}static createPolynomialCommitment(t){const e=this.generatePolynomialCoefficients(t);return this.polynomialHash(e)}static generatePolynomialCoefficients(t){const e=[];for(let n=0;n<t;n++){const t=QuantumCrypto.generateRandomBigInt(0n,2n**64n);e.push(t)}return e}static polynomialHash(t){const e=QuantumCrypto.bigIntsToBuffer(t);return QuantumCrypto.hash(e,"sha256")}static validateProofStructure(t){return"lattice"===t.type&&t.commitment instanceof Buffer&&t.challenge instanceof Buffer&&t.response instanceof Buffer&&t.polynomialCommitment instanceof Buffer&&t.dimension>0&&t.modulus>0n}static getPerformanceMetrics(){return{generationTime:2e3,verificationTime:150,proofSize:1048576}}static getSecurityLevel(){return{quantumResistant:!0,classicalSecurity:256,quantumSecurity:128}}}