Answer the question
In order to leave comments, you need to log in
How to perform addition modulo 2^512?
Hello! It is necessary to perform the operation of addition modulo 2^512 for the "Stiribog" hashing algorithm described by the GOST 34.11-2012 standard. Two 512-bit numbers are added.
I studied the examples of implementations of this operation that I found, but they all use a bitwise shift, which is not in the language used. For example, in JavaScript it looks like this:
/* https://github.com/rudonick/crypto/blob/670cef467fc4ed19896baee6044710779a0843fb/gostR3411.js#L184 */
function add512(x, y) {
var CF = 0, w0, w1;
for (var i = 0; i < 16; i++) {
w0 = (x[i] & 0xffff) + (y[i] & 0xffff) + (CF || 0);
w1 = (x[i] >>> 16) + (y[i] >>> 16) + (w0 >>> 16);
x[i] = (w0 & 0xffff) | (w1 << 16);
CF = (w1 >>> 16);
}
}
/* https://github.com/okazymyrov/stribog/blob/master/C/standard/stribog.c */
void AddModulo512(const unsigned char *a,const unsigned char *b,unsigned char *c)
{
int i = 0, t = 0;
for(i=63;i>=0;i--)
{
t = a[i] + b[i] + (t >> 8);
c[i] = t & 0xFF;
}
}
Answer the question
In order to leave comments, you need to log in
A bitwise shift to the right by 1 bit is a division by 2 with the fractional part discarded.
A bitwise shift left by 1 bit is a multiplication by 2. Do you know
about GMP ?
I studied the examples of implementations of this operation that I found, but they all use a bitwise shift
void AddModulo512(const uint32_t *a,const uint32_t *b, uint32_t *c)
{
unsigned carry = 0;
unsigned i;
for (i = 0; i < 16; ++i) {
c[i] = a[i] + b[i] + carry;
carry = a[i] + carry < carry || c[i] < b[i];
}
}
It is not known what features of your programming language.
You can pervert and rewrite the code in C without using shift:
void AddModulo512(const unsigned char *a,const unsigned char *b,unsigned char *c)
{
int i = 0, t = 0;
unsigned char * pResult = (unsigned char *)(&t);
unsigned char * pNext = pResult + 1;
for(i=63;i>=0;i--)
{
t = a[i] + b[i] + *pNext;
c[i] = *pResult;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question