Skip to content

Instantly share code, notes, and snippets.

@davibennun
Created October 22, 2024 18:31
Show Gist options
  • Save davibennun/6684528c870f5be217d53a1a08869603 to your computer and use it in GitHub Desktop.
Save davibennun/6684528c870f5be217d53a1a08869603 to your computer and use it in GitHub Desktop.
non-linear transformation functions

Here are several examples of non-linear transformation functions that you can use to obfuscate numbers while preserving their relative order. Each function applies a different non-linear mathematical transformation to the input values. These transformations will make it more difficult to reverse engineer the original numbers.

1. Exponential Transformation

This function applies an exponential transformation with a base, making the values grow rapidly as the input increases.

function obfuscateExponential(arr) {
    const base = 1.2; // Base of the exponential growth

    const obfuscatedArr = arr.map(num => Math.pow(base, num));
    return obfuscatedArr;
}

let numbers = [10, 100, 1000];
console.log(obfuscateExponential(numbers)); // Output: Exponentially transformed numbers

2. Sigmoid Transformation

The sigmoid function maps inputs into a bounded range (typically between 0 and 1), creating an S-shaped curve. This is useful for compressing values within a fixed range while still maintaining non-linearity.

function sigmoid(x) {
    return 1 / (1 + Math.exp(-x));
}

function obfuscateSigmoid(arr) {
    const scaleFactor = 10; // Scaling factor for the sigmoid input
    const obfuscatedArr = arr.map(num => sigmoid(num / scaleFactor));

    return obfuscatedArr;
}

let numbers = [10, 100, 1000];
console.log(obfuscateSigmoid(numbers)); // Output: Sigmoid-transformed numbers in the range [0, 1]

3. Polynomial Transformation

This transformation applies a polynomial function to the input values. The degree of the polynomial can be adjusted to control the non-linearity.

function obfuscatePolynomial(arr) {
    const a = 0.1, b = 2.5, c = 5; // Polynomial coefficients

    const polynomialTransform = num => a * Math.pow(num, 2) + b * num + c;
    const obfuscatedArr = arr.map(polynomialTransform);

    return obfuscatedArr;
}

let numbers = [10, 100, 1000];
console.log(obfuscatePolynomial(numbers)); // Output: Polynomial-transformed numbers

4. Logarithmic Scaling with Randomization

A logarithmic transformation combined with a randomized offset makes it more difficult to deduce the original numbers.

function obfuscateLogarithmic(arr) {
    const logBase = 10; // Logarithmic base
    const randomFactor = () => Math.random() * 0.5; // Add randomization to the transformation

    const obfuscatedArr = arr.map(num => Math.log(num) / Math.log(logBase) + randomFactor());

    return obfuscatedArr;
}

let numbers = [10, 100, 1000];
console.log(obfuscateLogarithmic(numbers)); // Output: Logarithmically transformed numbers with added noise

5. Trigonometric Transformation

This example uses a sine wave to apply a non-linear transformation. Be aware that trigonometric functions introduce periodicity, so you need to adjust the scale to avoid too much repetition.

function obfuscateSine(arr) {
    const amplitude = 100; // Amplitude of the sine wave
    const frequency = 0.01; // Frequency of the wave

    const sineTransform = num => amplitude * Math.sin(frequency * num);
    const obfuscatedArr = arr.map(sineTransform);

    return obfuscatedArr;
}

let numbers = [10, 100, 1000];
console.log(obfuscateSine(numbers)); // Output: Sine-wave transformed numbers

6. Piecewise Non-linear Transformation

This function splits the input range into different intervals, applying different transformations to each range. It creates non-linear effects while adding complexity to the transformation.

function obfuscatePiecewiseNonLinear(arr) {
    const piecewiseTransform = (num) => {
        if (num < 100) {
            return Math.pow(num, 1.5) + 20;  // Non-linear for small values
        } else if (num < 1000) {
            return Math.log(num) * 100 + 50; // Logarithmic transformation for medium values
        } else {
            return Math.pow(num, 0.5) * 200; // Square root scaling for large values
        }
    };

    const obfuscatedArr = arr.map(piecewiseTransform);
    return obfuscatedArr;
}

let numbers = [10, 100, 1000];
console.log(obfuscatePiecewiseNonLinear(numbers)); // Output: Piecewise non-linear transformed numbers

7. Tanh (Hyperbolic Tangent) Transformation

The hyperbolic tangent function maps values between -1 and 1 and creates a smooth S-shaped curve. You can scale and shift the results as needed.

function obfuscateTanh(arr) {
    const scaleFactor = 0.01; // Scale the input to control the steepness
    const shiftFactor = 1.5;  // Shift to make sure outputs are non-negative

    const obfuscatedArr = arr.map(num => Math.tanh(num * scaleFactor) + shiftFactor);
    return obfuscatedArr;
}

let numbers = [10, 100, 1000];
console.log(obfuscateTanh(numbers)); // Output: Tanh-transformed numbers

Summary of Methods:

  • Exponential Transformation: Rapid growth, hard to reverse.
  • Sigmoid Transformation: Compresses numbers into a specific range.
  • Polynomial Transformation: Provides flexibility with various non-linear coefficients.
  • Logarithmic Scaling with Randomization: Applies randomness to log-based scaling for added obfuscation.
  • Trigonometric (Sine) Transformation: Adds periodic non-linearity.
  • Piecewise Non-linear Transformation: Applies different functions to different ranges.
  • Tanh Transformation: Smooth non-linear function with bounded output.

By combining these methods, you can add multiple layers of complexity to make it very difficult for someone to reverse-engineer the original numbers while preserving their order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment