Browse Source

Add Point decode from hex

pull/3/head
arnaucube 3 years ago
parent
commit
87847a400c
3 changed files with 36 additions and 1725 deletions
  1. +1
    -1714
      package-lock.json
  2. +15
    -2
      src/index.ts
  3. +20
    -9
      test/index.test.ts

+ 1
- 1714
package-lock.json
File diff suppressed because it is too large
View File


+ 15
- 2
src/index.ts

@ -1,7 +1,7 @@
import { randomBytes } from 'crypto' import { randomBytes } from 'crypto'
import * as BigInteger from 'bigi' import * as BigInteger from 'bigi'
import { getCurveByName, Point } from 'ecurve' import { getCurveByName, Point } from 'ecurve'
import { keccak256 } from "@ethersproject/keccak256"
import { keccak256 } from '@ethersproject/keccak256'
const ecparams = getCurveByName('secp256k1') const ecparams = getCurveByName('secp256k1')
const G = ecparams.G const G = ecparams.G
@ -12,6 +12,19 @@ export { ecparams }
export type UserSecretData = { a: BigInteger, b: BigInteger, f: Point } export type UserSecretData = { a: BigInteger, b: BigInteger, f: Point }
export type UnblindedSignature = { s: BigInteger, f: Point } export type UnblindedSignature = { s: BigInteger, f: Point }
/**
* Imports a Point from hex string where X and Y coordinates were encoded as 32
* & 32 bytes in LittleEndian.
*/
export function importPointFromHex(pointHex: string) {
const xBuff = Buffer.from(pointHex.substr(0, 64), 'hex').reverse().toString('hex')
const yBuff = Buffer.from(pointHex.substr(64), 'hex').reverse().toString('hex')
const x = BigInteger.fromHex(xBuff)
const y = BigInteger.fromHex(yBuff)
const p = Point.fromAffine(ecparams, x, y)
return p
}
export function newBigFromString(s: string) { export function newBigFromString(s: string) {
let a = new BigInteger(null, null, null) let a = new BigInteger(null, null, null)
a.fromString(s, null) a.fromString(s, null)
@ -22,7 +35,7 @@ function random(bytes: number) {
let k: BigInteger let k: BigInteger
do { do {
k = BigInteger.fromByteArrayUnsigned(randomBytes(bytes)) as unknown as BigInteger k = BigInteger.fromByteArrayUnsigned(randomBytes(bytes)) as unknown as BigInteger
} while (k.toString() == "0" && k.gcd(n).toString() != "1")
} while (k.toString() == '0' && k.gcd(n).toString() != '1')
return k return k
} }

+ 20
- 9
test/index.test.ts

@ -1,13 +1,13 @@
import * as assert from 'assert' import * as assert from 'assert'
import * as BigInteger from 'bigi' import * as BigInteger from 'bigi'
// import { Point } from 'ecurve'
import { keccak256 } from "@ethersproject/keccak256"
import { Point } from 'ecurve'
import { keccak256 } from '@ethersproject/keccak256'
import { newBigFromString, ecparams, newKeyPair, newRequestParameters, blind, blindSign, unblind, verify } from "../src/index"
import { newBigFromString, ecparams, importPointFromHex, newKeyPair, newRequestParameters, blind, blindSign, unblind, verify } from '../src/index'
describe("keccak256", function () {
it("keccak256", async () => {
const m = BigInteger.fromBuffer(Buffer.from("test", 'utf8'))
describe('keccak256', function () {
it('keccak256', async () => {
const m = BigInteger.fromBuffer(Buffer.from('test', 'utf8'))
const mHex = m.toString(16) const mHex = m.toString(16)
const hHex = keccak256('0x' + mHex) const hHex = keccak256('0x' + mHex)
assert.strictEqual('0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658', hHex) assert.strictEqual('0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658', hHex)
@ -16,14 +16,14 @@ describe("keccak256", function () {
}) })
}) })
describe("test blind", function () {
it("should blind", async () => {
describe('test blind', function () {
it('should blind', async () => {
const { sk, pk } = newKeyPair() const { sk, pk } = newKeyPair()
const { k, signerR } = newRequestParameters() const { k, signerR } = newRequestParameters()
const msg = BigInteger.fromBuffer( const msg = BigInteger.fromBuffer(
Buffer.from("test", 'utf8')
Buffer.from('test', 'utf8')
) )
assert.strictEqual('1952805748', msg.toString()) assert.strictEqual('1952805748', msg.toString())
@ -37,3 +37,14 @@ describe("test blind", function () {
assert(verified) assert(verified)
}) })
}) })
describe('import point from hex', function () {
it('', async () => {
// pointHex and expected values was generated from
// go-blindsecp256k1 (Point.Bytes())
const pointHex = '73a68e845e626a2d7f683dd2ceb57956f755d623c0b729af30a72c7bf4dee7a6932d08955c98cf21edf35f5df218b56c41014db06f513cecc85dc3d8671f9521'
const p = importPointFromHex(pointHex)
assert.strictEqual('75493613315673782629634797792529672610524641864826422940379513836118869976691', p.x.toString())
assert.strictEqual('15189800969738851359449622716277258420468338317311652509571160848111971610003', p.y.toString())
})
})

Loading…
Cancel
Save