<pre><code>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
</code></pre>
<p>Tracing the execution (for a successful input):</p>
<ul>
<li>Initial: <em>a b c</em></li>
<li><code>OP_DUP</code>: <em>a b c c</em></li>
<li><code>OP_8</code>: <em>a b c c 8</em></li>
<li><code>OP_LESSTHAN</code>: <em>a b c 1</em></li>
<li><code>OP_VERIFY</code>: <em>a b c</em></li>
<li><code>OP_SWAP</code>: <em>a c b</em></li>
<li><code>OP_DUP</code>: <em>a c b b</em></li>
<li><code>OP_8</code>: <em>a c b b 8</em></li>
<li><code>OP_LESSTHAN</code>: <em>a c b 1</em></li>
<li><code>OP_VERIFY</code>: <em>a c b</em></li>
<li><code>OP_ROT</code>: <em>c b a</em></li>
<li><code>OP_DUP</code>: <em>c b a a</em></li>
<li><code>OP_8</code>: <em>c b a a 8</em></li>
<li><code>OP_LESSTHAN</code>: <em>c b a 1</em></li>
<li><code>OP_VERIFY</code>: <em>c b a</em></li>
<li><code>OP_ADD</code>: <em>c (a+b)</em></li>
<li><code>OP_ADD</code>: <em>(a+b+c)</em></li>
<li><code>OP_15</code>: <em>(a+b+c) 15</em></li>
<li><code>OP_NUMEQUAL</code>: <em>1</em></li>
</ul>

CC BY-SA 4.0
2023-04-09T13:40:37.090Z
Wouldn't OP_PICK be simpler than duplicating and swapping/rotating?
Vojtěch Strnad
CC BY-SA 4.0
2023-04-09T13:43:04.597Z
I think it'd require the same number of opcodes and bytes, but feel free to suggest as an alternative.
Pieter Wuille
CC BY-SA 4.0
2023-04-09T13:48:33.863Z
No need, just making sure I haven't missed something obvious. I'm not particularly versed in Script.
Vojtěch Strnad
CC BY-SA 4.0
2023-04-09T12:45:32.190Z
2023-04-09T17:08:01.373Z
2023-04-09T17:08:01.373Z
Pieter Wuille
<p>This is the smallest script I could come up with, at 13 opcodes:</p>
<pre><code>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
</code></pre>
<p>It saves some bytes by using <code>OP_3DUP</code> to duplicate all three input numbers, and one byte by checking the addition first using <code>OP_NUMEQUALVERIFY</code> instead of <code>OP_NUMEQUAL</code>, which saves one <code>OP_VERIFY</code> opcode.</p>

CC BY-SA 4.0
2023-04-09T14:52:50.647Z
Nice, that's a lot simpler indeed.
Pieter Wuille
CC BY-SA 4.0
2023-04-09T16:44:01.880Z
Thanks, I think this is the shortest approach for this question.
bitcointapir
CC BY-SA 4.0
2023-04-09T14:22:50.323Z
2023-04-09T14:22:50.323Z
Vojtěch Strnad
<p>I want to create a script that takes 3 numbers and verifies that:</p>
<ol>
<li>Each of the numbers is less than 8.</li>
<li>The sum of the numbers is 15.</li>
</ol>
<p>How can I do that?</p>

<script><math>
Simple math in Script
bitcointapir
# Simple math in Script

(Asked by: username_0 on 2023-04-09)

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?

**Tags:** script, math


---

## 2 answers

### Answer by username_2 on 2023-04-09. Score: 5 | Accepted answer

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.

> **Comments:**
> **username_1**: Nice, that's a lot simpler indeed.  
> **username_0**: Thanks, I think this is the shortest approach for this question.  

---

### Answer by username_1 on 2023-04-09. Score: 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

> **Comments:**
> **username_2**: Wouldn't OP_PICK be simpler than duplicating and swapping/rotating?  
> **username_1**: I think it'd require the same number of opcodes and bytes, but feel free to suggest as an alternative.  
> **username_2**: No need, just making sure I haven't missed something obvious. I'm not particularly versed in Script.  

---

