Skip to content

Commit

Permalink
make shift non-destructive on a
Browse files Browse the repository at this point in the history
Shift operations mutated the input number a. Acording to the spec they shouldn't, and now they don't. At no additional computational penalty.
  • Loading branch information
dudedsy committed Jun 24, 2018
1 parent b8fae2b commit 29582f7
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions bn.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,13 @@ void bignum_lshift(struct bn* a, struct bn* b, int nbits)
require(b, "b is null");
require(nbits >= 0, "no negative shifts");

bignum_assign(b, a);
/* Handle shift in multiples of word-size */
const int nbits_pr_word = (WORD_SIZE * 8);
int nwords = nbits / nbits_pr_word;
if (nwords != 0)
{
_lshift_word(a, nwords);
_lshift_word(b, nwords);
nbits -= (nwords * nbits_pr_word);
}

Expand All @@ -339,11 +340,10 @@ void bignum_lshift(struct bn* a, struct bn* b, int nbits)
int i;
for (i = (BN_ARRAY_SIZE - 1); i > 0; --i)
{
a->array[i] = (a->array[i] << nbits) | (a->array[i - 1] >> ((8 * WORD_SIZE) - nbits));
b->array[i] = (b->array[i] << nbits) | (b->array[i - 1] >> ((8 * WORD_SIZE) - nbits));
}
a->array[i] <<= nbits;
b->array[i] <<= nbits;
}
bignum_assign(b, a);
}


Expand All @@ -352,13 +352,14 @@ void bignum_rshift(struct bn* a, struct bn* b, int nbits)
require(a, "a is null");
require(b, "b is null");
require(nbits >= 0, "no negative shifts");


bignum_assign(b, a);
/* Handle shift in multiples of word-size */
const int nbits_pr_word = (WORD_SIZE * 8);
int nwords = nbits / nbits_pr_word;
if (nwords != 0)
{
_rshift_word(a, nwords);
_rshift_word(b, nwords);
nbits -= (nwords * nbits_pr_word);
}

Expand All @@ -367,11 +368,11 @@ void bignum_rshift(struct bn* a, struct bn* b, int nbits)
int i;
for (i = 0; i < (BN_ARRAY_SIZE - 1); ++i)
{
a->array[i] = (a->array[i] >> nbits) | (a->array[i + 1] << ((8 * WORD_SIZE) - nbits));
b->array[i] = (b->array[i] >> nbits) | (b->array[i + 1] << ((8 * WORD_SIZE) - nbits));
}
a->array[i] >>= nbits;
b->array[i] >>= nbits;
}
bignum_assign(b, a);

}


Expand Down

0 comments on commit 29582f7

Please sign in to comment.