Initiation Of Cryptology Thanks To A Scilab™ Project Using Perfect Magic Shuffles

Why use Magic for teaching


Introduction
I am fascinated by Magic (or rather conjuring!) and for many years I have used this way of teaching, both in my physics classes and as higher education teacher trainer.I present all the notions in this paper, the researchers who have used Magic to teach and/or to research and finally I present my contributions.
The magician aim is to hide the principles he uses (using sleight of hand, Psychology, Maths, Physics, etc...) by disguising the trick.It would be great that the audience has no way of discovering how it is done thus allowing the Magic to remain.

The teacher can do exactly the opposite: unraveling a Magic trick to highlight the principles used!
I had the idea to use magic shuffles to explain the basis of cryptology in 2016.I presented it during a conference in INSA only for their students [1].
In this article I present the main notions of cryptology as message representation, encrypt function and decrypt function.I discuss about the integrity of the cryptology.In fact, these encrypt/decrypt method is based on the secret of algorithm.We know that this method can no more use for real application.What it is explained can be teach either with a top-bottom method or with a bottom-top method as project pedagogy.All the figures come from Scilab programs.
To be understable by students, I use playing cards and magic shuffles !• the message is represented by playing cards (as Hearts, Spades, Diamonds and Clubs), • the encrypt function is the function which describes the Faro shuffle using in many cards trick, • the decrypt function is not the inverse of the encrypt function but we use a magic property of this shuffle, i.e the idempotency.
The students know the magic trick secret and like that they are able to "perform" a magic representation for their friends and family.After that, if one (or more !)spectator wants to know the secret, they would be able to explain and so enjoy a certain success amount.ergodic theory which is in the center of the probability leveling phenomenon: if the deck was shuffled for a long time, all the possible permutations have the same probability.
We shall not present this principle here but it is necessary to know that it is based on a commonly used card shuffles on both sides of the Atlantic Ocean: the American shuffle named riffle shuffle too !

The fields I teach with Magic
Naturally, the first project I gave was in optics [17] for my students in university for the engineering program.
I thought that the amazing Gilbreath principle was based on mathematics because it is a self-working trick.That's why I began to think how to teach math with magic from primary school to the higher education [18,19].Thus, after I have readen an article about shuffles [20], and I wrote with the author a demonstration of Gilbreath's principle [21].
I think that all areas can be teached with magic but not all knowledge [24] !The aim of this paper is to introduce by magic way a part of cryptography notions.

Summary
Imagine that a secret agent 'A' must send an important message to another secret agent 'B'.This message is: "Japan Journal of Research".

''The agent A must naturaly encrypt the original message and the agent B must decrypt it ! ''
I present the main cryptography notions : • the representation of the message (here with playing cards but normally it is done with binary), • the encrypt phase of the message by the agent A thanks to an algorithm -which is based on cards shuffles, • the decrypt phase of the message by the agent B, by the same algorithm -and not with the inverse encrypt function, The figure 1 represents a simplified block diagram of En/ Decryption with an classic example.
Or you can teach with an up-bottom pedagogy (but you have to programming before !) either with a bottom-up pedagogy by giving a project under Scilab or Java for example.

Letters representation : 26 cards for 26 letters
The aim of the project is to crypt a message constituted by latin alphabetical letters.Let us note that 26 letters are used and 26 is equal to 2*13 (or 2*52/4).Therefore 2 whole card families are necessary to encode all of the letters but only two families!4 families are usually used in a 52 card deck.One of the 6 possibilities must be chosen.The figure 2 shows the used solution.Let us take the following message "Japan Journal of Research" as the sending message.The message representation thanks playing cards is shown in figure 3.

Informatics card deck representation
Each playing card could be represented by a number between 1 and N where N is the total card number in the deck.The figure 4 shows the case where N is equal to 52.Therefore each card is numbered from 1 to 52. on figures 8 and 9.
Let's say that the card which has the ith position will be at the jth position after a IN-Faro.There is a bijection noted f defined as follows : where ε(i)=0 if i is even and ε(i)=1 if i is odd.The reciprocal permutation is given in appendix A. The figure 5 shows one of thousand possibility of informatics representation of a 52 playing card deck.For example, the 10 of Diamond is represented by the number 30, the ace of Heart by the number 1 and the ace of spade by the number 52.An another way to represent the cards could be to give a number between 1 (for the Ace) and 13 (for the King).Therefore the first hundred would be for the Heart, the second hundred for the Spade, the third hundred for the Diamond and the fourth hundred for the Club.So the 10 of Diamond is represented by the number 310.

The Faro shuffle : definition and usefull propertie for a cryptography process
Returning to our example described above : sending the following message " Japan Journal of Research".
The Faro shuffle has many properties that have been widely studied as much by magicians and mathematicians.I made a rather informatic study described in [19].In [20], the study is discussed under a mathematical formulation showed in annex A.
I present a mathematical modelisation of the FARO shuffle.

The IN-Faro and OUT-Faro shuffle definition
The card deck -numbered from the bottom to the up-in the order 1, 2, 3, …, 2n is cut in the middle.The first half subdeck is numbered from 1 to n and the second is numbered from (n+1) to 2n.A new 2n card deck is created by taking alternately the first card of each subdeck.
Initially, the 52 card deck is in the following order (presented on figure 4).Each card is numbered from1 to 52: By cutting the deck between the cards 26 and 27 we obtain 2 subdeck (presented on figure 6).Its modelisation is shown in figure 7.

The IN-Faro ( in-shuffle)
If the first card (from the bottom) is taken of the second subdeck (in-shuffle), the new 2n card deck is numbered as follows: n+1, 1, n+2, 2, n+3, 3, … , 2n-1, n-1, 2n, n -as presented    The OUT-Faro (the out-shuffle) If the first card (from the bottom) is taken of the first subdeck (out-shuffle), the new 2n card deck is numbered as follows : 1, n+1, 2, n+2, 3, n+3, … , n-1, 2n-1, n, 2n -as presented on figures 10 and 11.Let's say that the card which has the i th position will be at the j th position after a OUT-Faro.There is a bijection noted g defined as follows : The reciprocal permutation is given in appendix B.

An interesting property of the Faro shuffle: its idempotency
Considering a deck of n=2p cards.After a Faro shuffle we determine if the card's order is the same as before.If it is not, we repeat the process until it's done.
We say that the function φ is N-times idempotent if : Figure 12 shows the number of necessary shuffles needed in order to a card deck return to its initial position versus the number of card deck.It is interesting to note that: • for n=2k, the number of OUT-Faro shuffle is exactly k, • the number of IN-Faro shuffle for a deck of n cards is the same as the OUT-Faro shuffle for a deck of (n+2) cards, • the number of Faro shuffle to do is never greater than the number of cards in the deck.
In step 2, I present the message encryption phases of the message done by agent A. In step 3, I present the agent B's decryption phase.

Step 2 : Encrypting phase made by the secret agent A
The message contains 25 characters.But to do a Faro shuffle, you need an even number of cards.That's why we add a space at the end of the message.So the message to encrypt contain now 26 characters, as shown on figure 3.
Thanks to figure 12, 18 IN-Faro (or 20 OUT-Faro) are needed in order to the deck returns to its initial order.Let's take the fewest number of shuffle, i.e : 18 IN-Faro.Mathematically, it is written so : (18)  (4) To get the number of Faro to do in order to encrypt the message, the total number of Faro N has to be divided by two.If the result is not an integer, only the integer part of the division is used : in this case, the deck is shuffling 9 times to encrypt the initial message.The following figures (13 and 14) are respectively the result of the first and the 9 th IN-Faro shuffles.
The final shuffled deck is given by the secret agent A to the secret agent B.  Mathematically the encrypt function can be written so : Step 3 : Decrypting phase made by the secret agent B The secret agent B counts the number of cards which gives him information on the total number of Faro to perform (and if it is IN or OUT-Faro!).Therefore he knows how many shuffles he has to do to decrypt the message (here 9 IN-Faro has to be done).The following figures (15, 16 and 3) show the first, the 8 th and the 9 th IN-Faro shuffle from the encrypt message shown on figure 14.Mathematically the decrypt function can be written so : (9)  (6)

Synthesis, conclusion, going further and prospects
Before I used magic to teach many different fields of knowledge : math, optics, electronics.This time, it is to introduce many elements of the cryptology chain.
I think that the objectives are reached and different function representing different shuffles can be used too.
But a discussion about security of this kind of encrypt function must be done.

Synthesis
We have described : • the message to send is represented by playing cards (as Hearts, Spades, Diamonds and Clubs), • the encrypt function is based on the function which describes the Faro shuffle using in many cards trick, • the decrypt function is not the inverse of the encrypt function but we use a magic property of this shuffle, i.e the idempotency.

Conclusion : the security of this encrypt function
Nowdays the algorithm based on card shuffle cannot be used as a encrypt function because it is unsecure!In fact, if the cryptography process is based on the secret of the algorithm so one day somebody will discover the algorithm!

Going further and prospects
To secure this cryptography chain, a private key can be introduced.So the figure 1 becomes the figure 17: In reality With the magic method

Binary with ASCII code
Playing cards

Encrypt function E
Many algorithms exist The function representing a real card shuffle (f or g) is used N times depending the message length.

Decrypt function D
Usually D=E-1 The same function (f or g) but the iteration number M can be changed To be sure that the received message is the well encryption message, the Hamming code can be used too.
So the ith card is the card initially numbered (i/2) if i is even and ((i+1)/2+n) if i is odd.This relation defines a permutation named f of integers 1, 2, 3, … , 2n that gives the new card place of the initially ith card after shuffling : This equation can be written as follows, where ε(i)=0 if i is even and ε(i)=1 if i is odd: The reciprocal permutation is simpler to write: We note the definition of the arithmetic congruence: a=b[n] means 'a-b is divisible by n'.This notation with congruence is very useful to determine the f-period.
For example, the card numbered 1 will be at the place numbered 2, the card numbered 4 will be at the place 8, , the card numbered n will be at the place 2n, the card numbered (n+1) will be at the place 1, the card numbered (n+2) will be at the place 3, and so on as presented on figure 8.
It is useful to translate the card numbering in order to obtain card position between 0 and (2n-1).So the new permutation f, named , becomes so : The new reciprocal is  -1 f so defined: Thanks to the introduced permutations, it is allowed to determine explicitly the in-shuffle period for an n=2p-1 and n=2p-1 -1 card deck.
This process defines then the permutation g of integers 1,2,3, … , 2n presented below: i n 2 g(i) i 1 2 its reciprocal permutation is : -1 2j -1 if 1 j n g (j) 2j-2n if n 1 j 2n The only cards, whose positions are unchanged by the permutation are the first and the last ones i.e. g(1)=1 and g(2n)=2n.
It is useful to translate the card numbering in order to obtain card position between 0 and (2n-1).So the new permutation f, named 1 g −  , becomes so: its reciprocal permutation is : So the 2n card deck out-shuffled is the same as the (2n-2) card deck in-shuffled (by ejecting the first and last card of the 2n card deck!).

Figure 1 .
Figure 1.Basic block diagram of En/Decryption

Figure 2 .
Figure 2. Letters representation with Heart and Spade.

Figure 3 .
Figure 3. Letters representation with Heart and Spade.

Figure 4 .
Figure 4.A numbered card deck from 1 to 52.

Figure 5 .
Figure 5.A New card deck with the bicycle TM order.

Figure 6 .
Figure 6.A New card deck with the bicycle TM order.

Figure 7 .
Figure 7. Two subdecks from a numbered n card deck -modelisation.

Figure 9 .
Figure 9.A numbered card deck from 1 to 52 deck after one IN-Faro shuffle.

Figure 11 .
Figure 11.A numbered card deck from 1 to 52 deck after one OUT-Faro shuffle.

Figure 12 .
Figure 12.Number of successive shuffles needed in order to a card deck returns to its initial position versus the number of card in the deck.

Figure 13 .
Figure 13.Encrypt message after one IN-Faro.

Figure 14 .
Figure 14.Encrypt message after nine IN-Faro : the sending message to the secret agent B.
If the first and the last card are gone away (so the numeration set is now {1, … , 2n-2}) then a new permutation is created.It's properties are the same as the in-shuffle permutation with only (2n-2) cards.

Table 1 .
Parallel of the reality and my method in the cryptology chainFigure 17.Basic block diagram of En/Decryption with private key : symmetric encryption diagram.