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: Binary Maths #82

Merged
merged 15 commits into from
Nov 16, 2021
Merged

Fix: Binary Maths #82

merged 15 commits into from
Nov 16, 2021

Conversation

tigh-latte
Copy link
Contributor

Currently, arithmeitc that overflows math.MaxInt64 and underflows math.MinInt64 just causes integer overflows within the script, meaning maths that should be compatible with large numbers just does not work on our interpreter.

Also, OP_NUM2BIN and OP_BIN2NUM are implemented incorrectly.

To fix this, I've fixed the implementations of the two aforementioned opcodes, and changed the scriptNum data type to wrap big.Int, implementing the correct math functions and byte conversion both to and from consensus, to and from standard big endian.

@codecov-commenter
Copy link

codecov-commenter commented Nov 15, 2021

Codecov Report

Merging #82 (7a9c1d1) into master (4f458d2) will increase coverage by 0.58%.
The diff coverage is 98.90%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #82      +/-   ##
==========================================
+ Coverage   83.94%   84.53%   +0.58%     
==========================================
  Files          35       35              
  Lines        3370     3504     +134     
==========================================
+ Hits         2829     2962     +133     
- Misses        394      395       +1     
  Partials      147      147              
Impacted Files Coverage Δ
bscript/interpreter/thread.go 94.30% <ø> (ø)
bscript/interpreter/number.go 97.94% <97.94%> (ø)
bscript/interpreter/config.go 100.00% <100.00%> (ø)
bscript/interpreter/operations.go 96.54% <100.00%> (+0.38%) ⬆️
bscript/interpreter/stack.go 93.56% <100.00%> (+0.03%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4f458d2...7a9c1d1. Read the comment docs.

// interpreting it as an integer, it provides the required behaviour.
type scriptNumber struct {
val *big.Int
afterGenesis bool
Copy link
Member

Choose a reason for hiding this comment

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

is it only utxos which are after genesis that are treated this way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nah every utxo, tho the ones before genesis are capped within the limits of pre-genesis

@@ -241,6 +241,7 @@ func (t *thread) CheckErrorCondition(finalScript bool) error {
if finalScript {
t.afterSuccess()
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This is my fav change

Copy link
Contributor

@theflyingcodr theflyingcodr left a comment

Choose a reason for hiding this comment

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

Yes

@mergify mergify bot merged commit 55823e4 into master Nov 16, 2021
@mergify mergify bot deleted the fix/binary-maths branch November 16, 2021 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants