A Proxy Re-Encryption library using Bilinear Map.
npm install bm-preG1 and G2. It must pefrom at first.
javascript
const PRE = require('bm-pre');
PRE.init({g: "this is g", h: "that is h", returnHex: true}).then(params => {
console.log(params)
//...
});
`
$3
PRE is supposed to encrypt symmetric key.
It's recommended to get the key from a random element in Fr and convert it to hex string instead of generating a random key and mapping it to Fr.
`javascript
const plain = PRE.randomGen();
`
$3
Generate key pairs of Delegator(A) and Delegatee(B).
`javascript
const A = PRE.keyGenInG1(params, {returnHex: true});
const B = PRE.keyGenInG2(params, {returnHex: true});
`
You can get public key from existing secret key using getPkFromG1 and getPkFromG1.
$3
A can of course encrypt and decrypt.
`javascript
const encrypted = PRE.enc(plain, A.pk, params, {returnHex: true});
const decrypted = PRE.dec(encrypted, A.sk, params);
console.log(plain === decrypted)
`
$3
A can generate reKey with A's secret key and B's public key.
`javascript
const reKey = PRE.rekeyGen(A.sk, B.pk, {returnHex: true});
`
$3
Anyone can convert encrypted with reKeyinto ciphertext that can be decrypted by B.
`javascript
const reEncypted = PRE.reEnc(encrypted, reKey, {returnHex: true});
const reDecrypted = PRE.reDec(reEncypted, B.sk);
console.log(plain === reDecrypted)
`
$3
> Right now only signature by delegator is implemented, delegatee can have key pair with delegator's format (in G1) as well.
`javascript
//create hash for msg
const crypto = require('crypto');
const msg = "1111";
const hash = crypto.createHash('sha256');
hash.update(msg);
const msgHash = hash.digest('hex');
//sign hash and verify
const sig = PRE.sign(msgHash, A.sk);
const C = PRE.keyGenInG1(params, {returnHex: false});
console.log("A's signature", sig);
console.log("verify A's signature by A's pk:", PRE.verify(msgHash, sig, A.pk, params));
console.log("verify A's signature by C's pk:", PRE.verify(msgHash, sig, C.pk, params))
`
Tips
Almost every input parameters can either be hex string or Object in group. It'll automatically check the type and convert it to Object` during caculation if necessary.