1
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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).

$\endgroup$
5
  • $\begingroup$ Unsigned integers are not supported by Java, except for bytes. However the operations +, -, * and left shift behave the same on signed and unsigned integers. $\endgroup$
    – A. Hersean
    Sep 17 at 9:34
  • $\begingroup$ @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.) $\endgroup$ Sep 18 at 3:28
  • $\begingroup$ @A.Hersean How would I use modulo 2^32 in Verilog? $\endgroup$
    – tomneil
    Sep 21 at 0:08
  • $\begingroup$ @dave_thompson_085 Do you know if &0xFF is needed in Verilog and how I can implement modulo 2^32 arithmetic? $\endgroup$
    – tomneil
    Sep 21 at 0:10
  • $\begingroup$ @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. $\endgroup$ Sep 22 at 1:34
0
$\begingroup$

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 >>>.

$\endgroup$
2
  • $\begingroup$ 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)))? $\endgroup$
    – tomneil
    Sep 17 at 15:39
  • $\begingroup$ @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. $\endgroup$
    – fgrieu
    Sep 17 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.