@@ -57,7 +57,9 @@ function DynamicArray<
5757 * Note: Both the actual length and the values beyond the original ones will
5858 * be constant.
5959 */
60- from ( v : ( ProvableValue | Value ) [ ] | DynamicArrayBase < ProvableValue , Value > ) : DynamicArrayBase < ProvableValue , Value > ;
60+ from (
61+ v : ( ProvableValue | Value ) [ ] | DynamicArrayBase < ProvableValue , Value >
62+ ) : DynamicArrayBase < ProvableValue , Value > ;
6163} {
6264 let innerType : Provable < ProvableValue , Value > = ProvableType . get ( type ) ;
6365
@@ -308,32 +310,56 @@ class DynamicArrayBase<ProvableValue = any, Value = any> {
308310 /**
309311 * Increments the length of the current array by n elements, checking that the
310312 * new length is within the capacity.
313+ *
314+ * An optional error message can be provided to be used in case the inner
315+ * assertion fails.
316+ *
317+ * @param n
318+ * @param message
311319 */
312- increaseLengthBy ( n : Field ) : void {
320+ increaseLengthBy ( n : Field , message ?: string ) : void {
321+ let errorMessage =
322+ message ??
323+ `increaseLengthBy: cannot increase length because provided n would exceed capacity ${ this . capacity } .` ;
324+
313325 let newLength = this . length . add ( n ) . seal ( ) ;
314- newLength . lessThanOrEqual ( new Field ( this . capacity ) ) . assertTrue ( ) ;
326+ newLength . assertLessThanOrEqual ( new Field ( this . capacity ) , errorMessage ) ;
315327 this . length = newLength ;
316328 }
317329
318330 /**
319- * Decrements the length of the current array by n elements, checking that the
320- * n is less or equal than the current length.
331+ * Decrements the length of the current array by `n` elements, checking that
332+ * the `n` is less or equal than the current length.
333+ *
334+ * An optional error message can be provided to be used in case the inner
335+ * assertion fails.
336+ *
337+ * @param n
338+ * @param message
321339 */
322- decreaseLengthBy ( n : Field ) : void {
340+ decreaseLengthBy ( n : Field , message ?: string ) : void {
341+ let errorMessage =
342+ message ??
343+ `decreaseLengthBy: cannot decrease length because provided n is larger than current array length ${ this . length } ` ;
344+
323345 let oldLength = this . length ;
324- n . assertLessThanOrEqual ( this . length ) ;
346+ n . assertLessThanOrEqual ( this . length , errorMessage ) ;
325347 this . length = oldLength . sub ( n ) . seal ( ) ;
326348 }
327349
328350 /**
329351 * Sets the length of the current array to a new value, checking that the
330352 * new length is less or equal than the capacity.
331- *
332- * @param newLength
333- *
353+ *
354+ * An optional error message can be provided to be used in case the inner
355+ * assertion fails.
356+ *
357+ * @param newLength
358+ * @param message
359+ *
334360 * **Warning**: This does not change (add nor remove) the values of the array.
335361 */
336- setLengthTo ( n : Field ) : void {
362+ setLengthTo ( n : Field , message ?: string ) : void {
337363 n . assertLessThanOrEqual ( new Field ( this . capacity ) ) ;
338364 this . length = n ;
339365 }
@@ -436,7 +462,11 @@ class DynamicArrayBase<ProvableValue = any, Value = any> {
436462 }
437463
438464 /**
439- * @returns a new DynamicArray instance with the same values as the current
465+ * Copies the current dynamic array, returning a new instance with the same
466+ * values and length.
467+ *
468+ * @returns a new DynamicArray instance with the same values as the current.
469+ *
440470 */
441471 copy ( ) : this {
442472 let newArr = new ( < any > this . constructor ) ( ) ;
0 commit comments