# RC6 Integer operations in modulo 32 between two 32-bit blocks

I am new to cryptography and I am trying to code the RC6 (Rivest cipher 6) algorithm. The algorithm requires addition, subtraction and multiplication in modulo 232. If I am performing these operations between two 32-bit blocks how would this work?

Any help would be appreciated because I can't seem to find any detailed explanation on this which would help me write code on how to execute these operations.

This will depend on the language that you implement in. Java and other C-like languages have a built-in data type to represent unsigned 32-bit integers (this is why RC6 chose to use this form of arithmetic, so that its implementation in these languages is relatively straightforward). In such cases +, -, and * all automatically work mod $$2^{32}$$.

If you're using python, you can simply use the % operator which returns remainders mod whatever value is specified e.g. a=(b+c)%(2**32).

• Unsigned integers are not supported by Java, except for bytes. However the operations +, -, * and left shift behave the same on signed and unsigned integers. Sep 17 at 9:34
• @A.Hersean: you mean chars; Java byte is signed, which is a big nuisance in crypto code where you must (remember to) &0xFF or (byte) on nearly all references --although the JIT-compiler may optimize these into something like MOV.B. (But the types really used in the stack machine, int and long, are defined as exact-size twos-complement with wraparound, so yes they are equivalent to unsigned for the operations you list.) Sep 18 at 3:28
• @A.Hersean How would I use modulo 2^32 in Verilog? Sep 21 at 0:08
• @dave_thompson_085 Do you know if &0xFF is needed in Verilog and how I can implement modulo 2^32 arithmetic? Sep 21 at 0:10
• @tomneil: I know nothing at all about Verilog and cannot help you. If that is important to your question, it should be in your question. Sep 22 at 1:34

You want to work modulo $$2^{32}$$, except for shift counts where that should be modulo $$32$$.

The following is generic and works in Python.

code (result in z) operation
z = (x+y)&0xffffffff 32-bit addition of x and y
z = (x-y)&0xffffffff 32-bit subtraction x minus y
z = (x*y)&0xffffffff 32-bit multiplication of x and y
z = ((x<<(31&y))|(x>>(31&-y)))&0xffffffff 32-bit left-rotation of x by low 5 bits of y

In modern C or C++, use variables of type uint32_t defined in header <stdint.h> or <cstdint>, and optionally remove the &0xffffffff.

In Java, use variables of type int, remove the &0xffffffff, change >> to >>>.

• So if I was trying to do this in Verilog, I would also have to use variable of type uint32_t and define <stdint.h>? Then I could use the 32-bit operators by just using conventional x+y, x-y, x*y and ((x<<(31&y))|(x>>(31&-y)))? Sep 17 at 15:39
• @tomneil: my guess is if it compiles, it will work, and there's a chance it's not grossly inefficient thanks to automatic optimizations. But then my only contact with Verilog is once helping someone using it.
– fgrieu
Sep 17 at 15:52