diff --git a/CHANGELOG.md b/CHANGELOG.md index baf5beaf6..a6b14ffc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased](https://github.com/o1-labs/o1js/compare/d6abf1d97...HEAD) +### Fixes + +- Fix behavior of `Int64.modV2()` when the input is negative and the remainder should be 0 https://github.com/o1-labs/o1js/pull/1797 + ## [1.6.0](https://github.com/o1-labs/o1js/compare/1ad7333e9e...d6abf1d97) - 2024-07-23 ### Added diff --git a/src/lib/provable/int.ts b/src/lib/provable/int.ts index bc4e741f9..15c64c624 100644 --- a/src/lib/provable/int.ts +++ b/src/lib/provable/int.ts @@ -1147,14 +1147,22 @@ class Int64 extends CircuitValue implements BalanceChange { return Int64.create(UInt64.from(obj.magnitude), Sign.fromValue(obj.sgn)); } + /** + * Turns the {@link Int64} into a {@link BigInt}. + */ + toBigint() { + let abs = this.magnitude.toBigInt(); + let sgn = this.sgn.isPositive().toBoolean() ? 1n : -1n; + return sgn * abs; + } + /** * Turns the {@link Int64} into a string. */ toString() { - let abs = this.magnitude.toString(); - let sgn = this.isPositive().toBoolean() || abs === '0' ? '' : '-'; - return sgn + abs; + return this.toBigint().toString(); } + isConstant() { return this.magnitude.value.isConstant() && this.sgn.isConstant(); } @@ -1334,7 +1342,11 @@ class Int64 extends CircuitValue implements BalanceChange { let isNonNegative = this.magnitude .equals(UInt64.zero) .or(this.sgn.isPositive()); - rest = Provable.if(isNonNegative, rest, y_.value.sub(rest)); + rest = Provable.if( + isNonNegative.or(rest.equals(0)), + rest, + y_.value.sub(rest) + ); return new Int64(new UInt64(rest.value)); }