Discussion:
Implementing a simple encryption algorithm
(too old to reply)
August Karlstrom
2019-09-28 12:19:38 UTC
Permalink
I'm trying to implement the tiny encryption algorithm (TEA) in Oberon
(latest revision). However, I can't find a way to perform bitwise XOR
(exclusive or) on integers. I know that for sets the symmetric
difference can be calculated using / but there is no way to convert an
integer to a set. Any clues?

https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
https://www.miasap.se/obnc/oberon-report.html


-- August
c***@gmail.com
2019-09-28 23:21:36 UTC
Permalink
Post by August Karlstrom
I'm trying to implement the tiny encryption algorithm (TEA) in Oberon
(latest revision). However, I can't find a way to perform bitwise XOR
(exclusive or) on integers. I know that for sets the symmetric
difference can be calculated using / but there is no way to convert an
integer to a set. Any clues?
https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
https://www.miasap.se/obnc/oberon-report.html
'Convert' or 'Typecast'?

You can use SYSTEM.VAL to typecast an INTEGER to a SET. e.g. setVal := SYSTEM.VAL(SET, intVal) if that is what you need in this case assuming both data types are the same size in the implementation of Oberon that you are using.

Some extended implementations of Oberon (e.g. the Astrobe compilers, Component Pascal) have a built-in function BITS that is equivalent. The following relationships are true:

intVal = ORD(BITS(intVal))
setVal = BITS(ORD(setVal))

Regards,
Chris Burrows
CFB Software
https://www.astrobe.com
August Karlstrom
2019-09-29 09:36:51 UTC
Permalink
Post by c***@gmail.com
You can use SYSTEM.VAL to typecast an INTEGER to a SET.
Ah, of course. Thanks! Below is my implementation in Oberon. Is there a
more efficient way to implement logical right shift?

MODULE Tea;

(**Tiny Encryption Algorithm*)

IMPORT SYSTEM;

PROCEDURE Bits(x: INTEGER): SET;
RETURN SYSTEM.VAL(SET, x)
END Bits;


PROCEDURE Lsr(x, n: INTEGER): INTEGER; (*logical shift right*)
RETURN ORD(Bits(ROR(x, n)) - {32 - n .. 31})
END Lsr;


PROCEDURE Encrypt*(k0, k1, k2, k3: INTEGER; VAR v0, v1: INTEGER);
CONST delta = 9E3779B9H;
VAR sum, i: INTEGER;
BEGIN
sum := 0;
FOR i := 0 TO 31 DO
INC(sum, delta);
INC(v0, ORD(Bits(LSL(v1, 4) + k0) / Bits(v1 + sum) / Bits(Lsr(v1, 5)
+ k1)));
INC(v1, ORD(Bits(LSL(v0, 4) + k2) / Bits(v0 + sum) / Bits(Lsr(v0, 5)
+ k3)))
END
END Encrypt;


PROCEDURE Decrypt*(k0, k1, k2, k3: INTEGER; VAR v0, v1: INTEGER);
CONST delta = 9E3779B9H;
VAR sum, i: INTEGER;
BEGIN
sum := 0C6EF3720H;
FOR i := 0 TO 31 DO
DEC(v1, ORD(Bits(LSL(v0, 4) + k2) / Bits(v0 + sum) / Bits(Lsr(v0, 5)
+ k3)));
DEC(v0, ORD(Bits(LSL(v1, 4) + k0) / Bits(v1 + sum) / Bits(Lsr(v1, 5)
+ k1)));
DEC(sum, delta)
END
END Decrypt;

BEGIN
ASSERT(SYSTEM.SIZE(INTEGER) = 4);
ASSERT(SYSTEM.SIZE(SET) = 4)
END Tea.


-- August
c***@gmail.com
2019-10-01 11:40:25 UTC
Permalink
Post by August Karlstrom
Post by c***@gmail.com
You can use SYSTEM.VAL to typecast an INTEGER to a SET.
Ah, of course. Thanks! Below is my implementation in Oberon. Is there a
more efficient way to implement logical right shift?
Try ROR(LSL(ROR(x, n), n), n)

Regards,
Chris
August Karlstrom
2019-10-01 15:20:32 UTC
Permalink
Post by c***@gmail.com
Post by August Karlstrom
Post by c***@gmail.com
You can use SYSTEM.VAL to typecast an INTEGER to a SET.
Ah, of course. Thanks! Below is my implementation in Oberon. Is there a
more efficient way to implement logical right shift?
Try ROR(LSL(ROR(x, n), n), n)
Ah, that's a nice one. Thanks!


-- August

Continue reading on narkive:
Loading...