• Implementing a simple encryption algorithm

    From August Karlstrom@21:1/5 to All on Sat Sep 28 14:19:38 2019
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From cfbsoftware@gmail.com@21:1/5 to August Karlstrom on Sat Sep 28 16:21:36 2019
    On Saturday, September 28, 2019 at 9:49:42 PM UTC+9:30, August Karlstrom wrote:
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From August Karlstrom@21:1/5 to cfbsoftware@gmail.com on Sun Sep 29 11:36:51 2019
    On 2019-09-29 01:21, cfbsoftware@gmail.com wrote:
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From cfbsoftware@gmail.com@21:1/5 to August Karlstrom on Tue Oct 1 04:40:25 2019
    On Sunday, September 29, 2019 at 7:06:53 PM UTC+9:30, August Karlstrom wrote:
    On 2019-09-29 01:21, cfbsoftware wrote:
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From August Karlstrom@21:1/5 to cfbsoftware@gmail.com on Tue Oct 1 17:20:32 2019
    On 2019-10-01 13:40, cfbsoftware@gmail.com wrote:
    On Sunday, September 29, 2019 at 7:06:53 PM UTC+9:30, August Karlstrom wrote:
    On 2019-09-29 01:21, cfbsoftware wrote:
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)