diff --git a/Uebung 6/Uebung6_3/Makefile b/Uebung 6/Uebung6_3/Makefile new file mode 100644 index 0000000..6de748d --- /dev/null +++ b/Uebung 6/Uebung6_3/Makefile @@ -0,0 +1,52 @@ + +# Name of the binary for Development +BINARY = main +# Name of the binary for Release +FINAL = prototyp +# Object files +OBJS = main.o +# Compiler flags +CFLAGS = -Werror -Wall -std=c++17 -g -fsanitize=address,undefined -g +# Linker flags +LFLAGS = -fsanitize=address,undefined +#Which Compiler to use +COMPILER = g++ + + +# all target: builds all important targets +all: binary + +final : ${OBJS} + ${COMPILER} ${LFLAGS} -o ${FINAL} ${OBJS} + rm ${OBJS} + +binary : ${OBJS} + ${COMPILER} ${LFLAGS} -o ${BINARY} ${OBJS} + +# Links the binary +${BINARY} : ${OBJS} + ${COMPILER} ${LFLAGS} -o ${BINARY} ${OBJS} + + +# Compiles a source-file (any file with file extension .c) into an object-file +# +# "%" is a wildcard which matches every file-name (similar to * in regular expressions) +# Such a rule is called a pattern rule (because it matches a pattern, see https://www.gnu.org/software/make/manual/html_node/Pattern-Rules.html), +# which are a form of so called implicit rules (see https://www.gnu.org/software/make/manual/html_node/Implicit-Rules.html) +# "$@" and "$<" are so called automatic variables (see https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html) +%.o : %.cpp + ${COMPILER} -c ${CFLAGS} -o $@ $< + + +# Rules can not only be used for compiling a program but also for executing a program +run: ${BINARY} + ./${BINARY} + + +# Delete all build artifacts +clean : + rm -rf ${BINARY} ${OBJS} + + +# all and clean are a "phony" targets, meaning they are no files +.PHONY : all clean diff --git a/Uebung 6/Uebung6_3/main.cpp b/Uebung 6/Uebung6_3/main.cpp new file mode 100644 index 0000000..1c4b0e4 --- /dev/null +++ b/Uebung 6/Uebung6_3/main.cpp @@ -0,0 +1,88 @@ +#include +#include + +// Konstanten aus der Angabe +#define n 12 +#define k 6 +#define L 15 + +// Liste der Primzahlen. Durch die oben angeführten Konstanten ist +// sichergestellt dass diese eindeutig sind +uint16_t listofPrimes[L]; + +// Random n-bits Zahl generieren. MSB ist immer 1 +uint16_t createRandomNumber(uint8_t bits) { + + uint16_t result = 1; + for (uint8_t i = 0; i < bits - 1; i++) { + result <<= 1; + result |= rand() % 2; + } + return result; +} + +// Check ob eine Zahl Prim ist +bool checkPrime(int number) { + for (int i = 2; i < sqrt(number); i++) { + if (number % i == 0) { + + return false; + } + } + return true; +} + +// Schreibe n primzahlen größer als start in array +void findPrimesBiggerThan(uint16_t *array, int length, int start) { + for (int i = 0; i < length; i++) { + while (!checkPrime(start)) { + start++; + } + array[i] = start; + start++; + } +} + +// die zwei Zahlen die Alice und Bob anfangs aussuchen +int xAlice = createRandomNumber(n); +int xBob = createRandomNumber(n); + +// Daten die an Bob übermittelt werden. j = x_A mod p_i. Bob antwortet mit Bool +bool transmitToBob(int i, int j) { + + if (j != (xBob % listofPrimes[i])) { + return false; + } + return true; +} + +int main() { + // rand() mit timestamp seeden + srand(time(NULL)); + + // Primzahlen für Alice und Bob initialisieren + findPrimesBiggerThan(listofPrimes, L, pow(2, k)); + + // Alice sucht ein i zw. 1 und L aus + int iAlice = rand() % L - 1 + 1; + + // Counter für false positives + int falseCounter = 0; + + long long iterations = 1000000; + for (int i = 0; i < iterations; i++) { + // Frische Zahlen für Alice und Bob derinieren + xAlice = createRandomNumber(n); + xBob = createRandomNumber(n); + + // false positive, wenn Bob behauptet die Zahlen wären gleich, sie es aber + // nicht sind + if (transmitToBob(iAlice, xAlice % listofPrimes[iAlice]) && + xAlice != xBob) { + falseCounter++; + } + } + + // false-positive rate in Prozent. Empirisch: ca. 1% + std::cout << 100 * (float)(falseCounter) / iterations << std::endl; +}