// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.pqc.crypto.falcon;

class SamplerZ
{
    static int sample(final SamplerCtx samplerCtx, final double n, final double n2) {
        return sampler(samplerCtx, n, n2);
    }
    
    static int gaussian0_sampler(final FalconRNG falconRNG) {
        final int[] array = { 10745844, 3068844, 3741698, 5559083, 1580863, 8248194, 2260429, 13669192, 2736639, 708981, 4421575, 10046180, 169348, 7122675, 4136815, 30538, 13063405, 7650655, 4132, 14505003, 7826148, 417, 16768101, 11363290, 31, 8444042, 8086568, 1, 12844466, 265321, 0, 1232676, 13644283, 0, 38047, 9111839, 0, 870, 6138264, 0, 14, 12545723, 0, 0, 3104126, 0, 0, 28824, 0, 0, 198, 0, 0, 1 };
        final long prng_get_u64 = falconRNG.prng_get_u64();
        final int n = falconRNG.prng_get_u8() & 0xFF;
        final int n2 = (int)prng_get_u64 & 0xFFFFFF;
        final int n3 = (int)(prng_get_u64 >>> 24) & 0xFFFFFF;
        final int n4 = (int)(prng_get_u64 >>> 48) | n << 16;
        int n5 = 0;
        for (int i = 0; i < array.length; i += 3) {
            n5 += n4 - array[i] - (n3 - array[i + 1] - (n2 - array[i + 2] >>> 31) >>> 31) >>> 31;
        }
        return n5;
    }
    
    private static int BerExp(final FalconRNG falconRNG, final double n, final double n2) {
        final int n3 = (int)(n * 1.4426950408889634);
        final double n4 = n - n3 * 0.6931471805599453;
        final int n5 = n3;
        final long n6 = (FPREngine.fpr_expm_p63(n4, n2) << 1) - 1L >>> (n5 ^ ((n5 ^ 0x3F) & -(63 - n5 >>> 31)));
        int n7 = 64;
        int n8;
        do {
            n7 -= 8;
            n8 = (falconRNG.prng_get_u8() & 0xFF) - ((int)(n6 >>> n7) & 0xFF);
        } while (n8 == 0 && n7 > 0);
        return n8 >>> 31;
    }
    
    private static int sampler(final SamplerCtx samplerCtx, final double n, final double n2) {
        final int n3 = (int)FPREngine.fpr_floor(n);
        final double n4 = n - n3;
        final double n5 = n2 * n2 * 0.5;
        final double n6 = n2 * samplerCtx.sigma_min;
        int gaussian0_sampler;
        int n8;
        double n9;
        do {
            gaussian0_sampler = gaussian0_sampler(samplerCtx.p);
            final int n7 = samplerCtx.p.prng_get_u8() & 0xFF & 0x1;
            n8 = n7 + ((n7 << 1) - 1) * gaussian0_sampler;
            n9 = n8 - n4;
        } while (BerExp(samplerCtx.p, n9 * n9 * n5 - gaussian0_sampler * gaussian0_sampler * 0.15086504887537272, n6) == 0);
        return n3 + n8;
    }
}
