2

I want to create a bitcoin locking script that takes 3 numbers, each of them are less than 8 and if sum of 3 numbers are 15 then script will return true. How can i do that?

The one who will redeem the transaction needs to provide only these 3 numbers.

Thanks

2 Answers2

4

This is the smallest script I could come up with, at 13 opcodes:

OP_3DUP
OP_ADD
OP_ADD
OP_15 OP_NUMEQUALVERIFY
OP_8 OP_LESSTHAN OP_VERIFY
OP_8 OP_LESSTHAN OP_VERIFY
OP_8 OP_LESSTHAN

It saves some bytes by using OP_3DUP to duplicate all three input numbers, and one byte by checking the addition first using OP_NUMEQUALVERIFY instead of OP_NUMEQUAL, which saves one OP_VERIFY opcode.

Vojtěch Strnad
  • 5,623
  • 1
  • 8
  • 31
2
OP_DUP OP_8 OP_LESSTHAN OP_VERIFY
OP_SWAP OP_DUP OP_8 OP_LESSTHAN OP_VERIFY
OP_ROT OP_DUP OP_8 OP_LESSTHAN OP_VERIFY
OP_ADD OP_ADD OP_15 OP_NUMEQUAL

Tracing the execution (for a successful input):

  • Initial: a b c
  • OP_DUP: a b c c
  • OP_8: a b c c 8
  • OP_LESSTHAN: a b c 1
  • OP_VERIFY: a b c
  • OP_SWAP: a c b
  • OP_DUP: a c b b
  • OP_8: a c b b 8
  • OP_LESSTHAN: a c b 1
  • OP_VERIFY: a c b
  • OP_ROT: c b a
  • OP_DUP: c b a a
  • OP_8: c b a a 8
  • OP_LESSTHAN: c b a 1
  • OP_VERIFY: c b a
  • OP_ADD: c (a+b)
  • OP_ADD: (a+b+c)
  • OP_15: (a+b+c) 15
  • OP_NUMEQUAL: 1
Pieter Wuille
  • 98,249
  • 9
  • 183
  • 287