const { expect } = require("chai"); const { ethers } = require("hardhat"); describe("MLDSA65Verifier: FIPS 204 ML-DSA-65 on chain", function () { this.timeout(600_000); let V, ml, sdk, kd, pk, sk, dataA, dataB; const ctx = ethers.toUtf8Bytes("HASHFORT_V3_OP"); before(async () => { ({ ml_dsa65: ml } = await import("@noble/post-quantum/ml-dsa.js")); sdk = await import("../sdk/mldsa65.mjs"); V = await (await ethers.getContractFactory("MLDSA65Verifier")).deploy(); kd = await (await ethers.getContractFactory("KeyData")).deploy(); ({ publicKey: pk, secretKey: sk } = ml.keygen(ethers.getBytes(ethers.id("mldsa test seed")))); const { A, B } = sdk.precompute(pk); const store = async (d) => { const tx = await kd.store(d); const r = await tx.wait(); return ethers.getCreateAddress({ from: await kd.getAddress(), nonce: r.nonce === undefined ? (await ethers.provider.getTransactionCount(await kd.getAddress())) - 1 : 0 }); }; // deterministic: KeyData uses CREATE, nonces start at 1 await (await kd.store(A)).wait(); await (await kd.store(B)).wait(); dataA = ethers.getCreateAddress({ from: await kd.getAddress(), nonce: 1 }); dataB = ethers.getCreateAddress({ from: await kd.getAddress(), nonce: 2 }); expect((await ethers.provider.getCode(dataA)).length).to.equal(2 + 2 * (A.length + 1)); }); it("accepts signatures made by the browser library, for many messages", async () => { for (let i = 0; i < 5; i++) { const msg = ethers.getBytes(ethers.id("op " + i)); const sig = ml.sign(msg, sk, { context: ctx }); expect(sig.length).to.equal(3309); expect(await V.verify(msg, ctx, sig, dataA, dataB)).to.equal(true, "message " + i); } }); it("rejects a different message, context, key data or any tampered byte", async () => { const msg = ethers.getBytes(ethers.id("withdraw everything")); const sig = ml.sign(msg, sk, { context: ctx }); expect(await V.verify(ethers.getBytes(ethers.id("withdraw everything!")), ctx, sig, dataA, dataB)).to.equal(false); expect(await V.verify(msg, ethers.toUtf8Bytes("OTHER"), sig, dataA, dataB)).to.equal(false); expect(await V.verify(msg, "0x", sig, dataA, dataB)).to.equal(false); expect(await V.verify(msg, ctx, sig, dataB, dataA)).to.equal(false); for (const i of [0, 47, 48, 1000, 3247, 3248, 3300, 3308]) { const s = sig.slice(); s[i] ^= 0x04; expect(await V.verify(msg, ctx, s, dataA, dataB)).to.equal(false, "byte " + i); } expect(await V.verify(msg, ctx, sig.slice(0, 3308), dataA, dataB)).to.equal(false); }); it("rejects a valid signature from another key", async () => { const other = ml.keygen(ethers.getBytes(ethers.id("other"))); const msg = ethers.getBytes(ethers.id("x")); expect(await V.verify(msg, ctx, ml.sign(msg, other.secretKey, { context: ctx }), dataA, dataB)).to.equal(false); }); it("checkExpansion proves the stored data is FIPS 204 for this public key, and catches a wrong key", async () => { expect(await V.checkExpansion(pk, dataA, dataB, { gasLimit: 400_000_000 })).to.equal(true); const other = ml.keygen(ethers.getBytes(ethers.id("other"))).publicKey; expect(await V.checkExpansion(other, dataA, dataB, { gasLimit: 400_000_000 })).to.equal(false); }); it("measures gas", async () => { const msg = ethers.getBytes(ethers.id("gas")); const sig = ml.sign(msg, sk, { context: ctx }); const g = await V.verify.estimateGas(msg, ctx, sig, dataA, dataB); const ge = await V.checkExpansion.estimateGas(pk, dataA, dataB, { gasLimit: 400_000_000 }); console.log(` verify: ${g.toLocaleString()} gas ยท checkExpansion (eth_call audit): ${ge.toLocaleString()} gas`); }); });