Commit 77a27c0c

77a27c0c0213Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

bigint: import mruby correctness fixes (Barrett gate, Montgomery pre-reduce, trim canonical zero)

Three correctness/UB fixes from mruby-bigint that the standalone library (adapted in d9637369) had not yet picked up. All three are pure algorithm / memory-safety fixes with no mrb_state / GC dependency, so they port verbatim.

  • mpz_mod: gate the Barrett path on its precondition x < 2^(2*bits(m)). The old gate (x->sz >= y->sz + 2) let x reach ~25 limbs against a 4-limb modulus, where mpz_barrett_reduce silently truncates high limbs and returns garbage. Add x->sz <= 2 * y->sz so out-of-range inputs fall through to the general division path. (mruby 4507b4a6)

  • mpz_powm_montgomery: pre-reduce base mod n before forming base * R^2. REDC requires its input T < R*N; an unreduced base (base >= n) overflows that bound and REDC truncates, giving a wrong result. (mruby 206b9f74)

  • trim: maintain the canonical invariant sz == 0 implies sn == 0. An inconsistent zero (sn != 0, sz == 0) can flow into mpz_sqr, miss the zero_p guard, and reach mpn_zero(NULL, 0) which is UB (memset nonnull arg). (mruby 6ac2c3dc, close #6849)

The Barrett/powm paths are reached by Integer#remainder / Integer#pow(b,e,m), not yet wired into spinel codegen, so these are latent until that lands; trim's canonical-zero fix is exercised today via loop-multiplication squaring. No spinel->mruby bigint changes are needed (mruby already has equal-or-better).

Gates: 950 tests pass, 57 benchmarks pass (bm_bigint_fib, bm_pidigits), reachable bigint paths match CRuby.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
77a27c0c0213
Parent
003c30228b13

lib/sp_bigint.cmodifiedView file

@@ -365,6 +365,8 @@
365365 while (x->sz && x->p[x->sz-1] == 0) {
366366 x->sz--;
367367 }
368+ /* Maintain invariant: sz == 0 implies sn == 0 (zero is canonical). */
369+ if (x->sz == 0) x->sn = 0;
368370 }
369371
370372 /* z = x + y, without regard for sign */
@@ -3139,8 +3141,11 @@
31393141 return;
31403142 }
31413143
3142- /* Barrett reduction for moderate-sized moduli (>= 4 limbs where setup is worthwhile) */
3143- if (y->sz >= 4 && y->sz <= 16 && x->sz >= y->sz + 2) {
3144+ /* Barrett reduction for moderate-sized moduli (>= 4 limbs where setup is worthwhile).
3145+ * Barrett's precondition is x < 2^(2*bits(m)); inputs beyond ~2*m.sz limbs
3146+ * violate it and the algorithm silently truncates high limbs. Fall through
3147+ * to general division for those. */
3148+ if (y->sz >= 4 && y->sz <= 16 && x->sz >= y->sz + 2 && x->sz <= 2 * y->sz) {
31443149 mpz_t mu;
31453150 mpz_init_temp(ctx, &mu, y->sz + 1);
31463151 mpz_barrett_mu(ctx, &mu, y);
@@ -5221,12 +5226,19 @@
52215226 mpz_init(ctx, &one_mont);
52225227 mpz_montgomery_reduce(ctx, &one_mont, &R2, n, rho);
52235228
5224- /* Convert base to Montgomery form: base_mont = base * R mod n = REDC(base * R^2) */
5225- mpz_t base_mont, temp;
5229+ /* Convert base to Montgomery form: base_mont = base * R mod n = REDC(base * R^2).
5230+ * REDC requires its input T to satisfy T < R*N. If `base` is not already
5231+ * reduced (e.g. base >= n), `base * R^2` can exceed R*N and REDC produces
5232+ * a wrong result. Pre-reduce base modulo n via mpz_mmod (the general
5233+ * division path) -- both operands are non-negative here so this is
5234+ * semantically equivalent to mpz_mod. */
5235+ mpz_t base_mont, base_reduced, temp;
52265236 mpz_init(ctx, &base_mont);
5237+ mpz_init(ctx, &base_reduced);
52275238 mpz_init_temp(ctx, &temp, n->sz * 4);
52285239
5229- mpz_mul(ctx, &temp, (mpz_t*)base, &R2);
5240+ mpz_mmod(ctx, &base_reduced, (mpz_t*)base, (mpz_t*)n);
5241+ mpz_mul(ctx, &temp, &base_reduced, &R2);
52305242 mpz_montgomery_reduce(ctx, &base_mont, &temp, n, rho);
52315243
52325244 /* Initialize accumulator to 1 in Montgomery form */
@@ -5258,6 +5270,7 @@
52585270 mpz_clear(ctx, &R2);
52595271 mpz_clear(ctx, &one_mont);
52605272 mpz_clear(ctx, &base_mont);
5273+ mpz_clear(ctx, &base_reduced);
52615274 mpz_clear(ctx, &temp);
52625275 mpz_clear(ctx, &acc);
52635276 pool_restore(ctx, pool_state);