-
Notifications
You must be signed in to change notification settings - Fork 168
SigType check for cost-analysis in SegwitV1 scripts #340
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
SigType check for cost-analysis in SegwitV1 scripts #340
Conversation
sanket1729
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let some comments. I think we can clean this up a lot if we move the Ctx as suggested. Please try that and let me know if there are any issues.
src/policy/compiler.rs
Outdated
| + match Ctx::sig_type() { | ||
| SigType::Ecdsa => 73.0 * k as f64, | ||
| SigType::Schnorr => 64. * k as f64, | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You would need a from_multi_a here. Add appropriate unreachable! depending on context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 2565f5e.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the implementation of from_multi_a is missing here for CompilerExtData. Note that the multi_a fragment operates differently from multi as it consumes empty signatures for dissatisfactions.
src/policy/compiler.rs
Outdated
| dissat_cost: Some(33.0), | ||
| sat_cost: match Ctx::sig_type() { | ||
| SigType::Ecdsa => 33.0, | ||
| SigType::Schnorr => 32., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be 33 because we also include the push_bytes in the cost.
src/policy/compiler.rs
Outdated
| sat_cost: 73.0, | ||
| sat_cost: match Ctx::sig_type() { | ||
| SigType::Ecdsa => 73., | ||
| SigType::Schnorr => 64., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should probably be 66 so that we include the varint prefix and the hash type flag. This is because the key itself is 64 bytes. It can optionally contain a sighash flag (1 byte) and there is also a pre-fix of varint type denoting the length of sig that follows. It will end up as <var_int_prefix><sig><sighash_type> on the stack.
We should also add a comment explaining why this is 66 and we need to update this everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 2565f5e
src/policy/compiler.rs
Outdated
| }) | ||
| } | ||
|
|
||
| fn and_n(a: Self, b: Self) -> Result<Self, types::ErrorKind> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self and reviewers: This is not new code added. This is moved.
src/policy/compiler.rs
Outdated
| let lookup_ext = |n| match n { | ||
| 0 => Some(l.comp_ext_data), | ||
| 1 => Some(r.comp_ext_data), | ||
| 0 => Some(l.comp_ext_data.clone()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need .clone() here? CompExtData is already Copy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the previous version having CompilerExtData<Ctx: ScriptKey>, the closure type changes to FnOnce since it moves the variable l out of its environment. This SO post answers for this issue.
b3825d3 to
2565f5e
Compare
sanket1729
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there
src/policy/compiler.rs
Outdated
| dissat_cost: Some(1.0 + 34.0), | ||
| sat_cost: match Ctx::sig_type() { | ||
| SigType::Ecdsa => 73.0 + 34.0, | ||
| SigType::Schnorr => 66.0 + 32.0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They key cost here and elsewhere would be 33 instead of 32
src/policy/compiler.rs
Outdated
| + match Ctx::sig_type() { | ||
| SigType::Ecdsa => 73.0 * k as f64, | ||
| SigType::Schnorr => 64. * k as f64, | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the implementation of from_multi_a is missing here for CompilerExtData. Note that the multi_a fragment operates differently from multi as it consumes empty signatures for dissatisfactions.
2565f5e to
9d8402f
Compare
src/miniscript/types/correctness.rs
Outdated
| } | ||
|
|
||
| fn from_multi(_: usize, _: usize) -> Self { | ||
| fn from_multi<Ctx: ScriptContext>(_: usize, _: usize) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need ctx here? multi should have SigType Ecdsa and multi_a should have Schnorr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would also resolve the awkward things that you had to do later with the 0 value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I introduced this to keep a check on misuse of SigType, but that is indeed taken care of in the consensus checks. Will update that in the following commit!
7cb61e1 to
25560a3
Compare
sanket1729
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code review ACK 25560a3
747876e Rectify `from_multi_a` max satisfaction size (Aman Rojjha) Pull request description: Cost incurred for a single Schnorr signature in the witness stack := `<var_int><64-byte signature><sig_type>` = 1 + 64 + 1 (as clarified by @sanket1729 in #340 ). ACKs for top commit: sanket1729: ACK 747876e Tree-SHA512: f20652240f1daa7b33b1d525870114e0bd6412e82845e80a94c616dedcfb3d8ae2f7ef19d7c08760f79270e0d465c18b713eff2e3e599b45c65f2a64da36713b
25560a3 to
60d6956
Compare
|
@sanket1729 I overlooked that you merged #346 and not this PR. My apologoies! |
ba29af1 to
63d8687
Compare
|
@sanket1729 @tcharding The Integration test fails at this run here because of curl stopping abruptly which should not have been the case. Kindly take a look into that, thanks! curl --connect-timeout 5 \
--max-time 10 \
--retry 5 \
--retry-delay 0 \
--retry-max-time 40 \
https://bitcoincore.org/bin/bitcoin-core-$BITCOINVERSION/bitcoin-$BITCOINVERSION-x86_64-linux-gnu.tar.gzP.S. Test passes on my fork and the next run :p. |
63d8687 to
52d6647
Compare
Unfortunately, there is little we can do if we face network connection issues. Trying again is the best solution we have. |
sanket1729
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 52d6647
…is in SegwitV1 scripts
52d66477274e9afa770fdb61f53ec8b13f68d750 Add SigType check in CompilerExtData for deciding key byte-lengths (Aman Rojjha)
Pull request description:
Previously, the struct `CompilerExtData` provided cost analysis for a given script considering the **SegwitV0** standards. This PR aims to track the type/ (context) of the script and provide the respective cost analysis.
ACKs for top commit:
sanket1729:
ACK 52d66477274e9afa770fdb61f53ec8b13f68d750
Tree-SHA512: 55f9ac4fb0de151fa3613195494d9c9fd7ba7e0e1982a0494d510a8f38e939a15d7b7a210ecccfed04af0947d2208e4edea501e2b93dfa220320c0a279cf65bd


Previously, the struct
CompilerExtDataprovided cost analysis for a given script considering the SegwitV0 standards. This PR aims to track the type/ (context) of the script and provide the respective cost analysis.