hyperledger / besu-native

Apache License 2.0
12 stars 35 forks source link

Simple implementation of mimc, light-tested #105

Closed AlexandreBelling closed 1 year ago

AlexandreBelling commented 1 year ago

Add an implementation of MiMC on the bn254 scalar field in rust.

MIMC5 is a hash function suited for BN256's scalar field.

It has modulus p = 21888242871839275222246405745257275088548364400416034343698204186575808495617

The MIMC paper (https://eprint.iacr.org/2016/492.pdf) states that monomial x^d is a permutation in Fp if gcd(d,p) == 1. Exponent for MIMC is 5, which satisfies the above condition.

fn mimc5_permutation(constants, x, k):
    h := x;
    for i = 0..constants.len() {
        h := h + k + constants[i]
        h := h^5;
    }
    h = h + k;
    return h;

Interface

pub fn mimc_hash<T : AsRef<[u8]>>(bytes: T) -> Vec<u8>;

The implementation targets full consistency with the present implementation:

Consistency testing with gnark's implementation

gnark and the present implementation are tested to give the same result for the strings.

github-advanced-security[bot] commented 1 year ago

You have successfully added a new CodeQL configuration /language:java. As part of the setup process, we have scanned this repository and found 7 existing alerts. Please check the repository Security tab to see all alerts.