Skip to content

Commit 8578fd4

Browse files
authored
math.big: fix zero base in big_mod_pow(), add tests (#24940)
1 parent 080b56f commit 8578fd4

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎vlib/math/big/big_test.v‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ struct PowTest {
399399

400400
// vfmt off
401401
const pow_test_data = [
402+
PowTest{ 0, 0, 1},
403+
PowTest{ 0, 1, 0},
404+
PowTest{ 1, 0, 1},
405+
PowTest{ 1, 1, 1},
402406
PowTest{ 2, 0, 1 },
403407
PowTest{ 2, 1, 2 },
404408
PowTest{ 2, 5, 32 },
@@ -422,6 +426,10 @@ struct ModPowTest {
422426

423427
// vfmt off
424428
const mod_pow_test_data = [
429+
ModPowTest{ 0, 0, 123, 1 },
430+
ModPowTest{ 0, 1, 123, 0 },
431+
ModPowTest{ 1, 0, 123, 1 },
432+
ModPowTest{ 1, 1, 123, 1 },
425433
ModPowTest{ 324, 315, 632, 512 },
426434
ModPowTest{ 65, 17, 3233, 2790 },
427435
ModPowTest{ 2790, 413, 3233, 65 },
@@ -437,6 +445,10 @@ struct BigModPowTest {
437445

438446
// vfmt off
439447
const big_mod_pow_test_data = [
448+
BigModPowTest{ 0, 0, 4205, 1 },
449+
BigModPowTest{ 0, 1, 4205, 0 },
450+
BigModPowTest{ 1, 0, 4205, 1 },
451+
BigModPowTest{ 1, 1, 4205, 1 },
440452
BigModPowTest{ 23, 35, 4205, 552 }, // passed to mod_pow
441453
BigModPowTest{
442454
'5155371529688',

‎vlib/math/big/integer.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ pub fn (base Integer) big_mod_pow(exponent Integer, modulus Integer) !Integer {
632632

633633
// 0^x == 0 (x != 0 due to previous clause)
634634
if base.signum == 0 {
635-
return one_int
635+
return zero_int
636636
}
637637

638638
if exponent.bit_len() == 1 {

0 commit comments

Comments
 (0)