o1-labs / o1js

TypeScript framework for zk-SNARKs and zkApps
https://docs.minaprotocol.com/en/zkapps/how-to-write-a-zkapp
Apache License 2.0
500 stars 110 forks source link

Fix Int64 sign checks and mod #1660

Closed mitschabaude closed 4 months ago

mitschabaude commented 4 months ago

Security fixes on Int64 which are enabled when switching to "v2" versions of some methods.

The problem that this attempts to solve:

Int64s represent signed integers as a sign: Sign and magnitude: UInt64. This allows for many efficient implementations. However, it also means that 0 has two representations: +0 and -0. This leads to ambiguity in the isPositive() function, which ignores the magnitude. [...] A malicious prover may choose to use either positive or negative zero. [...] For example, Int64.mod() uses the expression Provable.if(this.isPositive(), rest, y.value.sub(rest)) to select between rest and y - rest when computing the remainder modulo y. By choosing this to -0, an attacker may output y instead of 0.