const { expect } = require("chai"); const { ethers } = require("hardhat"); describe("SLHDSA128sVerifier: FIPS 205 SLH-DSA-SHA2-128s on chain", function () { this.timeout(900_000); let V, slh, pk, sk; const ctx = ethers.toUtf8Bytes("HASHFORT_V3_RECOVERY"); before(async () => { ({ slh_dsa_sha2_128s: slh } = await import("@noble/post-quantum/slh-dsa.js")); V = await (await ethers.getContractFactory("SLHDSA128sVerifier")).deploy(); ({ publicKey: pk, secretKey: sk } = slh.keygen(ethers.getBytes(ethers.id("slh seed")).slice(0, 48).length === 32 ? new Uint8Array(48).fill(9) : new Uint8Array(48).fill(9))); expect(pk.length).to.equal(32); }); let sig, msg; it("accepts a signature made by the browser library", async () => { msg = ethers.getBytes(ethers.id("recover to new key")); sig = slh.sign(msg, sk, { context: ctx }); expect(sig.length).to.equal(7856); expect(await V.verify(msg, ctx, sig, ethers.hexlify(pk))).to.equal(true); }); it("rejects a different message, context, key or any tampered byte", async () => { expect(await V.verify(ethers.getBytes(ethers.id("recover to another key")), ctx, sig, ethers.hexlify(pk))).to.equal(false); expect(await V.verify(msg, ethers.toUtf8Bytes("OTHER"), sig, ethers.hexlify(pk))).to.equal(false); const pk2 = pk.slice(); pk2[31] ^= 1; expect(await V.verify(msg, ctx, sig, ethers.hexlify(pk2))).to.equal(false); for (const i of [0, 15, 16, 100, 2927, 2928, 3500, 5000, 7855]) { const s = sig.slice(); s[i] ^= 0x01; expect(await V.verify(msg, ctx, s, ethers.hexlify(pk))).to.equal(false, "byte " + i); } expect(await V.verify(msg, ctx, sig.slice(0, 7855), ethers.hexlify(pk))).to.equal(false); }); it("accepts a second, independent signature", async () => { const m2 = ethers.getBytes(ethers.id("second")); expect(await V.verify(m2, ctx, slh.sign(m2, sk, { context: ctx }), ethers.hexlify(pk))).to.equal(true); }); it("measures gas", async () => { console.log(` verify: ${(await V.verify.estimateGas(msg, ctx, sig, ethers.hexlify(pk))).toLocaleString()} gas`); }); });