ZenGo-X / curv

Rust language general purpose elliptic curve cryptography.
MIT License
264 stars 111 forks source link

Negative BigInt should be an error #107

Open survived opened 3 years ago

survived commented 3 years ago

We use BigInt mostly in modular arithmetic where there's no negative numbers. However, negative BigInt can be mistakenly constructed and cause a wrong behaviour.

One way to work around this problem would be replacing BigInt with BigUint that has no sign. Achieving negative number (e.g. BigUint::zero() - BigUint::one()) would cause a panic. But this is not a reliable solution for cryptographic library.

Better option would be allowing BigInt to participate only in modular arithmetic operations. I.e. arithmetic expression should not be evaluated unless module value is specified. This reminds me a lazy evaluation from functional world. I've wrote a simple PoC: here.