Score:1

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

us flag

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.

Score:1
ru flag

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

A. Hersean avatar
cr flag
Unsigned integers are not supported by Java, except for bytes. However the operations +, -, * and left shift behave the same on signed and unsigned integers.
dave_thompson_085 avatar
cn flag
@A.Hersean: you mean `char`s; 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.)
us flag
@A.Hersean How would I use modulo 2^32 in Verilog?
us flag
@dave_thompson_085 Do you know if `&0xFF` is needed in Verilog and how I can implement modulo 2^32 arithmetic?
dave_thompson_085 avatar
cn flag
@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.
Score:0
ng flag

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

us flag
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)))?
fgrieu avatar
ng flag
@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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.