solidblu1992 / ethereum

GNU General Public License v3.0
18 stars 3 forks source link

Curve choices #6

Closed darioAnongba closed 6 years ago

darioAnongba commented 6 years ago

Hi again!

Sorry to keep bothering you but I finally finished implementing a simple system using UTXO and CTs and used your Python scripts for testing. Now, I wanted to rewrite some functions in JS in order to use them in a web app or in a mobile app. Sadly, I was faced with the harsh reality of crypto and Elliptic curves. I tried getting more information about alt_bn_128 but there is virtually nothing on the web. So I came back here to see if you can help me.

Basically my question is, why is all your implementation based on the alt_bn_128 curve? Why does it feel like most of your implementations are made "by hand", doesn't any library exist for these operations? I'm speaking about util.py, bn128_curve.py, etc. I found that those files are originally from the ethereum github repo.

I found a great package in JS : https://github.com/indutny/elliptic here. Sadly, the alt_bn_128 curve is not supported. What would imply using a different curve than alt_bn_128?

Thank you very much for your time.

solidblu1992 commented 6 years ago

I use alt_bn_128 because that is the only curve which has pre-compiles on Ethereum. You could drop use Secp256k1 instead however gas costs will be much higher (about 50% higher last time I tested?)

I can't remember exactly where I got the bn128_curve.py and bn128_field_elements.py but I didn't create those. Edit: it was from py_ecc.

solidblu1992 commented 6 years ago

Can you somehow modify that JS library to use alt_bn_128? Like, we know the curve is the same type as Secp256k1 (y^2 = x^3 + Ax + B): A = 0 B = 3 P = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 N = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001

For more information, check EIP-196. It looks like they have an implementation in C++ and in Rust as well.

darioAnongba commented 6 years ago

Thanks! I understand better. The definition for secp256k1 is:

defineCurve('secp256k1', {
  type: 'short',
  prime: 'k256',
  p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f',
  a: '0',
  b: '7',
  n: 'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141',
  h: '1',
  hash: hash.sha256,

  // Precomputed endomorphism
  beta: '7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee',
  lambda: '5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72',
  basis: [
    {
      a: '3086d221a7d46bcde86c90e49284eb15',
      b: '-e4437ed6010e88286f547fa90abfe4c3'
    },
    {
      a: '114ca50f7a8e2f3f657c1108d9d44cfd8',
      b: '3086d221a7d46bcde86c90e49284eb15'
    }
  ],

  gRed: false,
  g: [
    '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
    '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
    pre
  ]
});

It would surprise me if I could juste change the values and it would just work... I can try though.

I heard that with WebAssembly it is possible to use C++ functions in JS. I will try to make it work with their C++ library and let you know.

solidblu1992 commented 6 years ago

Dario,

I've come across another project which uses alt_bn_128 in JS. Maybe some of this would suit your needs?