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

Signed-right-shift with large shift amounts gives wrong result #716

Closed
brianhuffman opened this issue May 5, 2020 · 2 comments
Closed
Labels
bug Something not working correctly
Milestone

Comments

@brianhuffman
Copy link
Contributor

The signed right shift operator (>>$) produces the wrong result when given a shift amount too large to fit in a Haskell Int (2^63 or higher).

Cryptol> 0x55 >>$ 0x8000000000000000
0x55

Both the concrete and symbolic evaluation backends have this problem:

Cryptol> :prove \(x:[8]) -> x >>$ 0x8000000000000000 == x
Q.E.D.
(Total Elapsed Time: 0.008s, using "Z3")
Cryptol> :exhaust \(x:[8]) -> x >>$ 0x8000000000000000 == x
Using exhaustive testing.
Passed 256 tests.
Q.E.D.

Here is the offending function from the concrete backend. It includes an unsafe use of fromInteger (#637).

sshrV :: Value
sshrV =
nlam $ \_n ->
nlam $ \_k ->
wlam Concrete $ \(BV i x) -> return $
wlam Concrete $ \y ->
let signx = testBit x (fromInteger (i-1))
amt = fromInteger (bvVal y)
x' = if signx then x - bit (fromInteger i) else x
in return . VWord i . ready . WordVal . mkBv i $! x' `shiftR` amt

@brianhuffman brianhuffman added the bug Something not working correctly label May 5, 2020
@brianhuffman
Copy link
Contributor Author

The situation is a bit complicated in the symbolic backend. It appears that the bug only shows up when the shift amount is concrete.

Cryptol> :prove \(i:[64]) -> (0x55 >>$ i == 0x55) == (i == 0)
Q.E.D.
(Total Elapsed Time: 0.010s, using "Z3")

@atomb atomb added this to the 2.9.0 milestone May 5, 2020
@robdockins
Copy link
Contributor

Fixed via #724.

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

No branches or pull requests

3 participants