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

✨ Add log10 and log256 #594

Merged
merged 2 commits into from
Sep 6, 2023
Merged

✨ Add log10 and log256 #594

merged 2 commits into from
Sep 6, 2023

Conversation

Vectorized
Copy link
Owner

@Vectorized Vectorized commented Sep 6, 2023

Description

This PR also removes the existing revert for 0 input in log2.

@devtooligan

Actually, OZ's log10 isn't that optimal.

If you are free, help me use halmos to verify that the implementations are equivalent.

Z3 proof that oz's and solady's log10 are equivalent.

from z3 import *

def log10_oz(x):
    t = x >= 10 ** 64
    x = If(t, x / 10 ** 64, x)
    r = If(t, 64, 0)
    t = x >= 10 ** 32
    x = If(t, x / 10 ** 32, x)
    r = If(t, r + 32, r)
    t = x >= 10 ** 16
    x = If(t, x / 10 ** 16, x)
    r = If(t, r + 16, r)
    t = x >= 10 ** 8
    x = If(t, x / 10 ** 8, x)
    r = If(t, r + 8, r)
    t = x >= 10 ** 4
    x = If(t, x / 10 ** 4, x)
    r = If(t, r + 4, r)
    t = x >= 10 ** 2
    x = If(t, x / 10 ** 2, x)
    r = If(t, r + 2, r)
    t = x >= 10 ** 1
    r = If(t, r + 1, r)
    return r

def log10_solady(x):
    t = x >= 10 ** 38
    r = If(t, 38, 0)
    x = If(t, x / 10 ** 38, x)
    t = x >= 10 ** 19
    r = r + If(t, 19, 0)
    x = If(t, x / 10 ** 19, x)
    t = x >= 10 ** 10
    r = r + If(t, 10, 0)
    x = If(t, x / 10 ** 10, x)
    t = x >= 10 ** 5
    r = r + If(t, 5, 0)
    x = If(t, x / 10 ** 5, x)
    t = x >= 10 ** 3
    r = r + If(t, 3, 0)
    x = If(t, x / 10 ** 3, x)
    return r + If(x > 9, 1, 0) + If(x > 99, 1, 0)

x = Int('x')
solve(x >= 0, x < 2**256, log10_oz(x) != log10_solady(x))

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge fmt?
  • Ran forge snapshot?
  • Ran forge test?

Pull requests with an incomplete checklist will be thrown out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant