Skip to content

Commit

Permalink
test: add jest and write frontend test for applySeed
Browse files Browse the repository at this point in the history
  • Loading branch information
ni-jessica committed Dec 1, 2023
1 parent dc0a675 commit 6f95816
Show file tree
Hide file tree
Showing 6 changed files with 4,305 additions and 2,680 deletions.
11 changes: 6 additions & 5 deletions frontend/app/psi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type ServerResponse = {
breachedPasswords: string[];
};

function hashToPoint(input: string): Uint8Array {
export function hashToPoint(input: string): Uint8Array {
const hash = sodium.crypto_generichash(
sodium.crypto_core_ristretto255_HASHBYTES,
sodium.from_string(input)
Expand All @@ -18,20 +18,20 @@ function hashToPoint(input: string): Uint8Array {
* @param input the string to be encrypted
* @returns input with a secret key applied and the key's inverse
*/
function applySeed(input: string): [ArrayBuffer, Uint8Array] {
export function applySeed(input: string): [Uint8Array, Uint8Array] {
// generate random seed
const seed = sodium.crypto_core_ristretto255_scalar_random();
// get seed inverse
const seedInverse =
sodium.crypto_core_ristretto255_scalar_invert(seed);
const point = hashToPoint(input);
// apply seed
const seededInput = sodium.crypto_scalarmult_ristretto255(
const seededPassword = sodium.crypto_scalarmult_ristretto255(
seed,
point
);
console.log(new TextDecoder().decode(seededInput.buffer));
return [seededInput.buffer, seedInverse];
console.log("seeded password: ", new TextDecoder().decode(seededPassword.buffer));
return [seededPassword, seedInverse];
}

function computeIntersection(
Expand Down Expand Up @@ -81,6 +81,7 @@ export async function checkSecurity(password: string) {
}
);
const data = await response.json();
console.log(data)
if (computeIntersection(data, keyInverse)) {
return { status: "success" };
} else {
Expand Down
6 changes: 6 additions & 0 deletions frontend/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
transform: {'^.+\\.ts?$': 'ts-jest'},
testEnvironment: 'node',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};
2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"react-password-checklist": "^1.5.0"
},
"devDependencies": {
"@types/jest": "^29.5.10",
"@types/libsodium-wrappers-sumo": "^0.7.8",
"@types/node": "^20",
"@types/node-fetch": "^2.6.7",
Expand All @@ -31,6 +32,7 @@
"eslint-config-next": "13.5.4",
"postcss": "^8",
"tailwindcss": "^3",
"ts-jest": "^29.1.1",
"typescript": "^5"
}
}
25 changes: 25 additions & 0 deletions frontend/tests/psi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { applySeed, hashToPoint } from "../app/psi";
const sodium = require("libsodium-wrappers-sumo");

beforeAll(async () => {
await sodium.ready;
});

describe("testing applySeed", () => {
test("applying secret key should be same as original value", () => {
const password = "TestPass1&";

// hash the password
const hashedPassword = hashToPoint(password);
// apply the seed
const [seededPassword, seedInverse] = applySeed(password);
// apply the inverse to the seeded password
const inversedSeededPassword = sodium.crypto_scalarmult_ristretto255(
seedInverse,
seededPassword
);

// check that the seeded+inversed password is the same as the hashed password
expect(Buffer.compare(inversedSeededPassword, hashedPassword)).toBe(0);
});
});
2 changes: 1 addition & 1 deletion frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
, "jest.config.js" ],
"exclude": [
"node_modules"
]
Expand Down
Loading

0 comments on commit 6f95816

Please sign in to comment.