Skip to content

Commit

Permalink
Merge pull request #1797 from o1-labs/fix/mod-int64
Browse files Browse the repository at this point in the history
Fix Int64 mod v2
  • Loading branch information
mitschabaude committed Aug 21, 2024
2 parents 0617674 + ecac253 commit 9b57f3d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions src/lib/provable/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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));
}

Expand Down

0 comments on commit 9b57f3d

Please sign in to comment.