poseidonHash function
Computes the Poseidon hash of inputs over the BN254 scalar field.
Supports 1–16 inputs directly, and 17–32 inputs by splitting the list in half and hashing recursively. Throws an ArgumentError for an empty list and an Exception for more than 32 inputs.
Implementation
BigInt poseidonHash(List<BigInt> inputs) {
if (inputs.isEmpty) {
throw ArgumentError('poseidonHash requires at least one input');
}
try {
if (inputs.length <= poseidonNumToHashFN.length) {
final hashFN = poseidonNumToHashFN[inputs.length - 1];
return hashFN(inputs);
} else if (inputs.length <= 32) {
final hash1 = inputs.sublist(0, 16);
final hash2 = inputs.sublist(16);
return poseidonHash([poseidonHash(hash1), poseidonHash(hash2)]);
} else {
throw Exception(
'Yet to implement: Unable to hash a vector of length ${inputs.length}',
);
}
} catch (e) {
throw Exception('poseidonHash error: $e');
}
}