Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Showstopper upstream bug - static object #5

Closed
mratsim opened this issue Jun 11, 2019 · 1 comment
Closed

Showstopper upstream bug - static object #5

mratsim opened this issue Jun 11, 2019 · 1 comment

Comments

@mratsim
Copy link
Owner

mratsim commented Jun 11, 2019

There are 2 kinds of implementation possible

  1. Like in any language, the prime number is a global stored in the binary. Main trade-offs are:
  • We don't use the typesystem at all to check that A + B are compatible (because using same modulo)
  • {.noSideEffect.} tracking will be cumbersome.
  • No constant folding
  • We need a way to deal with "different modulus" error. Can we use exceptions given that we want to run even on embedded, using result + error code or just error code makes library usage clunky as well.
  1. Embed the prime number in the type system.

Upstream: nim-lang/Nim#11142

@mratsim
Copy link
Owner Author

mratsim commented Feb 23, 2020

Workaround via static curve configuration.

This doesn't allow us to configure an arbitrary finite field but is enough for cryptography.
This also allow the library to ensure constants are only defined once and so stored only once in ROM to optimize binary sizes.

when not defined(testingCurves):
declareCurves:
# Barreto-Naehrig curve, pairing-friendly, Prime 254 bit, ~100-bit security
# https://eprint.iacr.org/2013/879.pdf
# Usage: Zero-Knowledge Proofs / zkSNARKs in ZCash and Ethereum 1
# https://eips.ethereum.org/EIPS/eip-196
curve BN254:
bitsize: 254
modulus: "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"
# Equation: Y^2 = X^3 + 3
curve BLS12_381:
bitsize: 381
modulus: "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
# Equation: y^2 = x^3 + 4
curve P256: # secp256r1 / NIST P-256
bitsize: 256
modulus: "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff"
else:
# Fake curve for testing field arithmetic
declareCurves:
curve Fake101:
bitsize: 7
modulus: "0x65" # 101 in hex
curve Mersenne61:
bitsize: 61
modulus: "0x1fffffffffffffff" # 2^61 - 1
curve Mersenne127:
bitsize: 127
modulus: "0x7fffffffffffffffffffffffffffffff" # 2^127 - 1
curve P256: # secp256r1 / NIST P-256
bitsize: 256
modulus: "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff"
curve BLS12_381:
bitsize: 381
modulus: "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant