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

cryptol: Prelude.(!!): negative index #111

Closed
jpziegler opened this issue Sep 23, 2014 · 5 comments
Closed

cryptol: Prelude.(!!): negative index #111

jpziegler opened this issue Sep 23, 2014 · 5 comments
Labels
bug Something not working correctly
Milestone

Comments

@jpziegler
Copy link
Contributor

I was playing around in Cryptol with a toy problem (digits of pi) and received the following error, crashing everything and dumping me out of the repl. My Cryptol version is 2.0.0 (86b2a80).

Main> ((pi`{a=4} 0) && 0xffff0000000000) == 0x243f0000000000
cryptol: Prelude.(!!): negative index

Here's the code I was running:

type D = 14
M = `(16^^D):[D*8]
SHIFT = `(4 * D):[D*8]
MASK = M - 1

S j n = lsum+rsum
  where
    lsums = [ (s + ((modexp 16 (n-k) r)<<SHIFT)/r) && MASK where r = 8*k+j
            | k <- [0...] | s <- [0]#lsums ]
    lsum = lsums@n
    rsums = [ t + xp/(8*(n+k)+j) where xp = M >> (4*k)
            | k <- [1...] | t <- [0]#rsums ]
    rsum = rsums@(max n 12)

modexp a n m =
  if n == 0 then 1
  else if n % 2 == 0 then modexp ((a * a) % m) (n / 2) m
  else (a * modexp ((a * a) % m) (n / 2) m) % m

pi : {a} (112 - a >= 1, 112 >= a) => [112 - a] -> [D*4]
pi n = reverse(take`{D*4}(reverse((4*(S 1 n') - 2*(S 4 n') - (S 5 n') - (S 6 n'\
)) && MASK where n' = 0#n-1)))
@TomMD
Copy link
Contributor

TomMD commented Sep 23, 2014

I can reproduce this on a fresh build of cryptol from master, thanks for the report!

@TomMD
Copy link
Contributor

TomMD commented Sep 23, 2014

A simpler version strongly implies that the implementation of @ is a Haskell (list !!) . fromIntegral:

Main> [1..(2^^128)] @ (~zero : [64])
Assuming a = 129
cryptol: Prelude.(!!): negative index

@TomMD TomMD closed this as completed in 2cd1ee3 Sep 23, 2014
@TomMD
Copy link
Contributor

TomMD commented Sep 23, 2014

I pushed a quick fix but the issue also raises the question of how much sanity checking Cryptol should do for the user. @kiniry @dylanmc Is it Cryptol's job to tell the user when they are being absurd by doing something that, while sematically meaningful, will not terminate or is likely to take a long time (such as this case, iterating through 2^64 or even more elements in a list)?

@weaversa
Copy link
Collaborator

Though, the real culprit is 1-based indexing.

n' = 0#n-1

@jpziegler
Copy link
Contributor Author

Yeah... not my prettiest code. It's a work in progress! ;)

@acfoltzer acfoltzer added this to the Cryptol 2.1 milestone Oct 16, 2014
@acfoltzer acfoltzer added the bug Something not working correctly label Oct 16, 2014
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

4 participants