Skip to content

[Parameterized Modules] newtype crash #812

@WeeknightMVP

Description

@WeeknightMVP

Consider a parameterized block cipher specification and nested instantiation:

BlockCipher_.cry

module BlockCipher_ where

parameter
    type _Key: *
    type _Block: *
    type constraint Cmp _Block

    _encrypt: Op
    _decrypt: Op

type Op = _Key -> _Block -> _Block

newtype BlockCipher = { encrypt: Op, decrypt: Op }
// blockCipher = BlockCipher { encrypt = _encrypt, decrypt = _decrypt }  // <-- induces crash

_XORCipher_.cry

module _XORCipher_ = BlockCipher_ where

parameter
    type _KeyBlock: *
    type constraint (Cmp _KeyBlock, Logic _KeyBlock)

type _Key = _KeyBlock
type _Block = _KeyBlock

_encrypt = (^)`{_KeyBlock}
_decrypt = (^)`{_KeyBlock}

_XOR32Cipher.cry

module _XOR32Cipher = _XORCipher_ where

type _KeyBlock = [32]

Alas, uncommenting the indicated line in BlockCipher_ causes a crash when loading _XOR32Cipher:

_XORCipher_(parameterized)> :m BlockCipher_
Loading module BlockCipher_
BlockCipher_(parameterized)> :m _XORCipher_ 
Loading module _XORCipher_
_XORCipher_(parameterized)> :m _XOR32Cipher 
Loading module _XOR32Cipher
cryptol.exe: You have encountered a bug in Cryptol's implementation.
*** Please create an issue at https://github.com/GaloisInc/cryptol/issues

%< ---------------------------------------------------
  Revision:  1e1f7af812b122e0dcd4f4d36a2a84a4864427a4
  Branch:    HEAD (uncommited files present)
  Location:  [Eval] evalExpr
  Message:   var `BlockCipher_::BlockCipher` is not defined
...
_XOR32Cipher::BlockCipher -> <function>
...
 _XOR32Cipher::BlockCipher -> <function>
CallStack (from HasCallStack):
  panic, called at src\Cryptol\Utils\Panic.hs:21:9 in cryptol-2.8.1-CsLNPSw0BJM1P20gctBZPn:Cryptol.Utils.Panic
  panic, called at src\Cryptol\Eval.hs:158:9 in cryptol-2.8.1-CsLNPSw0BJM1P20gctBZPn:Cryptol.Eval
%< ---------------------------------------------------

Modifying _XOR32Cipher to instantiate BlockCipher_ directly (with the same line uncommented) causes a similar crash.

Given this issue, achieving the desired effect (to export an encapsulated block cipher instance, e.g. as a parameter for cipher modes) requires exporting aliases for BlockCipher_ parameters...

BlockCipher_.cry

type Key = _Key
type Block = _Block

encrypt = _encrypt
decrypt = _decrypt

...and wrapping _XOR32Cipher:

module XOR32Cipher where

import _XOR32Cipher

blockCipher = BlockCipher { encrypt=encrypt, decrypt=decrypt }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions