You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
3.5 KiB

  1. import * as assert from 'assert'
  2. import * as BigInteger from 'bigi'
  3. import { keccak256 } from '@ethersproject/keccak256'
  4. import { pointFromHex, newKeyPair, newRequestParameters, blind, blindSign, unblind, verify, signatureFromHex, signatureToHex, messageToBigNumber, pointToHex } from '../src/index'
  5. describe('keccak256', function () {
  6. it('should hash strings and big numbers', async () => {
  7. const m = BigInteger.fromBuffer(Buffer.from('test', 'utf8'))
  8. const mHex = m.toString(16)
  9. const hHex = keccak256('0x' + mHex)
  10. assert.strictEqual('0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658', hHex)
  11. const h = BigInteger.fromHex(hHex.slice(2))
  12. assert.strictEqual('70622639689279718371527342103894932928233838121221666359043189029713682937432', h.toString())
  13. })
  14. })
  15. describe('test blind', function () {
  16. it('should blind', async () => {
  17. const { sk, pk } = newKeyPair()
  18. const { k, signerR } = newRequestParameters()
  19. const msg = BigInteger.fromBuffer(
  20. Buffer.from('test', 'utf8')
  21. )
  22. assert.strictEqual('1952805748', msg.toString())
  23. const { mBlinded, userSecretData } = blind(msg, signerR)
  24. const sBlind = blindSign(sk, mBlinded, k)
  25. const sig = unblind(sBlind, userSecretData)
  26. const verified = verify(msg, sig, pk)
  27. assert(verified)
  28. })
  29. })
  30. describe('import point from hex', function () {
  31. it('should import a point', async () => {
  32. // pointHex and expected values was generated from
  33. // go-blindsecp256k1 (Point.Bytes())
  34. const pointHex = '73a68e845e626a2d7f683dd2ceb57956f755d623c0b729af30a72c7bf4dee7a6932d08955c98cf21edf35f5df218b56c41014db06f513cecc85dc3d8671f9521'
  35. const p = pointFromHex(pointHex)
  36. assert.strictEqual('75493613315673782629634797792529672610524641864826422940379513836118869976691', p.x.toString())
  37. assert.strictEqual('15189800969738851359449622716277258420468338317311652509571160848111971610003', p.y.toString())
  38. assert.strictEqual(pointToHex(pointFromHex(pointHex)), pointHex)
  39. })
  40. it("should import a signature", () => {
  41. const originalSignatureHex = "36cbb281c7d3f51fa6c027c39d1ec1115e72f4cf78beda1292ef16736894f327a77b4a7e7ba7b7daa54de6a111cb69b6a89bd8c15756a3d0bc620e77b134a9be2cf4d4758c2a5c59381f30bca694b585d9bbb192d349f7220b4e414503cd7eb6"
  42. const { s, f } = signatureFromHex(originalSignatureHex)
  43. assert.strictEqual(s.toString(10), "18070569205902663717816547129479407690196063715098828904678654635357913664310")
  44. assert.strictEqual(f.affineX.toString(10), "86238402060026834577259540812071998074585426519937266202081591629132264536999")
  45. assert.strictEqual(f.affineY.toString(10), "82544976118490904161760686015396902971720732626625394239692986344240783356972")
  46. assert.strictEqual(signatureToHex(signatureFromHex(originalSignatureHex)), originalSignatureHex)
  47. })
  48. it("should import a signature with leading 0's on the big integers", () => {
  49. const originalSignatureHex = "089a89f07bd41560454b1640fd30e51ee088d1be8355275a88e38a38f2e7a3000080fcc14af9c47c8066c6726e7d3cac9370494d5c67936b2978d6cecf5a4d00003ef00b060c47ba874c0764d662eff2d0e9daa8ba766f4aa6a2be8ec3d37500"
  50. const { s, f } = signatureFromHex(originalSignatureHex)
  51. assert.strictEqual(s.toString(10), "289596905226702587911033129111285060518391679824489553383063470810094737928")
  52. assert.strictEqual(f.affineX.toString(10), "136673983650666514404918584888974483560635967304551499498329425981927620608")
  53. assert.strictEqual(f.affineY.toString(10), "208182647280612121090241159906896377333338287898138605151276081386137599488")
  54. assert.strictEqual(signatureToHex(signatureFromHex(originalSignatureHex)), originalSignatureHex)
  55. })
  56. })