 Let's go ahead and take a look at what's known as an LFSR or linear feedback shift register method for generating a pseudo random stream of bits for use. So the linear feedback shift register is a method for generating a pseudo random stream of binary that we're going to go ahead and use with our XOR cipher or exclusive or cipher that we've previously learned about. What's great about a linear feedback shift register is that this is a technique that became really popular when transistors and more modern computers were in use because instead of using software like Python, the programming language that we've been using in this course, this type of method could actually be used in hardware, meaning with the batteries and wires and processors. Those tools could generate these random or pseudo random ones and zeros. When we say pseudo random, what we mean is it's not truly random. If I had a random grouping of ones and zeros that I was going to use for my encoding my cipher, it would be impossible for the receiver to generate that same random bits in order to decipher the message. That's why we say pseudo random on the first appearance, they're going to look random, but some initial settings and some rules that we're going to use are going to allow for the recipient of the message to generate the same random looking groups of ones and zeros so they can decode our message. To learn how a linear feedback shift register works, we're going to perform the following thought experiment here. Let's say we've got four people here and they're kind of imitators, copycats. They are going to decide every day of the week whether or not they're going to wear a hat, but instead of just deciding for themselves, they're going to base their decisions on what other people in this group did the previous day. So we've got our four people, we've got our woman with the brown hair, we've got our blonde man with the mustache, we've got the person with the white hair and glasses, and then we've got the woman with the bow and her hair. And they're going to act according to the rules dictated here on the left. So the blonde man, he's going to copy whatever the woman with the brown hair did the previous day. So if on day one the woman with the brown hair wore a hat, on day two the blonde man would wear a hat. If on day one the woman with the brown hair did not wear a hat, the man with the blonde hair would not wear a hat also. Same rules applies with the man with the white hair and glasses, he's going to copy whatever the blonde man did the day before. And then the woman with the bow and her hair, same rules, she's going to copy whatever the man with the white hair and glasses did the day before. The only person in this group that's going to play by a different set of rules is the woman with the brown hair. She's going to look at both what the man with the white hair and glasses did and the woman with the bow, and if only one of them wore a hat the day before, then she'll wear a hat the next day. If neither of them wore a hat or both of them wore a hat the day before, she will not wear a hat on the next day. So that's the rules of our system. Again, this might seem as an odd way to start off thinking about how to generate ones and zeros in a pseudo-random way, but as we kind of keep track of what happens in this system, we're going to see that the result is something that we could use to generate a pseudo-random stream of bits. So we're going to need an initial state for the system. We'll start with the man with the white hair and glasses wearing our fancy little top hat there. And we're going to keep track in our table whether people are wearing hats or not with ones and zeros. So since he's the only person who's wearing a hat, we'll put a one in that row and everybody else will have a zero. And if we try and predict what's going to happen on day two, we can look at the rules and kind of sort that out. The man with the blonde hair, he'll look to the left, he copies what she's doing. So on day one, she had a zero. On day two, this man will have a zero. The man with the white hair and glasses will look to his left and see no hat. So he's going to switch it up tomorrow. He's wearing a hat now but since the man with the blonde hair to his left is not wearing a hat, tomorrow he's going to copy him and not wear a hat. The one with the bow in her hair, she'll look to her left and see that he's wearing a hat. So tomorrow she'll wear a hat. And then our complicated person here on the left, she's going to look at both the two people on the right most columns and since exactly one of them is wearing a hat, tomorrow she will wear a hat. So filling in the table with that information and updating our hats and our diagram. We can see that now the first and the last person are wearing hats in the middle two are not. And we can sort through and let this run a few iterations. So we're on day three, day four, day five. Feel free to pause at any moment and make sure that you can predict from one day to the next. So you're understanding the rules of the system. And when we get down to day 12, we'll go ahead and stop and look at what's going on here. And there's certainly a few patterns that we can see are taking place. We can see this shifting of bits happen from left to right. So on day one, in the leftmost column, we had a zero. And then on day two, in the column to the right, there was a zero. And then a column to the right on day three was a zero. And a column to the right, there's a zero for day four. And then the same thing happens everywhere in the table. And that's where the shifting part of the linear feedback shift register comes in, is that these things, these bits shift from left to right, starting with the person on the far left. And that kind of makes sense if you think about the rules of the situation, is that everybody's copying the person on their left. So as soon as this person on the far left makes a decision on whether or not they're going to wear a hat, that has implications that shift down the timeline to future people to the right. The feedback component of the linear feedback shift register comes from that. There's information in the system that goes back into the beginning of the system to kind of restart what's happening on the leftmost column. So the person on the left takes the information from the two columns on the right, makes a decision, and that information gets fed back to that person's decision. Now, where are we going to get our random stream of ones and zeros? We're going to take a look at that last column. If we were to just look at the numbers in that last column, zero, one, zero, one, zero, one, zero, one, one, one, and you knew nothing about this situation in the rules at play, that would probably look random to you. Even though there's a nice deterministic system that created that. So if all I did was put this up on the screen, zero, one, zero, zero, one, one, one, zero, one, one, one, that would be a great bit stream for us to use as part of our XOR cipher. Because if somebody else were to have the exact same initial value, the seed, and the same rules, they would be able to recreate the stream of ones and zeros, but only if they had that information. So in this system, the seed value and the rules and the number of bits are what determine a particular pseudo random bit stream. Those are like our keys for the system. So how could we kind of go from this game of swapping around hats to something maybe a little bit more mathematical or something that you would actually imagine a computer could do? So we're going to take this exact same system, but maybe abstract it away a little bit. This diagram in this rule system is actually communicating the exact same information. We've got four bits of information, which are analogous to our four people that are going to be wearing hats or not. We're going to put in these boxes, a particular state of the system. So here's our seed value, 0010. These arrows are dictating which bits go into making the decision that gets fed back into the beginning of the system at bit four. So you can see in this case, one and zero are going to go into the XOR symbol. You might have picked up on that when we were going through the exercise with the hat, but the leftmost bit is essentially an XOR of the rightmost two bits. So in this case, one XORed with zero is going to give us our one. So the next state of the system, we have a one in bit four. The rules of the system dictated here on the left, that the writing for this is simply stating that bit one, its value was determined using the previous state of bit two. That's what that little apostrophe or we'll say prime value denotes, is that the previous state of bit two was passed over to the current state of bit one. The previous state of bit three was what we got passed into bit two and so on. The last one here is the most important, oftentimes it'll be the only one that actually gets written down, is that bit four, the furthest left bit, that's determined by the sum of the previous states of bit one and bit two. So it's the same thing as showing these two lines going into the XOR. Even though we actually are doing an XOR, it's kind of tradition or common that we don't use the XOR symbol when showing that bit wise addition here. It would certainly be correct if you wrote it, it's just not not as common. So to see another one of these systems in play, here's another example. So let's maybe start with our diagram. We've got a five bit system. The seed value is one zero one one zero, we can see that in there. And then we have three bits that are being XOR together to determine what bit five is going to be on the next go around. So we could write that as a definition, if we only need to show the last one, is that bit five is determined as the previous states of bits one two and four summed together. If we were to organize that in a table, which you see over to the right, we can follow the same process we did with the hat game. And then highlighting the last column, the bit number one, that's what's going to be used for our bit stream. So we would end up with zero one one zero one one one zero, as written out down here below. So that's what's going on with a linear feedback shift register. And that's how we can use this system with this set of rules to create a pseudo random meaning a recreatable random looking stream of ones and zeros that can be used with the XOR cipher.