<p>Yes, that would save space. However, there are two ways in which your scripts are different.</p>

<ol>
<li>It checks if there is a value that hashes to the hash of x at the beginning,</li>
<li>It performs the operation in step 1 whether we're executing branch 1 or 2.</li>
</ol>

<p>Your first script can be rewritten like so to fix that:</p>

<pre><code>OP_NOTIF
  &lt;key A&gt; OP_CHECKSIGVERIFY
  OP_0
OP_ELSE
  OP_1
OP_ENDIF
OP_SWAP
&lt;key B&gt; OP_CHECKSIG
OP_IF
  OP_SHA256 &lt;hash of secret x&gt; OP_EQUAL
OP_ENDIF
</code></pre>

<h3>Space savings</h3>

<p>Each public key is 34 bytes (33 bytes for compressed key plus 1 for data push). Blocks are about 617k at the moment, and the total fees for a block are about 0.223 BTC. Each public key therefore costs:</p>

<pre><code>34 / 617000 * 0.223 * 1e8 = 1228 satoshis
</code></pre>

<p>A small amount, but it would add up over time.</p>

CC BY-SA 3.0
2016-03-26T14:40:02.630Z
2016-03-26T14:40:02.630Z
Nick ODell
<blockquote>
  <p>But I want to know if this does actually make sense 
  when creating Scripts to use them to save transaction space?</p>
</blockquote>

<p>How much money are you saving with shorter scripts? 0.01 cent per tx?</p>

<p>So, the answer is: yes, there is some sense. But the economy is too low to think about it</p>

CC BY-SA 3.0
2016-03-26T13:12:27.133Z
2016-03-26T13:12:27.133Z
amaclin
<p>I have some questions regarding the programming of economic Scripts, so Scripts that save some transaction space compared to functionally the same Scripts but with redundancy by e.g. using the same public keys twice.
One example comes from the Script used for the atomic swap in the wiki: 
<a href="https://en.bitcoin.it/wiki/Contract#Example_5:_Trading_across_chains" rel="nofollow">https://en.bitcoin.it/wiki/Contract#Example_5:_Trading_across_chains</a></p>

<p><code>OP_IF 
  2 &lt;key A&gt; &lt;key B&gt; 2 OP_CHECKMULTISIGVERIFY
OP_ELSE
  &lt;key B&gt; OP_CHECKSIGVERIFY OP_SHA256 &lt;hash of secret x&gt; OP_EQUALVERIFY
OP_ENDIF</code></p>

<p>In the script above the public key for B is referenced twice. But it can be rewritten to:</p>

<p><code>OP_SHA256 &lt;hash of secret x&gt; OP_EQUAL
OP_NOTIF
  &lt;key A&gt; OP_CHECKSIGVERIFY
OP_ENDIF
&lt;key B&gt; OP_CHECKSIG</code></p>

<p>Functionally it is the same script, but does not use the public key of B twice and thus saves space in the transaction.
The difference is the way the scriptSig must now be provided with the signatures or secret.</p>

<p>Other than that, is there anything against using the script above instead of the one from the wiki?</p>

<p>And what about the following example:</p>

<p><code>OP_SHA256 &lt;hash of secret x&gt; OP_EQUAL
OP_IF
  &lt;K2&gt; OP_CHECKSIG
OP_ELSE
  2 &lt;K1&gt; &lt;K2&gt; &lt;K3&gt; 3 OP_CHECKMULTISIG
OP_ENDIF</code></p>

<p>It is similar to the atomic swap above, but now there is a mediator involved.
Again the public key  is used twice. But it can be rewritten to:</p>

<p><code>OP_SHA256 &lt;hash of secret x&gt; OP_EQUAL
2 &lt;K2&gt;
OP_ROT
OP_IF
  OP_NIP
  OP_CHECKSIG
OP_ELSE
  &lt;K1&gt; OP_SWAP &lt;K3&gt; OP_CHECKMULTISIG
OP_ENDIF</code></p>

<p>This script accepts the exact same scriptSigs as the one before. It involves two more opcodes (OP_SWAP and OP_NIP), which are two bytes in total. But it saves 31 bytes in total by only using the public key K2 once.
It may not be so readable, but is there anything against using this script compared to the first one?</p>

<p>There are many similar cases I can come up with using the exotic opcodes like OP_SWAP, OP_ROLL, and so on. But I want to know if this does actually make sense when creating Scripts to use them to save transaction space? </p>

<script>
Save transaction space with economic Scripts
Bjarne Magnussen
# Save transaction space with economic Scripts

(Asked by: username_0 on 2016-03-26)

I have some questions regarding the programming of economic Scripts, so Scripts that save some transaction space compared to functionally the same Scripts but with redundancy by e.g. using the same public keys twice.
One example comes from the Script used for the atomic swap in the wiki:
<https://en.bitcoin.it/wiki/Contract#Example_5:_Trading_across_chains>

`OP_IF
2 <key A> <key B> 2 OP_CHECKMULTISIGVERIFY
OP_ELSE
<key B> OP_CHECKSIGVERIFY OP_SHA256 <hash of secret x> OP_EQUALVERIFY
OP_ENDIF`

In the script above the public key for B is referenced twice. But it can be rewritten to:

`OP_SHA256 <hash of secret x> OP_EQUAL
OP_NOTIF
<key A> OP_CHECKSIGVERIFY
OP_ENDIF
<key B> OP_CHECKSIG`

Functionally it is the same script, but does not use the public key of B twice and thus saves space in the transaction.
The difference is the way the scriptSig must now be provided with the signatures or secret.

Other than that, is there anything against using the script above instead of the one from the wiki?

And what about the following example:

`OP_SHA256 <hash of secret x> OP_EQUAL
OP_IF
<K2> OP_CHECKSIG
OP_ELSE
2 <K1> <K2> <K3> 3 OP_CHECKMULTISIG
OP_ENDIF`

It is similar to the atomic swap above, but now there is a mediator involved.
Again the public key is used twice. But it can be rewritten to:

`OP_SHA256 <hash of secret x> OP_EQUAL
2 <K2>
OP_ROT
OP_IF
OP_NIP
OP_CHECKSIG
OP_ELSE
<K1> OP_SWAP <K3> OP_CHECKMULTISIG
OP_ENDIF`

This script accepts the exact same scriptSigs as the one before. It involves two more opcodes (OP\_SWAP and OP\_NIP), which are two bytes in total. But it saves 31 bytes in total by only using the public key K2 once.
It may not be so readable, but is there anything against using this script compared to the first one?

There are many similar cases I can come up with using the exotic opcodes like OP\_SWAP, OP\_ROLL, and so on. But I want to know if this does actually make sense when creating Scripts to use them to save transaction space?

**Tags:** script


---

## 2 answers

### Answer by username_1 on 2016-03-26. Score: 1

Yes, that would save space. However, there are two ways in which your scripts are different.

1. It checks if there is a value that hashes to the hash of x at the beginning,
2. It performs the operation in step 1 whether we're executing branch 1 or 2.

Your first script can be rewritten like so to fix that:

```
OP_NOTIF
  <key A> OP_CHECKSIGVERIFY
  OP_0
OP_ELSE
  OP_1
OP_ENDIF
OP_SWAP
<key B> OP_CHECKSIG
OP_IF
  OP_SHA256 <hash of secret x> OP_EQUAL
OP_ENDIF

```

### Space savings

Each public key is 34 bytes (33 bytes for compressed key plus 1 for data push). Blocks are about 617k at the moment, and the total fees for a block are about 0.223 BTC. Each public key therefore costs:

```
34 / 617000 * 0.223 * 1e8 = 1228 satoshis

```

A small amount, but it would add up over time.


---

### Answer by username_2 on 2016-03-26. Score: 0

> But I want to know if this does actually make sense
> when creating Scripts to use them to save transaction space?

How much money are you saving with shorter scripts? 0.01 cent per tx?

So, the answer is: yes, there is some sense. But the economy is too low to think about it


---

