Bitcoin Forum
April 26, 2024, 12:28:27 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1] 2 3 »  All
  Print  
Author Topic: Number of m-of-n ouputs per transaction  (Read 6116 times)
No_2 (OP)
Hero Member
*****
Offline Offline

Activity: 901
Merit: 1031


BTC: the beginning of stake-based public resources


View Profile
April 26, 2014, 03:18:16 PM
Last edit: April 23, 2015, 10:02:58 AM by No_2
Merited by ABCbits (1)
 #1

How many m-of-n contracts can exsist as outputs for a transaction?

For example can I have a 3BTC input and spend 1BTC to a 2-of-3, 1BTC to a 5-of-5 and 1BTC to a 7-of-11 on the same contract transaction?

Or can only 1 m-of-n exsist per transaction?
1714134507
Hero Member
*
Offline Offline

Posts: 1714134507

View Profile Personal Message (Offline)

Ignore
1714134507
Reply with quote  #2

1714134507
Report to moderator
1714134507
Hero Member
*
Offline Offline

Posts: 1714134507

View Profile Personal Message (Offline)

Ignore
1714134507
Reply with quote  #2

1714134507
Report to moderator
In order to achieve higher forum ranks, you need both activity points and merit points.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714134507
Hero Member
*
Offline Offline

Posts: 1714134507

View Profile Personal Message (Offline)

Ignore
1714134507
Reply with quote  #2

1714134507
Report to moderator
1714134507
Hero Member
*
Offline Offline

Posts: 1714134507

View Profile Personal Message (Offline)

Ignore
1714134507
Reply with quote  #2

1714134507
Report to moderator
1714134507
Hero Member
*
Offline Offline

Posts: 1714134507

View Profile Personal Message (Offline)

Ignore
1714134507
Reply with quote  #2

1714134507
Report to moderator
fbueller
Sr. Member
****
Offline Offline

Activity: 412
Merit: 266


View Profile
April 30, 2014, 12:10:09 AM
 #2

How many m-of-n contracts can exsist as outputs for a transaction?

For example can I have a 3BTC input and spend 1BTC to a 2-of-3, 1BTC to a 5-of-5 and 1BTC to a 7-of-11 on the same contract?

Or can only 1 m-of-n exsist per transaction?

Its no different to a normal transactions limits on the number of vouts. They wouldn't be on the same contract. Each vout is separate to the rest.

Whysis you think it would be different?

Bitwasp Developer.
No_2 (OP)
Hero Member
*****
Offline Offline

Activity: 901
Merit: 1031


BTC: the beginning of stake-based public resources


View Profile
April 30, 2014, 01:03:32 PM
 #3

How many m-of-n contracts can exsist as outputs for a transaction?

For example can I have a 3BTC input and spend 1BTC to a 2-of-3, 1BTC to a 5-of-5 and 1BTC to a 7-of-11 on the same contract?

Or can only 1 m-of-n exsist per transaction?

Its no different to a normal transactions limits on the number of vouts. They wouldn't be on the same contract. Each vout is separate to the rest.

Whysis you think it would be different?

I should have been clearer:

Quote
For example can I have a 3BTC input and spend it to three outputs: 1BTC to a 2-of-3, 1BTC to a 5-of-5 and 1BTC to a 7-of-11 on the same contract transaction?
No_2 (OP)
Hero Member
*****
Offline Offline

Activity: 901
Merit: 1031


BTC: the beginning of stake-based public resources


View Profile
April 30, 2014, 01:34:10 PM
Last edit: April 30, 2014, 03:05:07 PM by No_2
 #4

Yes as long as the outputs don't exceed the the value amount of the inputs. There are some other rules that that restrict you thou, a 7-11 redeem script would be huge, and may exceed the amount of bytes that is allowed.

Thanks, this is what I thought would be the case. I understand there is a byte limit, but not sure if this is a 'hard limit', or just tends to incur fees as I also understand that there are fees (by consensus) associated with transactions over a certain byte size. E.g. if you create a transaction that aggregates many small inputs and this takes up a lot of data on the blockchain then it incurs a fee.

So, assuming the inputs are always more than or equal to the outputs, my questions are:

1. It is possible to have multiple m-of-n contracts as outputs for a transaction? From what I understand this should be possible but costly due to the bytes required to script the transaction.

2. Is there a size above which a miner (by consensus) will not mine a transaction to a block? You mention the number of bytes that are allowed? Is there a hard limit here or does the fee just increase above a certain byte size in relation to how many extra bytes are required to write the transaction to the blockchain?

3. I have been told that their is a hard limit of 20-of-20 for any given contract, as per the source code in version 0.8. I have also now read that their is a limit of 16-of-16 which is true? Or is it more complicated than that?
aceat64
Full Member
***
Offline Offline

Activity: 307
Merit: 102



View Profile
April 30, 2014, 03:56:19 PM
Merited by ABCbits (1)
 #5

If you are sending to P2SH addresses then your initial transaction works just like normal. The usual size, fees and dust limits apply. This is because the P2SH addresses are just regular addresses other than the fact they are a hash of a script instead of the hash of a public key.

Where things can get trickier is in redeeming any of those outputs in subsequent transactions. As stated earlier, the redemption script for a 7-of-11 might be too large.
DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218
Merit: 1079


Gerald Davis


View Profile
April 30, 2014, 05:05:26 PM
Last edit: March 19, 2015, 04:26:20 PM by DeathAndTaxes
Merited by ABCbits (11)
 #6

If you are sending to P2SH addresses then your initial transaction works just like normal. The usual size, fees and dust limits apply. This is because the P2SH addresses are just regular addresses other than the fact they are a hash of a script instead of the hash of a public key.

Where things can get trickier is in redeeming any of those outputs in subsequent transactions. As stated earlier, the redemption script for a 7-of-11 might be too large.

This is a critical point.  P2SH makes things easier.  For someone to send you funds you can just provide an address.  The outputs are also smaller (as the output just contains a fixed 32 byte script which contains the hash of the redeemScript).  However P2SH is a double edged sword.  It can make it more difficult for you to spend funds you receive if you create scripts which are non-standard and worse you can even created scripts which are invalid which the network can't verify until funds have already been 'locked up' at that address. See note at the bottom on testing and risk of losing of funds.

'Sending' funds TO a P2SH address
P2SH hash is in the output of the tx
You can have a nearly unlimited number of P2SH addresses as outputs
The output is a fixed length hash of the script.
Actual script not "known" to the network, only "normal" tx validation applies.  
The tx is not significantly different than a "normal" tx.  Instead of putting a hash of the pubkey in the output you are putting a hash of the script in the output.

'Spending' funds FROM a P2SH address
The redeemScript which hashes to the hash in the unspent output's PkScript is added to the ScriptSig portion of the 'spending' transaction's input.
In tx validation the script is hashed and compared to script hash in the prior tx's unspent output.
If that validates then the ScriptSig is validated normally.
The validation rules for scripts apply (see below).  Actually they apply for all scripts but a "normal" (P2PkH script can't exceed the limit).

Quote
1. It is possible to have multiple m-of-n contracts as outputs for a transaction? From what I understand this should be possible but costly due to the bytes required to script the transaction.
This is where I think you may still be confused.  With P2SH, an n of m address is simply the HASH of the script.   3 of 3 keys, 7 of 11 keys, 15 of 15 keys, any other arbitrary script.  Instead of the conditions for spending the output being in the output just a hash of that script it placed in the output.  So the direct answer is yes; the size of the script doesn't matter as the output contains a fixed sized hash of the script not the script itself.  However pushes are limited to 520 bytes and since the whole redeemScript needs to be pushed to the stack to validate it's hash any address produced from the hash of a script larger than 520 bytes is effectively unspendable (without a hardfork).

Quote
2. Is there a size above which a miner (by consensus) will not mine a transaction to a block? You mention the number of bytes that are allowed? Is there a hard limit here or does the fee just increase above a certain byte size in relation to how many extra bytes are required to write the transaction to the blockchain?
Once again it is important to distinguish funding TO a P2SH address and spending FROM a P2SH address.

For funding a P2SH address (i.e. sending value to the P2SH address, the hash of the script is on the output side of the tx) in all but the most extreme edge cases there are no constraints.   Transactions have no hard size limit (other than they will need to fit in a 1MB block).  To be relayed by most nodes the tx will need to pass IsStandard which limits you to "only" 100KB but with the output being only ~40 bytes you could have thousands of such outputs.  

Now SPENDING FROM a P2SH address is a little more complex as there are restrictions on what scripts are valid and/or standard.   Also see the note at the end about the risk of losing funds due to delayed script validation.  

A tx is invalid if any of the following are true
  • Block Size is >1,000 KB (this is a block level check but obviously a tx which can't fit into a block <=1MB could never be confirmed at least not until the 1MB limit is raised).
  • A script is >10KB (this is per script so tx can be larger if it contains multiple scripts each less than 10KB).
  • The size of the value being pushed in a script is >520 bytes (effectively limits P2SH scripts to 520 bytes as the redeemScript is pushed to the stack).
  • The script contains more than 201 operations (excluding pushes).  OP_CHECKMULTISIG with m keys is considered m operations for the purpose of this check.
  • The number of keys (n) for a OP_CHECKMULTISIG operation is > 20
  • The number of signatures (m) for a OP_CHECKMULTISIG operation is > than number of keys (n)

Your script generation code should validate the script against all validation rules to avoid an "oh shit" moment.  See the warning about loss of funds at the bottom and be sure you understand how the 'delayed validation' of P2SH scripts makes it easier to lose funds.  This doesn't make P2SH bad but it does give you enough rope to hang yourself.  The bolded limitation is the easiest to violate.  Since redeemScripts are pushed to the stack in P2SH input validation the entire script must be less than 520 bytes.  Always do a hard check on the length of your redeemScript.  Don't trust any rule of thumbs.  For example a m-of-15 multisig script is <520 bytes if all keys are uncompressed however if your code accidentally generated one or more uncompressed keys you have now violated the 520 byte limit and created an address which is unspendable.

If the tx is valid but not standard it won't be relayed by most (virtually all) nodes and won't be included in a block by most miners.  To get it confirmed you will need to bypass the peer to peer network and push the transaction directly to a miner who accepts non-standard transactions.  There is no "miner discovery" mechanism in the protocol so you will need to manually check with each miner.   Also the miner may not accept any non-standard txn and may have specific fee requirements so this should only be done if you absolutely can't accomplish the task with standard transactions.  If the transaction is standard it will be relayed automatically by nodes and included in consideration for the next block by most miners so special handling is needed.

A transaction is not standard if any of the following are true
  • The tx size > 100KB
  • The scriptSig size is > 500 now 1650 bytes.
  • The value of any output is less than the dust threshold (5,460 now 546 satoshis).
  • The tx is low priority and doesn't include the minimum fee to relay (10,000 now 1,000 satoshis).**
  • The tx is not final (nLockTime block has not been created yet).***
  • The tx version is greater than the current version (which is 1).
  • For 'native' multisig, the number of keys is > 3.
  • For P2SH transactions, the number of SigOps is > 15.

** Technically this doesn't make the txn non-standard but even if standard the txn won't be relayed by most nodes if it has insufficient fee or priority so it may help to just consider it part of the IsStandard check.

*** The txn itself is only considered non-standard not invalid but if you include a nlocktime txn with a future nlocktime in a block then the block is invalid.

Note:  It is possible to lose funds with P2SH so extensive testing on testnet is strongly recommended.  The risk comes from the fact that the network is unaware of the script contents at the time the funds are "sent" to the P2SH address.   For a simplistic example create an otherwise valid Bitcoin script which is greater than 10KB in size.  Generate a P2SH address from the hash of the script.  Make a payment to the P2SH address.   The tx sending funds to the P2SH address will validated and be processed by the network without issue.  However any attempt to "spend" the coins sent to the P2SH address will fail as the only valid method of "redeeming" the output is the script and the script breaks the 10KB validation rule.  Those funds can never be spent (or at least can't be spent until the 10KB limit is raised or removed).
telepatheic
Jr. Member
*
Offline Offline

Activity: 56
Merit: 1


View Profile
April 30, 2014, 07:00:57 PM
 #7

Quote
Yup n=3 is the limit for IsStandard right now.

That isn't true. If you read the code, the limit is with the size of sigScript if you are using P2SH:

Code:
txin.scriptSig.size() > 500

Signatures are of length 72 bytes and public keys are of length 33 bytes (if compacted) so 4 of 6 is about the limit. I've managed 3 of 4 and it passed as a standard transaction.
DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218
Merit: 1079


Gerald Davis


View Profile
April 30, 2014, 07:38:13 PM
Last edit: April 30, 2014, 10:30:20 PM by DeathAndTaxes
 #8

Quote
Yup n=3 is the limit for IsStandard right now.

That isn't true. If you read the code, the limit is with the size of sigScript if you are using P2SH:

Code:
txin.scriptSig.size() > 500

Signatures are of length 72 bytes and public keys are of length 33 bytes (if compacted) so 4 of 6 is about the limit. I've managed 3 of 4 and it passed as a standard transaction.

Looks like you are right.

Actually I believe it is 10 bytes in encoding (DER) + 32 bytes for r, s, x, and y ea.
So for compressed keys the ScriptSig (r, s, x and encoding) is 106 bytes.
For uncompressed keys the ScriptSig (r, s, x, y, and encoding) is 138 bytes.

FLOOR(500 / 106) = 4
FLOOR(500 / 138) = 3
telepatheic
Jr. Member
*
Offline Offline

Activity: 56
Merit: 1


View Profile
April 30, 2014, 07:45:56 PM
Merited by ABCbits (1)
 #9

Quote
So why 4 of 6, why not 4 of 20?  4 of 20 would still only be 4 signatures in the script sig.

Because with multisignature you have to put all the possible public keys into the transaction even if they aren't used to sign it. So 4 of 6 requires 4 signatures and 6 public keys.
oleganza
Full Member
***
Offline Offline

Activity: 200
Merit: 104


Software design and user experience.


View Profile WWW
April 30, 2014, 07:46:57 PM
 #10

Quote
Yup n=3 is the limit for IsStandard right now.

That isn't true. If you read the code, the limit is with the size of sigScript if you are using P2SH:

Code:
txin.scriptSig.size() > 500

Signatures are of length 72 bytes and public keys are of length 33 bytes (if compacted) so 4 of 6 is about the limit. I've managed 3 of 4 and it passed as a standard transaction.

This 500 byte limit is in IsStandardTx() check. You can still get your 10-of-20 multisig transaction included in block if your really need it.

Bitcoin analytics: blog.oleganza.com / 1TipsuQ7CSqfQsjA9KU5jarSB1AnrVLLo
DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218
Merit: 1079


Gerald Davis


View Profile
April 30, 2014, 08:02:15 PM
Last edit: April 30, 2014, 10:29:43 PM by DeathAndTaxes
 #11

Quote
So why 4 of 6, why not 4 of 20?  4 of 20 would still only be 4 signatures in the script sig.

Because with multisignature you have to put all the possible public keys into the transaction even if they aren't used to sign it. So 4 of 6 requires 4 signatures and 6 public keys.

Doh.  Of Course. Smiley

So as long as all 4 keys are compressed then it would look like 4 of 6 meets IsStandard.
4*72 + 6*34 = 492 bytes < 500 bytes

However if any of the keys are uncompressed then it is limited to 3 of 4 keys.
3*72 + 4*66 = 480 bytes < 500 bytes.

I used 34/66 bytes as I believe (going from memory here) the encoding used in tx for the PubKey contains one byte for the length.  Thanks I learned something today.

Actually there is a more restrictive check on the actual script so the max is 3 of 3 (for IsStandard) even if the size is under 500 bytes.
https://github.com/bitcoin/bitcoin/blob/ae7e5d7cebd9466d0c095233c9273e72e88fede1/src/script.cpp#L1414
Only applies to native multisig not P2SH.

telepatheic
Jr. Member
*
Offline Offline

Activity: 56
Merit: 1


View Profile
April 30, 2014, 08:17:55 PM
 #12

Quote
I used 34/66 bytes as I believe (going from memory here) the encoding used in tx for the PubKey contains one byte for the length.

That is correct, you also need to add an extra byte to the signature as well technically! The DER encoding is really unnecessary. I'm doing a refactor of bitcoin to get rid of some of these stupid extra bytes.
DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218
Merit: 1079


Gerald Davis


View Profile
April 30, 2014, 08:44:34 PM
Last edit: March 19, 2015, 04:29:29 PM by DeathAndTaxes
Merited by ABCbits (3)
 #13

The DER encoding is really unnecessary. I'm doing a refactor of bitcoin to get rid of some of these stupid extra bytes.

Well at the same time any altcoin should probably make a few other changes.
1 ) Move signature outside the input (i.e. header, inputs, outputs, signature)
2 ) Make hash for signature the entire tx (forced immutability)
3 ) Enforce a restricted canonical signature (r < half order)
4 ) Always require compressed keys (simplifies encoding, key export, signature verification, etc)
5 ) Use pubkey regeneration to remove the need for pubkeys in the tx input
6 ) Remove edge case tx types (like pay to PubKey vs pay to PubKeyHash)
7 ) Remove unnecessary DER encoding from the blockchain.  Purpose of DER is to allow a standardized format for sharing signatures between applications.  Bitcoin clients don't need that capability.
8 ) Make tx fees explicit.  A nice safety check against malformed txs at the expense of a few bytes (far less than what is saved by removing redundant encoding and pubkeys).
9 ) Make input values explicit.  Makes txn validation by hardware wallets easier.
10) Consider using Schnorr signatures.  Single signature needed for multi-sig making transactions smaller.

It would greatly simplify the codebase with essentially no loss of functionality.  Sadly I don't think such breaking changes will happen with Bitcoin.
telepatheic
Jr. Member
*
Offline Offline

Activity: 56
Merit: 1


View Profile
April 30, 2014, 09:03:59 PM
 #14

This isn't really the place to have that discussion but if you are interested join us at http://www.reddit.com/r/conceptcoin
jonald_fyookball
Legendary
*
Offline Offline

Activity: 1302
Merit: 1004


Core dev leaves me neg feedback #abuse #political


View Profile
April 30, 2014, 09:18:21 PM
 #15

I took a look at your link telepatheic.  I don't get why
you would want to use a trusted source for randomness
when you could use a trustless method like NXT does.

DT, any plans for your own altcoin?

telepatheic
Jr. Member
*
Offline Offline

Activity: 56
Merit: 1


View Profile
April 30, 2014, 09:58:13 PM
 #16

Quote
why you would want to use a trusted source for randomness when you could use a trustless method like NXT does.

Because you can cheat at NXT forging. If you have control of a particular block then you can affect who will get to forge blocks in the future. (If you can't affect a future block, then another block between now and that future block must be able to affect it, repeat this argument until you arrive at the future block, someone must have had the final say on who gets to forge it.) Again, this isn't the place for this sort of discussion, post on the conceptcoin subreddit if you want more details of why an independent random source is needed.
telepatheic
Jr. Member
*
Offline Offline

Activity: 56
Merit: 1


View Profile
April 30, 2014, 10:07:27 PM
 #17

Quote
Actually there is a more restrictive check on the actual script so the max is 3 of 3 (for IsStandard) even if the size is under 500 bytes.

I should have mentioned that earlier. That check applies to outputs (where standard multi-sig pub keys go) only not inputs (where P2SH pub keys go).

So 3 of 3 is the maximum standard normal multi-signature transaction and 4 of 6 is the maximum P2SH transaction.
DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218
Merit: 1079


Gerald Davis


View Profile
April 30, 2014, 10:36:48 PM
Last edit: March 19, 2015, 04:30:17 PM by DeathAndTaxes
Merited by ABCbits (1)
 #18

Quote
Actually there is a more restrictive check on the actual script so the max is 3 of 3 (for IsStandard) even if the size is under 500 bytes.

I should have mentioned that earlier. That check applies to outputs (where standard multi-sig pub keys go) only not inputs (where P2SH pub keys go).

So 3 of 3 is the maximum standard normal multi-signature transaction and 4 of 6 is the maximum P2SH transaction.

Making sense now.

Standard
Native MultSig = max 3-of-3 (https://github.com/bitcoin/bitcoin/blob/master/src/script.cpp#L1414 "if (n < 1 || n > 3)")
P2SH w/ all compressed keys = max of 7-of-15 (https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp#L521 "if(txin.scriptSig.size() > 500)")
P2SH w/ all uncompressed keys = max of 7-of-7 (https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp#L521 "if(txin.scriptSig.size() > 500)")

Non-standard but Valid
Native MultiSig = max of 20-of-20 (update me with line reference of limit for valid OP_CHECKMULTISIG)
P2SH w/ all compressed keys = max of 15-of-15 (https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki#520-byte-limitation-on-serialized-script-size)

Invalid
Native MultiSig = more than 20-of-20
P2SH = more than 15-of-15

Anyone else want to comment on the accuracy of that?
telepatheic
Jr. Member
*
Offline Offline

Activity: 56
Merit: 1


View Profile
April 30, 2014, 10:54:59 PM
 #19

Quote
Native MultiSig = max of 16-of-16

Don't you mean 20 of 20: The following transaction is a native 20 of 20 transaction: https://blockchain.info/tx/c4aaf7fbec7a9a079e670e50f6a672315451c7618814494ab1f89cf3fd97b3bb
DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218
Merit: 1079


Gerald Davis


View Profile
May 01, 2014, 08:00:08 PM
Last edit: July 22, 2014, 10:35:30 PM by DeathAndTaxes
 #20

Nice find, a 14 20-of-20.
Pages: [1] 2 3 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!