Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary index shuffling #10

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 2 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,31 +174,6 @@ function newCoefficients(intercept: number, degree: number): Readonly<Uint8Array
return coefficients;
}

// Creates a set of values from [1, 256).
// Returns a psuedo-random shuffling of the set.
function newCoordinates(): Readonly<Uint8Array> {
const coordinates = new Uint8Array(255);
for (let i = 0; i < 255; i++) {
coordinates[i] = i + 1;
}

// Pseudo-randomize the array of coordinates.
//
// This impl maps almost perfectly because both of the lists (coordinates and randomIndices)
// have a length of 255 and byte values are between 0 and 255 inclusive. The only value that
// does not map neatly here is if the random byte is 255, since that value used as an index
// would be out of bounds. Thus, for bytes whose value is 255, wrap around to 0.
const randomIndices = getRandomBytes(255);
for (let i = 0; i < 255; i++) {
const j = randomIndices[i]! % 255; // Make sure to handle the case where the byte is 255.
const temp = coordinates[i]!;
coordinates[i] = coordinates[j]!;
coordinates[j] = temp;
}

return coordinates;
}

// Helpers for declarative argument validation.
const AssertArgument = {
instanceOf(object: any, constructor: Function, message: string) {
Expand Down Expand Up @@ -256,11 +231,10 @@ export async function split(

const result: Uint8Array[] = [];
const secretLength = secret.byteLength;
const xCoordinates = newCoordinates();

for (let i = 0; i < shares; i++) {
const share = new Uint8Array(secretLength + 1);
share[secretLength] = xCoordinates[i]!;
share[secretLength] = i + 1;
result.push(share);
}

Expand All @@ -271,8 +245,7 @@ export async function split(
const coefficients = newCoefficients(byte, degree);

for (let j = 0; j < shares; ++j) {
const x = xCoordinates[j]!;
const y = evaluate(coefficients, x, degree);
const y = evaluate(coefficients, j + 1, degree);
result[j]![i] = y;
}
}
Expand Down