<!-- canonical: https://0xsimao.com/findings/singularity-generating-numbers-smaller-doing -->

# Generating numbers smaller than P by doing % P might be vulnerable

Low/Info · Three Sigma · Private DeFi access · 26th February, 2024

Finding 3S-SG-L07 of the Singularity security review.

- Protocol: https://www.thesingularity.network/
- Report: /reports/singularity
- Source: https://cdn.sanity.io/files/qoqld077/production/45b8aac56a2b8e3b557df4329ac9bf8220b64012.pdf

---

### Description

```solidity
For example, in BaseInputBuilder::_bytifyToNoir()
_bytifyToNoir(address value) internal view returns (bytes32) {
    return bytes32(uint256(keccak256(abi.encode(value))) % _primeField);
}
```

1) the hashes are not unique, num A and num B = A + P will yield the same %
2) doing % P introduces a bias in the hash where smaller values are more frequent. This
makes it easier to brute force solutions
Thus, it may be possible (not sure about the required power) to specify a different address
that leads to the same number supplief in the proof
See this [writeup](https://medium.com/aztec-protocol/vulnerabilities-found-in-aztec-2-0-9b80c8bf416c), Bug: Generating prime field elements.
Possible attack scenario: withdraw with a different address, leading to lost funds.

### Recommendation

Use the Mimc254 contract to generate safer hashes.

### Status

Addressed in [#8b300f0](https://github.com/portalgateme/darkpool-v1-zk-contracts-fork/commit/8b300f07a28f2c073154c8de3e9e90ba64ef1934).
