Draft: Do not use ctypes
This is a version of the blst binding without ctypes. It gives better performance for the scalar field operations, around 20-25%. For the others primitives, the allocation is too small to give a significant improvement.
It also allows more granularity at a lower level. The code is verbose on purpose. The code could be simplified by using preprocessor directive like for the custom blocks boilerplate or for G1/G2 primitives.
We could add a lower
Benchmarka
Before:
dune exec ./benchmark/bench_fr.exe
Estimated testing time 1m40s (10 benchmarks x 10s). Change using '-quota'.
┌───────────────────┬──────────────┬────────────┬──────────┬──────────┬────────────┐
│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ Percentage │
├───────────────────┼──────────────┼────────────┼──────────┼──────────┼────────────┤
│ Addition Fr │ 81.38ns │ 18.00w │ │ │ 0.05% │
│ Multiplication Fr │ 97.13ns │ 18.00w │ │ │ 0.06% │
│ Opposite Fr │ 79.46ns │ 18.00w │ │ │ 0.05% │
│ Substraction Fr │ 77.91ns │ 18.00w │ │ │ 0.05% │
│ Square Fr │ 93.35ns │ 18.00w │ │ │ 0.06% │
│ Inverse Fr │ 2_290.31ns │ 76.00w │ │ │ 1.48% │
│ Pow Fr │ 154_402.28ns │ 27_686.63w │ 115.29w │ 115.29w │ 100.00% │
│ Double Fr │ 91.12ns │ 18.00w │ │ │ 0.06% │
│ of_bytes_exn Fr │ 294.60ns │ 48.00w │ │ │ 0.19% │
│ to_bytes Fr │ 171.43ns │ 28.00w │ │ │ 0.11% │
└───────────────────┴──────────────┴────────────┴──────────┴──────────┴────────────┘
After:
> dune exec ./benchmark/bench_fr.exe
Estimated testing time 1m40s (10 benchmarks x 10s). Change using '-quota'.
┌───────────────────┬──────────────┬────────────┬──────────┬──────────┬────────────┐
│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ Percentage │
├───────────────────┼──────────────┼────────────┼──────────┼──────────┼────────────┤
│ Addition Fr │ 59.95ns │ 3.00w │ │ │ 0.05% │
│ Multiplication Fr │ 78.34ns │ 3.00w │ │ │ 0.06% │
│ Opposite Fr │ 59.30ns │ 3.00w │ │ │ 0.05% │
│ Substraction Fr │ 59.85ns │ 3.00w │ │ │ 0.05% │
│ Square Fr │ 77.55ns │ 3.00w │ │ │ 0.06% │
│ Inverse Fr │ 2_365.98ns │ 23.00w │ │ │ 1.84% │
│ Pow Fr │ 128_776.93ns │ 12_522.02w │ 48.92w │ 48.92w │ 100.00% │
│ Double Fr │ 59.49ns │ 3.00w │ │ │ 0.05% │
│ of_bytes_exn Fr │ 212.43ns │ 14.00w │ │ │ 0.16% │
│ to_bytes Fr │ 120.60ns │ 9.00w │ │ │ 0.09% │
└───────────────────┴──────────────┴────────────┴──────────┴──────────┴────────────┘
Benchmarks in other libraries:
- Poseidon128 (https://gitlab.com/dannywillems/ocaml-ec) Before:
> dune exec ./benches/bench_poseidon_orchard.exe
Estimated testing time 10s (1 benchmarks x 10s). Change using '-quota'.
┌──────────────────────────────────────────────────────────────────────────────────────────┬──────────┬─────────┬──────────┬──────────┬────────────┐
│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ Percentage │
├──────────────────────────────────────────────────────────────────────────────────────────┼──────────┼─────────┼──────────┼──────────┼────────────┤
│ Benchmark one permutation of Poseidon from Orchard with blst backend on an input of 3 el │ 168.14us │ 29.39kw │ 13.94w │ 13.94w │ 100.00% │
│ ement │ │ │ │ │ │
└──────────────────────────────────────────────────────────────────────────────────────────┴──────────┴─────────┴──────────┴──────────┴────────────┘
After:
> dune exec ./benches/bench_poseidon_orchard.exe
Estimated testing time 10s (1 benchmarks x 10s). Change using '-quota'.
┌──────────────────────────────────────────────────────────────────────────────────────────┬──────────┬─────────┬──────────┬──────────┬────────────┐
│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ Percentage │
├──────────────────────────────────────────────────────────────────────────────────────────┼──────────┼─────────┼──────────┼──────────┼────────────┤
│ Benchmark one permutation of Poseidon from Orchard with blst backend on an input of 3 el │ 115.90us │ 4.91kw │ 1.60w │ 1.60w │ 100.00% │
│ ement │ │ │ │ │ │
└──────────────────────────────────────────────────────────────────────────────────────────┴──────────┴─────────┴──────────┴──────────┴────────────┘