big: Add internal_int_montgomery_calc_normalization.

This commit is contained in:
Jeroen van Rijn
2021-08-27 16:41:16 +02:00
parent 893cc013b5
commit 33df335ec9
2 changed files with 19 additions and 25 deletions
+17 -24
View File
@@ -40,39 +40,32 @@ int_prime_is_divisible :: proc(a: ^Int, allocator := context.allocator) -> (res:
The method is slightly modified to shift B unconditionally upto just under The method is slightly modified to shift B unconditionally upto just under
the leading bit of b. This saves alot of multiple precision shifting. the leading bit of b. This saves alot of multiple precision shifting.
*/ */
/* internal_int_montgomery_calc_normalization :: proc(a, b: ^Int, allocator := context.allocator) -> (err: Error) {
internal_int_montgomery_calc_normalization :: proc(a, b: ^Int) -> (err: Error) { context.allocator = allocator;
/*
How many bits of last digit does b use.
*/
bits := internal_count_bits(b) % _DIGIT_BITS;
int x, bits; if b.used > 1 {
mp_err err; power := ((b.used - 1) * _DIGIT_BITS) + bits - 1;
internal_int_power_of_two(a, power) or_return;
/* how many bits of last digit does b use */
bits = mp_count_bits(b) % MP_DIGIT_BIT;
if (b->used > 1) {
if ((err = mp_2expt(a, ((b->used - 1) * MP_DIGIT_BIT) + bits - 1)) != MP_OKAY) {
return err;
}
} else { } else {
mp_set(a, 1uL); internal_one(a);
bits = 1; bits = 1;
} }
/* now compute C = A * B mod b */ /*
for (x = bits - 1; x < (int)MP_DIGIT_BIT; x++) { Now compute C = A * B mod b.
if ((err = mp_mul_2(a, a)) != MP_OKAY) { */
return err; for x := bits - 1; x < _DIGIT_BITS; x += 1 {
} internal_int_shl1(a, a) or_return;
if (mp_cmp_mag(a, b) != MP_LT) { if internal_cmp_mag(a, b) != -1 {
if ((err = s_mp_sub(a, b, a)) != MP_OKAY) { internal_sub(a, a, b) or_return;
return err;
}
} }
} }
return nil; return nil;
} }
*/
/* /*
Sets up the Montgomery reduction stuff. Sets up the Montgomery reduction stuff.
+2 -1
View File
@@ -1548,7 +1548,8 @@ _private_int_log :: proc(a: ^Int, base: DIGIT, allocator := context.allocator) -
which uses the comba method to quickly calculate the columns of the reduction. which uses the comba method to quickly calculate the columns of the reduction.
Based on Algorithm 14.32 on pp.601 of HAC. Based on Algorithm 14.32 on pp.601 of HAC.
*/ */
_private_montgomery_reduce_comba :: proc(x, n: ^Int, rho: DIGIT) -> (err: Error) { _private_montgomery_reduce_comba :: proc(x, n: ^Int, rho: DIGIT, allocator := context.allocator) -> (err: Error) {
context.allocator = allocator;
W: [_WARRAY]_WORD = ---; W: [_WARRAY]_WORD = ---;
if x.used > _WARRAY { return .Invalid_Argument; } if x.used > _WARRAY { return .Invalid_Argument; }