kadena-io / pact

The Pact Smart Contract Language
https://docs.kadena.io/build/pact
BSD 3-Clause "New" or "Revised" License
579 stars 100 forks source link

Edmund/continue poseidon #1313

Closed edmundnoble closed 10 months ago

edmundnoble commented 10 months ago

This PR supersedes #1274.

Remaining work: check test vectors, check gas. Edit: both done.

The performance improvements in this PR go from this:

defaultMain [bench "poseidon" $ whnf poseidon [1,2,3,4,5,6,7,8]]
benchmarking poseidon
time                 1.696 ms   (1.686 ms .. 1.710 ms)
                     0.999 R²   (0.999 R² .. 1.000 R²)
mean                 1.704 ms   (1.695 ms .. 1.722 ms)
std dev              41.47 μs   (25.65 μs .. 74.70 μs)
variance introduced by outliers: 13% (moderately inflated)

to this:

defaultMain [bench "poseidon" $ whnf poseidon [1,2,3,4,5,6,7,8]]
benchmarking poseidon
time                 1.222 ms   (1.215 ms .. 1.230 ms)
                     0.999 R²   (0.999 R² .. 1.000 R²)
mean                 1.222 ms   (1.212 ms .. 1.244 ms)
std dev              49.37 μs   (21.97 μs .. 99.33 μs)

and from this:

defaultMain [bench "poseidon" $ whnf poseidon [1,2]]
benchmarking poseidon
time                 182.7 μs   (182.1 μs .. 183.3 μs)
                     1.000 R²   (1.000 R² .. 1.000 R²)
mean                 182.0 μs   (181.2 μs .. 182.7 μs)
std dev              2.628 μs   (2.202 μs .. 3.368 μs)

to this:

defaultMain [bench "poseidon" $ whnf poseidon [1,2]]
benchmarking poseidon
time                 159.5 μs   (155.2 μs .. 164.8 μs)
                     0.996 R²   (0.993 R² .. 0.999 R²)
mean                 155.5 μs   (153.7 μs .. 157.8 μs)
std dev              6.763 μs   (5.227 μs .. 10.27 μs)
variance introduced by outliers: 43% (moderately inflated)

PR checklist:

Additionally, please justify why you should or should not do the following:

edmundnoble commented 10 months ago

Gas graph: 2023-10-26-172425_666x447_scrot The actual inputs don't affect the time taken to compute the hash from what I can tell, only the number of them, so that's the X axis. The Y axis is micros * 2.5, i.e. gas as a unit of time.

edmundnoble commented 10 months ago

Here's where exactly the constants came from: https://github.com/iden3/circomlibjs/blob/main/src/poseidon_constants.js. I've patched the PR to use the exact same constants (in hex, just like they do it) so it's easier to compare. I have verified by hand that the output for these test vectors matches the output from https://github.com/iden3/circomlibjs/blob/main/src/poseidon_reference.js.