@@ -302,15 +302,14 @@ export class MpZ {
302302 const q = this . size ;
303303 const z = new StaticArray < u32 > ( q + 1 ) ;
304304
305- let k : u64 = 0 ;
305+ let k : bool = 0 ;
306306 for ( let i : i32 = 0 ; i < q ; ++ i ) {
307307 const lx = unchecked ( this . _data [ i ] ) ;
308308 const ly = rhs . size > i ? unchecked ( rhs . _data [ i ] ) : 0 ;
309- k += u64 ( lx ) + u64 ( ly ) ;
310- unchecked ( ( z [ i ] = LOW ( k ) ) ) ;
311- k = HIGH ( k ) ;
309+ unchecked ( ( z [ i ] = lx + ly + k ) ) ;
310+ k = z [ i ] < lx || ( k && z [ i ] === lx ) ;
312311 }
313- unchecked ( ( z [ q ] = LOW ( k ) ) ) ;
312+ unchecked ( ( z [ q ] = k ) ) ;
314313
315314 return new MpZ ( z ) ;
316315 }
@@ -330,6 +329,31 @@ export class MpZ {
330329 return new MpZ ( z ) ;
331330 }
332331
332+ /**
333+ * #### `#inc(): MpZ`
334+ *
335+ * Returns the increment of this MpZ (`this + 1`).
336+ */
337+ @operator . prefix ( '++' )
338+ @operator . postfix ( '++' )
339+ inc ( ) : MpZ {
340+ if ( this . isNeg ) return this . _udec ( ) . negate ( ) ; // -a + 1 = -(a - 1)
341+ return this . _uinc ( ) ;
342+ }
343+
344+ protected _uinc ( ) : MpZ {
345+ const q = this . size ;
346+ const z = new StaticArray < u32 > ( q + 1 ) ;
347+
348+ let k : bool = 1 ;
349+ for ( let i : i32 = 0 ; i < q ; ++ i ) {
350+ unchecked ( ( z [ i ] = k + this . _data [ i ] ) ) ;
351+ k = unchecked ( z [ i ] < this . _data [ i ] ) ;
352+ }
353+ unchecked ( ( z [ q ] = k ) ) ;
354+ return new MpZ ( z ) ;
355+ }
356+
333357 // *** Subtraction ***
334358
335359 /**
@@ -393,6 +417,34 @@ export class MpZ {
393417 return new MpZ ( z ) ;
394418 }
395419
420+ /**
421+ * #### `#dec(): MpZ`
422+ *
423+ * Returns the decrement of this MpZ (`this - 1`).
424+ */
425+ @operator . prefix ( '--' )
426+ @operator . postfix ( '--' )
427+ dec ( ) : MpZ {
428+ if ( this . isNeg ) return this . _uinc ( ) . negate ( ) ; // -a - 1 = -(a + 1)
429+ if ( this . eqz ( ) ) return MpZ . ONE . negate ( ) ;
430+ return this . _udec ( ) ;
431+ }
432+
433+ protected _udec ( ) : MpZ {
434+ const q = this . size ;
435+ const z = new StaticArray < u32 > ( q ) ;
436+
437+ let k : bool = 1 ;
438+ for ( let i : i32 = 0 ; i < q ; ++ i ) {
439+ const lx = unchecked ( this . _data [ i ] ) ;
440+
441+ unchecked ( ( z [ i ] = lx - k ) ) ;
442+ k = k > lx ? 1 : 0 ;
443+ }
444+
445+ return new MpZ ( z ) ;
446+ }
447+
396448 // *** Multiplication ***
397449
398450 /**
@@ -948,7 +1000,7 @@ export class MpZ {
9481000 */
9491001 // @ts -ignore
9501002 protected not ( ) : MpZ {
951- return this . isNeg ? this . _usubU32 ( 1 ) : this . _uaddU32 ( 1 ) . negate ( ) ;
1003+ return this . isNeg ? this . _udec ( ) : this . _uinc ( ) . negate ( ) ;
9521004 }
9531005
9541006 /**
@@ -1385,20 +1437,6 @@ export class MpZ {
13851437 return lhs . sub ( rhs ) ;
13861438 }
13871439
1388-
1389- @operator . prefix ( '++' )
1390- @operator . postfix ( '++' )
1391- protected inc ( ) : MpZ {
1392- return this . _uaddU32 ( 1 ) ;
1393- }
1394-
1395-
1396- @operator . prefix ( '--' )
1397- @operator . postfix ( '--' )
1398- protected dec ( ) : MpZ {
1399- return this . _usubU32 ( 1 ) ;
1400- }
1401-
14021440 /**
14031441 * ### Comparison Operators
14041442 *
0 commit comments