// SPDX-License-Identifier: MIT pragma solidity 0.8.39; import {Test} from "forge-std/Test.sol"; import {PQRecoveryRegistry} from "./PQRecoveryRegistry.sol"; contract PQRecoveryRegistryTest is Test { PQRecoveryRegistry reg; address alice = makeAddr("alice"); address attacker = makeAddr("attacker"); bytes pk = hex"aabbccdd"; bytes32 constant S1 = keccak256("secret-1"); bytes32 constant S2 = keccak256("secret-2"); function setUp() public { reg = new PQRecoveryRegistry(); vm.warp(1_000_000); } function _register() internal returns (bytes32 c) { c = reg.commitmentOf(1, pk); bytes32 r = reg.rotationHashOf(alice, S1); // compute before prank: a view call would consume it vm.prank(alice); reg.register(1, c, r); } function test_commitmentFormat() public view { assertEq(reg.commitmentOf(1, pk), keccak256(abi.encodePacked(uint8(1), pk))); } function test_register() public { bytes32 c = _register(); PQRecoveryRegistry.Entry memory e = reg.entryOf(alice); assertEq(e.commitment, c); assertEq(e.alg, 1); assertEq(e.registeredAt, 1_000_000); assertTrue(reg.registeredBy(alice, 1_000_000)); assertFalse(reg.registeredBy(alice, 999_999)); assertFalse(reg.registeredBy(attacker, 1e9)); } function test_registerOnce_ecdsaCannotOverwrite() public { bytes32 c = _register(); bytes32 r = reg.entryOf(alice).rotationHash; // Models a quantum attacker who can sign as alice: a second register never changes the entry. A different // commitment is only recorded as a dispute; the same one is refused. vm.warp(block.timestamp + 5); vm.prank(alice); reg.register(1, bytes32(uint256(1)), bytes32(uint256(2))); PQRecoveryRegistry.Entry memory e = reg.entryOf(alice); assertEq(e.commitment, c); assertEq(e.rotationHash, r); assertEq(e.registeredAt, 1_000_000); assertEq(e.contestedAt, 1_000_005); // Later disputes keep the first dispute time. vm.warp(block.timestamp + 5); vm.prank(alice); reg.register(2, bytes32(uint256(3)), 0); assertEq(reg.entryOf(alice).contestedAt, 1_000_005); vm.prank(alice); vm.expectRevert(PQRecoveryRegistry.AlreadyRegistered.selector); reg.register(1, c, 0); } function test_keyAuthorized_directTx() public { // tx.origin == msg.sender: the account's own key signed the transaction. vm.prank(alice, alice); reg.register(1, bytes32(uint256(1)), 0); assertTrue(reg.entryOf(alice).keyAuthorized); } function test_keyAuthorized_viaContractCode() public { // A call that reaches register through code (tx.origin != account) is recorded as not key-authorized. vm.prank(alice, attacker); reg.register(1, bytes32(uint256(1)), 0); assertFalse(reg.entryOf(alice).keyAuthorized); } function test_badInput() public { vm.startPrank(alice); vm.expectRevert(PQRecoveryRegistry.BadInput.selector); reg.register(0, bytes32(uint256(1)), bytes32(uint256(1))); vm.expectRevert(PQRecoveryRegistry.BadInput.selector); reg.register(1, 0, bytes32(uint256(1))); } function test_zeroRotationHashIsPermanent() public { vm.prank(alice); reg.register(1, bytes32(uint256(1)), 0); bytes32 c2 = reg.commitmentOf(2, hex"1234"); bytes32 next = reg.rotationHashOf(alice, S1); // No secret hashes to zero, so nothing can ever rotate it, whatever the next hash. reg.seal(reg.sealOf(alice, 2, c2, next, S1)); reg.seal(reg.sealOf(alice, 2, c2, 0, S1)); vm.warp(block.timestamp + reg.ROTATION_DELAY()); vm.expectRevert(PQRecoveryRegistry.WrongSecret.selector); reg.rotate(alice, 2, c2, next, S1); vm.expectRevert(PQRecoveryRegistry.WrongSecret.selector); reg.rotate(alice, 2, c2, 0, S1); } function test_rotateToPermanent() public { _register(); bytes32 c2 = reg.commitmentOf(2, hex"1234"); reg.seal(reg.sealOf(alice, 2, c2, 0, S1)); vm.warp(block.timestamp + reg.ROTATION_DELAY()); reg.rotate(alice, 2, c2, 0, S1); assertEq(reg.entryOf(alice).rotationHash, 0); } // ---- registerFor ---- function _digest(address account, uint8 alg, bytes32 c, bytes32 r, uint256 deadline) internal view returns (bytes32) { bytes32 sh = keccak256(abi.encode(reg.REGISTER_TYPEHASH(), account, alg, c, r, deadline)); return keccak256(abi.encodePacked("\x19\x01", reg.domainSeparator(), sh)); } function _sign(uint256 pk, bytes32 d) internal pure returns (bytes memory) { (uint8 v, bytes32 r, bytes32 s) = vm.sign(pk, d); return abi.encodePacked(r, s, v); } function test_registerFor_eoa() public { (address bob, uint256 pk) = makeAddrAndKey("bob"); bytes32 c = reg.commitmentOf(1, hex"ab"); bytes32 r = reg.rotationHashOf(bob, S1); uint256 dl = block.timestamp + 1 hours; bytes memory sig = _sign(pk, _digest(bob, 1, c, r, dl)); vm.prank(attacker); // a relayer pays reg.registerFor(bob, 1, c, r, dl, sig); assertEq(reg.entryOf(bob).commitment, c); assertTrue(reg.entryOf(bob).keyAuthorized); // Replay is impossible: the account is registered. vm.expectRevert(PQRecoveryRegistry.AlreadyRegistered.selector); reg.registerFor(bob, 1, c, r, dl, sig); } function test_registerFor_wrongSignerOrFieldsRejected() public { (address bob,) = makeAddrAndKey("bob"); (, uint256 evePk) = makeAddrAndKey("eve"); (, uint256 bobPk) = makeAddrAndKey("bob"); uint256 dl = block.timestamp + 1 hours; bytes memory sig = _sign(evePk, _digest(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl)); vm.expectRevert(PQRecoveryRegistry.BadSignature.selector); reg.registerFor(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl, sig); // Bob's signature does not carry over to a different commitment. sig = _sign(bobPk, _digest(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl)); vm.expectRevert(PQRecoveryRegistry.BadSignature.selector); reg.registerFor(bob, 1, bytes32(uint256(7)), bytes32(uint256(6)), dl, sig); } function test_registerFor_expired() public { (address bob, uint256 pk) = makeAddrAndKey("bob"); uint256 dl = block.timestamp - 1; bytes memory sig = _sign(pk, _digest(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl)); vm.expectRevert(PQRecoveryRegistry.BadDeadline.selector); reg.registerFor(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl, sig); } function test_registerFor_highSRejected() public { (address bob, uint256 pk) = makeAddrAndKey("bob"); uint256 dl = block.timestamp + 1 hours; bytes32 d = _digest(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl); (uint8 v, bytes32 r, bytes32 s) = vm.sign(pk, d); uint256 n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141; bytes memory mall = abi.encodePacked(r, bytes32(n - uint256(s)), v == 27 ? uint8(28) : uint8(27)); vm.expectRevert(PQRecoveryRegistry.BadSignature.selector); reg.registerFor(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl, mall); } function test_registerFor_crossChainRejected() public { (address bob, uint256 pk) = makeAddrAndKey("bob"); uint256 dl = block.timestamp + 1 hours; bytes memory sig = _sign(pk, _digest(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl)); vm.chainId(10); vm.expectRevert(PQRecoveryRegistry.BadSignature.selector); reg.registerFor(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl, sig); } function test_registerFor_erc1271() public { Wallet1271 w = new Wallet1271(); uint256 dl = block.timestamp + 1 hours; w.approve(_digest(address(w), 1, bytes32(uint256(5)), bytes32(uint256(6)), dl)); reg.registerFor(address(w), 1, bytes32(uint256(5)), bytes32(uint256(6)), dl, hex""); assertEq(reg.entryOf(address(w)).commitment, bytes32(uint256(5))); assertFalse(reg.entryOf(address(w)).keyAuthorized); // A digest the wallet did not approve is refused. vm.expectRevert(PQRecoveryRegistry.BadSignature.selector); reg.registerFor(address(w), 1, bytes32(uint256(5)), bytes32(uint256(6)), dl + 1, hex""); } function test_registerFor_noCodeNoSigFails() public { vm.expectRevert(PQRecoveryRegistry.BadSignature.selector); reg.registerFor(alice, 1, bytes32(uint256(5)), bytes32(uint256(6)), block.timestamp, hex""); } function test_registerFor_reverting1271Fails() public { Revert1271 w = new Revert1271(); vm.expectRevert(PQRecoveryRegistry.BadSignature.selector); reg.registerFor(address(w), 1, bytes32(uint256(5)), bytes32(uint256(6)), block.timestamp, hex""); } function test_registerFor_deadlineCap() public { (address bob, uint256 pk) = makeAddrAndKey("bob"); uint256 dl = block.timestamp + reg.MAX_SIGNATURE_LIFETIME(); bytes memory sig = _sign(pk, _digest(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl)); vm.expectRevert(PQRecoveryRegistry.BadDeadline.selector); reg.registerFor(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl, sig); dl = block.timestamp + reg.MAX_SIGNATURE_LIFETIME() - 1; sig = _sign(pk, _digest(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl)); reg.registerFor(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl, sig); } function test_registerFor_7702EoaUsesItsKey() public { (address bob, uint256 pk) = makeAddrAndKey("bob"); Wallet1271 delegate = new Wallet1271(); vm.etch(bob, abi.encodePacked(hex"ef0100", address(delegate))); uint256 dl = block.timestamp + 1 hours; bytes32 d = _digest(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl); // The delegate approving the digest is not enough: a 7702 EOA must sign with its own key. Wallet1271(bob).approve(d); vm.expectRevert(PQRecoveryRegistry.BadSignature.selector); reg.registerFor(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl, hex""); reg.registerFor(bob, 1, bytes32(uint256(5)), bytes32(uint256(6)), dl, _sign(pk, d)); assertTrue(reg.entryOf(bob).keyAuthorized); } function test_spentHashMarked() public { _register(); bytes32 h1 = reg.rotationHashOf(alice, S1); bytes32 c2 = reg.commitmentOf(2, hex"1234"); bytes32 next = reg.rotationHashOf(alice, S2); reg.seal(reg.sealOf(alice, 2, c2, next, S1)); vm.warp(block.timestamp + reg.ROTATION_DELAY()); reg.rotate(alice, 2, c2, next, S1); assertTrue(reg.spent(h1)); assertFalse(reg.spent(next)); } /// The layout a fork reads from state, exactly as the ERC specifies it. function test_storageLayoutPacked() public { vm.prank(alice, alice); reg.register(0x11, bytes32(uint256(0xc0)), bytes32(uint256(0xd0))); vm.warp(block.timestamp + 3); vm.prank(alice); reg.register(0x12, bytes32(uint256(0xc1)), 0); // dispute bytes32 base = keccak256(abi.encode(alice, uint256(0))); assertEq(vm.load(address(reg), base), bytes32(uint256(0xc0))); assertEq(vm.load(address(reg), bytes32(uint256(base) + 1)), bytes32(uint256(0xd0))); uint256 w = uint256(vm.load(address(reg), bytes32(uint256(base) + 2))); assertEq(uint64(w), 1_000_000); // registeredAt, bits 0-63 assertEq(uint64(w >> 64), 1_000_000); // updatedAt, bits 64-127 assertEq(uint32(w >> 128), 0); // nonce, bits 128-159 assertEq(uint8(w >> 160), 0x11); // alg, bits 160-167 assertEq(uint8(w >> 168), 1); // keyAuthorized, bits 168-175 assertEq(uint64(w >> 176), 1_000_003); // contestedAt, bits 176-239 assertEq(w >> 240, 0); bytes32 sh = bytes32(uint256(0x5ea1)); reg.seal(sh); assertEq(uint256(vm.load(address(reg), keccak256(abi.encode(sh, uint256(1))))), block.timestamp); } function test_entriesOf() public { bytes32 c = _register(); address[] memory a = new address[](2); a[0] = alice; a[1] = attacker; PQRecoveryRegistry.Entry[] memory es = reg.entriesOf(a); assertEq(es[0].commitment, c); assertEq(es[1].registeredAt, 0); } function test_rotate() public { _register(); bytes32 c2 = reg.commitmentOf(2, hex"1234"); bytes32 next = reg.rotationHashOf(alice, S2); reg.seal(reg.sealOf(alice, 2, c2, next, S1)); vm.warp(block.timestamp + reg.ROTATION_DELAY()); vm.prank(attacker); // a sponsor may submit; the secret is the authority reg.rotate(alice, 2, c2, next, S1); PQRecoveryRegistry.Entry memory e = reg.entryOf(alice); assertEq(e.commitment, c2); assertEq(e.alg, 2); assertEq(e.nonce, 1); assertEq(e.registeredAt, 1_000_000); // the cutoff anchor never moves assertEq(e.rotationHash, next); } function test_rotate_wrongSecret() public { _register(); bytes32 c2 = reg.commitmentOf(2, hex"1234"); bytes32 next = reg.rotationHashOf(alice, S2); reg.seal(reg.sealOf(alice, 2, c2, next, S2)); vm.warp(block.timestamp + reg.ROTATION_DELAY()); vm.expectRevert(PQRecoveryRegistry.WrongSecret.selector); reg.rotate(alice, 2, c2, next, S2); } function test_rotate_needsAgedSeal() public { _register(); bytes32 c2 = reg.commitmentOf(2, hex"1234"); bytes32 next = reg.rotationHashOf(alice, S2); vm.expectRevert(PQRecoveryRegistry.SealTooYoung.selector); reg.rotate(alice, 2, c2, next, S1); reg.seal(reg.sealOf(alice, 2, c2, next, S1)); vm.warp(block.timestamp + reg.ROTATION_DELAY() - 1); vm.expectRevert(PQRecoveryRegistry.SealTooYoung.selector); reg.rotate(alice, 2, c2, next, S1); } /// The attacker sees S1 in the owner's reveal and seals their own key. Their seal is too young, and once /// the owner's reveal lands S1 is spent, so the attacker's seal is useless. function test_frontrunRevealFails() public { _register(); bytes32 c2 = reg.commitmentOf(2, hex"1234"); bytes32 next = reg.rotationHashOf(alice, S2); reg.seal(reg.sealOf(alice, 2, c2, next, S1)); vm.warp(block.timestamp + reg.ROTATION_DELAY()); bytes32 cA = reg.commitmentOf(2, hex"dead"); bytes32 nextA = reg.rotationHashOf(alice, keccak256("attacker")); reg.seal(reg.sealOf(alice, 2, cA, nextA, S1)); vm.expectRevert(PQRecoveryRegistry.SealTooYoung.selector); reg.rotate(alice, 2, cA, nextA, S1); reg.rotate(alice, 2, c2, next, S1); vm.warp(block.timestamp + reg.ROTATION_DELAY()); vm.expectRevert(PQRecoveryRegistry.WrongSecret.selector); reg.rotate(alice, 2, cA, nextA, S1); } function test_staleSealRejectedAfterRotation() public { _register(); // A seal for the secret-2 epoch, made before secret 2 became live. bytes32 c3 = reg.commitmentOf(3, hex"33"); bytes32 next3 = reg.rotationHashOf(alice, keccak256("s3")); reg.seal(reg.sealOf(alice, 3, c3, next3, S2)); bytes32 c2 = reg.commitmentOf(2, hex"1234"); bytes32 next = reg.rotationHashOf(alice, S2); reg.seal(reg.sealOf(alice, 2, c2, next, S1)); vm.warp(block.timestamp + reg.ROTATION_DELAY()); reg.rotate(alice, 2, c2, next, S1); vm.warp(block.timestamp + reg.ROTATION_DELAY()); vm.expectRevert(PQRecoveryRegistry.SealPredatesUpdate.selector); reg.rotate(alice, 3, c3, next3, S2); } function test_rotationHashBindsAccountAndChain() public { assertTrue(reg.rotationHashOf(alice, S1) != reg.rotationHashOf(attacker, S1)); bytes32 h = reg.rotationHashOf(alice, S1); vm.chainId(10); assertTrue(reg.rotationHashOf(alice, S1) != h); } /// No secret other than the registered one rotates the entry, even with a correctly aged seal. function testFuzz_wrongSecretNeverRotates(bytes32 guess) public { vm.assume(guess != S1 && guess != 0); _register(); bytes32 c2 = reg.commitmentOf(2, hex"1234"); bytes32 next = reg.rotationHashOf(alice, S2); reg.seal(reg.sealOf(alice, 2, c2, next, guess)); vm.warp(block.timestamp + reg.ROTATION_DELAY()); vm.expectRevert(PQRecoveryRegistry.WrongSecret.selector); reg.rotate(alice, 2, c2, next, guess); } /// Registering from any other address never touches alice's entry. function testFuzz_registerOnlyWritesSender(address other, bytes32 c, bytes32 r) public { vm.assume(other != alice && other != address(0) && c != 0); bytes32 mine = _register(); vm.prank(other); reg.register(3, c, r); assertEq(reg.entryOf(alice).commitment, mine); assertEq(reg.entryOf(other).commitment, c); } function test_doubleSeal() public { reg.seal(bytes32(uint256(7))); vm.expectRevert(PQRecoveryRegistry.AlreadySealed.selector); reg.seal(bytes32(uint256(7))); } } contract Wallet1271 { mapping(bytes32 => bool) public ok; function approve(bytes32 d) external { ok[d] = true; } function isValidSignature(bytes32 d, bytes calldata) external view returns (bytes4) { return ok[d] ? bytes4(0x1626ba7e) : bytes4(0xffffffff); } } contract Revert1271 { function isValidSignature(bytes32, bytes calldata) external pure returns (bytes4) { revert(); } } contract PQRecoveryRegistryHardeningTest is Test { PQRecoveryRegistry reg; address alice = makeAddr("alice"); bytes32 constant S1 = keccak256("secret-1"); function setUp() public { reg = new PQRecoveryRegistry(); vm.warp(1_000_000); bytes32 r = reg.rotationHashOf(alice, S1); vm.prank(alice); reg.register(1, bytes32(uint256(1)), r); } function test_sameNextHashRejected() public { bytes32 r = reg.rotationHashOf(alice, S1); reg.seal(reg.sealOf(alice, 2, bytes32(uint256(2)), r, S1)); vm.warp(block.timestamp + reg.ROTATION_DELAY()); vm.expectRevert(PQRecoveryRegistry.BadInput.selector); reg.rotate(alice, 2, bytes32(uint256(2)), r, S1); } function test_zeroSecretRejected() public { address bob = makeAddr("bob"); bytes32 r0 = reg.rotationHashOf(bob, 0); vm.prank(bob); reg.register(1, bytes32(uint256(1)), r0); reg.seal(reg.sealOf(bob, 2, bytes32(uint256(2)), bytes32(uint256(9)), 0)); vm.warp(block.timestamp + reg.ROTATION_DELAY()); vm.expectRevert(PQRecoveryRegistry.BadInput.selector); reg.rotate(bob, 2, bytes32(uint256(2)), bytes32(uint256(9)), 0); } function test_delayIsSevenDays() public view { assertEq(reg.ROTATION_DELAY(), 7 days); } } /// 7702 delegate that registers an attacker-chosen key whenever the EOA receives ETH. contract HookDelegate { PQRecoveryRegistry public immutable reg; constructor(PQRecoveryRegistry r) { reg = r; } receive() external payable { reg.register(0x01, keccak256("attacker pk"), bytes32(0)); } } /// An unrelated dapp the EOA calls: pays the caller (like a withdraw/claim). contract Payer { function withdraw() external { (bool ok,) = msg.sender.call{value: 1}(""); require(ok); } receive() external payable {} } contract PQRecoveryRegistryDelegationTest is Test { PQRecoveryRegistry reg; uint256 pk = 0xA11CE; address eoa; function setUp() public { vm.warp(1_000_000); reg = new PQRecoveryRegistry(); eoa = vm.addr(pk); } function _sig(address acct, uint8 alg, bytes32 c, bytes32 r, uint256 dl) internal view returns (bytes memory) { bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", reg.domainSeparator(), keccak256(abi.encode(reg.REGISTER_TYPEHASH(), acct, alg, c, r, dl)) ) ); (uint8 v, bytes32 rr, bytes32 s) = vm.sign(pk, digest); return abi.encodePacked(rr, s, v); } /// F1 (fixed): a 7702 delegate running inside a tx the EOA sent can register, but never with keyAuthorized. /// Run with --evm-version prague. function test_7702DelegateInSelfSentTx_notKeyAuthorized() public { HookDelegate hook = new HookDelegate(reg); Payer payer = new Payer(); vm.deal(address(payer), 1 ether); vm.signAndAttachDelegation(address(hook), pk); assertEq(eoa.code.length, 23); vm.prank(eoa, eoa); payer.withdraw(); PQRecoveryRegistry.Entry memory e = reg.entryOf(eoa); assertEq(e.commitment, keccak256("attacker pk")); assertFalse(e.keyAuthorized, "delegate code must not earn keyAuthorized"); } /// F1 (fixed): a plain EOA sending its own register still earns keyAuthorized; a delegated one signs instead. function test_plainEoaRegisterKeyAuthorized_delegatedSigns() public { vm.prank(eoa, eoa); reg.register(1, bytes32(uint256(0xA)), bytes32(0)); assertTrue(reg.entryOf(eoa).keyAuthorized); uint256 pk2 = 0xB0B; address eoa2 = vm.addr(pk2); HookDelegate hook = new HookDelegate(reg); vm.signAndAttachDelegation(address(hook), pk2); vm.prank(eoa2, eoa2); reg.register(1, bytes32(uint256(0xA)), bytes32(0)); assertFalse(reg.entryOf(eoa2).keyAuthorized); } /// F2 (fixed): a signature always expires before the earliest rotation, so it cannot be replayed as a contest. function test_noReplayAfterRotation_atBoundary() public { bytes32 secret = keccak256("s"); bytes32 rh = reg.rotationHashOf(eoa, secret); uint256 R = block.timestamp; uint256 dl = R + 7 days; // the old boundary is now refused bytes memory sig = _sig(eoa, 1, bytes32(uint256(0xA)), rh, dl); vm.expectRevert(PQRecoveryRegistry.BadDeadline.selector); reg.registerFor(eoa, 1, bytes32(uint256(0xA)), rh, dl, sig); dl = R + 7 days - 1; sig = _sig(eoa, 1, bytes32(uint256(0xA)), rh, dl); reg.registerFor(eoa, 1, bytes32(uint256(0xA)), rh, dl, sig); reg.seal(reg.sealOf(eoa, 2, bytes32(uint256(0xB)), bytes32(0), secret)); vm.warp(R + 7 days - 1); vm.expectRevert(PQRecoveryRegistry.SealTooYoung.selector); reg.rotate(eoa, 2, bytes32(uint256(0xB)), bytes32(0), secret); vm.warp(R + 7 days); reg.rotate(eoa, 2, bytes32(uint256(0xB)), bytes32(0), secret); vm.prank(address(0xBAD)); vm.expectRevert(PQRecoveryRegistry.BadDeadline.selector); reg.registerFor(eoa, 1, bytes32(uint256(0xA)), rh, dl, sig); assertEq(reg.entryOf(eoa).contestedAt, 0); } /// F3: a conflicting signature can be replayed by anyone until its deadline: event spam, no state change. function test_contestReplaySpam_onlyEvents() public { vm.prank(eoa, eoa); reg.register(1, bytes32(uint256(0xA)), bytes32(0)); uint256 dl = block.timestamp + 1 days; bytes memory sig = _sig(eoa, 1, bytes32(uint256(0xC)), bytes32(0), dl); reg.registerFor(eoa, 1, bytes32(uint256(0xC)), bytes32(0), dl, sig); uint64 first = reg.entryOf(eoa).contestedAt; bytes32 w = vm.load(address(reg), bytes32(uint256(keccak256(abi.encode(eoa, uint256(0)))) + 2)); for (uint256 i; i < 20; ++i) { vm.warp(block.timestamp + 60); vm.expectEmit(address(reg)); emit PQRecoveryRegistry.Contested(eoa, 1, bytes32(uint256(0xC)), true); reg.registerFor(eoa, 1, bytes32(uint256(0xC)), bytes32(0), dl, sig); } assertEq(reg.entryOf(eoa).contestedAt, first); assertEq(vm.load(address(reg), bytes32(uint256(keccak256(abi.encode(eoa, uint256(0)))) + 2)), w); } /// P1/P2 fuzz: any sequence of second registrations never changes anything but contestedAt, set once. function testFuzz_contestsNeverMoveEntry(bytes32[4] memory cs, uint8[4] memory algs, uint32[4] memory dts) public { vm.prank(eoa, eoa); reg.register(1, bytes32(uint256(0xA)), keccak256("r")); PQRecoveryRegistry.Entry memory before = reg.entryOf(eoa); uint64 firstContest; for (uint256 i; i < 4; ++i) { vm.warp(block.timestamp + 1 + dts[i]); bool same = cs[i] == bytes32(uint256(0xA)) && algs[i] == 1; vm.prank(eoa, address(uint160(i))); // also through code (origin != sender) try reg.register(algs[i], cs[i], keccak256(abi.encode(i))) { assertTrue(!same && algs[i] != 0 && cs[i] != 0); if (firstContest == 0) firstContest = uint64(block.timestamp); } catch {} PQRecoveryRegistry.Entry memory a = reg.entryOf(eoa); assertEq(a.commitment, before.commitment); assertEq(a.rotationHash, before.rotationHash); assertEq(a.registeredAt, before.registeredAt); assertEq(a.updatedAt, before.updatedAt); assertEq(a.nonce, before.nonce); assertEq(a.alg, before.alg); assertEq(a.keyAuthorized, before.keyAuthorized); assertEq(a.contestedAt, firstContest); } } /// P5 with every field at its max width. function test_layoutMaxWidths() public { bytes32 secret = keccak256("s"); vm.warp(type(uint64).max - 30 days); bytes32 rh0 = reg.rotationHashOf(eoa, secret); vm.prank(eoa, eoa); reg.register(0xff, bytes32(uint256(1)), rh0); reg.seal(reg.sealOf(eoa, 0xfe, bytes32(uint256(2)), bytes32(0), secret)); bytes32 sh = reg.sealOf(eoa, 0xfe, bytes32(uint256(2)), bytes32(0), secret); vm.warp(block.timestamp + 7 days); vm.prank(eoa, address(1)); reg.register(0x01, bytes32(uint256(3)), 0); // contest reg.rotate(eoa, 0xfe, bytes32(uint256(2)), bytes32(0), secret); uint256 base = uint256(keccak256(abi.encode(eoa, uint256(0)))); uint256 w = uint256(vm.load(address(reg), bytes32(base + 2))); PQRecoveryRegistry.Entry memory e = reg.entryOf(eoa); assertEq(vm.load(address(reg), bytes32(base)), e.commitment); assertEq(uint64(w), e.registeredAt); assertEq(uint64(w >> 64), e.updatedAt); assertEq(uint32(w >> 128), 1); assertEq(uint8(w >> 160), 0xfe); assertEq(uint8(w >> 168), 1); assertEq(uint64(w >> 176), e.contestedAt); assertEq(w >> 240, 0); assertEq(uint256(vm.load(address(reg), keccak256(abi.encode(sh, uint256(1))))), reg.sealedAt(sh)); bytes32 spentKey = keccak256(abi.encode(reg.rotationHashOf(eoa, secret), uint256(2))); assertEq(uint256(vm.load(address(reg), spentKey)), 1); } }