Fermat's Little Theorem - cryptography

How do you compute the following using Fermat's Little Theorem?
2^1,000,006 mod 101
2^-1,000,005 mod 11

You know that a^(p-1) === 1 mod p, so...
2^10 === 1 mod 11
2^(-1,000,005) = 2^(-1,000,000)*2^(-5) = 1 * 2^(-5) = 2^(-5)*2^(10) = 32 mod 11 = -1 = 10
From this, can you see how to work the larger number? The process is the same.
It's FLT all the way. I messed up.

Since 101 and 11 are prime, then (respectively) 2^100 and 2^10 are congruent to 1 mod 101 and 11.
Try to express 2^1000006 in terms of 2^100 and 2^-1000005 in terms of 2^10. You should be able to reduce each problem to something easy to calculate.

Related

Understanding Remainder operator

Just doing some basic modulo operations and trying to wrap my head around the below operations with questions marks.
0%5 // 0 - Totally understand
1%5 // 1 ?
2%5 // 2 ?
3%5 // 3 ?
4%5 // 4 ?
5%5 // 0 - Totally understand
Perhaps I'm thinking in the wrong way. For example 1/5 would return a Double of 0.2 and not a single integer so how does it return a remainder of 1?
I understand these. It makes sense but the above I can't wrap my head around.
9%4 // 1
10%2 // 0
10%6 // 4
Be great if someone could explain this. Seems I'm having a brain fart. Source of learning.
From the same Basic Operators page that you link to:
The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder).
Specifically for 1 % 5:
5 doesn't fit in 1, so it fits 0 times.
This means that 1 can be described as
1 = (5 * multiplier) + remainder
Since the multiplier is 0, the remainder is 1
1 = (5 * 0) + remainder
1 = remainder
If we instead look at 6 % 5 the remainder is also 1. This is because 5 fit in 6 one time:
6 = (5 * multiplier) + remainder
6 = (5 * 1) + remainder
6-5 = remainder
1 = remainder
This / the division operator when you say 1/5 if division is in integer it'll give 0 , but this 1.0/0.5 when you make it in Double , it'll give 0.2
but % the modulo operator when you say 1%5 = 1 because you have 1 = 0*5 + 1 which means that 1 has zero number of 5 and the reminder is 1

round up to next multiple of 10 vb

How can I round a value UP to the next multiple of 10 in VB ?
e.g.
19 -> 20
35 -> 40
21 -> 30
I found so things saying use a round function but when i type it in to my IDE (Microsoft Visual Studio Express 2015) it doesn't recognise it.
Thanks in advance
To round up use the ceiling function. (info here)
myNumber = Math.Ceiling(myNumber / 10) * 10
Deviding it first by 10 and then multiplying it again with 10 will do the trick.
Update: in case you are wondering, there are no problems with Integer
This is an Integer-based solution.
myNumber = If(myNumber Mod 10 = 0, myNumber, If(myNumber > 0, 10, 0) + 10 * (myNumber \ 10))
It doesn't suffer from rounding and is also is at least 2x faster than using Math.Ceiling.

Division with modulus remainders

How can you do division with modulus remainders?
For example: Find the remainder when 9^2012 is divided by 11.
Using modular arithmetic, 9 == 1(mod 4), so 9^2012 == 1^2012(mod 4). Therefore, 9^2012 == 1(mod 4). Also, 11 == 3(mod 4). To answer the question, I'm trying to do 1(mod 4)/3(mod 4). Is there any way to do this?
There two important theories that would help you to solve this problem :-
Modular Exponentiation
Fermat's little theorem
Modular Exponentiation :- This simple recursive formula to make you understand :-
(a^n)%p = ((a^(n-1))%p*a)%p
The above formula can help you prevent the overflow which occurs if a^n is large.
Moreover fast exponention can be done using O(logn)
Fermat's little theorem :- If p is prime in your case 11 is prime then (a^(p-1))%p = 1 for any a that is co-prime to p hence (a^n)%p = (a^(n%(p-1)))%p
your example :-
9^2012 % 11 = ?
11 is prime and 9 is co-prime to 11 then using fermat's theorem
9^2012 % 11 = (9^(2012%10)) % 11
2012%10 = 2
9^2012 % 11 = 9^2 % 11 = 4

How to test a bit in a decimal number

I have a set of decimal numbers. I need to check if a specific bit is set in each of them. If the bit is set, I need to return 1, otherwise return 0.
I am looking for a simple and fast way to do that.
Say, for example, I am checking if the third bit is set. I can do (number AND (2^2)), it will return 4 if the bit is set, otherwise it will return 0. How do I make it to return 1 instead of 4?
Thank you!
if ((number AND (2^bitnumber) <> 0) then return 1 else return 0 end if
If you can change your return type to boolean then this is more elegant
return ((number AND (2^bitnumber)) <> 0)
While the division solution is a simple one, I would think a bit-shift operation would be more efficient. You'd have to test it to be sure, though. For instance, if you are using 1 based bit indexes, you could do this:
Dim oneOrZero As Integer = (k And 2 ^ (n - 1)) >> (n - 1)
(Where k is the number and n is the bit index). Of, if you are using 0 based bit indexes, you could just do this:
Dim oneOrZero As Integer = (k And 2 ^ n) >> n
Sorry, guys, I am too slow today.
To test a bit number "n" in a decimal number "k":
(k AND 2^(n-1))/(2^(n-1))
will return 1 if the bit is set, otherwise will return 0.
=====================================================
Hi again, guys!
I compared the performance of the three proposed solutions with zero-based indexes, and here are the results:
"bit-shift solution" - 8.31 seconds
"if...then solution" - 8.44 seconds
"division solution" - 9.41 seconds
The times are average of the four consecutive runs.
Surprisingly for me, the second solution outperformed the third one.
However, after I modified the "division solution" this way:
p = 2 ^ n : oneOrZero = (k And p) / p
it started to run in 7.48 seconds.
So, this is the fastest of the proposed solutions (despite of what Keith says :-).
Thanks everybody for the help!
I really don't know if it can help anyone more than the above, but, here we go.
When I need to fast check a bit in number I compare the decimal-value of this bit directly.
I mean, if I would need to see of the 6th bit is on (32), I check its decimal value, like this:
if x and 32 = 32 then "the bit is ON"
Try for instance check 38 with 32, 4 and 2... and the other bits.
You will see only the actual bits turned on.
I hope it can help.
Yes! Simply use a bit mask. I enumerate the bits, then AND the number with the bit value. Very little math on the PC side as it uses lookup tables instead. The AND basically shuts off all the other bits except the one you are interested in. Then you check it against itself to see if it's on/off.
Enum validate
bit1 = 1
bit2 = 2
bit3 = 4
bit4 = 8
bit5 = 16
bit6 = 32
bit7 = 64
bit8 = 128
End Enum
If num And validate.bit3 = validate.bit3 Then true

Congruent integers and modulus

I'm new to the topic here :/
Could anyone please tell me how to solve the following?
Show that 36^2004 + 17^768 x 27^412 is divisible by 19.
Thanks!
Simple identities can be used to solve the above, the important of them being:
(a + b) mod c = a mod c + b mod c
Also,
ab mod c = (a mod c)*(b mod c)
This can be used to solve very big exponents also, for example, if you are to solve:
24^3100 mod 19
You could probably break it up as:
24^(310*100) mod 19
which can be further written as:
24^310 mod 19 x 24^100 mod 19
You can further break it down to values you could actually compute and solve. For example, if you keep on breaking down 100, you could end up solving
(24^4 mod 19)^25
and so on and so forth. Since this is a homework question, I can only provide hints and not the complete solution.
You can also do it with the fast exponentiation method where the exponent is expressed in powers of two.