Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic teal #2126

Merged
merged 19 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Docs and tests for divw
  • Loading branch information
jannotti committed Apr 27, 2021
commit 1b98e69dde681009eec14402c75afc22ca1af0ac
4 changes: 3 additions & 1 deletion data/transactions/logic/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var opDocByName = map[string]string{
"~": "bitwise invert value X",
"mulw": "A times B out to 128-bit long result as low (top) and high uint64 values on the stack",
"addw": "A plus B out to 128-bit long result as sum (top) and carry-bit uint64 values on the stack",
"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.",
"intcblock": "prepare block of uint64 constants for use by intc",
"intc": "push Ith constant from intcblock to stack",
"intc_0": "push constant 0 from intcblock to stack",
Expand Down Expand Up @@ -159,6 +160,7 @@ var opDocExtras = map[string]string{
"bytecblock": "`bytecblock` loads the following program bytes into an array of byte-array constants in the evaluator. These constants can be referred to by `bytec` and `bytec_*` which will push the value onto the stack. Subsequent calls to `bytecblock` reset and replace the bytes constants available to the script.",
"*": "Overflow is an error condition which halts execution and fails the transaction. Full precision is available from `mulw`.",
"+": "Overflow is an error condition which halts execution and fails the transaction. Full precision is available from `addw`.",
"/": "`divw` is available to divide the two-element values produced by `mulw` and `addw`.",
"txn": "FirstValidTime causes the program to fail. The field is reserved for future use.",
"gtxn": "for notes on transaction fields available, see `txn`. If this transaction is _i_ in the group, `gtxn i field` is equivalent to `txn field`.",
"gtxns": "for notes on transaction fields available, see `txn`. If top of stack is _i_, `gtxns field` is equivalent to `gtxn _i_ field`. gtxns exists so that _i_ can be calculated, often based on the index of the current transaction.",
Expand Down Expand Up @@ -194,7 +196,7 @@ type OpGroup struct {

// OpGroupList is groupings of ops for documentation purposes.
var OpGroupList = []OpGroup{
{"Arithmetic", []string{"sha256", "keccak256", "sha512_256", "ed25519verify", "+", "-", "/", "*", "<", ">", "<=", ">=", "&&", "||", "==", "!=", "!", "len", "itob", "btoi", "%", "|", "&", "^", "~", "mulw", "addw", "getbit", "setbit", "getbyte", "setbyte", "concat", "substring", "substring3"}},
{"Arithmetic", []string{"sha256", "keccak256", "sha512_256", "ed25519verify", "+", "-", "/", "*", "<", ">", "<=", ">=", "&&", "||", "==", "!=", "!", "len", "itob", "btoi", "%", "|", "&", "^", "~", "mulw", "addw", "divw", "getbit", "setbit", "getbyte", "setbyte", "concat", "substring", "substring3"}},
{"Loading Values", []string{"intcblock", "intc", "intc_0", "intc_1", "intc_2", "intc_3", "pushint", "bytecblock", "bytec", "bytec_0", "bytec_1", "bytec_2", "bytec_3", "pushbytes", "arg", "arg_0", "arg_1", "arg_2", "arg_3", "txn", "gtxn", "txna", "gtxna", "gtxns", "gtxnsa", "global", "load", "store"}},
{"Flow Control", []string{"err", "bnz", "bz", "b", "return", "pop", "dup", "dup2", "dig", "swap", "select", "assert"}},
{"State Access", []string{"balance", "min_balance", "app_opted_in", "app_local_get", "app_local_get_ex", "app_global_get", "app_global_get_ex", "app_local_put", "app_global_put", "app_local_del", "app_global_del", "asset_holding_get", "asset_params_get"}},
Expand Down
24 changes: 21 additions & 3 deletions data/transactions/logic/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,15 +774,33 @@ func opAddw(cx *evalContext) {
cx.stack[last].Uint = sum
}

func uint128(hi uint64, lo uint64) *big.Int {
whole := new(big.Int).SetUint64(hi)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is interesting why does not big package like stack-allocated Ints... Have you tried

whole := &big.Int{}
whole.SetUint64(hi)
...

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's fine too, but why prefer two lines / statements instead of a single expression?

I wanted to use syntax that gave a single expression that produced a big.Int from a u64, especially since I needed similar syntax two lines later to create the low word.

whole.Lsh(whole, 64)
whole.Add(whole, new(big.Int).SetUint64(lo))
return whole
}

func opDivwImpl(hiNum, loNum, hiDen, loDen uint64) (hiQuo uint64, loQuo uint64, hiRem uint64, loRem uint64) {
return 1, 2, 3, 4
dividend := uint128(hiNum, loNum)
divisor := uint128(hiDen, loDen)

quo, rem := new(big.Int).QuoRem(dividend, divisor, new(big.Int))
return new(big.Int).Rsh(quo, 64).Uint64(),
quo.Uint64(),
new(big.Int).Rsh(rem, 64).Uint64(),
rem.Uint64()
}

func opDivw(cx *evalContext) {
loDen := len(cx.stack) - 1
hiDen := loDen - 1
loNum := hiDen - 1
hiNum := loDen - 1
if cx.stack[loDen].Uint == 0 && cx.stack[hiDen].Uint == 0 {
cx.err = errors.New("/ 0")
return
}
loNum := loDen - 2
hiNum := loDen - 3
hiQuo, loQuo, hiRem, loRem :=
opDivwImpl(cx.stack[hiNum].Uint, cx.stack[loNum].Uint, cx.stack[hiDen].Uint, cx.stack[loDen].Uint)
cx.stack[hiNum].Uint = hiQuo
Expand Down
64 changes: 38 additions & 26 deletions data/transactions/logic/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,34 +536,46 @@ int 1 // ret 1
`, 2)
}

func TestUint128(t *testing.T) {
x := uint128(0, 3)
require.Equal(t, x.String(), "3")
x = uint128(1, 3)
require.Equal(t, x.String(), "18446744073709551619")
x = uint128(1, 5)
require.Equal(t, x.String(), "18446744073709551621")
jannotti marked this conversation as resolved.
Show resolved Hide resolved
}

func TestDivw(t *testing.T) {
t.Parallel()
// 2:0 / 1:0 == 2r0 == 0:2,0:0
testAccepts(t, `int 2; int 0; int 1; int 0; divw;
int 0; ==; assert;
int 0; ==; assert;
int 2; ==; assert;
int 0; ==; assert; int 1`, 4)

// 2:0 / 0:1 == 2:0r0 == 2:0,0:0
testAccepts(t, `int 2; int 0; int 0; int 1; divw;
int 0; ==; assert;
int 0; ==; assert;
int 0; ==; assert;
int 2; ==; assert; int 1`, 4)

// 0:7777 / 1:0 == 0:0r7777 == 0:0,0:7777
testAccepts(t, `int 0; int 7777; int 1; int 0; divw;
int 7777; ==; assert;
int 0; ==; assert;
int 0; ==; assert;
int 0; ==; assert; int 1`, 4)

// 10:0 / 0:0 ==> panic
testPanics(t, `int 10; int 0; int 0; int 0; divw;
pop; pop; pop; pop; int 1`, 4)
}
jannotti marked this conversation as resolved.
Show resolved Hide resolved

func TestDivZero(t *testing.T) {
t.Parallel()
for v := uint64(1); v <= AssemblerMaxVersion; v++ {
t.Run(fmt.Sprintf("v=%d", v), func(t *testing.T) {
ops, err := AssembleStringWithVersion(`int 0x111111111
int 0
/
pop
int 1`, v)
require.NoError(t, err)
sb := strings.Builder{}
cost, err := Check(ops.Program, defaultEvalParams(&sb, nil))
if err != nil {
t.Log(hex.EncodeToString(ops.Program))
t.Log(sb.String())
}
require.NoError(t, err)
require.True(t, cost < 1000)
pass, err := Eval(ops.Program, defaultEvalParams(&sb, nil))
if pass {
t.Log(hex.EncodeToString(ops.Program))
t.Log(sb.String())
}
require.False(t, pass)
require.Error(t, err)
isNotPanic(t, err)
})
}
testPanics(t, "int 0x11; int 0; /; pop; int 1", 1)
}

func TestModZero(t *testing.T) {
Expand Down