fix bls lint

This commit is contained in:
Marin Petrunić 2019-09-17 21:05:13 +02:00
parent b8c262791a
commit fe31dbc974
4 changed files with 109 additions and 104 deletions

View File

@ -15,46 +15,6 @@ export class G1point {
this.point = point;
}
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;
}
public static fromBytesCompressed(value: bytes48): G1point {
assert(value.length === FP_POINT_LENGTH, `Expected g1 compressed input to have ${FP_POINT_LENGTH} bytes`);
value = Buffer.from(value);
@ -127,4 +87,44 @@ export class G1point {
} while (ecp.is_infinity());
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

@ -16,47 +16,6 @@ export class G2point {
this.point = point;
}
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
]);
}
public static hashToG2(message: Hash, domain: Domain): G2point {
const padding = Buffer.alloc(G2_HASH_PADDING, 0);
@ -246,4 +205,46 @@ 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

@ -91,7 +91,12 @@ export function verify(publicKey: BLSPubkey, messageHash: Hash, signature: BLSSi
* @param signature
* @param domain
*/
export function verifyMultiple(publicKeys: BLSPubkey[], messageHashes: Hash[], signature: BLSSignature, domain: Domain): boolean {
export function verifyMultiple(
publicKeys: BLSPubkey[],
messageHashes: Hash[],
signature: BLSSignature,
domain: Domain
): boolean {
if(publicKeys.length === 0 || publicKeys.length != messageHashes.length) {
return false;
}

View File

@ -15,28 +15,6 @@ export class PrivateKey {
this.value = value;
}
public getValue(): BIG {
return this.value;
}
public sign(message: G2point): G2point {
return message.mul(this.value);
}
public signMessage(message: Hash, domain: Domain): G2point {
return G2point.hashToG2(message, domain).mul(this.value);
}
public toBytes(): BLSSecretKey {
const buffer = Buffer.alloc(FP_POINT_LENGTH, 0);
this.value.tobytearray(buffer, 0);
return buffer.slice(FP_POINT_LENGTH - SECRET_KEY_LENGTH);
}
public toHexString(): string {
return `0x${this.toBytes().toString("hex")}`;
}
public static fromBytes(bytes: Uint8Array): PrivateKey {
assert(bytes.length === SECRET_KEY_LENGTH, "Private key should have 32 bytes");
const value = Buffer.from(bytes);
@ -61,4 +39,25 @@ export class PrivateKey {
return PrivateKey.fromBytes(random.randomBuffer(SECRET_KEY_LENGTH));
}
public getValue(): BIG {
return this.value;
}
public sign(message: G2point): G2point {
return message.mul(this.value);
}
public signMessage(message: Hash, domain: Domain): G2point {
return G2point.hashToG2(message, domain).mul(this.value);
}
public toBytes(): BLSSecretKey {
const buffer = Buffer.alloc(FP_POINT_LENGTH, 0);
this.value.tobytearray(buffer, 0);
return buffer.slice(FP_POINT_LENGTH - SECRET_KEY_LENGTH);
}
public toHexString(): string {
return `0x${this.toBytes().toString("hex")}`;
}
}