-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Reuse sighash computations across evaluation #4562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Have you actually benchmarked this? The vast majority of CHECKMULTISIG's are just two signatures, three at most. |
|
No we didn't and probably the gains won't be very big. I'm also still thinking what good be a way to benchmark this... |
|
#4555 is removing a feature in EvalScript() that lets the callee determine what type of sighashes a scriptSig contains. For instance I might want to verify that a scriptSig only contains SIGHASH_ANYONECANPAY sigs to see if I can add/remove inputs. As for why each CHECKSIG can use a different nHashType and thus sighash, that's just flexibility. For instance GreenAddress.it's nLockTime scheme uses that feature - the refund scripts that they sign with nLockTime are signed with ANYONECANPAY|NONE letting the other key do whatever they want too. What do you mean by "why they're are placed after the signatures instead of somewhere else"? |
|
I don't understand that use case. Is the refund script signed with anyone can pay and something else at the same time? Is it multisig? Probably is too late to change that, but I would like to understand if there was a motivation I'm missing or it was just a design mistake. |
|
@jtimon Yeah, it's multisig. The vchSig.back() bit is just standard conservatism to make sure a signature can't be reused for something unintended. I remember finding a case where that prevented a security issue awhile back, although for the life of me I don't remember the details exactly. Making the sighash be a part of the CTxIn structure doesn't really make sense in a system with scripts that's meant to be expendable. Anyway CPU usage isn't the bottleneck for any of this stuff. |
|
@simondlr see #4498 for style |
|
@jtimon That's a bit of a stretch. I wouldn't call it a design flaw just because it's not understood (it adds a tiny bit of complexity, and has no known drawbacks for performance or security). |
|
@jtimon reread my answer above pointing out how Green address already makes use of the fact that there can be more than one SIGHASH flag in a deployed, production, application. Premature optimisation is the root of all evil; the scalability problems Bitcoin has aren't going to be solved with little single digit improvements everywhere at the cost of making the protocol less generic. |
|
@sipa I was going to say that it adds some complexity and minimally hurts for no good reason (that I know). |
|
@jtimon I'd be inclined to make Bitcoin more generic, not less, in the event of a protocol redesign. That CHECKSIG is multiple operations combined into one is a mistake, not something we should push even further. |
|
I've rewritten it to use statically allocated hashes rather than an std::map, so it doesn't even have dynamic memory overhead. I think the improvement is small, but maybe worthwhile. I'm not going to spend more time on it, however. |
|
Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/p4562_31dd5b37272f162155b3dd944a6f8c2b74c7448c/ for binaries and test log. |
|
Initially I thought the optimization was going to be a side effect in some refactorings I'm working on. Thank you @sipa for showing me that this is the best I could do optimization-wise and realizing that wasn't going to be the case. This PR has been definitely useful for me, but since we know the optimization will be small and only for multisig, I'm not convinced that the additional software complexity is worth it. |
|
Needs rebase, and hasn't received any interest. Closing. |
(As suggested by @jtimon)
We're currently recomputing the signature hash multiple times during an OP_CHECKMULTISIG validation, mostly all with the same data and nHashType anyway. Implement a small per-evaluation cache to reuse those values.