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

Fix Modular Exponentiation Test #24

Closed
tk-codes opened this issue Apr 18, 2019 · 2 comments
Closed

Fix Modular Exponentiation Test #24

tk-codes opened this issue Apr 18, 2019 · 2 comments
Labels
bug Something isn't working

Comments

@tk-codes
Copy link
Collaborator

At the end of the function call (ret), evaluation stack should be empty.

The following test has more values. It should be fixed.

func TestVm_Exec_ModularExponentiation_ContractImplementation(t *testing.T) {
	code := []byte{
		PushInt, 1, 0, 4, // Base 4
		PushInt, 2, 0, 1, 241, // Modulus 497

		// Address 9
		// IF modulus equals 0
		Dup,
		PushInt, 0,
		Eq,
		JmpTrue, 0, 42, // Adjust address

		// Address 16
		PushInt, 1, 0, 1, // Counter (c)
		PushInt, 0, //i
		PushInt, 1, 0, 13, // Exp

		// Address 26
		//LOOP start: Stack: [[0 13] [0] [0 1] [0 1 241] [0 4]]
		Roll, 2,
		//Duplicate arguments
		Dup, //Stack: [[0 11 75] [0 11 75] [0 13] [0] [0 1] [0 4]]
		Roll, 4,
		Dup, // STACK Stack: [[04] [0 4] [0 11 75] [0 11 75] [0 13] [0 0] [0 1]]
		// PUT in order
		Roll, 1, //Stack: [[0 11 75] [0 4] [0 4] [0 11 75] [0 13] [0 0] [0 1]]
		Roll, 4, //Stack: [[0 0] [0 11 75] [0 4] [0 4] [0 11 75] [0 13] [0 1]]
		Roll, 4, //Stack: [[0 13] [0 0] [0 11 75] [0 4] [0 4] [0 11 75] [0 1]]
		Roll, 3, //Stack: [[0 4] [0 13] [0 0] [0 11 75] [0 4] [0 11 75] [0 1]]
		Roll, 4, //Stack: [[0 11 75] [0 4] [0 13] [0 0] [0 11 75] [0 4] [0 1]]
		Roll, 5, //Stack: [[0 1] [0 11 75] [0 4] [0 13] [0 0] [0 11 75] [0 4]]

		// Address 44
		// Order: counter, modulus, base, exp, i, modulus, base
		Call, 0, 73, 3, 1,
		// PUT in order
		Roll, 1,
		Roll, 1,

		// Address 53
		// Order: exp, i - counter, modulus, base,
		Dup,
		Roll, 1,
		PushInt, 1, 0, 1,
		Add,
		Dup,
		Roll, 1,
		Roll, 1,
		Roll, 2,
		Lt,
		JmpTrue, 0, 26, // Adjust address
		// LOOP END
		Halt,

		// Address 77
		// FUNCTION Order: c, modulus, base,
		LoadLoc, 2,
		LoadLoc, 0,
		Mul,
		LoadLoc, 1,
		Mod,
		Ret,
	}

	vm := NewTestVM([]byte{})
	mc := NewMockContext(code)
	mc.Fee = 1000
	vm.context = mc
	vm.Exec(true)

	expected := 445
	vm.evaluationStack.Pop()
	vm.evaluationStack.Pop()
	actual, _ := vm.evaluationStack.Pop()

	if ByteArrayToInt(actual[1:]) != expected {
		t.Errorf("Expected actual result to be '%v' but was '%v'", expected, actual)
	}
}
@tk-codes tk-codes added the bug Something isn't working label Apr 18, 2019
@tk-codes tk-codes reopened this Apr 18, 2019
@skas102
Copy link
Collaborator

skas102 commented Apr 19, 2019

We commented out the test. Currently it fails as we added a check to case Ret: which checks that the evaluation stack does habe the same amount of elements on it as the function has return types.

E.g. function (int, bool) test() {} has two return types (int and bool) and should therefore have two elements on the evaluation stack at the end of the function.

@tk-codes
Copy link
Collaborator Author

Resolved in #37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants