import * as pfcurve from '../src'; import * as crypto from 'crypto'; import {Field, FieldStatic, Fr} from '../src'; import {ProjectivePoint} from '../src/point-base'; import * as chai from 'chai'; const expect = chai.expect; interface IKeyPair { privKeyBytes: Buffer; privKeyValue: bigint; privKeyFq: TFQ; publicKeyBytes: Buffer; publicKeyPoint: TPoint; } const curve = pfcurve.findCurve('fp462bn') as pfcurve.Curve; const g1 = pfcurve.PointG1.BASE(curve); const g2 = pfcurve.PointG2.BASE(curve); function generateRandomBigint(l: number) { const a: number[] = []; for (let i=0; i, TFB extends Field, TPoint extends ProjectivePoint>(fqConstructor: FieldStatic, basePoint: TPoint): IKeyPair { const privKeyBytes = generateRandomBigint(57); const privKeyValue = curve.r - 1n; // pfcurve.toBigInt(privKeyBytes); const privKeyFq = fqConstructor.fromConstant(curve, privKeyValue); const publicKeyPoint = basePoint.multiply(privKeyValue); //pfcurve.PointG1.fromPrivateKey(curve, privKeyBytes) const publicKeyBytes: Buffer = null as any; // /(publicKeyPoint as any).toBytes(); return { privKeyBytes: pfcurve.toBytesBE(Buffer, privKeyBytes) as any, privKeyValue, privKeyFq, publicKeyBytes, publicKeyPoint }; } describe('Hello', () => { it('Test1', () => { const a = g1.toBytes(); console.log('a = ', a); }); it('Fr size', () => { console.log(pfcurve.Fr.MAX_BITS(curve)); console.log(pfcurve.Fq.MAX_BITS(curve)); }); it('aaaa', () => { const a = pfcurve.Fq.fromConstant(curve, 2n); console.log(a.invert().value); console.log(a.value); console.log(curve.P); console.log(a.invert().multiply(a).value); }); it('bbbb', () => { const a = pfcurve.Fq2.fromConstant(curve, 2n); console.log(a.invert().toTuple()); console.log(a.toTuple()); console.log(curve.P); console.log(a.invert().multiply(a).toTuple()); }); it('just - 1', () => { const clientKp = generateKeyPair(pfcurve.Fq, g1); const tempKp = generateKeyPair(pfcurve.Fr, g2); const rekey = tempKp.publicKeyPoint.multiply(pfcurve.Fr.fromConstant(curve, clientKp.privKeyFq.value).invert().value); // a^-1 * bG2 // const rekey = tempKp.publicKeyPoint.multiply(pfcurve.Fr.fromConstant(curve, clientKp.privKeyValue).invert().value); // a^-1 * bG2 // g2.multiply(clientKp.privKeyFq.invert().multiply(tempKp.privKeyFq)); // console.log("TP - 1: ", { // a: pfcurve.Fr.fromConstant(curve, clientKp.privKeyValue).invert().value.toString(16), // b: clientKp.privKeyFq.invert().value.toString(16), // c: pfcurve.Fr.fromConstant(curve, clientKp.privKeyValue).multiply(clientKp.privKeyFq.invert().value).value.toString(16), // d: pfcurve.Fr.fromConstant(curve, clientKp.privKeyValue).invert().multiply(clientKp.privKeyFq.value).value.toString(16), // Of cause... // // }); const z = pfcurve.pairing(g1, g2); const r = pfcurve.Fq.fromConstant(curve, generateRandomBigint(57)); const dataKey = z.pow(r.value); const encryptedKey = clientKp.publicKeyPoint.multiply(r); // raG1 const middleEncryptedKey = pfcurve.pairing(encryptedKey, rekey); // e(raG1, a^-1 * bG2) = e(G1,G2)^(rb) const decryptedKey = middleEncryptedKey.pow(tempKp.privKeyFq.invert().value); // z^(rb)^(b^-1) // console.log('dataKey = ', dataKey.toTuple()); // console.log('decryptedKey = ', decryptedKey.toTuple()); // console.log('dataKeyBits = ', dataKey.toTuple().map(v => pfcurve.bitLen(v))); expect(dataKey).eql(decryptedKey); }); });