Make VerifyWitnessProgram use a Span stack #18388

pull sipa wants to merge 1 commits into bitcoin:master from sipa:202003_span_witstack changing 2 files +23 −7
  1. sipa commented at 11:25 pm on March 19, 2020: member

    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).

  2. fanquake deleted a comment on Mar 19, 2020
  3. fanquake requested review from ajtowns on Mar 19, 2020
  4. fanquake added the label Validation on Mar 19, 2020
  5. DrahtBot commented at 0:12 am on March 20, 2020: member

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #18267 (BIP-325: Signet [consensus] by kallewoof)
    • #17977 ([WIP] Implement BIP 340-342 validation (Schnorr/taproot/tapscript) by sipa)
    • #16653 (script: add simple signature support (checker/creator) by kallewoof)
    • #13062 (Make script interpreter independent from storage type CScript by sipa)

    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.

  6. instagibbs commented at 6:54 pm on March 20, 2020: member

    utACK https://github.com/bitcoin/bitcoin/pull/18388/commits/c7b0131e3bc784263ce59c837be7d3ede59c45cd

    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.

  7. sipa commented at 6:57 pm on March 20, 2020: member
    @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).
  8. instagibbs commented at 6:59 pm on March 20, 2020: member
    @sipa took the words right out of my mouth :) I think the latter is perfect for our use-case
  9. sipa force-pushed on Mar 21, 2020
  10. sipa commented at 1:30 am on March 21, 2020: member
    @instagibbs Done.
  11. theStack approved
  12. theStack commented at 1:09 pm on March 21, 2020: member
    Code-Review ACK https://github.com/bitcoin/bitcoin/commit/ef540f796adef1f128584bd073076062585f0bd9 (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.)
  13. in src/span.h:63 in ef540f796a outdated
    58@@ -57,4 +59,13 @@ constexpr Span<A> MakeSpan(A (&a)[N]) { return Span<A>(a, N); }
    59 template<typename V>
    60 constexpr Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type> MakeSpan(V& v) { return Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type>(v.data(), v.size()); }
    61 
    62+/** Pop the last element off a span, and return a reference to that element. */
    63+template<typename T>
    


    elichai commented at 10:41 am on March 22, 2020:

    nit, missing space(git-clang-format)

    0template <typename T>
    

    sipa commented at 9:45 pm on March 23, 2020:
    Ok.
  14. elichai commented at 10:42 am on March 22, 2020: contributor
    tACK ef540f796adef1f128584bd073076062585f0bd9 Read the code, couldn’t spot any logic change. ran unit tests+functional tests
  15. in src/span.h:66 in ef540f796a outdated
    58@@ -57,4 +59,13 @@ constexpr Span<A> MakeSpan(A (&a)[N]) { return Span<A>(a, N); }
    59 template<typename V>
    60 constexpr Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type> MakeSpan(V& v) { return Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type>(v.data(), v.size()); }
    61 
    62+/** Pop the last element off a span, and return a reference to that element. */
    63+template<typename T>
    64+T& SpanPopBack(Span<T>& span)
    65+{
    66+    T& back = span.back();
    


    promag commented at 12:01 pm on March 23, 2020:
    assert size > 0?

    sipa commented at 9:45 pm on March 23, 2020:
    Ok.
  16. in src/span.h:31 in ef540f796a outdated
    26@@ -27,6 +27,8 @@ class Span
    27     constexpr C* data() const noexcept { return m_data; }
    28     constexpr C* begin() const noexcept { return m_data; }
    29     constexpr C* end() const noexcept { return m_data + m_size; }
    30+    constexpr C& front() const noexcept { return m_data[0]; }
    


    promag commented at 12:04 pm on March 23, 2020:
    No strong opinion but maybe assert in these?

    elichai commented at 12:26 pm on March 23, 2020:
    FWIW the stl version currently in gcc trunk doesn’t assert anything(as usual in stl) https://godbolt.org/z/QNytT8

    instagibbs commented at 2:51 pm on March 23, 2020:
    for size checks? It’s UB in stl so I figure just keep that behavior eh?

    sipa commented at 8:25 pm on March 23, 2020:

    I’d rather not add asserts here. std::span doesn’t have them, so if we’re expecting to someday replace Span with that, it should be subject to the same interface. And that interface requires the caller to make sure values are not out of range.

    Spans may also be used in pretty tight loops some day, in which case asserts may be actually undesirable.


    promag commented at 8:51 pm on March 23, 2020:
    Makes sense, you can resolve this.
  17. promag commented at 12:04 pm on March 23, 2020: member
    Code review ACK.
  18. Make VerifyWitnessProgram use a Span stack
    This allows for very cheap transformations on the range of elements that
    are to be passed to ExecuteWitnessScript.
    2b0fcff7f2
  19. sipa force-pushed on Mar 23, 2020
  20. elichai commented at 10:13 pm on March 23, 2020: contributor
    ReACK on the diff 2b0fcff7f26d59fed4bcafd1602325122a206c67
  21. ajtowns commented at 5:06 am on March 24, 2020: member
    ACK 2b0fcff7f26d59fed4bcafd1602325122a206c67
  22. in src/span.h:31 in 2b0fcff7f2
    27@@ -27,6 +28,8 @@ class Span
    28     constexpr C* data() const noexcept { return m_data; }
    29     constexpr C* begin() const noexcept { return m_data; }
    30     constexpr C* end() const noexcept { return m_data + m_size; }
    31+    constexpr C& front() const noexcept { return m_data[0]; }
    


    jnewbery commented at 7:14 pm on March 24, 2020:
    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.
  23. jnewbery commented at 7:14 pm on March 24, 2020: member
    utACK 2b0fcff7f26d59fed4bcafd1602325122a206c67
  24. laanwj added this to the milestone 0.20.0 on Mar 26, 2020
  25. fanquake merged this on Mar 27, 2020
  26. fanquake closed this on Mar 27, 2020

  27. jasonbcox referenced this in commit 65d338095b on Nov 22, 2020
  28. kittywhiskers referenced this in commit 6e94d52251 on Mar 10, 2021
  29. kittywhiskers referenced this in commit c910f43b2c on Mar 23, 2021
  30. kittywhiskers referenced this in commit 5e9ba680fa on Mar 23, 2021
  31. kittywhiskers referenced this in commit 70bbf19a83 on Apr 18, 2021
  32. kittywhiskers referenced this in commit 9304ba296f on Apr 23, 2021
  33. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-21 18:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me