Skip to content

Commit

Permalink
chore: make prettier happy
Browse files Browse the repository at this point in the history
  • Loading branch information
10d9e committed May 4, 2024
1 parent dcc5b78 commit b110d8a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@ contract PrivateTaxCalculation {

# References

https://github.com/jahali6128/paillier-solidity
https://github.com/jahali6128/paillier-solidity
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
"bigint-conversion": "^2.4.3",
"paillier-bigint": "^3.4.3"
}
}
}
118 changes: 59 additions & 59 deletions test/Paillier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,81 @@ import { expect } from "chai";

// Public key
interface PublicKey {
value: string;
value: string;
}

// Ciphertext
interface Ciphertext {
value: string;
value: string;
}

describe("Paillier", function () {
it("should add 2 ciphertexts", async function () {
const Paillier = await ethers.deployContract("Paillier");
it("should add 2 ciphertexts", async function () {
const Paillier = await ethers.deployContract("Paillier");

const { publicKey, privateKey } =
await paillierBigint.generateRandomKeys(256);
const a: bigint = BigInt(1);
const b: bigint = BigInt(2);
const enc_a: Ciphertext = {
value: ethers.toBeHex(publicKey.encrypt(a)),
};
const enc_b: Ciphertext = {
value: ethers.toBeHex(publicKey.encrypt(b)),
};
const { publicKey, privateKey } =
await paillierBigint.generateRandomKeys(256);
const a: bigint = BigInt(1);
const b: bigint = BigInt(2);
const enc_a: Ciphertext = {
value: ethers.toBeHex(publicKey.encrypt(a)),
};
const enc_b: Ciphertext = {
value: ethers.toBeHex(publicKey.encrypt(b)),
};

// Public key
const pub_n: PublicKey = {
value: ethers.toBeHex(publicKey.n),
};
// Public key
const pub_n: PublicKey = {
value: ethers.toBeHex(publicKey.n),
};

// bit length will differ to what has been stated in this script.
// if using 256-bit key, bit_length will be 264 as "0x" prefix may have been factored in
// Now lets deploy the contract and test the addition
const enc_sum = await Paillier.add(enc_a, enc_b, pub_n);
const enc_sum_int = bigIntConversion.hexToBigint(enc_sum[0]);
// bit length will differ to what has been stated in this script.
// if using 256-bit key, bit_length will be 264 as "0x" prefix may have been factored in
// Now lets deploy the contract and test the addition
const enc_sum = await Paillier.add(enc_a, enc_b, pub_n);
const enc_sum_int = bigIntConversion.hexToBigint(enc_sum[0]);

// Conversion to int for convenience
const dec_sum = Number(privateKey.decrypt(enc_sum_int));
console.log("Decrypted Sum:", dec_sum);
// Conversion to int for convenience
const dec_sum = Number(privateKey.decrypt(enc_sum_int));
console.log("Decrypted Sum:", dec_sum);

// We want dec_sum to equal 3
expect(dec_sum).to.equal(3);
});
// We want dec_sum to equal 3
expect(dec_sum).to.equal(3);
});

it("should encrypt zero", async function () {
const { publicKey, privateKey } =
await paillierBigint.generateRandomKeys(256);
const Paillier = await ethers.deployContract("Paillier");
// Arbitary random number - 1000000
const rand = ethers.toBeHex(Math.floor(Math.random() * 1000000));
it("should encrypt zero", async function () {
const { publicKey, privateKey } =
await paillierBigint.generateRandomKeys(256);
const Paillier = await ethers.deployContract("Paillier");
// Arbitary random number - 1000000
const rand = ethers.toBeHex(Math.floor(Math.random() * 1000000));

// Public key
const pub_n = { value: ethers.toBeHex(publicKey.n) };
const enc_zero = await Paillier.encryptZero(rand, pub_n);
const enc_zero_int = bigIntConversion.hexToBigint(enc_zero[0]);
const dec_zero = Number(privateKey.decrypt(enc_zero_int));
expect(dec_zero).to.equal(0);
});
// Public key
const pub_n = { value: ethers.toBeHex(publicKey.n) };
const enc_zero = await Paillier.encryptZero(rand, pub_n);
const enc_zero_int = bigIntConversion.hexToBigint(enc_zero[0]);
const dec_zero = Number(privateKey.decrypt(enc_zero_int));
expect(dec_zero).to.equal(0);
});

it("should multiply encrypted value by a scalar", async function () {
const { publicKey, privateKey } =
await paillierBigint.generateRandomKeys(256);
const [owner] = await ethers.getSigners();
const Paillier = await ethers.deployContract("Paillier");
const a: bigint = BigInt(2);
const b: bigint = BigInt(5);
const enc_a = { value: ethers.toBeHex(publicKey.encrypt(a)) };
it("should multiply encrypted value by a scalar", async function () {
const { publicKey, privateKey } =
await paillierBigint.generateRandomKeys(256);
const [owner] = await ethers.getSigners();
const Paillier = await ethers.deployContract("Paillier");
const a: bigint = BigInt(2);
const b: bigint = BigInt(5);
const enc_a = { value: ethers.toBeHex(publicKey.encrypt(a)) };

// Public key
const pub_n = { value: ethers.toBeHex(publicKey.n) };
const enc_scalar = await Paillier.mul(enc_a, b, pub_n);
// Public key
const pub_n = { value: ethers.toBeHex(publicKey.n) };
const enc_scalar = await Paillier.mul(enc_a, b, pub_n);

// returns tuple so get first index
const enc_scalar_int = bigIntConversion.hexToBigint(enc_scalar[0]);
// returns tuple so get first index
const enc_scalar_int = bigIntConversion.hexToBigint(enc_scalar[0]);

const dec_scalar = Number(privateKey.decrypt(enc_scalar_int));
console.log("Decrypted Scalar:", dec_scalar);
expect(dec_scalar).to.equal(10);
});
const dec_scalar = Number(privateKey.decrypt(enc_scalar_int));
console.log("Decrypted Scalar:", dec_scalar);
expect(dec_scalar).to.equal(10);
});
});

0 comments on commit b110d8a

Please sign in to comment.