Browse Source

Allowing to decode points from the curve

pull/1/head
Jør∂¡ 3 years ago
parent
commit
3d3d3c0c8d
2 changed files with 28 additions and 12 deletions
  1. +11
    -2
      src/index.ts
  2. +17
    -10
      test/index.test.ts

+ 11
- 2
src/index.ts

@ -15,6 +15,11 @@ export const ecParams = { G, n }
export type UserSecretData = { a: BigNumber, b: BigNumber, f: Point } export type UserSecretData = { a: BigNumber, b: BigNumber, f: Point }
export type UnblindedSignature = { s: BigNumber, f: Point } export type UnblindedSignature = { s: BigNumber, f: Point }
export function messageToBigNumber(message: string) {
const msg = Buffer.from(message, 'utf8')
return new BigNumber(msg)
}
export function hashBigNumber(m: BigNumber) { export function hashBigNumber(m: BigNumber) {
const mHex = m.toString(16) const mHex = m.toString(16)
@ -24,10 +29,14 @@ export function hashBigNumber(m: BigNumber) {
return keccak256('0x0' + mHex).slice(2) // Trim 0x return keccak256('0x0' + mHex).slice(2) // Trim 0x
} }
export function bigNumberFromString(s: string) {
export function stringToBigNumber(s: string) {
return new BigNumber(s) return new BigNumber(s)
} }
export function decodePoint(hexPoint: string): Point {
return secp256k1.keyFromPublic(Buffer.from(hexPoint, "hex")).getPublic()
}
function random(bytes: number) { function random(bytes: number) {
let k: BigNumber let k: BigNumber
do { do {
@ -96,7 +105,7 @@ export function verify(m: BigNumber, s: UnblindedSignature, q: Point) {
const sG = G.mul(s.s) const sG = G.mul(s.s)
const hHex = hashBigNumber(m) const hHex = hashBigNumber(m)
const h = new BigNumber(Buffer.from(hHex, "hex")) const h = new BigNumber(Buffer.from(hHex, "hex"))
const rx = s.f.getX().mod(n) const rx = s.f.getX().mod(n)

+ 17
- 10
test/index.test.ts

@ -1,7 +1,9 @@
import * as assert from 'assert' import * as assert from 'assert'
import { import {
bigNumberFromString,
stringToBigNumber,
messageToBigNumber,
decodePoint,
ecParams, ecParams,
newKeyPair, newKeyPair,
newRequestParameters, newRequestParameters,
@ -15,26 +17,31 @@ import {
} from "../src/index" } from "../src/index"
describe("keccak256", function () { describe("keccak256", function () {
it("keccak256", async () => {
const msg = Buffer.from("test", 'utf8')
const m = new BigNumber(msg)
it("should hash strings and big numbers", async () => {
const m = messageToBigNumber("test")
assert.strictEqual('1952805748', m.toString())
const hHex = hashBigNumber(m) const hHex = hashBigNumber(m)
assert.strictEqual(hHex, '9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658') assert.strictEqual(hHex, '9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658')
const h = new BigNumber(Buffer.from(hHex, "hex")) const h = new BigNumber(Buffer.from(hHex, "hex"))
assert.strictEqual(h.toString(), '70622639689279718371527342103894932928233838121221666359043189029713682937432') assert.strictEqual(h.toString(), '70622639689279718371527342103894932928233838121221666359043189029713682937432')
}) })
it("should decode points in the secp256k1 curve", () => {
const tokenR = "7cfe4af054e13b4e7231d876d23205fb5f939ac8185271ca6b64c635a365faae259fb8cabdb06dde39d1ebeada3cb75cb9739621a79c61a8cf1e9a38abaf782a"
const point = decodePoint("04" + tokenR)
assert.strictEqual(point.getX().toString(16), tokenR.substr(0, 64))
assert.strictEqual(point.getY().toString(16), tokenR.substr(64))
})
}) })
describe("test blind", function () {
it("should blind", async () => {
describe("blind signatures", function () {
it("should blind, unblind and verify", async () => {
const { sk, pk } = newKeyPair() const { sk, pk } = newKeyPair()
const { k, signerR } = newRequestParameters() const { k, signerR } = newRequestParameters()
const msg = new BigNumber(
Buffer.from("test", 'utf8')
)
assert.strictEqual('1952805748', msg.toString())
const msg = messageToBigNumber("test")
const { mBlinded, userSecretData } = blind(msg, signerR) const { mBlinded, userSecretData } = blind(msg, signerR)

Loading…
Cancel
Save