On Fri, Jun 1, 2018 at 1:03 PM, Johnson Lau <jl2012@xbt.hk> wrote:
On 1 Jun 2018, at 11:03 PM, Russell O'Connor <roconnor@blockstream.io> wrote:
On Thu, May 31, 2018 at 2:35 PM, Johnson Lau via bitcoin-dev <bitcoin-dev@lists.linuxfoundation.org> wrote:

  Double SHA256 of the serialization of:

Should we replace the Double SHA256 with a Single SHA256?  There is no possible length extension attack here.  Or are we speculating that there is a robustness of Double SHA256 in the presence of SHA256 breaking?

I suggest putting `sigversion` at the beginning instead of the end of the format.  Because its value is constant, the beginning of the SHA-256 computation could be pre-computed in advance.  Furthermore, if we make the `sigversion` exactly 64-bytes long then the entire first block of the SHA-256 compression function could be pre-computed.

Can we add CHECKSIGFROMSTACK or do you think that would go into a separate BIP?

I think it’s just a tradition to use double SHA256. One reason we might want to keep dSHA256 is a blind signature might be done by giving only the single SHA256 hash to the signer. At the same time, a non-Bitcoin signature scheme might use SHA512-SHA256. So a blind signer could distinguish the message type without learning the message.


I make it a 0x01000000 at the end of the message because the last 4 bytes has been the nHashType in the legacy/BIP143 protocol. Since the maximum legacy nHashType is 0xff, no collision could ever occur.

Putting a 64-byte constant at the beginning should also work, since a collision means SHA256 is no longer preimage resistance. I don’t know much about SHA256 optimisation. How good it is as we put a 64-byte constant at the beginning, while we also make the message 64-byte longer?

In theory, having a fixed 64 byte constant at the beginning results in zero overhead for those 64 bytes.  An implementation would just start the usual SHA-256 algorithm with a different pre-computed and fixed initial value than SHA-256's standard initial value.  The SHA-256 padding counter would also need to start at 64*8 bits rather than starting at 0 bits.  In practice, assuming a OpenSSL-like implementation of SHA-256, it should be easy to implement this optimization. One would replace SHA256_Init call with a variant that initializes the SHA256_CTX to this pre-computed value and sets SHA256_CTX's num counter to the appropriate value.  Non-optimized implementations can still just add the 64 byte prefix and use any SHA-256 implementation.

For CHECKSIGFROMSTACK (CSFS), I think the question is whether we want to make it as a separate opcode, or combine that with CHECKSIG. If it is a separate opcode, I think it should be a separate BIP. If it is combined with CHECKSIG, we could do something like this: If the bit 10 of SIGHASH2 is set, CHECKSIG will pop one more item from stack, and serialize its content with the transaction digest. Any thought?

I prefer a different opcode for CHECKSIGFROMSTACK because I dislike opcodes that pop a non-static number of elements off the stack.  Popping a dynamic number of stack elements makes it more difficult to validate that a Script pubkey doesn't allow any funny business.