Skip to content

Commit 61745a3

Browse files
committed
refactor: replace E* enums with constants for improved readability
1 parent bf7d8e9 commit 61745a3

File tree

11 files changed

+77
-77
lines changed

11 files changed

+77
-77
lines changed

source/algorithms/baby-step-giant-step/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chalk from "@/shared/chalk";
22

33
import babyStepGiantStep from "@/algorithms/baby-step-giant-step";
4-
import { ESymbols } from "@/shared/constants";
4+
import { SYMBOLS } from "@/shared/constants";
55

66
describe("Finding the Discrete Log for the given numbers", () => {
77
test.each([
@@ -11,7 +11,7 @@ describe("Finding the Discrete Log for the given numbers", () => {
1111
[227801, 155104, 291563, 74399],
1212
[62712, 30084, 83437, 68793],
1313
])(
14-
`%p^x ${ESymbols.Congruent} %p % %p.\n\tx = ${chalk.greenBright("%p")}`,
14+
`%p^x ${SYMBOLS.CONGRUENT} %p % %p.\n\tx = ${chalk.greenBright("%p")}`,
1515
(generator, base, modulo, result) => {
1616
expect(
1717
babyStepGiantStep(BigInt(generator), BigInt(base), BigInt(modulo)),

source/algorithms/baby-step-giant-step/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ESymbols } from "@/shared/constants";
21
import chalk from "@/shared/chalk";
32
import { createAlgorithmPrompt, type PromptOptions } from "@/shared/prompt";
43
import euclidean from "@/algorithms/euclidean";
4+
import { SYMBOLS } from "@/shared/constants";
55
import fastModularExponentiation from "@/algorithms/fast-modular-exponentiation";
66
import { createOptionalWasmInvoker, fitsInI64 } from "@/shared/wasm";
77

@@ -73,8 +73,8 @@ export default function main(generator: bigint, base: bigint, modulo: bigint) {
7373
const runPrompt = createAlgorithmPrompt(
7474
"baby-step-giant-step",
7575
async ({ ask, writeLine }) => {
76-
writeLine(`\tgenerator^x ${ESymbols.Congruent} base % modulo. x = result`);
77-
writeLine(chalk.gray(`\t92^x ${ESymbols.Congruent} 13 % 5. x = 3`));
76+
writeLine(`\tgenerator^x ${SYMBOLS.CONGRUENT} base % modulo. x = result`);
77+
writeLine(chalk.gray(`\t92^x ${SYMBOLS.CONGRUENT} 13 % 5. x = 3`));
7878

7979
const { generator, base, modulo } = await ask<{
8080
generator: number;
@@ -103,7 +103,7 @@ const runPrompt = createAlgorithmPrompt(
103103

104104
const result = main(BigInt(generator), BigInt(base), BigInt(modulo));
105105
writeLine(
106-
`\t${generator}^x ${ESymbols.Congruent} ${base} % ${modulo}. x = ${result}`,
106+
`\t${generator}^x ${SYMBOLS.CONGRUENT} ${base} % ${modulo}. x = ${result}`,
107107
);
108108

109109
return {

source/algorithms/fast-modular-exponentiation/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chalk from "@/shared/chalk";
22

33
import fastModularExponentiation from "@/algorithms/fast-modular-exponentiation";
4-
import { ESymbols } from "@/shared/constants";
4+
import { SYMBOLS } from "@/shared/constants";
55

66
describe("Calculating the remainder from doing modulus for a number with exponentiation", () => {
77
test.each([
@@ -11,7 +11,7 @@ describe("Calculating the remainder from doing modulus for a number with exponen
1111
[985019284, 118293113, 13, 6],
1212
[1314520, 17, 11, 4],
1313
])(
14-
`%p^%p % %p ${ESymbols.Congruent} x.\n\tx = ${chalk.greenBright("%p")}`,
14+
`%p^%p % %p ${SYMBOLS.CONGRUENT} x.\n\tx = ${chalk.greenBright("%p")}`,
1515
(base, exponent, modulo, result) => {
1616
expect(
1717
fastModularExponentiation(

source/algorithms/fast-modular-exponentiation/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chalk from "@/shared/chalk";
22
import { createAlgorithmPrompt, type PromptOptions } from "@/shared/prompt";
33

4-
import { ESymbols } from "@/shared/constants";
4+
import { SYMBOLS } from "@/shared/constants";
55
import { createOptionalWasmInvoker, fitsInI64 } from "@/shared/wasm";
66

77
const runWasmPowMod = createOptionalWasmInvoker<
@@ -58,8 +58,8 @@ export default function main(
5858
const runPrompt = createAlgorithmPrompt(
5959
"fast-modular-exponentiation",
6060
async ({ ask, writeLine }) => {
61-
writeLine(`\tbase^exponent % modulo ${ESymbols.Congruent} x. x = result`);
62-
writeLine(chalk.gray(`\t2^100 % 71 ${ESymbols.Congruent} 20. x = 20`));
61+
writeLine(`\tbase^exponent % modulo ${SYMBOLS.CONGRUENT} x. x = result`);
62+
writeLine(chalk.gray(`\t2^100 % 71 ${SYMBOLS.CONGRUENT} 20. x = 20`));
6363

6464
const { base, exponent, modulo } = await ask<{
6565
base: number;
@@ -88,7 +88,7 @@ const runPrompt = createAlgorithmPrompt(
8888

8989
const result = main(BigInt(base), BigInt(exponent), BigInt(modulo));
9090
writeLine(
91-
`\t${base}^${exponent} % ${modulo} ${ESymbols.Congruent} ${result}. x = ${result}`,
91+
`\t${base}^${exponent} % ${modulo} ${SYMBOLS.CONGRUENT} ${result}. x = ${result}`,
9292
);
9393

9494
return {

source/command.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import chalk from "@/shared/chalk";
22
import { join } from "path";
33

44
import { format, inquire } from "@/shared/utilities";
5-
import { EChoices } from "@/shared/constants";
5+
import { CHOICES } from "@/shared/constants";
66
import { getInquirer } from "@/shared/inquirer";
77

88
async function main(message = "What do you want to do?") {
@@ -14,31 +14,31 @@ async function main(message = "What do you want to do?") {
1414
name: "_",
1515
message,
1616
choices: [
17-
{ name: EChoices.Demonstrate, value: EChoices.Demonstrate },
18-
{ name: EChoices.Execute, value: EChoices.Execute },
19-
{ name: EChoices.Exit, value: EChoices.Exit },
17+
{ name: CHOICES.DEMONSTRATE, value: CHOICES.DEMONSTRATE },
18+
{ name: CHOICES.EXECUTE, value: CHOICES.EXECUTE },
19+
{ name: CHOICES.EXIT, value: CHOICES.EXIT },
2020
],
2121
},
2222
]);
2323

2424
switch (purpose) {
25-
case EChoices.Demonstrate:
25+
case CHOICES.DEMONSTRATE:
2626
await inquire.procedure(
2727
join(__dirname, "key-encryption"),
2828
"Which cryptograph procedure do you want to demonstrate?",
2929
format.filename,
3030
);
3131
main();
3232
break;
33-
case EChoices.Execute:
33+
case CHOICES.EXECUTE:
3434
await inquire.procedure(
3535
join(__dirname, "algorithms"),
3636
"Which cryptograph algorithm do you want to execute?",
3737
format.foldername,
3838
);
3939
main();
4040
break;
41-
case EChoices.Exit:
41+
case CHOICES.EXIT:
4242
console.log(chalk.gray("Successfully terminated the program.\n"));
4343
break;
4444
default:

source/key-encryption/DiffieHellman.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import chalk from "@/shared/chalk";
33
import babyStepGiantStep from "@/algorithms/baby-step-giant-step";
44
import fastModularExponentiation from "@/algorithms/fast-modular-exponentiation";
55
import primitiveRootSearch from "@/algorithms/primitive-root-search";
6-
import { EActors } from "@/shared/constants";
6+
import { ACTORS } from "@/shared/constants";
77
import { inquire, log, wrap } from "@/shared/utilities";
88
import { randomBigIntBetween } from "@/shared/random";
99

@@ -12,11 +12,11 @@ export async function prompt() {
1212
"There are three people in this Diffie-Hellman key exchange process:",
1313
);
1414
console.log(
15-
`\t${EActors.Alice} - Party A\n\t${EActors.Bob} - Party B\n\t${EActors.Eve} - Eavesdropper`,
15+
`\t${ACTORS.ALICE} - Party A\n\t${ACTORS.BOB} - Party B\n\t${ACTORS.EVE} - Eavesdropper`,
1616
);
1717

1818
const [p, g, a, b, publicA, publicB] = await inquire.continue(
19-
`${EActors.Alice} and ${EActors.Bob} select a public prime p and primitive root g, then choose private values a and b:`,
19+
`${ACTORS.ALICE} and ${ACTORS.BOB} select a public prime p and primitive root g, then choose private values a and b:`,
2020
() => {
2121
const [p] = wrap.randomize(8, 8, 1);
2222
const [, roots] = primitiveRootSearch(Number(p));
@@ -40,14 +40,14 @@ export async function prompt() {
4040
);
4141

4242
await inquire.continue(
43-
`${EActors.Alice} and ${EActors.Bob} derive the same shared secret independently:`,
43+
`${ACTORS.ALICE} and ${ACTORS.BOB} derive the same shared secret independently:`,
4444
() => {
4545
const secretAlice = fastModularExponentiation(publicB, a, p);
4646
const secretBob = fastModularExponentiation(publicA, b, p);
4747

4848
log.list([
49-
{ name: `${EActors.Alice}'s secret`, value: secretAlice },
50-
{ name: `${EActors.Bob}'s secret`, value: secretBob },
49+
{ name: `${ACTORS.ALICE}'s secret`, value: secretAlice },
50+
{ name: `${ACTORS.BOB}'s secret`, value: secretBob },
5151
]);
5252

5353
console.log(
@@ -59,7 +59,7 @@ export async function prompt() {
5959
);
6060

6161
await inquire.continue(
62-
`${EActors.Eve} can also recover the secret for small teaching parameters by solving the discrete log problem:`,
62+
`${ACTORS.EVE} can also recover the secret for small teaching parameters by solving the discrete log problem:`,
6363
() => {
6464
const aRecovered = babyStepGiantStep(g, publicA, p);
6565
const bRecovered = babyStepGiantStep(g, publicB, p);
@@ -72,7 +72,7 @@ export async function prompt() {
7272
log.list([
7373
{ name: "Recovered a", value: aRecovered },
7474
{ name: "Recovered b", value: bRecovered },
75-
{ name: `${EActors.Eve}'s recovered shared secret`, value: secretEve },
75+
{ name: `${ACTORS.EVE}'s recovered shared secret`, value: secretEve },
7676
]);
7777
},
7878
);

source/key-encryption/ElGamal.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import babyStepGiantStep from "@/algorithms/baby-step-giant-step";
44
import euclidean from "@/algorithms/euclidean";
55
import fastModularExponentiation from "@/algorithms/fast-modular-exponentiation";
66
import primitiveRootSearch from "@/algorithms/primitive-root-search";
7-
import { EActors } from "@/shared/constants";
7+
import { ACTORS } from "@/shared/constants";
88
import { log, inquire, wrap } from "@/shared/utilities";
99
import { randomBigIntBetween } from "@/shared/random";
1010

1111
export async function prompt() {
1212
try {
1313
console.log("There are three people in this ElGamal encryption process:");
1414
console.log(
15-
`\t${EActors.Alice} - Receiver\n\t${EActors.Bob} - Sender\n\t${EActors.Eve} - Eavesdropper`,
15+
`\t${ACTORS.ALICE} - Receiver\n\t${ACTORS.BOB} - Sender\n\t${ACTORS.EVE} - Eavesdropper`,
1616
);
1717

1818
const [p, g, r, x, y] = await inquire.continue(
19-
`${EActors.Alice} is going to pick prime number P, generator g, and random numbers r and x:`,
19+
`${ACTORS.ALICE} is going to pick prime number P, generator g, and random numbers r and x:`,
2020
() => {
2121
const [p] = wrap.randomize(8, 8, 1);
2222
const [, roots] = primitiveRootSearch(Number(p));
@@ -34,14 +34,14 @@ export async function prompt() {
3434
{ name: "x", value: x },
3535
]);
3636

37-
console.log(`\n\t${EActors.Alice} generates y:`);
37+
console.log(`\n\t${ACTORS.ALICE} generates y:`);
3838
const y = fastModularExponentiation(g, x, p);
3939
console.log(`\ty: ${chalk.gray(y)}`);
4040

4141
console.log(
42-
`\n\t${EActors.Alice} sends ${chalk.bold.bgCyan(
42+
`\n\t${ACTORS.ALICE} sends ${chalk.bold.bgCyan(
4343
"(g, r, p, y)",
44-
)} as the public key to ${EActors.Bob} and ${EActors.Eve}.`,
44+
)} as the public key to ${ACTORS.BOB} and ${ACTORS.EVE}.`,
4545
);
4646

4747
return [p, g, r, x, y];
@@ -50,7 +50,7 @@ export async function prompt() {
5050

5151
const message = "This is a hardcoded secret message.";
5252
const [keyEncrypted, arrayOfEncryptedCode] = await inquire.continue(
53-
`${EActors.Bob} encrypts the message and sends it back to ${EActors.Alice} (while ${EActors.Eve} is eavesdropping).`,
53+
`${ACTORS.BOB} encrypts the message and sends it back to ${ACTORS.ALICE} (while ${ACTORS.EVE} is eavesdropping).`,
5454
() => {
5555
const keyEncrypted = fastModularExponentiation(g, r, p);
5656
const sharedSecret = fastModularExponentiation(y, r, p);
@@ -70,21 +70,21 @@ export async function prompt() {
7070
);
7171
console.log(
7272
`\n\t${
73-
EActors.Alice
73+
ACTORS.ALICE
7474
} can decrypt the message since she has the random number ${chalk.bold.bgCyan(
7575
"(x)",
7676
)}.\n\tDecrypted message: ${chalk.gray(messageDecrypted)}\n\t${
77-
EActors.Alice
78-
} verifies the message with ${EActors.Bob} privately.`,
77+
ACTORS.ALICE
78+
} verifies the message with ${ACTORS.BOB} privately.`,
7979
);
8080
return [keyEncrypted, arrayOfEncryptedCode];
8181
},
8282
);
8383

8484
await inquire.continue(
85-
`${EActors.Eve} is going to decrypt the secret message.`,
85+
`${ACTORS.EVE} is going to decrypt the secret message.`,
8686
() => {
87-
console.log(`\tNow ${EActors.Eve} has the following stuff:`);
87+
console.log(`\tNow ${ACTORS.EVE} has the following stuff:`);
8888
log.list([
8989
{ name: "g", value: g },
9090
{ name: "r", value: r },
@@ -95,7 +95,7 @@ export async function prompt() {
9595
]);
9696

9797
console.log(
98-
`\n\t${EActors.Eve} is going to figure out what the random number x is using Discrete Log with the information she has.`,
98+
`\n\t${ACTORS.EVE} is going to figure out what the random number x is using Discrete Log with the information she has.`,
9999
);
100100

101101
const xEavesdropped = babyStepGiantStep(g, y, p);
@@ -118,8 +118,8 @@ export async function prompt() {
118118
);
119119
console.log(
120120
`\tDecrypted message: ${chalk.gray(messageEavesdropped)}\n\t${
121-
EActors.Eve
122-
} verifies the message with ${EActors.Bob}.`,
121+
ACTORS.EVE
122+
} verifies the message with ${ACTORS.BOB}.`,
123123
);
124124
},
125125
);

source/key-encryption/RSA.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import euclidean from "@/algorithms/euclidean";
44
import extendedEuclidean from "@/algorithms/extended-euclidean";
55
import fastModularExponentiation from "@/algorithms/fast-modular-exponentiation";
66
import pollardP1Factorization from "@/algorithms/pollard-p-1-factorization";
7-
import { EActors } from "@/shared/constants";
7+
import { ACTORS } from "@/shared/constants";
88
import { log, inquire, wrap } from "@/shared/utilities";
99

1010
export async function prompt() {
1111
try {
1212
console.log("There are three people in this RSA encryption process:");
1313
console.log(
14-
`\t${EActors.Alice} - Receiver\n\t${EActors.Bob} - Sender\n\t${EActors.Eve} - Eavesdropper`,
14+
`\t${ACTORS.ALICE} - Receiver\n\t${ACTORS.BOB} - Sender\n\t${ACTORS.EVE} - Eavesdropper`,
1515
);
1616

1717
const [p, q, n, r] = await inquire.continue(
18-
`${EActors.Alice} is going to pick prime numbers P and Q, and then generate n with P * Q, r with (P - 1) * (Q - 1):`,
18+
`${ACTORS.ALICE} is going to pick prime numbers P and Q, and then generate n with P * Q, r with (P - 1) * (Q - 1):`,
1919
() => {
2020
const [p, q] = wrap.randomize(16, 8, 2);
2121

@@ -33,7 +33,7 @@ export async function prompt() {
3333
);
3434

3535
const [e, d] = await inquire.continue(
36-
`${EActors.Alice} selects the public exponent e and computes private exponent d from e * d % r = 1:`,
36+
`${ACTORS.ALICE} selects the public exponent e and computes private exponent d from e * d % r = 1:`,
3737
() => {
3838
const commonExponents = [65537n, 257n, 17n, 5n, 3n];
3939
const e = commonExponents.find((value) => euclidean(value, r) === 1n);
@@ -58,9 +58,9 @@ export async function prompt() {
5858
);
5959

6060
await inquire.continue(
61-
`${EActors.Alice} sends e and n as the public key to ${EActors.Bob} and ${EActors.Eve}.`,
61+
`${ACTORS.ALICE} sends e and n as the public key to ${ACTORS.BOB} and ${ACTORS.EVE}.`,
6262
() => {
63-
console.log(`\t${EActors.Alice} has the following numbers:`);
63+
console.log(`\t${ACTORS.ALICE} has the following numbers:`);
6464
log.list([
6565
{ name: "P", value: p },
6666
{ name: "Q", value: q },
@@ -74,7 +74,7 @@ export async function prompt() {
7474

7575
const message = "This is a hardcoded secret message.";
7676
const arrayOfEncryptedCode = await inquire.continue(
77-
`${EActors.Bob} encrypts the message and sends it back to ${EActors.Alice} (while ${EActors.Eve} is eavesdropping).`,
77+
`${ACTORS.BOB} encrypts the message and sends it back to ${ACTORS.ALICE} (while ${ACTORS.EVE} is eavesdropping).`,
7878
() => {
7979
const arrayOfEncryptedCode = wrap.encrypt(message, (code) => {
8080
return fastModularExponentiation(BigInt(code), e, n);
@@ -89,22 +89,22 @@ export async function prompt() {
8989
);
9090
console.log(
9191
`\n\t${
92-
EActors.Alice
92+
ACTORS.ALICE
9393
} can decrypt the message since she has the private key ${chalk.bold.bgCyan(
9494
"(d, n)",
9595
)}.\n\tDecrypted message: ${chalk.gray(messageDecrypted)}\n\t${
96-
EActors.Alice
97-
} verifies the message with ${EActors.Bob} privately.`,
96+
ACTORS.ALICE
97+
} verifies the message with ${ACTORS.BOB} privately.`,
9898
);
9999

100100
return arrayOfEncryptedCode;
101101
},
102102
);
103103

104104
await inquire.continue(
105-
`${EActors.Eve} is going to decrypt the secret message.`,
105+
`${ACTORS.EVE} is going to decrypt the secret message.`,
106106
() => {
107-
console.log(`\tNow ${EActors.Eve} has the following stuff:`);
107+
console.log(`\tNow ${ACTORS.EVE} has the following stuff:`);
108108
log.list([
109109
{ name: "n", value: n },
110110
{ name: "e", value: e },
@@ -113,7 +113,7 @@ export async function prompt() {
113113

114114
console.log(
115115
`\n\t${
116-
EActors.Eve
116+
ACTORS.EVE
117117
} is going to factor n and derive private key d from the public key ${chalk.bold.bgCyan(
118118
"(e, n)",
119119
)} and other information.`,
@@ -144,8 +144,8 @@ export async function prompt() {
144144
);
145145
console.log(
146146
`\tDecrypted message: ${chalk.gray(messageEavesdropped)}\n\t${
147-
EActors.Eve
148-
} verifies the message with ${EActors.Bob}.`,
147+
ACTORS.EVE
148+
} verifies the message with ${ACTORS.BOB}.`,
149149
);
150150
},
151151
);

0 commit comments

Comments
 (0)