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

Cannot import scalar values from BLS12-381 Ethereum test suite #9

Closed
jochem-brouwer opened this issue Jun 20, 2020 · 6 comments
Closed

Comments

@jochem-brouwer
Copy link

Hi there. At EthereumJS-VM we are using this library in order to get the EIP 2537 in the VM (the BLS12-381 EIP).

Currently, I am struggling importing the first test case of this EIP. The input value has 128 bytes of G1 point data (i.e. the coordinates, first 64 bytes and second 64 bytes are the coordinate x,y-values) and the final 32 bytes are the scalar value we want to multiply with.

Here is the code:

const mcl = require('mcl-wasm')
mcl.init(mcl.BLS12_381).then(function() {
  const x_1 = "12196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f56"
  const y_1 = "06ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992fee"
  const scalar =   "b3c940fe79b6966489b527955de7599194a9ac69a6ff58b8d99e7b1084f0464e"
  //let scalar2 =  "351376bf068f448c76382a97c33a68f81021f6eff4af9e52080ecee12e5ddccf" // this was generated from setCSPRNG()

  const mcl_str = "1 " + x_1 + " " + y_1

  const point = new mcl.G1()
  point.setStr(mcl_str, 16)

  const r = new mcl.Fr()
  r.setStr(scalar, 16)
  console.log(r.getStr(16))

  result = mcl.mul(point, r)

  console.log(result.getStr(16))
})

Problem: this throws:

Error: err _wrapInput b3c940fe79b6966489b527955de7599194a9ac69a6ff58b8d99e7b1084f0464e

According to the test, this value should be correct. If I try the scalar2 value, which was generated by setCSPRNG then it runs fine.

@jochem-brouwer
Copy link
Author

jochem-brouwer commented Jun 20, 2020

If I check the second test, which has scalar value 4d0e25bf3f6fc9f4da25d21fdc71773f1947b7a8a775b8177f7eca990b05b71d, the code runs fine.

Could this have to do with the hex string starting with the character 'b'?

@jochem-brouwer
Copy link
Author

Other values, including those not starting with 'a-f' characters, do not seem to import. Converting the values to decimals also fails.

@herumi
Copy link
Owner

herumi commented Jun 21, 2020

If you want BLS12-381 for Ethereum, then I think it is better to use bls-eth-wasm.

@jochem-brouwer
Copy link
Author

Hey there. I think there might be some confusion here. I am referring to this EIP, which is supposed to get included in the next hard fork of Ethereum 1.0. This indeed has BLS curves which ETH 2.0 also appears to use. However, for Ethereum 1.0, we need some low-level calls to these curves.

Specifically, we need G1/2 addition, multiplication and multi-multiplication (mulVec), pairing operations, and the mapToG1 and mapToG2 functions for these curves.

The b3c940fe79b6966489b527955de7599194a9ac69a6ff58b8d99e7b1084f0464e hex number is used in the official tests; however, mcl-wasm does not seem to allow this scalar on the Fr field.

@herumi
Copy link
Owner

herumi commented Jun 23, 2020

mcl.Fr() is a finite field class of the prime r.
The value b3c940fe79b6966489b527955de7599194a9ac69a6ff58b8d99e7b1084f0464e is bigger than r, then mcl.deserialize() throws an exception.
I added a method setBigEndianMod(x) and it sets a value of (x % r).
Could you try it?

@jochem-brouwer
Copy link
Author

Hey @herumi, this indeed works! It also looks (at least for this single test case) as it returns the expected value. Thanks a lot!

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

No branches or pull requests

2 participants