<!-- canonical: https://0xsimao.com/findings/m-0-nonce-erc5805-raises-nonces -->

# _checkAndIncrementNonce in ERC5805 raises a ReusedNonce error for non used nonces

Low/Info · Three Sigma · Stablecoin framework · 8th January, 2024

Finding 3S-M^0-N13 of the M^0 Minter Gateway security review.

- Protocol: https://www.m0.org/
- Report: /reports/m-0
- Codebase: https://github.com/0xsimao/ttg/tree/a8127901fa1f24a2e821cf4d9854a1aa6ac8088c
- Source: https://cdn.sanity.io/files/qoqld077/production/1cdafafad874aba76e062ad8c216c98338c096db.pdf

---

### Description

The [ERC5805._checkAndIncrementNonce](https://github.com/MZero-Labs/ttg/blob/a8127901fa1f24a2e821cf4d9854a1aa6ac8088c/src/abstract/ERC5805.sol#L49-L57) function makes sure the nonce being used in a
signature is the right one. If it is, it increments it:

```solidity
function _checkAndIncrementNonce(address account_, uint256 nonce_)
internal {
    uint256 currentNonce_ = nonces[account_];
    if (nonce_ != currentNonce_) revert ReusedNonce(nonce_, currentNonce_);
    unchecked {
```

nonces[account_] = currentNonce_ + 1; // Nonce realistically
cannot overflow.

```solidity
}
}
```

The error being raised when nonce_ != currentNonce_ is misleading. The if statement
doesn't necessarily mean that the nonce_ value being used has been used in past
signatures, rather it means that it is not the expected current nonce of the account. For
example, if the current nonce is 1 and the user signs the data with nonce 3, this should
revert, but nonce 3 hasn't been used yet.

### Recommendation

Consider renaming the error to more accurately describe the issue. For example,
NotCurrentNonce.

### Status

Addressed in [#d75ef4a](https://github.com/MZero-Labs/common/commit/d75ef4aff5b7d9a06ed13ca8dc89c9ecc470baad), [#4e20a80](https://github.com/MZero-Labs/ttg/commit/4e20a80138557eb27fc0650d11f7a4b1a0916bfe).
