fix ts errors

This commit is contained in:
Marin Petrunić 2019-09-17 21:02:32 +02:00
parent 6143104204
commit b8c262791a
4 changed files with 14 additions and 93 deletions

View File

@ -5,7 +5,7 @@ import assert from "assert";
import {calculateYFlag, getModulus} from "./utils"; import {calculateYFlag, getModulus} from "./utils";
import * as random from "secure-random"; import * as random from "secure-random";
import {FP_POINT_LENGTH} from "../constants"; import {FP_POINT_LENGTH} from "../constants";
import {BLSPubkey, bytes48} from "@chainsafe/eth2.0-types"; import {bytes48} from "@chainsafe/eth2.0-types";
export class G1point { export class G1point {
@ -28,10 +28,6 @@ export class G1point {
return new G1point(sum); return new G1point(sum);
} }
public addRaw(other: bytes48): G1point {
return this.add(G1point.fromBytesCompressed(other));
}
public equal(other: G1point): boolean { public equal(other: G1point): boolean {
return this.point.equals(other.point); return this.point.equals(other.point);
} }
@ -131,44 +127,4 @@ export class G1point {
} while (ecp.is_infinity()); } while (ecp.is_infinity());
return new G1point(ecp); return new G1point(ecp);
} }
public mul(value: BIG): G1point {
const newPoint = this.point.mul(value);
return new G1point(newPoint);
}
public add(other: G1point): G1point {
const sum = new ctx.ECP();
sum.add(this.point);
sum.add(other.point);
sum.affine();
return new G1point(sum);
}
public equal(other: G1point): boolean {
return this.point.equals(other.point);
}
public toBytes(): bytes48 {
const buffer = Buffer.alloc(FP_POINT_LENGTH, 0);
this.point.getX().tobytearray(buffer, 0);
return buffer;
}
public getPoint(): ECP {
return this.point;
}
public toBytesCompressed(): bytes48 {
const output = this.toBytes();
const c = true;
const b = this.point.is_infinity();
const a = !b && calculateYFlag(this.point.getY());
const flags = ((a ? 1 << 5 : 0) | (b ? 1 << 6 : 0) | (c ? 1 << 7 : 0));
const mask = 31;
output[0] &= mask;
output[0] |= flags;
return output;
}
} }

View File

@ -1,6 +1,6 @@
import {BIG} from "@chainsafe/milagro-crypto-js/src/big"; import {BIG} from "@chainsafe/milagro-crypto-js/src/big";
import {ECP2} from "@chainsafe/milagro-crypto-js/src/ecp2"; import {ECP2} from "@chainsafe/milagro-crypto-js/src/ecp2";
import {sha256} from 'js-sha256'; import {sha256} from "js-sha256";
import ctx from "../ctx"; import ctx from "../ctx";
import * as random from "secure-random"; import * as random from "secure-random";
import {calculateYFlag, getModulus, padLeft} from "./utils"; import {calculateYFlag, getModulus, padLeft} from "./utils";
@ -37,7 +37,7 @@ export class G2point {
return this.point; return this.point;
} }
public toBytesCompressed(): bytes48 { public toBytesCompressed(): Buffer {
const xReBytes = Buffer.alloc(FP_POINT_LENGTH, 0); const xReBytes = Buffer.alloc(FP_POINT_LENGTH, 0);
const xImBytes = Buffer.alloc(FP_POINT_LENGTH, 0); const xImBytes = Buffer.alloc(FP_POINT_LENGTH, 0);
this.point.getX().getA().tobytearray(xReBytes, 0); this.point.getX().getA().tobytearray(xReBytes, 0);
@ -95,7 +95,7 @@ export class G2point {
} }
public static fromCompressedBytes(value: bytes48): G2point { public static fromCompressedBytes(value: bytes48): G2point {
assert(value.length === 2 * FP_POINT_LENGTH, 'Expected signature of 96 bytes'); assert(value.length === 2 * FP_POINT_LENGTH, "Expected signature of 96 bytes");
value = Buffer.from(value); value = Buffer.from(value);
const xImBytes = value.slice(0, FP_POINT_LENGTH); const xImBytes = value.slice(0, FP_POINT_LENGTH);
const xReBytes = value.slice(FP_POINT_LENGTH); const xReBytes = value.slice(FP_POINT_LENGTH);
@ -246,45 +246,4 @@ export class G2point {
} }
} }
public add(other: G2point): G2point {
const sum = new ctx.ECP2();
sum.add(this.point);
sum.add(other.point);
sum.affine();
return new G2point(sum);
}
public mul(value: BIG): G2point {
const newPoint = this.point.mul(value);
return new G2point(newPoint);
}
public equal(other: G2point): boolean {
return this.point.equals(other.point);
}
public getPoint(): ECP2 {
return this.point;
}
public toBytesCompressed(): Buffer {
const xReBytes = Buffer.alloc(FP_POINT_LENGTH, 0);
const xImBytes = Buffer.alloc(FP_POINT_LENGTH, 0);
this.point.getX().getA().tobytearray(xReBytes, 0);
this.point.getX().getB().tobytearray(xImBytes, 0);
const c1 = true;
const b1 = this.point.is_infinity();
const a1 = !b1 && calculateYFlag(this.point.getY().getB());
const flags = ((a1 ? 1 << 5 : 0) | (b1 ? 1 << 6 : 0) | (c1 ? 1 << 7 : 0));
const mask = 31;
xImBytes[0] &= mask;
xImBytes[0] |= flags;
xReBytes[0] &= mask;
return Buffer.concat([
xImBytes,
xReBytes
]);
}
} }

View File

@ -101,19 +101,25 @@ export function verifyMultiple(publicKeys: BLSPubkey[], messageHashes: Hash[], s
const eCombined = new ctx.FP12(1); const eCombined = new ctx.FP12(1);
// @ts-ignore
const reduction = messageHashes.reduce((previous, current, index) => { const reduction = messageHashes.reduce((previous, current, index) => {
// @ts-ignore
if(previous.hash && current.equals(previous.hash)) { if(previous.hash && current.equals(previous.hash)) {
return { return {
hash: previous.hash, hash: previous.hash,
// @ts-ignore
publicKey: previous.publicKey ? publicKey: previous.publicKey ?
// @ts-ignore
previous.publicKey.addRaw(publicKeys[index]) previous.publicKey.addRaw(publicKeys[index])
: :
G1point.fromBytesCompressed(publicKeys[index]), G1point.fromBytesCompressed(publicKeys[index]),
}; };
} else if(!!previous.hash) { } else if(previous.hash) {
// @ts-ignore
const g2 = G2point.hashToG2(previous.hash, domain); const g2 = G2point.hashToG2(previous.hash, domain);
eCombined.mul( eCombined.mul(
ElipticCurvePairing.pair( ElipticCurvePairing.pair(
// @ts-ignore
previous.publicKey, previous.publicKey,
g2 g2
) )

View File

@ -34,11 +34,11 @@ export class PrivateKey {
} }
public toHexString(): string { public toHexString(): string {
return `0x${this.toBytes().toString('hex')}`; return `0x${this.toBytes().toString("hex")}`;
} }
public static fromBytes(bytes: Uint8Array): PrivateKey { public static fromBytes(bytes: Uint8Array): PrivateKey {
assert(bytes.length === SECRET_KEY_LENGTH, 'Private key should have 32 bytes'); assert(bytes.length === SECRET_KEY_LENGTH, "Private key should have 32 bytes");
const value = Buffer.from(bytes); const value = Buffer.from(bytes);
return new PrivateKey( return new PrivateKey(
ctx.BIG.frombytearray( ctx.BIG.frombytearray(
@ -53,7 +53,7 @@ export class PrivateKey {
public static fromHexString(value: string): PrivateKey { public static fromHexString(value: string): PrivateKey {
return PrivateKey.fromBytes( return PrivateKey.fromBytes(
Buffer.from(value.replace('0x', ''), 'hex') Buffer.from(value.replace("0x", ""), "hex")
); );
} }