Search code examples
javascriptnode.jsmongodbmongoose-schemapaillier

Is there a schema to store this object with typeof, Integer { value: 340895965n } in mongoose with node.js


I'm very new to nodejs. For a while I've been working with paillier-js module which returns a object typeof() of this nature: Integer { value: 340895965n }. Is there a schema that can allow for appropriate storage of this Object with mongoose. I have tried passing the Object directly to mongodb which is successful but when I view it using the MongoDB compass, I get empty { }

const paillier = require('paillier-js');
const bigInt = require('big-integer');


const { publicKey, privateKey } = paillier.generateRandomKeys(16);


//Set the values for the public key for the encryption
publicKey.n.value =  55687n
publicKey._n2.value = 3101041969n;
publicKey.g.value = 1696460192n;

//Set the values for the privateKey for the decryption
privateKey.lambda.value =  27608n;
privateKey.mu.value = 11489n;
privateKey._p.value = 239n;
privateKey._q.value = 233n;
privateKey.publicKey = publicKey


let num1 = 101;
let num2 = 104;
let num3 = 1;

let bn1 = bigInt(num1).mod(publicKey.n);

while (bn1.lt(0)) bn1 = bn1.add(publicKey.n); 
let bn2 = bigInt(num2).mod(publicKey.n);
while (bn2.lt(0)) bn2 = bn2.add(publicKey.n); 
let bn3 = bigInt(num3).mod(publicKey.n);
while (bn3.lt(0)) bn3 = bn3.add(publicKey.n);


let c1 = publicKey.encrypt(bn1);
console.log("C1: "+ c1);
console.log(c1, "is of type ", typeof(c1))

//c1 = c1.value


let c2 = publicKey.encrypt(bn2);
console.log("C2: ",c2);
//c2 = c2.value

let c3 = publicKey.encrypt(bn3);
console.log("C3: "+c3);
//c3 = c3.value

console.log(typeof(c1))

let encryptedSum = publicKey.addition(c1, c2, c3);
let decryptedSum = privateKey.decrypt(encryptedSum);

console.log('Decrypted addition:', decryptedSum.toString());

Solution

  • Not familiar with MongoDB, but you can use toString() on a BigInt, converting it to a string before storing in MongoDB, and then of course when reading it from MongoDB, you'll need to reapply BigInt. Ie,

    x = 14142352423459034590345934959032n;
    
    // convert x to string...
    xs = x.toString();
    
    // convert string to BigInt
    bix = BigInt(xs);
    

    Hope this helps...