Make bits2int reduce mod n (#53)

This commit is contained in:
Ken MacKay
2015-10-12 21:29:57 -07:00
parent 0283b542ef
commit 6826dd4789
+11 -6
View File
@@ -407,7 +407,7 @@ uECC_VLI_API void uECC_vli_modAdd(uECC_word_t *result,
const uECC_word_t *mod, const uECC_word_t *mod,
wordcount_t num_words) { wordcount_t num_words) {
uECC_word_t carry = uECC_vli_add(result, left, right, num_words); uECC_word_t carry = uECC_vli_add(result, left, right, num_words);
if (carry || uECC_vli_cmp_unsafe(result, mod, num_words) >= 0) { if (carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) {
/* result > mod (result = mod + remainder), so subtract mod to get remainder. */ /* result > mod (result = mod + remainder), so subtract mod to get remainder. */
uECC_vli_sub(result, result, mod, num_words); uECC_vli_sub(result, result, mod, num_words);
} }
@@ -1023,19 +1023,24 @@ static void bits2int(uECC_word_t *native,
if (bits_size > num_n_bytes) { if (bits_size > num_n_bytes) {
bits_size = num_n_bytes; bits_size = num_n_bytes;
} }
uECC_vli_clear(native, num_n_words);
uECC_vli_bytesToNative(native, bits, bits_size, curve); uECC_vli_bytesToNative(native, bits, bits_size, curve);
if (bits_size * 8 <= (unsigned)curve->num_n_bits) { if (bits_size * 8 <= (unsigned)curve->num_n_bits) {
return; return;
} }
int shift = bits_size * 8 - curve->num_n_bits; int shift = bits_size * 8 - curve->num_n_bits;
uECC_word_t carry = 0; uECC_word_t carry = 0;
uECC_word_t *end = native; uECC_word_t *ptr = native + num_n_words;
native += num_n_words; while (ptr-- > native) {
while (native-- > end) { uECC_word_t temp = *ptr;
uECC_word_t temp = *native; *ptr = (temp >> shift) | carry;
*native = (temp >> shift) | carry;
carry = temp << (uECC_WORD_BITS - shift); carry = temp << (uECC_WORD_BITS - shift);
} }
/* Reduce mod curve_n */
if (uECC_vli_cmp_unsafe(curve->n, native, num_n_words) != 1) {
uECC_vli_sub(native, native, curve->n, num_n_words);
}
} }
static int uECC_sign_with_k(const uint8_t *private_key, static int uECC_sign_with_k(const uint8_t *private_key,