8000 smt: abstract multiplication by daejunpark · Pull Request #340 · a16z/halmos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

smt: abstract multiplication #340

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

Merged
merged 5 commits into from
Aug 14, 2024
Merged

smt: abstract multiplication #340

merged 5 commits into from
Aug 14, 2024

Conversation

daejunpark
Copy link
Collaborator

Abstract multiplication in a similar way to division and modulo operations.

The actual bitvector multiplication is used when either argument is concrete. Also, when an argument is a power of two, the left-shift is used instead of multiplication.

return w2

if is_power_of_two(i1):
return w2 << int(math.log(i1, 2))
Copy link
Collaborator
@0xkarmacoma 0xkarmacoma Aug 13, 2024

Choose a reason for hiding this comment

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

Suggested change
return w2 << int(math.log(i1, 2))
return w2 << i1.bit_length() - 1

quick check:

~/projects/halmos on code-fastpath wip *21 !2 ?50 ❯ python -m timeit --setup='import math' 'int(math.log(32, 2))'                                                                                                                                                                     
2000000 loops, best of 5: 121 nsec per loop

~/projects/halmos on code-fastpath wip *21 !2 ?50 ❯ python -m timeit 'int(32).bit_length() - 1'                                                                                                                                                                 
5000000 loops, best of 5: 66.1 nsec per loop

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done: 2ee4120

@@ -1348,7 +1356,35 @@ def arith(self, ex: Exec, op: int, w1: Word, w2: Word) -> Word:
return w1 - w2

if op == EVM.MUL:
return w1 * w2
if is_bv_value(w1) and is_bv_value(w2):
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we invoke is_bv_value(x) once for each and reuse the results? in the worst case it results in 6 calls to that function, which itself makes more function calls

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done: e8490ee

@@ -1348,7 +1356,35 @@ def arith(self, ex: Exec, op: int, w1: Word, w2: Word) -> Word:
return w1 - w2

if op == EVM.MUL:
return w1 * w2
if is_bv_value(w1) and is_bv_value(w2):
return w1 * w2
Copy link
Collaborator

Choose a reason for hiding this comment

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

just to double check: we're fine here in case of overflow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, and it's indeed intended.

@daejunpark daejunpark requested a review from 0xkarmacoma August 13, 2024 22:20
@daejunpark daejunpark merged commit d790f72 into main Aug 14, 2024
33 checks passed
@daejunpark daejunpark deleted the smt/abstract-mult branch August 14, 2024 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
0