ElementsProject / rust-elements

Rust support for Elements transaction/block deserialization
Creative Commons Zero v1.0 Universal
50 stars 31 forks source link

Secp operations during txid computation #202

Open RCasatta opened 2 months ago

RCasatta commented 2 months ago

In a performance profile in a downstream project I noticed txid() is taking too much time even considering it's an expensive computation, in the flame graph there were calls to secp function which I don't think should happen?

Can share the performance graph if can help understand

apoelstra commented 2 months ago

Sure. What secp function was it?

RCasatta commented 2 months ago

I had to leave the PC... I remember fe_sqrt and others, will post tomorrow

RCasatta commented 2 months ago

Here are a convenient screenshot and the profiler json (to be loaded from chrome dev tools, performance tab)

For what I understand is how the PedersenCommitment is serialized with secp256k1_pedersen_commitment_serialize

image

Trace-20240510T085107.json

apoelstra commented 2 months ago

Ok, yeah, the issue is in libsecp256k1-zkp, not here. For some bizarre reason secp256k1_pedersen_commitment_serialize decompresses the point then recompresses it, when all it needs to do is a memcmp (I think). So this function is wasting a lot of time for no reason.

RCasatta commented 1 week ago

Issue has been fixed in the upstream lib, I think we need

apoelstra commented 5 days ago

Bumping secp-zkp requires bumping secp, which requires bumping bitcoin, which requires bumping bitcoind, which requires bumping elementsd.

I am almost done this. Opened https://github.com/RCasatta/elementsd/pull/18

apoelstra commented 5 days ago

The next version of rust-bitcoin will break out the "types that never change" into a new primitives crate which should be sufficient for most of the other crates in the ecosystem, and which should rarely have major version bumps, which will make this all way less painful.