herumi / bls-wasm

BLS signature for Node.js by WebAssembly
47 stars 26 forks source link

Public Key Generator #14

Closed d4rk5h0t closed 2 years ago

d4rk5h0t commented 2 years ago

We need to have a basepoint other than what is provided by default. as in go library, there is a function named SetGeneratorOfPublicKey providing this I was wondering how we can achieve the same with the js library I've added it to the js library on my fork ready to PR but the problem is my tests are failing can you help me out?

added section:

    // change curveType
    exports.blsInit = (curveType = exports.ethMode ? exports.BLS12_381 : exports.BN254) => {
      const r = mod._blsInit(curveType, MCLBN_COMPILED_TIME_VAR)
      if (r) throw ('blsInit err ' + r)
    }
    exports.blsSetGeneratorOfPublicKey = (gen) => {
      const r = mod._blsSetGeneratorOfPublicKey(gen)
      if (r) throw ('blsSetGeneratorOfPublicKey err ' + r)
    }

test that is failing:


function generatorOfPublicKeyTest() {
  let gen = new bls.PublicKey();
  gen.deserializeUncompressed("1 24aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8 13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801 606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be");
  bls.blsSetGeneratorOfPublicKey(gen)
  let sk = new bls.SecretKey()
  sk.setInt(1);
  let pk = sk.getPublicKey();
  console.log(sk.serializeToHexStr())
  console.log(pk.serializeToHexStr())
  assert(pk.serializeToHexStr() == "93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8");
}

the output of failing test:

> bls-wasm@0.4.7 test
> node test/test.js

name=BLS12_381 curve order=52435875175126190479447740508185965837690552500527637822603658699938581184513
secretKey b51e7aeee544971b65965c9fb2da8e0312ed852c16d90344516ec2a2160c713b
publicKey 7ac55e61b8dac68f1edb723c8dca51e1af6091c7f4f10272d5e2fdb2ef59809dd50111c48ef817977dabd11ef0ec33188ca054ea19321c12d2f39129df7cf6d8f6a5848f2f567a45dc6551220e5837fe1b2b2ad93328db7955f978976d695a0c
msg doremifa
signature 804d0732e3fe750c9a6a4c3b676880e2d749fd68221b56bdd231f2d60acb88313c08a1ad47682c48bcfea8a38b3d820d
idxVec=4,7,8,9
/work/bls-wasm/src/bls.js:112
        if (r === 0 || r !== buf.length) throw new Error('err _wrapDeserialize', buf)
                                               ^

Error: err _wrapDeserialize
    at work/bls-wasm/src/bls.js:112:48
    at exports.PublicKey._setter (work/bls-wasm/src/bls.js:273:19)
    at exports.PublicKey.deserializeUncompressed (work/bls-wasm/src/bls.js:522:14)
    at generatorOfPublicKeyTest (work/bls-wasm/test/test.js:230:7)
    at work/bls-wasm/test/test.js:16:7
herumi commented 2 years ago

I've added some functions. https://github.com/herumi/bls-wasm/compare/master...dev How about it?