Skip to content

Commit

Permalink
Add 0-255 uint8 bounds checking to arithmetic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
asta-li committed Aug 1, 2023
1 parent cd8422d commit 12e6f75
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,23 @@ const EXP_TABLE: Readonly<Uint8Array> = new Uint8Array([
// Combines two numbers in GF(2^8).
// This can be used for both addition and subtraction.
function add(a: number, b: number): number {
if (!Number.isInteger(a) || a < 0 || a > 255) {
throw new RangeError('Number is out of Uint8 range');
}
if (!Number.isInteger(b) || b < 0 || b > 255) {
throw new RangeError('Number is out of Uint8 range');
}
return a ^ b;
}

// Divides two numbers in GF(2^8).
function div(a: number, b: number): number {
if (!Number.isInteger(a) || a < 0 || a > 255) {
throw new RangeError('Number is out of Uint8 range');
}
if (!Number.isInteger(b) || b < 0 || b > 255) {
throw new RangeError('Number is out of Uint8 range');
}
// This should never happen
if (b === 0) {
throw new Error('cannot divide by zero');
Expand All @@ -72,6 +84,12 @@ function div(a: number, b: number): number {

// Multiplies two numbers in GF(2^8).
function mult(a: number, b: number): number {
if (!Number.isInteger(a) || a < 0 || a > 255) {
throw new RangeError('Number is out of Uint8 range');
}
if (!Number.isInteger(b) || b < 0 || b > 255) {
throw new RangeError('Number is out of Uint8 range');
}
const logA = LOG_TABLE[a]!;
const logB = LOG_TABLE[b]!;
const sum = (logA + logB) % 255;
Expand Down

0 comments on commit 12e6f75

Please sign in to comment.