Save transaction space with economic Scripts 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 &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

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

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

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 &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

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 &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

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?
Yes, that would save space. However, there are two ways in which your scripts are different.


It checks if there is a value that hashes to the hash of x at the beginning,
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
  &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


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.