Skip to content
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

Make VerifyWitnessProgram use a Span stack #18388

Merged
merged 1 commit into from
Mar 27, 2020

Conversation

sipa
Copy link
Member

@sipa sipa commented Mar 19, 2020

Here is a follow-up to #18002, again with the goal of simplifying (potential) BIP341 code.

Instead of passing a begin and end iterator of the initial stack to ExecuteWitnessScript, they are turned into a Span<const valtype>, representing a span of valtypes in memory. This allows VerifyWitnessProgram to operate on that span directly, instead of juggling iterators around (which would be exacerbated by #17977 if trying to avoid copying the stack).

@DrahtBot
Copy link
Contributor

DrahtBot commented Mar 20, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@instagibbs
Copy link
Member

instagibbs commented Mar 20, 2020

utACK c7b0131

wondering if there's some short-hand we could do to "split" the Spans or something like pop_back which also returns the popped element since that seems to be a common use-case of it now and going forward.

@sipa
Copy link
Member Author

sipa commented Mar 20, 2020

@instagibbs No reason why a pop_back couldn't be added to Span, but since C++20's std::span doesn't have it, I'd rather not in order to maintain compatibility. Maybe a utility function T& PopSpan(Span<T>& span) is useful (which pops, and returns the popped element).

@instagibbs
Copy link
Member

@sipa took the words right out of my mouth :) I think the latter is perfect for our use-case

@sipa sipa force-pushed the 202003_span_witstack branch from c7b0131 to ef540f7 Compare March 21, 2020 01:26
@sipa
Copy link
Member Author

sipa commented Mar 21, 2020

@instagibbs Done.

@instagibbs
Copy link
Member

utACK ef540f7

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code-Review ACK ef540f7
(By the way, getting used to Span, I'm more and more surprised that such a useful lightweight representation hasn't been in the C++ standard for years.)

Copy link
Contributor

@elichai elichai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK ef540f7
Read the code, couldn't spot any logic change.
ran unit tests+functional tests

src/span.h Outdated Show resolved Hide resolved
Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK.

src/span.h Outdated Show resolved Hide resolved
src/span.h Show resolved Hide resolved
This allows for very cheap transformations on the range of elements that
are to be passed to ExecuteWitnessScript.
@sipa sipa force-pushed the 202003_span_witstack branch from ef540f7 to 2b0fcff Compare March 23, 2020 21:45
@elichai
Copy link
Contributor

elichai commented Mar 23, 2020

ReACK on the diff 2b0fcff

@ajtowns
Copy link
Contributor

ajtowns commented Mar 24, 2020

ACK 2b0fcff

@theStack
Copy link
Contributor

re-ACK 2b0fcff

@instagibbs
Copy link
Member

re-ACK 2b0fcff

Copy link
Contributor

@jnewbery jnewbery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 2b0fcff

@@ -27,6 +28,8 @@ class Span
constexpr C* data() const noexcept { return m_data; }
constexpr C* begin() const noexcept { return m_data; }
constexpr C* end() const noexcept { return m_data + m_size; }
constexpr C& front() const noexcept { return m_data[0]; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewers: these functions front() and back() are no longer used in this PR, but are used in the taproot implementation, so there's no harm in including them here.

@Empact
Copy link
Contributor

Empact commented Mar 26, 2020

ACK 2b0fcff

@laanwj laanwj added this to the 0.20.0 milestone Mar 26, 2020
@fanquake fanquake merged commit 5464616 into bitcoin:master Mar 27, 2020
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Nov 22, 2020
Summary:
2b0fcff7f26d59fed4bcafd1602325122a206c67 Make VerifyWitnessProgram use a Span stack (Pieter Wuille)

Pull request description:

  Here is a follow-up to #18002, again with the goal of simplifying (potential) BIP341 code.

  Instead of passing a begin and end iterator of the initial stack to `ExecuteWitnessScript`, they are turned into a `Span<const valtype>`, representing a span of `valtype`s in memory. This allows `VerifyWitnessProgram` to operate on that span directly, instead of juggling iterators around (which would be exacerbated by #17977 if trying to avoid copying the stack).

---

Ignored the SegWit stuff but brought in the Span functionality, might be
used in the future also less merge conflicts

Backport of Core [[bitcoin/bitcoin#18388 | PR18388]]

Test Plan:
  ninja

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D8495
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.