2

I want to create a script that takes 3 numbers and verifies that:

  1. Each of the numbers is less than 8.
  2. The sum of the numbers is 15.

How can I do that?

2 Answers 2

5

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.

2
  • 1
    Nice, that's a lot simpler indeed. Commented Apr 9, 2023 at 14:52
  • Thanks, I think this is the shortest approach for this question. Commented Apr 9, 2023 at 16:44
3
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
3
  • 1
    Wouldn't OP_PICK be simpler than duplicating and swapping/rotating? Commented Apr 9, 2023 at 13:40
  • 1
    I think it'd require the same number of opcodes and bytes, but feel free to suggest as an alternative. Commented Apr 9, 2023 at 13:43
  • 1
    No need, just making sure I haven't missed something obvious. I'm not particularly versed in Script. Commented Apr 9, 2023 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.