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.

55 lines
1.5 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. } from "../src/index"
  17. describe("keccak256", function () {
  18. it("should hash strings and big numbers", async () => {
  19. const m = messageToBigNumber("test")
  20. assert.strictEqual('1952805748', m.toString())
  21. const hHex = hashBigNumber(m)
  22. assert.strictEqual(hHex, '9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658')
  23. const h = new BigNumber(Buffer.from(hHex, "hex"))
  24. assert.strictEqual(h.toString(), '70622639689279718371527342103894932928233838121221666359043189029713682937432')
  25. })
  26. it("should decode points in the secp256k1 curve", () => {
  27. const tokenR = "7cfe4af054e13b4e7231d876d23205fb5f939ac8185271ca6b64c635a365faae259fb8cabdb06dde39d1ebeada3cb75cb9739621a79c61a8cf1e9a38abaf782a"
  28. const point = decodePoint("04" + tokenR)
  29. assert.strictEqual(point.getX().toString(16), tokenR.substr(0, 64))
  30. assert.strictEqual(point.getY().toString(16), tokenR.substr(64))
  31. })
  32. })
  33. describe("blind signatures", function () {
  34. it("should blind, unblind and verify", async () => {
  35. const { sk, pk } = newKeyPair()
  36. const { k, signerR } = newRequestParameters()
  37. const msg = messageToBigNumber("test")
  38. const { mBlinded, userSecretData } = blind(msg, signerR)
  39. const sBlind = blindSign(sk, mBlinded, k)
  40. const sig = unblind(sBlind, userSecretData)
  41. const verified = verify(msg, sig, pk)
  42. assert(verified)
  43. })
  44. })