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.<div><br></div><div>P.S. Is there already an unsigned int32 type? Is there a better way to create one?<br>
<div><br></div><div>Here's the code:</div><div><br></div><div><div>INTERFACE Tea;</div><div><br></div><div>TYPE UInt32 = BITS 32 FOR [0 .. 16_FFFFFFFF];</div><div><br></div><div>PROCEDURE Encrypt(VAR v: ARRAY OF UInt32; VAR k: ARRAY OF UInt32);</div>
<div>PROCEDURE Decrypt(VAR v: ARRAY OF UInt32; VAR k: ARRAY OF UInt32);</div><div><br></div><div>END Tea.</div></div><div><br></div><div>***</div><div><br></div><div><div>MODULE Tea;</div><div><br></div><div>FROM Word IMPORT Xor, LeftShift, RightShift;</div>
<div><br></div><div>VAR</div><div> delta := 16_9e3779b9;</div><div><br></div><div>PROCEDURE Encrypt(VAR v: ARRAY OF UInt32; VAR k: ARRAY OF UInt32) =</div><div> VAR</div><div> v0 := v[0]; v1 := v[1];</div><div> k0 := k[0]; k1 := k[1]; </div>
<div> k2 := k[2]; k3 := k[3]; </div><div> sum: UInt32 := 0;</div><div><br></div><div> BEGIN</div><div> FOR i := 0 TO 31 DO</div><div> sum := sum + delta;</div><div> v0 := v0 + (Xor(LeftShift(v1, 4) + k0, Xor((sum + v1), RightShift(v1, 5) + k1)));</div>
<div> v1 := v1 + (Xor(LeftShift(v0, 4) + k2, Xor((sum + v0), RightShift(v0, 5) + k3)));</div><div> END;</div><div> v[0] := v0; v[1] := v1;</div><div> END Encrypt;</div><div><br></div><div>PROCEDURE Decrypt(VAR v: ARRAY OF UInt32; VAR k: ARRAY OF UInt32) =</div>
<div> VAR</div><div> v0 := v[0]; v1 := v[1];</div><div> k0 := k[0]; k1 := k[1];</div><div> k2 := k[2]; k3 := k[3];</div><div> sum: UInt32 := 16_c6ef3720;</div><div> BEGIN</div><div> FOR i := 0 TO 32 DO</div>
<div> v1 := v1 - Xor(LeftShift(v0, 4) + k2, Xor((sum + v0), RightShift(v0, 5) + k3));</div><div> v0 := v0 - Xor(LeftShift(v1, 4) + k0, Xor((sum + v1), RightShift(v1, 5) + k1));</div><div> sum := sum - delta;</div>
<div> END;</div><div> v[0] := v0; v[1] := v0;</div><div> END Decrypt;</div><div><br></div><div>BEGIN</div><div>END Tea.</div></div><div><br></div><div>***</div><div><br></div><div><div>MODULE Main;</div><div><br></div>
<div>IMPORT IO, Fmt, Tea;</div><div><br></div><div>VAR</div><div> v := ARRAY [0..1] OF Tea.UInt32 {16_48690000, 0};</div><div> k := ARRAY [0..3] OF Tea.UInt32 {16_74657374, 0, 0, 0};</div><div><br></div><div>BEGIN</div>
<div> Tea.Encrypt(v,k);</div><div> IO.Put(Fmt.Unsigned(v[0]));</div><div> IO.Put(" - ");</div><div> IO.Put(Fmt.Unsigned(v[1]));</div><div> IO.Put("\n");</div><div>END Main.</div></div></div><div><br>
</div><div>And the error:</div><div><br></div><div><div>***</div><div>*** runtime error:</div><div>*** An enumeration or subrange value was out of range.</div><div>*** file "../Tea.m3", line 18</div><div>***</div>
<div><br></div><div>Aborted</div></div><div><br></div><div><br></div>