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

width not working correctly #549

Closed
weaversa opened this issue Oct 10, 2018 · 5 comments
Closed

width not working correctly #549

weaversa opened this issue Oct 10, 2018 · 5 comments

Comments

@weaversa
Copy link
Collaborator

width does not seem to work correctly.

Cryptol> `(width 39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319)
Showing a specific instance of polymorphic result:
  * Using 'Integer' for type argument 'rep' of 'Cryptol::number'
384
Cryptol> width 39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319
Showing a specific instance of polymorphic result:
  * Using '512' for type argument 'n' of 'Cryptol::width'
  * Using '10' for type argument 'bits' of 'Cryptol::width'
512
@brianhuffman
Copy link
Contributor

You don't need to use such a big number to get a similar result:

Cryptol> width 20
Showing a specific instance of polymorphic result:
  * Using '8' for type argument 'n' of 'Cryptol::width'
  * Using '4' for type argument 'bits' of 'Cryptol::width'
8
Cryptol> `(width 20)
Showing a specific instance of polymorphic result:
  * Using 'Integer' for type argument 'rep' of 'Cryptol::number'
5

I think the real reason for the confusion here is that we screwed up by giving the type-level width operator and the value-level width function the same name. They do different things. (I think it would be better to call the value-level one length.)

The type-level width computes the minimum number of bits required to represent the given size argument. On the other hand, the value-level width ignores its argument; its result only depends on its argument's type.

So why does width 20 evaluate to 8 instead of 5? The reason is that width 20 is polymorphic, so the REPL needs to choose a type instance to evaluate:

Cryptol> :t width 20
width 20 : {n, m} (n >= 5, m >= width n, fin n, fin m) => [m]

When you type width 20, Cryptol arbitrarily chooses n = 8 and m = 4. (It used to be the case that Cryptol would always choose the smallest possible values for numeric type variables, but we don't guarantee this.) The result then simply comes from the value of the type variable n, which is 8.

@yav
Copy link
Member

yav commented Oct 10, 2018

We do some model shrinking to try and find "smaller" examples, but what exactly constitutes smaller is not very obvious. The behavior might also differ based on the solver you are using. For example, I get slightly different behavior then both of you:

  • on @weaversa's example I get 384, using 15 bits
  • while width 20 doesn't even work for some reason
  • however, width 10 does work with result 7, represented in 4 bits

So I think it would be good to see if we can come up with some more consistent behavior.

@brianhuffman
Copy link
Contributor

I went ahead and renamed the value-level width function to length (see #550)

@brianhuffman
Copy link
Contributor

Is there anything left that we need to do for this ticket? Are there any more tweaks to the defaulting that we want to do, or should we close?

@kiniry
Copy link
Member

kiniry commented Jun 20, 2019

Syntax.md and Programming Cryptol need to be updated given the rename of value-level width to length. There are also, I imagine, a number of examples that need to be updated. I just was bit by this rename in work on our SHA2 specification, e.g.

brianhuffman pushed a commit that referenced this issue Jun 20, 2019
It is now called `length` and has a generalized type.

Cf. #549.
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

No branches or pull requests

4 participants