File size: 7,270 Bytes
fbb0715 e621c1d fbb0715 e621c1d fbb0715 e621c1d fbb0715 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
import { RingLwe } from './lattice';
import { ParamValidator, ValidationResult, Severity } from './validator';
export enum NoiseType {
Uniform = "Uniform",
Gaussian = "Gaussian",
Cbd = "Cbd",
}
export interface FuzzConfig {
iterations: number;
n: number;
q: number;
noiseParam: number;
noiseType: NoiseType;
parallel: boolean;
fuzzEdgeCases: boolean;
fuzzCiphertextMalleability: boolean;
}
export interface FuzzStats {
iterations: number;
failures: number;
failureRate: number;
edgeCaseFailures: number;
malleabilityPanics: number;
avgKeygenUs: number;
avgEncryptUs: number;
avgDecryptUs: number;
totalElapsedMs: number;
dualAttackComplexityBits: number;
primalAttackBits: number;
keyRecoveryBits: number;
sideChannelIndex: number;
observedNoiseMax: number;
theoreticalFailureProb: number;
expectedFailures: number;
}
export enum Verdict {
Pass = "Pass",
CorrectnessFail = "CorrectnessFail",
SecurityFail = "SecurityFail",
CriticalFail = "CriticalFail",
}
export interface FuzzResult {
config: FuzzConfig;
stats: FuzzStats;
validation: ValidationResult;
verdict: Verdict;
recommendations: string[];
}
export class FuzzEngine {
private config: FuzzConfig;
constructor(config: FuzzConfig) {
this.config = config;
}
public async run(): Promise<FuzzResult> {
const start = performance.now();
// 1. Parameter Validation
let validation: ValidationResult;
switch (this.config.noiseType) {
case NoiseType.Cbd:
validation = ParamValidator.validateKyber(this.config.n, this.config.q, this.config.noiseParam);
break;
case NoiseType.Gaussian:
default:
validation = ParamValidator.validateGaussian(this.config.n, this.config.q, this.config.noiseParam);
break;
}
// 2. Correctness Trials
const trials = await this.runCorrectnessTrials();
// 3. Edge Cases
const edgeCaseFailures = this.config.fuzzEdgeCases ? await this.runEdgeCases() : 0;
// 4. Malleability
const malleabilityPanics = this.config.fuzzCiphertextMalleability ? await this.runMalleability() : 0;
const failures = trials.filter(t => !t.success).length;
const failureRate = failures / (trials.length || 1);
const avgKeygenUs = trials.reduce((acc, t) => acc + t.keygenUs, 0) / (trials.length || 1);
const avgEncryptUs = trials.reduce((acc, t) => acc + t.encryptUs, 0) / (trials.length || 1);
const avgDecryptUs = trials.reduce((acc, t) => acc + t.decryptUs, 0) / (trials.length || 1);
const totalElapsedMs = performance.now() - start;
// 5. Security Estimation
const security = this.estimateSecurity();
const stats: FuzzStats = {
iterations: trials.length,
failures,
failureRate,
edgeCaseFailures,
malleabilityPanics,
avgKeygenUs,
avgEncryptUs,
avgDecryptUs,
totalElapsedMs,
dualAttackComplexityBits: security.dual,
primalAttackBits: security.primal,
keyRecoveryBits: security.keyRec,
sideChannelIndex: security.sc,
observedNoiseMax: 0,
theoreticalFailureProb: validation.failureProb,
expectedFailures: validation.expectedFailuresIn500 * (this.config.iterations / 500.0),
};
const verdict = this.determineVerdict(stats, validation);
const recommendations = this.buildRecommendations(stats, validation);
return {
config: this.config,
stats,
validation,
verdict,
recommendations,
};
}
private async runCorrectnessTrials() {
const trials = [];
const lwe = new RingLwe(
this.config.n,
this.config.q,
this.config.noiseParam,
this.config.noiseType === NoiseType.Cbd
);
for (let i = 0; i < this.config.iterations; i++) {
const t0 = performance.now();
const { pk, sk } = lwe.keygen();
const keygenUs = (performance.now() - t0) * 1000;
const ptxt = Math.random() > 0.5 ? 1 : 0;
const t1 = performance.now();
const ct = lwe.encrypt(pk, ptxt as 0 | 1);
const encryptUs = (performance.now() - t1) * 1000;
const t2 = performance.now();
const dtxt = lwe.decrypt(sk, ct);
const decryptUs = (performance.now() - t2) * 1000;
trials.push({
success: ptxt === dtxt,
keygenUs,
encryptUs,
decryptUs
});
// Yield occasionally to prevent UI freezing
if (i > 0 && i % 100 === 0) {
await new Promise(r => setTimeout(r, 0));
}
}
return trials;
}
private async runEdgeCases(): Promise<number> {
const lwe = new RingLwe(
this.config.n,
this.config.q,
this.config.noiseParam,
this.config.noiseType === NoiseType.Cbd
);
let failures = 0;
// Zero messages
for (let i = 0; i < 10; i++) {
const { pk, sk } = lwe.keygen();
const ct = lwe.encrypt(pk, 0);
if (lwe.decrypt(sk, ct) !== 0) failures++;
}
// One messages
for (let i = 0; i < 10; i++) {
const { pk, sk } = lwe.keygen();
const ct = lwe.encrypt(pk, 1);
if (lwe.decrypt(sk, ct) !== 1) failures++;
}
return failures;
}
private async runMalleability(): Promise<number> {
const lwe = new RingLwe(
this.config.n,
this.config.q,
this.config.noiseParam,
this.config.noiseType === NoiseType.Cbd
);
const { pk, sk } = lwe.keygen();
const ct = lwe.encrypt(pk, 1);
// Corrupt v component
ct.v[0] = (ct.v[0] + 1) % this.config.q;
try {
lwe.decrypt(sk, ct);
return 0; // Decryption should not throw even on corrupted data
} catch {
return 1;
}
}
private estimateSecurity() {
const n = this.config.n;
const q = this.config.q;
const sigma = this.config.noiseParam;
const ratio = Math.log2(q / Math.max(sigma, 0.1));
const dual = 0.265 * n * ratio;
const primal = dual * 1.05;
const keyRec = (n * Math.log2(q)) / (Math.max(sigma, 0.1) * 8.0);
const sc = Math.max(0.1, Math.min(15.0, sigma * 2.0 + n / 200.0));
return { dual, primal, keyRec, sc };
}
private determineVerdict(stats: FuzzStats, val: ValidationResult): Verdict {
const correctnessFail = stats.failures > 0 || stats.edgeCaseFailures > 0;
const securityFail = stats.dualAttackComplexityBits < 80.0 || !val.passes;
if (correctnessFail && securityFail) return Verdict.CriticalFail;
if (correctnessFail) return Verdict.CorrectnessFail;
if (securityFail) return Verdict.SecurityFail;
return Verdict.Pass;
}
private buildRecommendations(stats: FuzzStats, val: ValidationResult): string[] {
const recs = [];
if (stats.failures > 0) {
recs.push(`⚠️ ${stats.failures} / ${stats.iterations} failures. Noise ${val.noiseBudgetRms.toFixed(1)} ≥ q/4. Reduce β or increase q.`);
}
if (stats.dualAttackComplexityBits < 80.0) {
recs.push(`🔴 Security critically low (${stats.dualAttackComplexityBits.toFixed(0)} bits).`);
}
if (stats.failures === 0 && val.passes) {
recs.push(`✅ All trials passed. Parameters look sound.`);
}
return recs;
}
}
|