From a719fc4ed950f3b86bf644d702d0fb2afc5b03cd Mon Sep 17 00:00:00 2001 From: overflowerror Date: Wed, 23 Jan 2019 01:31:18 +0100 Subject: [PATCH] Update README.md --- README.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e23ecdf..d74a9d9 100644 --- a/README.md +++ b/README.md @@ -51,17 +51,18 @@ sfuid_decode(string, &result); ## How does this work? -It's basically a kind of MCG (multiplicative congruential generator) but instead of the last entry in the series beeing the factor I used the current ID. That means the result is not as random because the distance between consecutive ID is basically constant. The reason I did this is to save computation time (also 128 bit won't be enough for numbers that size). +It's basically a kind of MCG (multiplicative congruential generator) but instead of the last entry in the series being the parameter I used the current ID. That means the result is not as random because the distance between consecutive ID is basically constant. The reason I did this is to save computation time (exponentiation would work, but 128 bit won't be enough for numbers that size). So the formular to get a specific ID is: +s(i) := p\*i mod m -![s(i) := p\*i mod m](https://raw.githubusercontent.com/overflowerror/null/master/projects/libsfuid/formular.gif?token=AEuWLXTFaZj0xp6HvvskmihKWeIQcRIdks5cR6sxwA%3D%3D) +The condition for p and m is that their GCD is 1. I chose m to be a power of 2 so it's prime factors are only 2s. For p I chose a prime that is about 25 % of m. For that I hardcoded a list of possible primes candidates into the program. -The condition for p and m is that their GCD is 1. I chose m to be a power of 2 so it's prime factors are only 2. For p I chose a prime that is about 25 % of m. For that I hardcoded a list of possible primes candidated into the program. +To make it more efficient the program calculates m to be the greatest power of 2 that fits in the output space. +For example: Let the character set be "0-9" (10 characters) and the length of the result is 4, the output space would be 10^4. The biggest power of 2 in that space is 2^13. That's m. The p would be 2039 (about 25 % of m). -To make it most efficient the program calculates m to be the greatest power of 2 that fits in the output space. -For example: Let's our character set is "0-9" (10 characters) and the length of the result is 4, the output space would be 10^4. The biggest power of 2 in that space is 2^13. That's our m. The p would be 2039 (about 25 % of m). - -(Note to myself: Maybe just using a big Mersenne prime (like 2^61-1) would also work. That would maximize the output space efficiency, because it's guaranteed that x^y with y > 1 is not a prime.) +(Note to myself: Maybe just using a big Mersenne prime (like 2^61-1) would also work. That would maximize the useable output space efficiency, because it's guaranteed that x^y with y > 1 is not a prime, thus m can be size of the output space.) The result of the MCG is then converted into the string by treating it as a number with the length of the charset as its basis. + +As for the decoding we have to solve the congruence equation x == p * i (mod m) for i. We can do that by multiplying x the multiplicative inverse of p and m.