[M3devel] Enumeration or subrange value out of range

Ken Durocher kcdurocher at gmail.com
Mon Nov 29 18:14:56 CET 2010


I am trying to write the Tiny Encryption Algorithm (TEA) in Modula-3, but I
ran into a problem.  The code compiles fine, but I get a runtime error. I'm
running 5.8.6 RELEASE on AMD64.

P.S. Is there already an unsigned int32 type?  Is there a better way to
create one?

Here's the code:

INTERFACE Tea;

TYPE UInt32 = BITS 32 FOR [0 .. 16_FFFFFFFF];

PROCEDURE Encrypt(VAR v: ARRAY OF UInt32; VAR k: ARRAY OF UInt32);
PROCEDURE Decrypt(VAR v: ARRAY OF UInt32; VAR k: ARRAY OF UInt32);

END Tea.

***

MODULE Tea;

FROM Word IMPORT Xor, LeftShift, RightShift;

VAR
  delta := 16_9e3779b9;

PROCEDURE Encrypt(VAR v: ARRAY OF UInt32; VAR k: ARRAY OF UInt32) =
  VAR
    v0 := v[0]; v1 := v[1];
    k0 := k[0]; k1 := k[1];
    k2 := k[2]; k3 := k[3];
    sum: UInt32 := 0;

  BEGIN
    FOR i := 0 TO 31 DO
      sum := sum + delta;
      v0 := v0 + (Xor(LeftShift(v1, 4) + k0, Xor((sum + v1), RightShift(v1,
5) + k1)));
      v1 := v1 + (Xor(LeftShift(v0, 4) + k2, Xor((sum + v0), RightShift(v0,
5) + k3)));
    END;
    v[0] := v0; v[1] := v1;
  END Encrypt;

PROCEDURE Decrypt(VAR v: ARRAY OF UInt32; VAR k: ARRAY OF UInt32) =
  VAR
    v0 := v[0]; v1 := v[1];
    k0 := k[0]; k1 := k[1];
    k2 := k[2]; k3 := k[3];
    sum: UInt32 := 16_c6ef3720;
  BEGIN
    FOR i := 0 TO 32 DO
      v1 := v1 - Xor(LeftShift(v0, 4) + k2, Xor((sum + v0), RightShift(v0,
5) + k3));
      v0 := v0 - Xor(LeftShift(v1, 4) + k0, Xor((sum + v1), RightShift(v1,
5) + k1));
      sum := sum - delta;
    END;
    v[0] := v0; v[1] := v0;
  END Decrypt;

BEGIN
END Tea.

***

MODULE Main;

IMPORT IO, Fmt, Tea;

VAR
  v := ARRAY [0..1] OF Tea.UInt32 {16_48690000, 0};
  k := ARRAY [0..3] OF Tea.UInt32 {16_74657374, 0, 0, 0};

BEGIN
  Tea.Encrypt(v,k);
  IO.Put(Fmt.Unsigned(v[0]));
  IO.Put(" - ");
  IO.Put(Fmt.Unsigned(v[1]));
  IO.Put("\n");
END Main.

And the error:

***
*** runtime error:
***    An enumeration or subrange value was out of range.
***    file "../Tea.m3", line 18
***

Aborted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20101129/007fe18f/attachment-0001.html>


More information about the M3devel mailing list