From d4eef99b2ed907395ceba516268f1d5b96a7c548 Mon Sep 17 00:00:00 2001 From: alteropen Date: Mon, 10 Feb 2025 13:25:07 +0000 Subject: [PATCH] Finished project --- main.cpp | 573 +++++++++++++++++++++++++++++++++++++++++++ versions/001/old.cpp | 46 ++++ 2 files changed, 619 insertions(+) create mode 100644 main.cpp create mode 100644 versions/001/old.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..0c50e4e --- /dev/null +++ b/main.cpp @@ -0,0 +1,573 @@ +#include +# include +# include +# include +# include +# include +using namespace std; + +typedef long long int Lint; // 64 bits +typedef double Ldouble; +struct security { + int num_shares; + int num_required; +}; + +struct shareStruct { + Lint y; + int x; +}; + +bool isPrime(Lint n) { + int flag = 0; + for (int i = 2; i <= n / i; ++i) { + if (n % i == 0) { + flag = 1; + break; + } + } + if (flag == 0) return true; + else return false; +} + +Lint genRandInt(int n) { + // Returns a random number + // between 2**(n-1)+1 and 2**n-1 + //long max = (long)powl(2, n) - 1; + //long min = (long)powl(2, n - 1) + 1; + long max = (long)pow(2, n) - 1; + long min = (long)pow(2, n - 1) + 1; + Lint result = min + (rand() % ( max - min + 1 ) ); + //cout << "Random num: " << result << endl; + return result; +} + +Lint genPrime() { + Lint prime = 10; + + while (isPrime(prime) == false) { + int complexity = 50; + prime = genRandInt(complexity); + } + return prime; +} + +Lint getSecret() { + int secret; + Lint total = 1; + string total_string = ""; + string secret_text; + + cout << "Enter share secret:"; + cin.ignore(); + getline(cin, secret_text); + cout << endl; + + for (int i=0; i < size(secret_text); i++) { + char character = secret_text[i]; + string character_value = to_string(int(character)); + + int append_size = 3 - size(character_value); + string append_string = ""; + for (int i=0; i num_shares) and (num_required < 7)) { + cout << "Enter the number of shares required (2-6): "; + cin >> num_required; + cout << endl << "Enter the number of shares to be created: "; + cin >> num_shares; + cout << endl; + } + security securityOptions; + securityOptions.num_shares = num_shares; + securityOptions.num_required = num_required; + + return securityOptions; +} + +Lint getPolyY(const int* poly, int poly_len, int poly_x, const Lint prime) { + Lint total = 0; + Lint poly_y = 0; + + for (int i=0; i> required; + cout << endl; + + shareStruct* shares = new shareStruct[required]; + for (int i = 0; i < required; i++) { + cout << "Enter number of next share: "; + shareStruct share; + // RBP + share.x = 1; + // RBP + cin >> share.x; + + cout << "Enter the share secret: "; + // RBP + share.y = 3; + // RBP + cin >> share.y; + cout << endl; + + shares[i] = share; + } + + inputs.shares = shares; + inputs.required = required; + return inputs; +} + +struct polyTerm { + Lint coefficient; + int power; +}; + +struct linearEquation { + shareStruct point; + polyTerm* terms; +}; + +linearEquation* constructEquations(const int required, shareStruct shares[]) { + linearEquation* equations = new linearEquation[required]; + shareStruct share; + polyTerm term; + + for (int i = 0; i < required; i++) { + share = shares[i]; + linearEquation equation; + polyTerm* terms = new polyTerm[required]; + + for (int j = 0; j < required; j++) { + term.power = required - 1 - j; + terms[j] = term; + } + + equation.terms = terms; + equation.point.x = share.x; + equation.point.y = share.y; + + // RBP + //cout << "y: " << equation.point.y << endl; + //cout << "x: " << equation.point.x << endl; + //cout << "i: " << i << endl; + // RBP + + equations[i] = equation; + // dont delete terms from memory as its referanced in equations + } + return equations; +} + +struct matrix{ + Lint** matrix; + int dimension_x; + int dimension_y; +}; +struct matrix_system { + matrix A; + matrix B; + matrix X; +}; + +matrix_system formMatrix(const linearEquation* equations, int required) { + Lint** matrixA = new Lint*[required]; + Lint** matrixB = new Lint*[required]; + + for (int i=0; i < required; i++) { + linearEquation equation = equations[i]; + Lint* lineA = new Lint[required]; + for (int j=0; j < required; j++) { + lineA[j] = pow(equation.point.x, equation.terms[j].power); + } + matrixA[i] = lineA; + + Lint* lineB = new Lint[1]; + lineB[0] = equation.point.y; + matrixB[i] = lineB; + } + + matrix matrixA_data; matrix matrixB_data; + matrixA_data.matrix = matrixA; matrixB_data.matrix = matrixB; + + matrixA_data.dimension_x = required; matrixB_data.dimension_x = 1; + matrixA_data.dimension_y = required; matrixB_data.dimension_y = required; + + matrix_system matricies; + matricies.A = matrixA_data; matricies.B = matrixB_data; + + return matricies; +} + +Lint** findMinor(Lint** matrixA, const int dimension, const int pos_x, const int pos_y) { + Lint** matrixB = new Lint*[dimension-1]; + int matrixB_pos_x = 0; int matrixB_pos_y = 0; + + for (int i=0; i> selection; + cout << endl; + if (selection == 1) { + solve(); + } + else if (selection == 2) { + newSecret(); + } + else if (selection == 3) { + internalTest(); + } + else { + exit = true; + } + } + return 0; +} diff --git a/versions/001/old.cpp b/versions/001/old.cpp new file mode 100644 index 0000000..e96d23d --- /dev/null +++ b/versions/001/old.cpp @@ -0,0 +1,46 @@ +# include +# include +# include +# include +using namespace std; + +int genRandInt(int range_start, int range_end) { + random_device dev; + mt19937 rng(dev()); + uniform_int_distribution dist6(range_start, range_end); + return dist6(rng); +} + +int genPolyPoints(int poly_func) { +} + +int genPoly(int degree) { + int poly[degree+1]; + + for (int i = 0; i < degree+1; i++) { + int random_num = genRandInt(1, 99999999999999999); + poly[i] = random_num; + } + + return poly; +} + +void genSecret(string secret, int shares, int required=shares) { + int poly_degree = required - 1; + int poly_func = genPoly(poly_degree); + int points = genPolyPoints(poly_func) +} + +void getShares(int num_shares) { + string shares[num_shares]; + cout << "Enter share secret:\n" + cin >> share; + cout << "\n" +} + +void encodeSecret(string secret) { +} + +int main() { + return 0; +}