-
-
Save MurageKibicho/2a68aabbed70651c3d79eee31c9b28fc to your computer and use it in GitHub Desktop.
85 and 86 weird. idk what the fuck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <openssl/sha.h> | |
#include <openssl/rand.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <gmp.h> | |
#include <assert.h> | |
#include <math.h> | |
#include "ff_asm_primes.h" | |
#define STB_DS_IMPLEMENTATION | |
#include "stb_ds.h" | |
#define INDEX(x, y, cols) ((x) * (cols) + (y)) | |
#define TRUE 1 | |
#define FALSE 0 | |
//clear && gcc Fourier.c -lm -lgmp -lcrypto -o m.o && ./m.o | |
int largestPossible = 1 << 30; | |
double GammaApproximation(double n, double k, double base) | |
{ | |
double result = 0; | |
result = (lgamma(n+1) -lgamma(n-k+1) -lgamma(k+1)) / log(base); | |
return result; | |
} | |
int ModularExponentiation(int base, int exp, int mod){int result = 1;base = base % mod;while(exp > 0){if(exp % 2 == 1){result = (result * base) % mod;}exp = exp >> 1;base = (base * base) % mod;}return result;} | |
int FastCheck_2IsAGenerator(int primeNumber){int result = FALSE;if(primeNumber == 2){result = TRUE;}if(primeNumber % 8 == 3 || primeNumber % 8 == 5){int primeNumberMinusOne = primeNumber - 1;int n = primeNumberMinusOne;int q = 0;if(n % 2 == 0){q = 2;int checkvalue = ModularExponentiation(2, primeNumberMinusOne / q, primeNumber);if(checkvalue == 1){result = FALSE;return result;}while(n % 2 == 0){n /= 2;}}for(int i = 3; i * i <= primeNumberMinusOne; i += 2){if(n % i == 0){q = i;int checkvalue = ModularExponentiation(2, primeNumberMinusOne / q, primeNumber);if(checkvalue == 1){result = FALSE;return result;}while(n % i == 0){n /= i;}}}if(n > 1){q = n;int checkvalue = ModularExponentiation(2, primeNumberMinusOne / q, primeNumber);if(checkvalue == 1){result = FALSE;return result;}}result = TRUE;}return result;} | |
double Find_Log_MPZ_Double(mpz_t x){if(mpz_cmp_ui(x, 0) == 0){return 0;}signed long int ex;const double di = mpz_get_d_2exp(&ex, x);return ( (log(di) + log(2) * (double) ex) /log(2));} | |
double FileForma_GammaApproximation(double n, double k){double result = 0;result = (lgamma(n+1) -lgamma(n-k+1) -lgamma(k+1)) / log(2);return result;} | |
int FindLargestNLogarithm(double searchLog, int base){int max = largestPossible;int min = 0;int mid = 0;double currentLog = 0;double k = (double)base;while(min <= max){mid = min + (max - min) / 2;currentLog = FileForma_GammaApproximation((double)mid, k);if(currentLog < searchLog){min = mid + 1;}else{max = mid - 1;}}return max;} | |
int FindLargestNoLog(int number, int base, mpz_t binomialCoefficient){int currentValue = 0;int k = base;int n = 0;while(currentValue <= number){mpz_bin_uiui(binomialCoefficient, n, k);currentValue = mpz_get_ui(binomialCoefficient);n++;}return n-2;} | |
void PrintIntArray(int length, int*array){for(int i = 0; i < length; i++){printf("%3d ", array[i]);}printf("\n");} | |
void PrintFloatArray(int length, float*array){for(int i = 0; i < length; i++){printf("(%3d: %.3f) ",i, array[i]);}printf("\n");} | |
void DimensionToInteger(mpz_t binomialCoefficient, mpz_t integer, int oneCount, int *array){assert(oneCount > 0);mpz_set_ui(integer, 0);for(int i = 0; i < oneCount; i++){mpz_bin_uiui(binomialCoefficient, array[i], i+1);mpz_add(integer, integer, binomialCoefficient);}} | |
void ChangeDimension(mpz_t binomialCoefficient, mpz_t integer, int oneCount, int *array){assert(oneCount > 0);double currentLogValue = 0.0f; int n = 0; int k = oneCount;while(mpz_cmp_ui(integer, 0) >= 0){if(k <= 1){break;}currentLogValue = Find_Log_MPZ_Double(integer);if(currentLogValue > 25.0){n = FindLargestNLogarithm(currentLogValue, k);}else{int number = mpz_get_ui(integer); n = FindLargestNoLog(number, k,binomialCoefficient);}if(n < 0){break;}mpz_bin_uiui(binomialCoefficient, n, k);mpz_sub(integer, integer, binomialCoefficient);array[k-1] = n;k -=1;}if(k == 1){array[k-1] = mpz_get_ui(integer);}} | |
typedef struct circle_struct *Circle; | |
struct circle_struct | |
{ | |
int circumference; | |
int index; | |
double x; | |
double y; | |
}; | |
Circle CreateCircle(int circumference) | |
{ | |
Circle circle = malloc(sizeof(struct circle_struct)); | |
circle->circumference = circumference; | |
circle->index = 0; | |
circle->x = 0; | |
circle->y = 0; | |
return circle; | |
} | |
void PrintCircle(Circle circle) | |
{ | |
if(circle) | |
{ | |
printf("(%d %.3f, %.3f)", circle->index,circle->x,circle->y); | |
} | |
} | |
void DestroyCircle(Circle circle) | |
{ | |
if(circle) | |
{ | |
free(circle); | |
} | |
} | |
void GetPointOnCircle(int circumference, int index, double *x, double *y) | |
{ | |
assert(index > -1); | |
assert(index < circumference); | |
double angleInRadians = (2.0 * M_PI * index) / circumference; // angleInRadians in radians | |
double radius = circumference / (2.0 * M_PI); | |
*x = radius * cos(angleInRadians); | |
*y = radius * sin(angleInRadians); | |
} | |
void FillCircle(Circle circle, int index) | |
{ | |
double x = 0;double y = 0; | |
GetPointOnCircle(circle->circumference, index, &x, &y); | |
circle->x = x; | |
circle->y = y; | |
circle->index = index; | |
} | |
void TestGetPoint() | |
{ | |
int circumference = 6; | |
int index = 5; | |
double x = 0;double y = 0; | |
GetPointOnCircle(circumference, index, &x, &y); | |
printf("%.3f %.3f\n",x,y); | |
} | |
void TestCircle() | |
{ | |
srand(43434); | |
int circumference[] = {3,5,7}; | |
double finalX = 0.0; | |
double finalY = 0.0; | |
int length = sizeof(circumference) / sizeof(int); | |
Circle *circles = malloc(length * sizeof(Circle)); | |
for(int i = 0; i < length; i++) | |
{ | |
circles[i] = CreateCircle(circumference[i]); | |
FillCircle(circles[i], rand() % circumference[i]); | |
finalX += circles[i]->x; | |
finalY += circles[i]->y; | |
PrintCircle(circles[i]); | |
} | |
printf("\n%.3f %.3f\n",finalX,finalY); | |
for(int i = 0; i < length; i++) | |
{ | |
DestroyCircle(circles[i]); | |
} | |
free(circles); | |
} | |
uint8_t *GenerateRandomBytes(size_t length, uint8_t *buffer) | |
{ | |
for(size_t i = 0; i < length; i++) | |
{ | |
buffer[i] = (uint8_t)(rand() % 256); | |
} | |
} | |
void BytesToMpz(const uint8_t *bytes, size_t length, mpz_t result) | |
{ | |
mpz_import(result, length, 1, sizeof(uint8_t), 1, 0, bytes); | |
} | |
void Sha256(const uint8_t *data, size_t length, uint8_t hash[SHA256_DIGEST_LENGTH]) | |
{ | |
SHA256_CTX sha256; | |
SHA256_Init(&sha256); | |
SHA256_Update(&sha256, data, length); | |
SHA256_Final(hash, &sha256); | |
} | |
void TestGenerator() | |
{ | |
int currentPrimeIndex = 0;int bSmooth = 10; | |
double logValue = 0.0f; | |
double compositeNumberBits = 256.0f; | |
int *moduli = NULL; | |
while(currentPrimeIndex < 5239) | |
{ | |
if(first5239[currentPrimeIndex] > bSmooth && FastCheck_2IsAGenerator(first5239[currentPrimeIndex]) == TRUE) | |
{ | |
arrput(moduli, first5239[currentPrimeIndex]); | |
logValue += log2(first5239[currentPrimeIndex]); | |
if(logValue > compositeNumberBits){break;} | |
} | |
currentPrimeIndex += 1; | |
} | |
PrintIntArray(arrlen(moduli), moduli); | |
arrfree(moduli); | |
} | |
void TestSHA() | |
{ | |
int currentPrimeIndex = 0;int bSmooth = 10; | |
double logValue = 0.0f; | |
double aX = 0.0f;double aY = 0.0f; | |
double bX = 0.0f;double bY = 0.0f; | |
double compositeNumberBits = 256.0f; | |
int *moduli = NULL; | |
while(currentPrimeIndex < 5239) | |
{ | |
if(first5239[currentPrimeIndex] > bSmooth && FastCheck_2IsAGenerator(first5239[currentPrimeIndex]) == TRUE) | |
{ | |
arrput(moduli, first5239[currentPrimeIndex]); | |
logValue += log2(first5239[currentPrimeIndex]); | |
if(logValue > compositeNumberBits){break;} | |
} | |
currentPrimeIndex += 1; | |
} | |
PrintIntArray(arrlen(moduli), moduli); | |
Circle *resA = malloc(arrlen(moduli) * sizeof(Circle)); | |
Circle *resB = malloc(arrlen(moduli) * sizeof(Circle)); | |
for(int i = 0; i < arrlen(moduli) ; i++) | |
{ | |
resA[i] = CreateCircle(moduli[i]); | |
resB[i] = CreateCircle(moduli[i]); | |
} | |
size_t blockSizeBytes = 32; | |
int trials = 100; | |
uint8_t *randomData = calloc(blockSizeBytes, sizeof(uint8_t)); | |
uint8_t hash[SHA256_DIGEST_LENGTH] = {0}; | |
mpz_t t0,t1,t2;mpz_inits(t0,t1,t2,NULL); | |
int modA,modB; | |
for(int i = 0; i < trials; i++) | |
{ | |
GenerateRandomBytes(blockSizeBytes, randomData); | |
BytesToMpz(randomData, blockSizeBytes, t0); | |
Sha256(randomData, blockSizeBytes, hash); | |
BytesToMpz(hash, blockSizeBytes, t1); | |
//gmp_printf("\n%d : %Zd %Zd\n", i, t0, t1); | |
printf("%d : ", i); | |
aX = 0;aY = 0;bX = 0;bY = 0; | |
for(int j = 0; j < arrlen(moduli); j++) | |
{ | |
modA = mpz_mod_ui(t2, t0, moduli[j]); | |
modB = mpz_mod_ui(t2, t1, moduli[j]); | |
FillCircle(resA[j], modA); | |
FillCircle(resB[j], modB); | |
aX = resA[j]->x;aY = resA[j]->y; | |
bX = resB[j]->x;bY = resB[j]->y; | |
//if(i == 86 || i == 85){PrintCircle(resA[j]);PrintCircle(resB[j]);} | |
//finalX += circles[i]->x; | |
//finalY += circles[i]->y; | |
} | |
printf("%.3f %.3f : %.3f %.3f\n",aX,aY,bX,bY); | |
} | |
mpz_clears(t0,t1,t2,NULL); | |
for(int i = 0; i < arrlen(moduli) ; i++) | |
{ | |
DestroyCircle(resA[i]); | |
DestroyCircle(resB[i]); | |
} | |
free(resA);free(resB); | |
free(randomData); | |
arrfree(moduli); | |
} | |
int main() | |
{ | |
TestSHA(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment