Backport of core#30186 and core#30306[[https://github.com/bitcoin/bitcoin/pull/30186 | core#30186]] and [[https://github.com/bitcoin/bitcoin/pull/30306 | core#30306]].
Note that we don't have the fuzz test yet, the point is to get the module up-to-date with upstream.
Depends on D16505.