Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/anonymousknight07/catalog_shamir-s-secret-sharing-algorithm.


https://github.com/anonymousknight07/catalog_shamir-s-secret-sharing-algorithm.

Last synced: about 10 hours ago
JSON representation

Awesome Lists containing this project

README

        

This code is designed to process a directory of JSON files, parse their content, and perform polynomial interpolation using the Lagrange method. The primary goal is to find a "secret" value based on the data provided in the JSON files, with some additional logic to identify wrong data points in specific test cases. Here's a step-by-step explanation:

### 1. **Imports and Initialization**

- **`fs` and `path` modules**:
- `fs`: Used to read the contents of files.
- `path`: Used to work with file and directory paths.
- **`toDecimal(value, base)`**: Converts a string `value` in a given `base` to a decimal integer using `parseInt()`.

### 2. **Lagrange Interpolation Method**

- **`lagrangeInterpolation(xValues, yValues, x)`**:
- This function calculates the value of a polynomial at point `x` using Lagrange interpolation.
- **Lagrange interpolation** is a method to estimate a polynomial that passes through a set of points `(xValues, yValues)`.
- For each `i-th` term, it constructs the Lagrange basis polynomial and multiplies it by `yValues[i]`, summing the terms to compute the final result.
- The result is rounded to the nearest integer to avoid floating-point precision issues.

### 3. **Polynomial Evaluation**

- **`evaluatePolynomial(xValues, yValues, x)`**: A wrapper function for `lagrangeInterpolation`, simply calls it with the given `xValues` and `yValues` to evaluate the polynomial at `x`.

### 4. **Finding Incorrect Data Points**

- **`findWrongPoints(xValues, yValues, k)`**:
- This function tries to find "wrong points" in the provided data based on a polynomial interpolation using only the first `k` points.
- **Procedure**:
1. Take the first `k` points from `xValues` and `yValues` to construct a subset of data.
2. For each point `(xValues[i], yValues[i])`, calculate the expected `y` value using Lagrange interpolation based on the first `k` points.
3. Compare the expected `y` with the actual `yValues[i]`. If the difference is larger than a small threshold (`1e-10`), mark it as a "wrong point" and store it.

### 5. **Finding the Secret from JSON Input**

- **`findSecret(jsonInput, isTestCase2)`**:
- Extracts the `x` and `y` values from the provided `jsonInput`.
- Uses the first `k` points to interpolate a polynomial and compute the "secret" by evaluating the polynomial at `x = 0`.
- **In Test Case 2**, it calls `findWrongPoints` to check if there are any wrong points in the data.
- Returns an object with two properties:
- `secret`: The computed secret value.
- `wrongPoints`: The wrong data points (if applicable).

### 6. **Processing Files in a Directory**

- **`processFiles(directoryPath)`**:
- This function reads all the files in the specified `directoryPath`, filters them for `.json` files, and processes each JSON file.
- For each file:
1. Read its contents and parse it as JSON.
2. Determine whether the input corresponds to "Test Case 2" by checking if `k === 6`.
3. Call `findSecret()` to compute the secret and identify any wrong points (in Test Case 2).
4. Print the results, including the secret and any wrong points found.

### 7. **Main Execution**

- The last part of the code sets the directory path as `./jsonFiles` and calls `processFiles(directoryPath)` to initiate the whole process.

---

### Example Workflow

1. **Directory Reading**: The program reads files from the directory `./jsonFiles`.
2. **File Processing**:
- Each JSON file is read, parsed, and its contents are passed to `findSecret()`.
- The `xValues` and `yValues` are extracted from the JSON data. These are interpolated using Lagrange interpolation to find a secret value at `x = 0`.
- If the data belongs to "Test Case 2" (i.e., when `k = 6`), wrong points are identified by comparing the actual values with expected values.
3. **Result**:
- The program prints the derived secret and any wrong points (for test case 2).