diff --git a/GNUmakefile.in b/GNUmakefile.in index 7e3b25e..0a90819 100644 --- a/GNUmakefile.in +++ b/GNUmakefile.in @@ -31,8 +31,8 @@ MAIN_OBJ= \ $(ED25519_OBJ) \ keccak.c.o -UTIL_CALCDIFF_OBJ= \ - calcdiff.c.o +UTIL_CALCEST_OBJ= \ + calcest.c.o TEST_BASE64_OBJ= \ test_base64.c.o \ @@ -69,10 +69,11 @@ ALL_C= $(patsubst %.c.o,%.c,$(filter %.c.o %.c,$(ALL_O))) CLEAN_O= $(filter %.o,$(ALL_O)) MAIN_LIB= -lpthread -lsodium @MAINLIB@ +UTIL_CALCEST_LIB= -lm TEST_ED25519_LIB= -lsodium MAIN_TGT= mkp224o -UTIL_TGT= calcdiff +UTIL_TGT= calcest TEST_TGT= test_base64 test_base32 test_base16 test_ed25519 MAIN_EXE= $(patsubst %,%@EXEEXT@,$(MAIN_TGT)) @@ -90,8 +91,8 @@ all: $(ALL_EXE) mkp224o@EXEEXT@: $(MAIN_OBJ) $(CC) $(LDFLAGS) $(CFLAGS) -o $@.tmp $^ $(MAIN_LIB) && $(MV) $@.tmp $@ -calcdiff@EXEEXT@: $(UTIL_CALCDIFF_OBJ) - $(CC) $(LDFLAGS) $(CFLAGS) -o $@.tmp $^ && $(MV) $@.tmp $@ +calcest@EXEEXT@: $(UTIL_CALCEST_OBJ) + $(CC) $(LDFLAGS) $(CFLAGS) -o $@.tmp $^ $(UTIL_CALCEST_LIB) && $(MV) $@.tmp $@ test_base64@EXEEXT@: $(TEST_BASE64_OBJ) $(CC) $(LDFLAGS) $(CFLAGS) -o $@.tmp $^ && $(MV) $@.tmp $@ diff --git a/calcest.c b/calcest.c new file mode 100644 index 0000000..23e88de --- /dev/null +++ b/calcest.c @@ -0,0 +1,43 @@ +#include +#include + +/* + * as per scribblemaniac's explanation: + * t - number of trials + * n - character count + * p - probability + * condition: >=1 matches + * formula: t = log(1-p)/log(1-1/32^n) + * comes from: + * distribution X~Binomial(t, 1/32^n) + * P(X>=1)=p + */ + +const double probs[] = { 0.5, 0.8, 0.9, 0.95, 0.99 }; +const int charcounts[] = { 2, 3, 4, 5, 6, 7 }; + +int main(void) +{ + printf(" |"); + for (int i = 0; i < sizeof(probs)/sizeof(probs[0]); ++i) { + printf(" %11d%% |",(int)((probs[i]*100)+0.5)); + } + printf("\n"); + + printf("---+"); + for (int i = 0; i < sizeof(probs)/sizeof(probs[0]); ++i) { + printf("--------------+"); + } + printf("\n"); + + for (int i = 0; i < sizeof(charcounts)/sizeof(charcounts[0]); ++i) { + printf("%2d |",charcounts[i]); + for (int j = 0; j < sizeof(probs)/sizeof(probs[0]); ++j) { + double t = log2(1 - probs[j]) / log2(1 - (1 / pow(32,charcounts[i]))); + printf(" %12.0f |",t); + } + printf("\n"); + } + + return 0; +}