GitHub

File tree

  • mrbgems/mruby-bigint/core

Original file line numberDiff line numberDiff line change

@@ -5098,8 +5098,10 @@ mpz_montgomery_reduce(mpz_ctx_t *ctx, mpz_t *result,

50985098

size_t k = n->sz; /* Number of limbs in modulus */

50995099

size_t x_len = x->sz;

51005100
5101-

/* Allocate workspace: need k+1 extra limbs for the product accumulation */

5102-

size_t work_size = x_len + k + 2;

5101+

/* Allocate workspace: Montgomery reduction writes k limbs at work[i] for i=0..k-1,

5102+

* so the maximum index accessed is work[2k-1] (from carry propagation).

5103+

* We need at least 2k limbs, plus extra if x_len > k. */

5104+

size_t work_size = (x_len > k) ? (x_len + k + 2) : (2 * k + 2);

51035105

size_t pool_state = pool_save(ctx);

51045106
51055107

mp_limb *work = NULL;

@@ -5263,6 +5265,7 @@ bint_set(mpz_ctx_t *ctx, struct RBigint *b, mpz_t *x)

52635265

}

52645266

else {

52655267

RBIGINT_SET_HEAP(b);

5268+

mpz_init(ctx, &b->as.heap); /* Initialize before mpz_move */

52665269

mpz_move(ctx, &b->as.heap, x);

52675270

}

52685271

}

Read the original on github.com ↗