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.

94 lines
3.6 KiB

  1. import * as assert from 'assert'
  2. import {
  3. stringToBigNumber,
  4. messageToBigNumber,
  5. decodePoint,
  6. ecParams,
  7. newKeyPair,
  8. newRequestParameters,
  9. blind,
  10. blindSign,
  11. unblind,
  12. verify,
  13. hashBigNumber,
  14. Point,
  15. BigNumber,
  16. signatureToHex,
  17. signatureFromHex
  18. } from "../src/index"
  19. describe("keccak256", function () {
  20. it("should hash strings and big numbers", async () => {
  21. const m = messageToBigNumber("test")
  22. assert.strictEqual('1952805748', m.toString())
  23. const hHex = hashBigNumber(m)
  24. assert.strictEqual(hHex, '9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658')
  25. const h = new BigNumber(Buffer.from(hHex, "hex"))
  26. assert.strictEqual(h.toString(), '70622639689279718371527342103894932928233838121221666359043189029713682937432')
  27. })
  28. it("should decode points in the secp256k1 curve", () => {
  29. const tokenR = "7cfe4af054e13b4e7231d876d23205fb5f939ac8185271ca6b64c635a365faae259fb8cabdb06dde39d1ebeada3cb75cb9739621a79c61a8cf1e9a38abaf782a"
  30. const point = decodePoint("04" + tokenR)
  31. assert.strictEqual(point.getX().toString(16), tokenR.substr(0, 64))
  32. assert.strictEqual(point.getY().toString(16), tokenR.substr(64))
  33. })
  34. })
  35. describe("blind signatures", function () {
  36. it("should blind, unblind and verify", async () => {
  37. const { sk, pk } = newKeyPair()
  38. const { k, signerR } = newRequestParameters()
  39. const msg = messageToBigNumber("test")
  40. const { mBlinded, userSecretData } = blind(msg, signerR)
  41. const sBlind = blindSign(sk, mBlinded, k)
  42. const sig = unblind(sBlind, userSecretData)
  43. const verified = verify(msg, sig, pk)
  44. assert(verified)
  45. })
  46. })
  47. describe("encoding", () => {
  48. it("should encode and decode signatures", () => {
  49. const { sk } = newKeyPair()
  50. const { k, signerR } = newRequestParameters()
  51. const msg = messageToBigNumber("test")
  52. const { mBlinded, userSecretData } = blind(msg, signerR)
  53. const sBlind = blindSign(sk, mBlinded, k)
  54. const sig = unblind(sBlind, userSecretData)
  55. const hexSignature = signatureToHex(sig)
  56. assert.strictEqual(hexSignature.length, 192)
  57. const decodedSig = signatureFromHex(hexSignature)
  58. assert.strictEqual(decodedSig.f.encode("hex", false).length, 130)
  59. assert.strictEqual(signatureToHex(signatureFromHex(hexSignature)), hexSignature)
  60. // explicit values
  61. const hexSignature2 = "089a89f07bd41560454b1640fd30e51ee088d1be8355275a88e38a38f2e7a3af9e80fcc14af9c47c8066c6726e7d3cac9370494d5c67936b2978d6cecf5a4d21bf3ef00b060c47ba874c0764d662eff2d0e9daa8ba766f4aa6a2be8ec3d37523"
  62. const decodedSig2 = signatureFromHex(hexSignature2)
  63. assert.strictEqual(decodedSig2.s.toString("hex"), "afa3e7f2388ae3885a275583bed188e01ee530fd40164b456015d47bf0899a08")
  64. assert.strictEqual(decodedSig2.f.encode("hex", false).substr(2), "9e80fcc14af9c47c8066c6726e7d3cac9370494d5c67936b2978d6cecf5a4d21bf3ef00b060c47ba874c0764d662eff2d0e9daa8ba766f4aa6a2be8ec3d37523")
  65. assert.strictEqual(signatureToHex(signatureFromHex(hexSignature2)), hexSignature2)
  66. // swapEndianness(s) starting with 0
  67. const hexSignature3 = "089a89f07bd41560454b1640fd30e51ee088d1be8355275a88e38a38f2e7a30f9e80fcc14af9c47c8066c6726e7d3cac9370494d5c67936b2978d6cecf5a4d21bf3ef00b060c47ba874c0764d662eff2d0e9daa8ba766f4aa6a2be8ec3d37523"
  68. const decodedSig3 = signatureFromHex(hexSignature3)
  69. assert.strictEqual(decodedSig3.s.toString("hex"), /* 0 */ "fa3e7f2388ae3885a275583bed188e01ee530fd40164b456015d47bf0899a08")
  70. assert.strictEqual(decodedSig3.f.encode("hex", false).substr(2), "9e80fcc14af9c47c8066c6726e7d3cac9370494d5c67936b2978d6cecf5a4d21bf3ef00b060c47ba874c0764d662eff2d0e9daa8ba766f4aa6a2be8ec3d37523")
  71. assert.strictEqual(signatureToHex(signatureFromHex(hexSignature3)), hexSignature3)
  72. })
  73. })