We have Matt with prototyping cryptographic protocols in Python with charm. Thank you So What is charm why they use that word? So charm is actually the name of a Python framework that came out of jhu it uses a mix of Python and C and It's used for prototyping cryptographic protocols so in this talk, I'm gonna go over a number of cryptographic primitives and I Intend to demo how some of them work in charm just so you can get idea of how you could play with some of this yourself and Some of these will be ones you're familiar with perhaps some not so much. So Let's start get started with that. All right, so some of the Basic primitives in charm are block ciphers and math so So as far as block ciphers are concerned They include DS triple des and a yes It does also have stream ciphers. I just didn't really look too much into them, but It's gonna be a lot of back-and-forth the microphone Okay, so I I just put basically as a a Stretch pad here a bunch of Python code that we can use to demo some of this. So I'm gonna Explain a little bit about what this is. So first I'm just going to just convince you that We it does support a yes encryption and decryption Here I'm just doing an example of CBC mode and when you create a new AES object The first argument is the key. The second is the mode and the third is the IV as applicable so You know submarine was it's a bit of a shout out to the medicine challenges They like to have a lot of music references and their challenges and that was their favorite AES key apparently, so It's kind of my favorite AES key not in production, but just Like it's a convenient one. For examples. It's happens to work out to be 16 characters. So that's nice All right, so I'm gonna jump into the console And I'm just going to restart it real quick and Okay, so, hold on one moment. Well, I guess that would be nice. It's intended for questions. I'm not sure if, yeah. Fred, what do you think about this? Yeah, okay. Yeah, it makes your life easier. Yeah. This whole story. I was, when I first saw the setup here, I was like, no, I don't understand. Somebody's like, do you know how to turn these off? I don't know how to turn these off. You do that one on, I'll grab it. Excellent. That's that hacker right there. Yep. Thank you. All right. That's a lot better. All right. Okay, so, first I'm just gonna copy some imports. Or you know what? It might be just faster for me to type. So, from, so the structure of Charm is that as a, some of the core stuff is under core. And then Crypto, AES, and we'll import the AES class. Oh, oopsies. What did I miss? Oh, Crypto, ha. Thank you. Hmm. Maybe it's a, it should be. I swear this was just working. Hmm. Let's see. So. Well, actually. Let me just make sure that, oh, no, wait, it's not the one. Okay. I know this is the first time I've done this. Let me just make sure that we can do some of the other ones, too, so it would be a real shame if the other demos don't work, okay, so, I'll do it, all right, there we go, okay, so, at least that one works, all right, so, perhaps, you'll take my word for it that AES works properly, as you can see, also, we have to import padding, so, it's not going to just assume what padding you want, it has PKCS7 padding included, so, if you just look at the code, we'll create a new AES object, we create a PKCS7 padding object, and then we can encrypt, you know, sample messages, and one thing to know, also, this is fairly common, I've noticed, with most implementations in software is that any time you want to decrypt, if you're doing both encryption and decryption in the same process, you need to create a separate instance of the object, so, you notice that before we decrypt, I create a new one with the same key and IV, and, yeah, trust me, it works, all right, so, move back to the slides a little bit, okay, so, as far as math is concerned, some core mathematical objects are integer groups, elliptic curve groups, and pairing groups, so, for integer groups, there are Schnorr groups and RSA groups. Schnorr groups are groups that are used for discrete log-based cryptosystems, they have the property of being a multiplicative group with a large, of a large prime order, so, it's a large prime order subgroup of a prime field, and RSA groups are the groups that use their RSA, and not just RSA, there are other cryptosystems that we'll see that use this same group. So, we already got RSA group here, and so, you can create an instance just by calling RSA group, and then it will allow you to just generate a new one, so, there's a pram gen method, and say, 1024, And that's just outputting the prime factors that make up the modulus. And the 1024 in this case is the size of the primes, not the final modulus. So this will be a 2048 modulus. And all the groups support getting random values. So you can do group.random. And you can multiply. Oh, I think it's the other side. No, maybe not. The other groups do. Okay. Anyway. Oops. Okay. So the elliptic curves, so both elliptic curves and pairing groups are, well, pairing groups are elliptic curves. But these elliptic curves are more the standard curves that, I think they include the standard NIST curves. I don't recall which ones they, other ones they include. These two files that I just mentioned are just put out, pointed out here is because this is how they split up their elliptic curve implementation. So the EC curve dot pi just specifies which curve it is. So it has just a huge list of variables or, so, for example, prime 256 V1 is, I believe, the NIST 256 bit prime P256. And EC group will implement all the group operations. So this is a very simple way to do this. So if you want to do this, if you want to do the group operations, or at least provide the interface to the group operations, everything should be implemented in C. So. We'll get the toolbox. And then we'll get the interface. And then we'll get the interface. And then we'll get the interface. And then we'll get the interface. And then we'll get the interface. And then we'll get the interface. And then we'll get the interface. And then we'll get the where we are going. So first import from EC curves, a prime 256 V1. And from EC group we'll get the EC group class. And then we'll have our group equals. And then we'll also group of prime 256 V1. And we can check things like group order. So that's how big the curve is. And we can get a random point P. And this one I know we can't multiply. So we can do point multiplication. We can add points. So if we make another random point Q. Then we can compute P plus Q. So we can do that. So I'm talking mostly about these math basics. Just because, so the whole idea here is if at any point you find out about some cryptosystem that you don't know of an existing implementation, you want to start working with it. So if you're, this is a more lower level one where you want to implement maybe some new scheme that uses elliptic curves. You can, and I misspelled pairing. I'm sorry. It's a good idea. So I'm going to go back to the elliptic curves. You can use elliptic curves or pairing curves. Was anybody here for the Pythia talk yesterday by any chance? Yeah. So I suspect that, I haven't actually tested this, but I suspect that it would be possible to implement that scheme using charm. Because you have access to pairings and from what little I could see all you need are hashes and elliptic curve pairings. But I could be wrong about that. Okay. So that's, that's all like basic, all the basics. Now we're going to get into what they call schemes. So we'll talk about a few of these. The first being public key cryptography. So we've got RSA and PyA. How many of you are familiar with PyA? Anybody? Okay. So PyA is used as an RSA group, but it's not a public key cryptography. It's a private key cryptography. But it has a neat property that the private key trapdoors discrete log. So you can have an additively homomorphic scheme and actually get back what the exponent is. So when you use the PyA class, PyA class, you can encrypt values of numbers and add them together. Or multiply them by scalars. So let's take a look at that. Okay. So. We'll get an import from charm.schemes. It's under public key encryption and then PKinc.py.99. It'll import pi99. And if we we call, we'll call lower case pi, pi 99. Oh, right. I forgot. So we need the RSA group object. So we'll just create a new one here. And then we can do key generation. It'll give us both a public key and a private key. And we'll specify our security parameter, which is just bit size. And so we have our public key, which you can see, so it's a generator G and modules N and then all of the other stuff. So we can see that it's a generator G and modules N and that also has N squared. That's what the N2 is. And private key is this lambda in U value, which allows us for doing the trap door of discrete log. And so if we want to encrypt the value, we'll, let's see, we'll do M1 equals, so I just, I have, those are just random digits in the little scratch file up there, so I'm just going to do a few more random digits. So I'm just going to do a few more random digits. So I'm going to do a few more digits. Like that. M2 be other random digits. M3 is M1 plus M2. And now we'll encrypt each one. The encrypt method requires the public key and the message you want to encrypt. And now I'll show you so we can do C3 equals C1 plus C2. So it lets you add cipher text. So cipher text structure is just a dictionary with the key being C and the value is just a long integer. And if we then decrypt it, yeah. So pi.decrypt, give it the public key, private key, and C3, I'll show that that's equal to M3. So you can see how this operation really is preserved through the encryption. And this can be quite useful in a number of schemes. So I think yesterday there were multiple talks that talked about homomorphic encryption. I plan on at the end of this one talking about a protocol that I worked on with another team that did secure text pattern matching using additively homomorphic encryption. And one of the cryptosystems they used was PiA. Okay. Oh, right. So this is a micro tymography, right. And I'll also demonstrate that in addition to adding you can also multiply by plain numbers. So it will let you, you know, do whatever times C3. Oh. Right. This is the one where I remember you have to put the number on the right side. There you go. So I mean all this really means is that you can do linear algebra, you can do matrix multiplication or vector math using encrypted values and unencrypted values. Okay so the next thing I'm just going to mention are commitment schemes. So commitment schemes allow for zero-knowledge proof. So this allows you to say I'm going to commit to this value but I'm not going to show you what it is first. So it has first binding property and hiding. So binding just means that I can't take back that that value was from me and hiding means that you won't know what the value was until I open the commitment. And they have, you know, they have support in here for Pedersen commitments. So this is just a quick overview of how Pedersen commitments work. Basically the way that Pedersen commitment works is that you're going to do algorithm encryption and then when you open the commitment, you just give them the private key and that allows them to decrypt it and verify that you give them the key and the encryption. So when you're doing this you give them the key and the encryption. And to message and you can show that they're the same thing. Okay. All right. So I'll talk a little bit about the protocol class. I'm going to go back and actually just show a little bit how it's structured. So one of the nice things about charm is that if you have a protocol that involves communication between two parties, then you can just write this in terms of methods that call, just return back values that would get sent back and forth. And it maintains the state machine of where you are in the protocol and handles for you the transport portion. So sending and receiving of data. And this also can be used as a sub-protocol. So you can, if you have a protocol that's contained in the protocol, you can have other protocols within your main protocol and refer back to what you already have contained in the bigger protocol. So let's, I'll show the, I'm just going to, actually, I'm going to get out of the presentation real quick for just a second. Okay. So the first, the init method just sets up all the sort of the common parameters that both sides of the protocol are going to have. And then in particular, it has what kind of, what are the party types. So are you going to refer to it as a sender, a receiver, or client and server. And the, there's a state machine that you set up in your init method. And this self.db just allows you to store data between different parts of the different method calls. The setup is all the information that tells it which instance it is, whether it's the server or the client. And this add instance is just used in the initialization method. And, yeah, it just has a variety of different useful methods that you can call to keep track of state, get state, and different points. Send messages and receive messages. So this is one thing that is, was not entirely clear to me when I was first using it. And it takes a bit of work. This is probably the hardest part of actually using this protocol class is that it's not necessarily the easiest thing to do. So when you initialize the class, you can pass it a socket, either a server socket or a client socket, and then it will transmit the data. But, you know, Python objects aren't just raw binary. You need to serialize and deserialize it. And it turns out that oftentimes it isn't smart enough to just serialize and deserialize all the objects it knows about. So you have to implement some serialization and deserialization in the protocol to get data back and forth. But once you have the appropriate methods and it just looks like passing data back and forth, one stage in the protocol will just be, will give you a return value and that will end up being the input to the next step in the protocol on the other side, just transparently. All right. So one of the first things that we did was we took a lot of data and we took a lot of data from the first types of protocols I'm going to talk about that is used is called sigma protocols. And so sigma protocols, they're called sigma protocols because of this three part structure that looks like the, in cryptographers' minds I guess, looks like the lines of a sigma, a great capital sigma. And so there are a number of these proofs where you want to show that you know something about a public value without revealing that information. And it follows this general structure of the prover sends some message A, the verifier sends back some string E, and then the prover sends a Z value and the verifier will either accept or reject the proof based on the values of X, A, E, and Z. And so I'll give you a little bit of an example here. So the first one is, you know, this is a discrete log based sigma protocol. So how can I prove to you that I know discrete log of a given group element and a given base without revealing that discrete log. And so here what we do is we start with a random exponentiation of our generator G and that's our A value. We send that back. The sends back an E value. So this 2 to the T less than size of G, that just means it's just some random value in the size of the group that we're working with. And then the prover sends back Z equals R plus EW. And if you look, since A is G to the R and H was originally G to the W, and the verifier is going to compute H to the E power, when you multiply all of this together, that should be the same thing as what's on the left side, that G to the Z value. So this allows you to hide the information about the original exponent in the new exponent. So I'm not going to quite show that because the sigma protocols aren't in charm already, aren't really, weren't implemented in the past. They were implemented with the intention of being independent protocols. It turns out that they use those for a different protocol called oblivious transfer. And so we'll talk a little bit about oblivious transfer. So the idea here is that you have, say a server has multiple messages and a client might want to know what one of those messages is, but they don't want to tell you what that message is and the server doesn't want to reveal the, all the, any other message besides the one the client's asking for. And this I think is a very interesting privacy preserving protocol. You can imagine a number of situations where you want to verify some information about, say, some domain, but you don't want to tell everybody that you want to check out this domain. It might be one that, you know, people are like, why are you looking there? So using a oblivious transfer is at least one way that I thought of that you could keep the privacy of this inquiry while still being able to satisfy these security considerations of doing validation on a domain. So this text just basically says what I was trying to say that, you know, in the original definition, it was just two messages. It's since been expanded to, you can have N messages and allow for, you know, up to K requests. And so I'll show you that they implemented what was called an adaptive oblivious transfer protocol in here. And it will demonstrate a little bit of how the protocol class is used, how it's subclassed in Python. So this is the protocol class. Okay. So you can see the first couple of lines under the protocol.net are talking about what I was saying before about how the protocol class maintains a state machine. So the way this works is it's a state machine. So it's a state machine. So the way this works is just by having a dictionary that maps which state you are, which is just a number, to the method that you have to call for the next part of the protocol. And, you know, typically it just goes in order one, two, three, four. But as you can see, I guess, like, the sender has the option of going from three to back to three or three to five. So the dictionary below, these are the transitions. So these basically are the nodes in your state machine. These are the transitions between them. And here's where we add the different types of parties, the receiver and sender. You'll notice a lot in charm a lot of these protocols use pairing groups. So we use a pairing group here. And then we have a set of methods. And then most of the, so these methods right here are actually ones I added because they didn't exist at the time. So when I tried first running this, I found that it would just crash. I was like, why, it was saying key. So what would happen is that when, because the serialization methods were not actually occurring, it was not actually sending and receiving data and then it would move on and try to access data that it didn't actually have. So, I had to go through and serialize the data. So more or less, what should work is you can pick up the data so you can use loads and dumps. However, there are a number of the classes that they have in here, pickle can't serialize. one of them being parent groups, the integer groups are also ones that just can't pickle. But those objects have their own serialization methods. So you can just call the serialized method on those and then it's just a byte string and you can pickle that. And then you can see that so in this case the first step in the protocol for the sender, that's the one that has all the messages is to set up these values and when a receiver first initiates contact, it's going to say, okay let's run sigma protocol 1. They structure all the sender methods together. So you'll see there's another one that involves where it's going to do the proving for sigma protocol one, do sigma protocol two, all the sender methods together. So three, so on and so forth. And then down here are the receiver methods and it will create the sigma protocol class and initialize it. And I had to extract out the necessary information for me to actually construct the values that need to go into the sigma protocol. In this case, the public key. And more or less works that way. At the bottom, they did allow this to be a script, although I couldn't actually get this to run properly here. So, I actually, what I did end up doing was, mostly because in PyCharm, I haven't found a way to run the same file with different parameters at the same time. So, I just, I don't know. If anybody has any questions, does anybody know how to do that? No? Okay. So, I just basically create two files that have the same functionality and just one gets the one parameter, the other gets the other, and it implements the protocol. And so. Locks? Oh, okay. So, yeah. So, I mean, I'm not exactly sure. So, I didn't end up doing that. So, what I did was I created two separate files. So, there's, this one's called oblivious transfer receiver and oblivious transfer sender. And in this case, the receiver acts as a server. So, let's hope this one actually works. Did work earlier today. So, it's operating a receiver and then, we'll do the sender. And, wow, that went fast. So, it appears to have gone through all the steps and, oh, that's the sender. The receiver said, yep, it looks like the protocol completed. So, this is, with a little bit of work of implementing the serialization and deserialization methods, this protocol is basically working. So, I'm going to go ahead and get ready out of the box in charm. All right. So, I'm going to now actually talk about that protocol I mentioned earlier. Also, I think it's pretty neat. And I think it would lend itself well to prototyping in charm here. So, when I actually did this, this was a while ago, about 7 years ago. We did the entire plus plus. But we looked into a Python based, a Python derivative called tasty L. And tasty stood for something about multi-party computation I think. I mentioned it in my talk last year. So I forgot what it stands for off the top of my head. But I think we always had problems getting it to actually work and compile. This should actually work. I haven't fully finished getting it to work. But I'm going to just quickly explain how the protocol works. So this, what we're trying to do here is we have a server that has some body of text. And you want to run a query on some sort of wild card based expression. So you can think of like grep with, without, you know, you can think of it as a, you know, you can think of it as a regular expression without allowing for multiple characters. Like you need to know exactly how many characters you're looking for. But you can, you can in principle ask for, say I want a pattern where like the first character is A through F and the second character is, you know, B through G or something like that. And then maybe the rest of them are all fixed characters. And or maybe, you know, you'll have just a like, it can be a little bit of a wild card star character. And this protocol should work. So the idea here is that what you're going to do is for each letter in your pattern, you're going to construct what we call, so this is CDV, a character delay vector. The idea is that let's assume that if we find that letter in the text we're searching, that it's part of our pattern, then how far away would we have to go until we reach the end of the pattern. So then we're going to say, we're going to go that much further and put a one there. And in the end, we should end up with a total of, like say for this pattern of TACT, we'll get a four in the location where the pattern is found. So I'll just go through real quick explaining this. So when you construct the CDV, you'll see, you first have a T here. That corresponds with this one here. The one at the end. Because that's saying that if this is that T, then we have to wait three more characters before we get to our pattern. And then if you get to see an A, that means, okay, if that was part of the pattern, then we're going to weight 3 more We have to wait two more characters. I'll be over here. The C corresponds to this one, and this T corresponds to this one right here. That's saying we're there, right? If the T we're on is that one, we're done. And then what you do is you start off with what we call an activation vector, and we go through each letter in the text and look at the corresponding character delay vector and add that chunk to the vector and keep moving forward. And then shift one over. So G had an all zero vector, so nothing happens here. The A has a 0, 0, 1, 0, so we get a one over here. There's this T, another T here, A, C, T. And what happens is... See, this one, as I said, corresponded to the end of the pattern, and it landed right here where that's the length of the pattern you want. If you want a wildcard situation, then you actually do that by saying, I'm going to look for one less. So say instead of looking for tact, I just wanted T, A, and then it could be anything, A, C, G, and T. Then instead of looking for... If I were looking for a four, I'd look for a three. And this actually comes from what I think are called time-delayed neural networks. That's where this terminology comes from, of activation vectors. So the whole idea being that the activation vector is how much time is delayed between when it sees it and when this neuron fires. But my colleagues realized, well, we could sort of borrow that concept and apply that. We could apply it here in this way. So in this protocol, what we do is we use an additively homomorphic encryption scheme. It can be pi A. There's an additive elgamol or an elliptic curve elgamol. And one of the things that's nice about this protocol is we make a few tweaks to allow us to use those bottom two. And that is... Instead of actually looking for four, what we do is we subtract by the four that we're looking for in every single spot. And that means we're really looking for zero. So what happens in our secure version is we do all this homomorphically. So what the server will do is it will generate this initial activation vector by creating an encryption of all zeros. And they'll all look the same. They'll all be different, because it's a probabilistic encryption scheme. And then the query that you send is going to be encryptions of all these values. So these are all going to be encrypted values. And the server won't be able to tell whether this is a zero or one. But because of the homomorphic nature of it, you can add and effectively subtract. And so we'll get back down here where this one will be zero. And then this protocol, the secure text pattern matching, adds a few extra steps to make sure that we don't leak any additional information. So what we also do is we multiply by random values. So anything else will become random except for the zeros, because anything times zero is zero. And then you can also shuffle around the values. So ultimately what you get is... So you can find out how many times that pattern showed up in the text, but not where. And you don't learn anything about... So in this protocol right here, you can learn that... You could get some information about how far off the text was at each location from your pattern. But by doing this... Subtracting by four is just to allow us to do a blinding step of just multiplying. That destroys a lot of the data. It destroys the information of the other ones. And then the shuffling just eliminates the information about where you found it. So I was hoping to actually demo this in Charm, but those serialization issues have been dogging me. So I don't quite have it working right now. But I can show you basically the structure of it. So... Okay. So... I wasn't sure if there was any way for me to just have one step on the server. Because really there's only one step. Each step is a... Between communications. So we'll either receive information and then send or do everything inside that. So... Since... Okay. All that happens is the server receives an encrypted request and then returns back a encrypted reply. There's really only one step here. But I put basically a dummy step here that just returns none. There. But... Okay. So... Go back up to the... Okay. And we'll see what happens. So... So here, you know, I... Again, have my states, my transitions, add parties, do all that kind of stuff. If you notice the... In the diagram I had before, it was using an alphabet of A, C, G, and T. And that was because one of the original applications of this protocol was for privacy preserving searches of DNA. So I think one of the applications they were saying is... Okay. One of the things that they suggested was you might have some researchers who know some gene mutations that make you more prone to cancer. You'd like to know, hey, am I more likely to have cancer? But you don't want other people to know what the answer is, right? Because that could mess with your insurance or anything like that. So you get your genes sequenced. You encrypt the... You encrypt your DNA in this special way, send the response back, and then you can see if... If the appropriate genes match any of the patterns they have. So, in this case, I just tried to keep it to a relatively small alphabet of just ASCII letters, digits, some white space and punctuation, but also allow you to specify the alphabet if you want. And the NIP method, you know, has two other parameters in it. One is a text file, so that's the text that's on the server side and the query. So only one... Only the server is going to fill in that one and only the client is going to fill in the query parameter there. And then I just have a few helper methods here. This one constructs the character delay vectors. This one does the encryption. This does the searching on the server side. So this does all the homomorphic encryption or homomorphic operations. And it does, you know, the blinding and the shuffling. And then in the actual... For the actual steps of the protocol, the client, you know, just first generates a key. It constructs the character delay vector and sends the... Both the character... All of the... The character delay vector, the pattern length and the public key. And then what it expects back is an encrypted activation vector and it just does a search through that to see are any of those... Do any of those values decrypt to zero? And if it does, it'll print pattern found. Otherwise, pattern not found. So, unfortunately, my demo's not quite working. So I'll basically leave it there. I think we have a few minutes for questions if anybody has any. But other than that, I think that's all I've got. So, thank you.