You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a lot of math oriented opcodes that have been requested by teal users.
sqrt, exp, expw, divmodw
byteslice oriented math - a set of opcodes that will treat byteslices as arbitrary precision uints.
Copy file name to clipboardExpand all lines: data/transactions/logic/README.md
+45-2Lines changed: 45 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ A program can either authorize some delegated action on a normal private key sig
27
27
* If the account has signed the program (an ed25519 signature on "Program" concatenated with the program bytes) then if the program returns true the transaction is authorized as if the account had signed it. This allows an account to hand out a signed program so that other users can carry out delegated actions which are approved by the program.
28
28
* If the SHA512_256 hash of the program (prefixed by "Program") is equal to the transaction Sender address then this is a contract account wholly controlled by the program. No other signature is necessary or possible. The only way to execute a transaction against the contract account is for the program to approve it.
29
29
30
-
The TEAL bytecode plus the length of any Args must add up to less than 1000 bytes (consensus parameter LogicSigMaxSize). Each TEAL op has an associated cost and the program cost must total less than 20000 (consensus parameter LogicSigMaxCost). Most ops have a cost of 1, but a few slow crypto ops are much higher. Prior to v4, program costs was estimated as the static sum of all opcode costs in a program (ignoring conditionals that might skip some code). Beginning with v4, a program's cost is tracked dynamically, while being evaluated. If the program exceeds its budget, it fails.
30
+
The TEAL bytecode plus the length of any Args must add up to less than 1000 bytes (consensus parameter LogicSigMaxSize). Each TEAL op has an associated cost and the program cost must total less than 20000 (consensus parameter LogicSigMaxCost). Most ops have a cost of 1, but a few slow crypto ops are much higher. Prior to v4, program costs was estimated as the static sum of all opcode costs in a program (ignoring conditionals that might skip some code). Beginning with v4, a program's cost is tracked dynamically, while being evaluated. If the program exceeds its budget, it fails.
31
31
32
32
## Execution modes
33
33
@@ -111,6 +111,11 @@ For two-argument ops, `A` is the previous element on the stack and `B` is the la
111
111
|`>=`| A greater than or equal to B => {0 or 1} |
112
112
|`&&`| A is not zero and B is not zero => {0 or 1} |
113
113
|`\|\|`| A is not zero or B is not zero => {0 or 1} |
114
+
|`shl`| A times 2^B, modulo 2^64 |
115
+
|`shr`| A divided by 2^B |
116
+
|`sqrt`| The largest integer X such that X^2 <= A |
117
+
|`bitlen`| The index of the highest bit in A. If A is a byte-array, it is interpreted as a big-endian unsigned integer |
118
+
|`exp`| A raised to the Bth power. Panic if A == B == 0 and on overflow |
114
119
|`==`| A is equal to B => {0 or 1} |
115
120
|`!=`| A is not equal to B => {0 or 1} |
116
121
|`!`| X == 0 yields 1; else 0 |
@@ -124,7 +129,8 @@ For two-argument ops, `A` is the previous element on the stack and `B` is the la
124
129
|`~`| bitwise invert value X |
125
130
|`mulw`| A times B out to 128-bit long result as low (top) and high uint64 values on the stack |
126
131
|`addw`| A plus B out to 128-bit long result as sum (top) and carry-bit uint64 values on the stack |
127
-
|`divw`| Pop four uint64 values. The deepest two are interpreted as a uint128 dividend (deepest value is high word), the top two are interpreted as a uint128 divisor. Four uint64 values are pushed to the stack. The deepest two are the quotient (deeper value is the high uint64). The top two are the remainder, low bits on top. |
132
+
|`divmodw`| Pop four uint64 values. The deepest two are interpreted as a uint128 dividend (deepest value is high word), the top two are interpreted as a uint128 divisor. Four uint64 values are pushed to the stack. The deepest two are the quotient (deeper value is the high uint64). The top two are the remainder, low bits on top. |
133
+
|`expw`| A raised to the Bth power as a 128-bit long result as low (top) and high uint64 values on the stack. Panic if A == B == 0 or if the results exceeds 2^128-1 |
128
134
|`getbit`| pop a target A (integer or byte-array), and index B. Push the Bth bit of A. |
129
135
|`setbit`| pop a target A, index B, and bit C. Set the Bth bit of A to C, and push the result |
130
136
|`getbyte`| pop a byte-array A and integer B. Extract the Bth byte of A and push it as an integer |
@@ -133,6 +139,42 @@ For two-argument ops, `A` is the previous element on the stack and `B` is the la
133
139
|`substring s e`| pop a byte-array A. For immediate values in 0..255 S and E: extract a range of bytes from A starting at S up to but not including E, push the substring result. If E < S, or either is larger than the array length, the program fails |
134
140
|`substring3`| pop a byte-array A and two integers B and C. Extract a range of bytes from A starting at B up to but not including C, push the substring result. If C < B, or either is larger than the array length, the program fails |
135
141
142
+
These opcodes take and return byte-array values that are interpreted
143
+
as big-endian unsigned integers. Returned values are the shortest
144
+
byte-array that can represent the returned value. For example, the
145
+
zero value is the empty byte-array.
146
+
147
+
Input lengths are limited to maximum length 64, which represents a 512
148
+
bit unsigned integer.
149
+
150
+
| Op | Description |
151
+
| --- | --- |
152
+
|`b+`| A plus B, where A and B are byte-arrays interpreted as big-endian unsigned integers |
153
+
|`b-`| A minus B, where A and B are byte-arrays interpreted as big-endian unsigned integers. Panic on underflow. |
154
+
|`b/`| A divided by B, where A and B are byte-arrays interpreted as big-endian unsigned integers. Panic if B is zero. |
155
+
|`b*`| A times B, where A and B are byte-arrays interpreted as big-endian unsigned integers. |
156
+
|`b<`| A is less than B, where A and B are byte-arrays interpreted as big-endian unsigned integers => { 0 or 1} |
157
+
|`b>`| A is greater than B, where A and B are byte-arrays interpreted as big-endian unsigned integers => { 0 or 1} |
158
+
|`b<=`| A is less than or equal to B, where A and B are byte-arrays interpreted as big-endian unsigned integers => { 0 or 1} |
159
+
|`b>=`| A is greater than or equal to B, where A and B are byte-arrays interpreted as big-endian unsigned integers => { 0 or 1} |
160
+
|`b==`| A is equals to B, where A and B are byte-arrays interpreted as big-endian unsigned integers => { 0 or 1} |
161
+
|`b!=`| A is not equal to B, where A and B are byte-arrays interpreted as big-endian unsigned integers => { 0 or 1} |
162
+
|`b%`| A modulo B, where A and B are byte-arrays interpreted as big-endian unsigned integers. Panic if B is zero. |
163
+
164
+
These opcodes operate on the bits of byte-array values. The shorter
165
+
array is interpeted as though left padded with zeros until it is the
166
+
same length as the other input. The returned values are the same
167
+
length as the longest input. Therefore, unlike array arithmetic,
168
+
these results may contain leading zero bytes.
169
+
170
+
| Op | Description |
171
+
| --- | --- |
172
+
|`b\|`| A bitwise-or B, where A and B are byte-arrays, zero-left extended to the greater of their lengths |
173
+
|`b&`| A bitwise-and B, where A and B are byte-arrays, zero-left extended to the greater of their lengths |
174
+
|`b^`| A bitwise-xor B, where A and B are byte-arrays, zero-left extended to the greater of their lengths |
175
+
|`b~`| A with all bits inverted |
176
+
177
+
136
178
### Loading Values
137
179
138
180
Opcodes for getting data onto the stack.
@@ -155,6 +197,7 @@ Some of these have immediate data in the byte or bytes after the opcode.
155
197
|`bytec_2`| push constant 2 from bytecblock to stack |
156
198
|`bytec_3`| push constant 3 from bytecblock to stack |
157
199
|`pushbytes bytes`| push the following program bytes to the stack |
200
+
|`bzero`| push a byte-array of length A, containing all zero bytes |
0 commit comments