1 00:00:00,000 --> 00:00:02,820 Our next presentation by Kathy Yun. 2 00:00:02,820 --> 00:00:08,280 Implementing a zero-knowledge proof or how to write bullet proofs in Rust. 3 00:00:08,440 --> 00:00:10,000 Give it up. 4 00:00:21,740 --> 00:00:23,380 Hello, everyone. 5 00:00:23,380 --> 00:00:25,200 Thank you for being out here. 6 00:00:25,820 --> 00:00:29,560 To change gears a little bit, we're going to do some math together. 7 00:00:29,560 --> 00:00:31,300 It's going to be fun, hopefully. 8 00:00:31,580 --> 00:00:33,280 For some context, I'm Kathy. 9 00:00:33,280 --> 00:00:39,900 I worked with a really awesome team at Chain and Interstellar of Henry and Oleg. 10 00:00:39,900 --> 00:00:45,160 And together we implemented bullet proofs in Rust and built a bunch of protocols on top of that. 11 00:00:45,160 --> 00:00:55,220 And today I just want to share some of that journey so that hopefully you can learn, maybe, find it interesting, pick up some tips, and have fun. 12 00:00:55,380 --> 00:01:04,400 So the outline is that first I'll tell you a little bit about the story, how we came to decide on implementing bullet proofs and what tools we used. 13 00:01:04,400 --> 00:01:06,260 And then I'll look into the code. 14 00:01:06,260 --> 00:01:08,980 I'll show you a little code snippet from our range proof code. 15 00:01:08,980 --> 00:01:13,640 I'll tell you about what we were able to do because we implemented it in Rust. 16 00:01:13,680 --> 00:01:19,320 And I'll talk a little bit about what we built above and beyond using that protocol. 17 00:01:19,680 --> 00:01:22,060 So first, let's talk about motivation. 18 00:01:22,600 --> 00:01:25,140 Why do we care about zero knowledge proofs? 19 00:01:25,160 --> 00:01:35,540 Zero knowledge proofs have been around for a really long time, but they've been getting a lot of interest recently because of their application in blockchains for blockchain privacy. 20 00:01:36,080 --> 00:01:37,440 So what does this mean? 21 00:01:37,440 --> 00:01:38,980 And how is this useful? 22 00:01:39,480 --> 00:01:49,700 So in a blockchain transaction, we have, you know, some inputs, some outputs, and for a financial transaction, at least, you want to make sure that the sum of the inputs balance out with the sum of the outputs. 23 00:01:49,920 --> 00:01:54,280 So trivially, when it's not encrypted, you can do it just by adding and checking. 24 00:01:54,480 --> 00:01:55,400 So that's cool. 25 00:01:55,400 --> 00:01:59,880 But what if you want to encrypt or hide what your inputs and outputs are? 26 00:01:59,960 --> 00:02:02,360 So then it's a little bit more complicated. 27 00:02:02,580 --> 00:02:07,640 And one way that you can do this is by using an additively homomorphic commitment. 28 00:02:07,820 --> 00:02:14,580 And so in the Bulletproofs paper, and in a lot of confidential transaction formats, people use Peterson commitments. 29 00:02:14,580 --> 00:02:21,700 But basically, what this allows you to do is it allows you to check if the commitments to the underlying values add up. 30 00:02:21,700 --> 00:02:30,480 And if they do, if A plus B equals C plus D, then you know that the underlying inputs add up to be equal to the underlying outputs with very high probability. 31 00:02:31,360 --> 00:02:32,460 So that's nice. 32 00:02:32,460 --> 00:02:34,880 But as I noted, this is actually a broken scheme. 33 00:02:34,880 --> 00:02:38,860 And the reason for that is you can actually commit to negative numbers. 34 00:02:38,860 --> 00:02:53,500 And therefore, you have an integer underflow, which means that you can sort of take your $10 or Bitcoin or whatever and go spend that, even though you should only be able to spend 9 because you only had 9 in the input. 35 00:02:53,700 --> 00:02:55,260 So that's bad. 36 00:02:55,260 --> 00:02:56,840 And we don't want to let that happen. 37 00:02:56,840 --> 00:03:01,140 But we still want to use this really awesome additively homomorphic commitment scheme. 38 00:03:01,320 --> 00:03:05,980 And so we can do in this case is we can add a zero-knowledge proof. 39 00:03:06,040 --> 00:03:09,160 I guess I should explain here what a zero-knowledge proof is exactly. 40 00:03:09,200 --> 00:03:16,900 A zero-knowledge proof is a proof that a statement is true about some input without having to reveal what that input is. 41 00:03:16,900 --> 00:03:22,440 So specifically in this case, an example of a zero-knowledge proof that we can use is a range proof. 42 00:03:22,440 --> 00:03:31,000 So a range proof is a proof that a certain input number is in range that you can verify without knowing what that input number is. 43 00:03:31,320 --> 00:03:35,580 So here you can make a proof that 3 is in range without revealing that it's actually 3. 44 00:03:35,580 --> 00:03:38,540 You only reveal the commitment, which is C. 45 00:03:38,940 --> 00:03:40,220 Does that make sense? 46 00:03:40,880 --> 00:03:41,500 Cool. 47 00:03:42,400 --> 00:03:42,900 Cool. 48 00:03:42,900 --> 00:03:45,800 So you use a zero-knowledge proof, and that way you can prove that it's in range. 49 00:03:45,800 --> 00:03:51,040 And if, for instance, you're making a commitment to a negative number, this range proof would actually fail. 50 00:03:51,080 --> 00:03:55,560 So this actually makes our confidential transaction not broken anymore. 51 00:03:55,700 --> 00:04:03,520 So that's really cool, and a lot of blockchain protocols use this general scheme, and it seems to be working so far. 52 00:04:03,520 --> 00:04:06,340 And why do we care about bulletproofs in particular? 53 00:04:06,340 --> 00:04:18,460 So bulletproofs is just one of many knowledge proof protocols, but the reason that we were looking at it was because it has really nice properties that we wanted in the context of a blockchain. 54 00:04:18,940 --> 00:04:31,640 So it has... or blockchains require a pretty constrained proof size if you're using proofs, because all the nodes that are full nodes have to receive and verify the proofs, and so you have to transmit all of these to all of the nodes. 55 00:04:31,640 --> 00:04:33,180 And bulletproofs does provide that. 56 00:04:33,180 --> 00:04:37,300 All of the proofs are in O of log N, and usually less than 1 kilobyte. 57 00:04:37,660 --> 00:04:51,920 And it has really fast verification, which is important because all the full nodes have to do verification, and bulletproofs provides really fast verification, as well as the ability to aggregate multiple proofs together into one proof that verifies more quickly, 58 00:04:51,920 --> 00:04:58,800 as well as the ability to batch multiple verifications of multiple proofs to have them basically run in parallel faster. 59 00:04:59,340 --> 00:05:12,360 And lastly, a really nice-to-have property is that you can actually have ad hoc logic, so you can code up a range proof, but you can also code up multiple other proofs of different statements, and you can do this without having trusted setup. 60 00:05:12,400 --> 00:05:27,480 So for those of you who are familiar with zk-SNARKs, for instance, those need a trusted reference string, which is why you have to have the secret setup ceremony, and that's sort of annoying for setting up new logic, and bulletproofs doesn't require that, 61 00:05:27,480 --> 00:05:29,380 because they don't require pairings. 62 00:05:29,860 --> 00:05:34,160 So that's why we at Chain picked bulletproofs. 63 00:05:34,160 --> 00:05:40,320 We were building a confidential blockchain protocol at the time, and so we said, hey, let's implement this paper. 64 00:05:40,320 --> 00:05:46,880 It just came out at the time in 2017, and then the next step was to actually understand how the paper worked. 65 00:05:47,060 --> 00:05:48,520 And that sort of felt like this. 66 00:05:48,520 --> 00:05:59,360 The paper says, we would like to make a proof of these statements, and then it says, okay, here's your massive verification equation that is like 30 lines long, and just like, you know, take it and run with it. 67 00:05:59,400 --> 00:06:09,200 And so as someone who's implementing the paper, it's nice to actually understand how the math works so that you can double-check that it's right, and so we really tried hard to do that. 68 00:06:09,200 --> 00:06:24,560 We spent I think like a full month basically just like writing down, you know, theories on the whiteboard of how you go from step one to step two, being like, no, that's wrong, and trying again, and then trying again, until finally we actually arrived at something that was fairly satisfactory. 69 00:06:24,620 --> 00:06:29,180 We sent it to Benedict, who is the author of bulletproofs, and he was like, cool, that looks good. 70 00:06:29,180 --> 00:06:42,220 And then we wrote it up on online notes so that no one else has to go through that, and they can just read our notes and, you know, follow along step by step as to how you get from the initial assumptions all the way to the final verification check. 71 00:06:42,620 --> 00:06:54,840 So hopefully this is like very easy to follow along with, and, you know, walks you through all of the different sort of manipulations to the initial assumptions that you have to do to arrive at the final math. 72 00:06:55,420 --> 00:06:59,760 So I guess you might be curious of what is that actual math. 73 00:06:59,760 --> 00:07:01,560 So I'll give you a little bit of an overview. 74 00:07:01,560 --> 00:07:08,160 I don't want to put everyone to sleep, so I'm not going to go too deep into the math, but hopefully just enough to give you like a little intuition of how it works. 75 00:07:08,720 --> 00:07:14,760 So the central building block of bulletproofs is this really efficient inner product argument. 76 00:07:15,020 --> 00:07:21,760 So as a refresher, an inner product is if you have two vectors and you take the sum of the entrywise multiplication. 77 00:07:21,860 --> 00:07:25,880 So the inner product of A and B equals C is the statement here. 78 00:07:26,240 --> 00:07:33,640 And an inner product argument is basically like a way to argue to someone that, in fact, the inner product of A and B is C. 79 00:07:33,920 --> 00:07:42,740 So like super naively, if I was just trying to prove that to you, I could send to you the vectors A and B and then send you the scalar C, and then you can do this math yourself. 80 00:07:42,740 --> 00:07:48,180 And you can check like either yes, it does equal C or no, the inner product of the two does not equal C. 81 00:07:48,380 --> 00:07:49,360 That's pretty easy. 82 00:07:49,360 --> 00:07:53,580 And that would take O of N space for me to send this proof to you. 83 00:07:53,580 --> 00:07:55,800 It'd be O of N where N is the length of the vectors. 84 00:07:56,000 --> 00:08:00,560 So that's like pretty inefficient if you have very large vectors A and B. 85 00:08:00,620 --> 00:08:06,300 And what Bulletproofs actually introduces is a really efficient way to make this argument. 86 00:08:06,300 --> 00:08:13,260 And this efficient way actually takes O of log N space instead of O of N space. 87 00:08:13,300 --> 00:08:23,320 And the intuition for how this works is that first you start off with the vectors A and B and you want to prove that C is the inner product of the vectors A and B. 88 00:08:23,580 --> 00:08:27,160 But at every step, you like do multiple rounds. 89 00:08:27,160 --> 00:08:30,880 And at every round, you actually half the size of vectors A and B. 90 00:08:30,880 --> 00:08:33,000 So you make A prime and B prime. 91 00:08:33,000 --> 00:08:43,120 And the way that you do this is you take a random scalar X and you multiply the first half of A with X and the second half of A with the inverse of X. 92 00:08:43,120 --> 00:08:44,460 And then you add those two. 93 00:08:44,460 --> 00:08:50,260 So by adding those two, you now create an A prime vector that is half the length of your original A. 94 00:08:50,280 --> 00:08:53,000 And you do something similar to B to make B prime. 95 00:08:53,000 --> 00:08:56,400 And then you have A and B the inner product of A prime and B prime. 96 00:08:56,640 --> 00:09:03,640 And then at this step, I'm not going to go into the details, but you basically make two commitments and then you send those commitments to the verifier. 97 00:09:03,740 --> 00:09:09,240 And then you repeat this step over and over again until you end at the base case where you just have two vectors of length one. 98 00:09:09,460 --> 00:09:22,100 So as you might have guessed, because you're having the vectors at every step and every step you make a constant number of commitments, this now takes O of log n space instead of O of n space. 99 00:09:22,100 --> 00:09:27,420 So that's actually the core tenet of how Bulletproof is able to be so space efficient. 100 00:09:28,980 --> 00:09:35,700 So you might be wondering, why do we even care about proving that C is equal to the inner product of A and B? 101 00:09:35,720 --> 00:09:47,140 And the reason for that is we can actually represent basically any statement we want as an inner product argument by applying math and cryptography to it. 102 00:09:47,540 --> 00:09:52,240 So as an example, I'll walk you through a little bit of how we can do this to a range statement. 103 00:09:52,820 --> 00:09:57,720 So here we start out with this statement that V is between zero and two to the n. 104 00:09:57,720 --> 00:10:15,680 We want to apply some math and cryptography to get it in the form of an inner product argument, where if and only if this inner product argument is true, if and only if C is equal to the inner product of A and B, then with very high probability, we know that V is in the range of zero and two to the n. 105 00:10:15,720 --> 00:10:16,640 Does that make sense? 106 00:10:16,640 --> 00:10:21,320 So that's our goal here is to do this jump between the left and the right. 107 00:10:21,520 --> 00:10:35,760 And the way we do that is actually by observing something really interesting, which is that if V is between zero and two to the n, you must be able to represent V as a binary number of length n. 108 00:10:35,840 --> 00:10:38,860 So this is just by definition of binary numbers. 109 00:10:39,100 --> 00:10:45,600 So for example, if V is seven, we know that seven is between zero and two to the n, where n is four. 110 00:10:45,880 --> 00:10:52,040 And therefore, you must be able to represent V as a binary number of length four, zero, one, one, one. 111 00:10:52,160 --> 00:10:52,800 Cool. 112 00:10:52,800 --> 00:10:59,520 And if V is outside like greater than two to the n, then we should not be able to represent it as a binary number of length four. 113 00:10:59,580 --> 00:11:00,020 Cool. 114 00:11:00,020 --> 00:11:05,840 So let's call this binary representation of V a sub L. 115 00:11:05,960 --> 00:11:17,540 So we can actually just say that V, the secret value, is the inner product of a sub L and the vector two to the n, where that's two to the three, two to the two, two to the one, two to the zero. 116 00:11:17,860 --> 00:11:18,180 Cool. 117 00:11:18,180 --> 00:11:25,600 So now actually, it seems like we have achieved our goal of representing our initial range statement as an inner product. 118 00:11:25,600 --> 00:11:29,280 But not yet, because you can do something malicious. 119 00:11:29,280 --> 00:11:32,820 Right here, we're assuming that a to the L is all zeros. 120 00:11:32,940 --> 00:11:38,500 But what if a sub L actually contains 100 in one of the digits? 121 00:11:38,500 --> 00:11:53,880 If that was the case, then a sub L would not just be a binary representation, and V could actually be incredibly outside of the range zero to the n, while still having this inner product argument, V equals a sub L and two to the n be true. 122 00:11:54,220 --> 00:12:02,720 So basically, we want to somehow guarantee that a sub L is only comprised of zeros and ones in order to make sure that V is actually in range. 123 00:12:03,600 --> 00:12:06,820 And the way that we do this is we add a second statement. 124 00:12:06,820 --> 00:12:15,240 We say that a sub L times a sub L minus one should be equal to zero for every index in a sub L. 125 00:12:15,380 --> 00:12:19,760 So if you think about this, this statement will only be true if a sub L is comprised of zeros and ones. 126 00:12:19,760 --> 00:12:26,440 If it was, for example, had a 100 at an index, then it would be a 100 times 99 equals not zero. 127 00:12:26,520 --> 00:12:29,040 So this checks that the bits are actually bits. 128 00:12:29,560 --> 00:12:40,800 So we continue to sort of add sort of, we continue to add these checks and different statements to our collection of statements. 129 00:12:41,000 --> 00:12:48,980 And then we add some more blinding factors, which basically blinding factors are a way to make sure that the secrets such as V and a sub L remain secret. 130 00:12:49,060 --> 00:12:50,280 And then we combine them. 131 00:12:50,280 --> 00:12:52,820 We just do a lot of math rearranging that's really tedious. 132 00:12:52,940 --> 00:13:02,760 And then we arrive at an inner product argument, C equals inner product of A and B, which is true, where if that is true, then V is in range with very high probability. 133 00:13:03,920 --> 00:13:11,000 So I'm actually going to be going more deeper into the math at another talk at Monero Village on Sunday. 134 00:13:11,000 --> 00:13:14,320 So if you want to learn more about the math, then please come to that. 135 00:13:14,320 --> 00:13:17,260 Or you can just read our notes on the Bulletproofs math. 136 00:13:17,260 --> 00:13:21,460 I'm just basically going to go through that and follow along at your own pace. 137 00:13:22,040 --> 00:13:29,520 So yeah, interestingly, Monero does use Bulletproofs and they do not use our implementation of Bulletproofs, although we're working on that. 138 00:13:30,380 --> 00:13:30,920 Cool. 139 00:13:30,920 --> 00:13:36,040 So now that we've understood the paper, mostly, we have to actually go and implement it. 140 00:13:36,040 --> 00:13:48,800 And there's a lot of things where cryptography papers will say, you need a prime order group, or you need just to apply a Fiat -Shamir heuristic, where it's not always very straightforward how as an implementer you do that. 141 00:13:48,800 --> 00:13:53,240 And so I'll talk a little bit about the tools that we use to implement these things. 142 00:13:53,480 --> 00:14:03,620 So starting with the prime order group, this picture is a little screenshot from the paper where the paper just says, let's need a cyclic group of prime order P. 143 00:14:03,620 --> 00:14:05,520 And as a reader, you're fine. 144 00:14:05,760 --> 00:14:08,140 We use a prime order group. 145 00:14:08,140 --> 00:14:10,560 But in practice, how do you implement this? 146 00:14:10,640 --> 00:14:14,440 So most people use elliptic curves because they're just really efficient. 147 00:14:14,760 --> 00:14:23,760 Edwards curves are really nice because they're really fast, have complete formulas, which means they don't have weird edge cases that you have to deal with one by one. 148 00:14:23,760 --> 00:14:26,420 And they're easy to implement in constant time. 149 00:14:26,420 --> 00:14:30,060 The problem, though, is that Edwards curves aren't prime order. 150 00:14:30,060 --> 00:14:40,120 Weierstrass curves, such as secp256k1, which is what Bitcoin uses, are prime order, but they don't have the nice performance and complete formula properties. 151 00:14:40,120 --> 00:14:44,820 So it'd be really awesome if we could just somehow get the best of both worlds. 152 00:14:44,860 --> 00:14:54,200 So in the past, what people have done is that they've said, well, we'll just use an Edwards curve in the place of a prime order group and it will be fine. 153 00:14:54,300 --> 00:14:56,160 And it was not fine. 154 00:14:56,800 --> 00:14:59,240 So that's not a good option. 155 00:14:59,280 --> 00:15:03,920 Don't do that, because then you'll have cofactor problems, which many people have learned before. 156 00:15:04,460 --> 00:15:13,160 However, we do have this really awesome protocol, two protocols, called decaf and ristretto, which actually do let us get the best of both worlds. 157 00:15:13,160 --> 00:15:21,540 So decaf was created by Mike Hamburg in 2015 and allows you to basically get a cofactor 4 reduction from an Edwards curve. 158 00:15:21,540 --> 00:15:22,460 That's really awesome. 159 00:15:22,460 --> 00:15:32,000 If you have an Edwards curve that has cofactor 4, which means that the order is 4 times a really large prime p, then you can actually create a prime group p from it. 160 00:15:32,280 --> 00:15:38,760 And then ristretto is actually something that was created by Mike Hamburg and Henry de Valence, which does a cofactor 8 reduction. 161 00:15:38,880 --> 00:15:47,640 And so the awesome thing about ristretto is that curve 255.19, which is an extremely fast Edwards curve, has cofactor 8. 162 00:15:47,640 --> 00:15:53,820 So because of the existence of ristretto, we can now create a prime order group over curve 255.19. 163 00:15:54,200 --> 00:15:57,700 So you can read more about decaf and ristretto at these links here. 164 00:15:58,620 --> 00:16:07,780 And what's really awesome about curve 255.19 is that it has extremely fast parallel formulas by this paper HWCD. 165 00:16:07,860 --> 00:16:18,280 And it's implemented as a library curve 255.19 Dalek, which uses these parallel formulas and uses AVX2 to get really good performance. 166 00:16:18,280 --> 00:16:27,820 So basically now because we have ristretto, we can take advantage of all of these performance speedups and make a prime order group over this curve 255.19 curve. 167 00:16:28,060 --> 00:16:35,860 And the end result is that, in fact, we do have extremely good performance if you compare it to other prime order groups. 168 00:16:35,940 --> 00:16:44,120 If you compare it to, for instance, secp256k1, the libsecp is four times slower than ristretto255. 169 00:16:44,120 --> 00:16:45,260 So that's really awesome. 170 00:16:45,260 --> 00:16:51,960 And I think if anyone's looking to build a prime order, use a prime order group, ristretto is sort of the obvious way to go. 171 00:16:52,140 --> 00:16:54,440 And it's implemented entirely in Rust. 172 00:16:54,440 --> 00:16:55,820 So that's an extra bonus. 173 00:16:56,660 --> 00:17:01,360 So that sort of covers our requirements for a prime order group. 174 00:17:01,420 --> 00:17:05,180 Next, we have to deal with the Fiat-Shamir heuristic. 175 00:17:05,180 --> 00:17:20,200 So if you've read through some interactive cryptography papers, you often see this snippet on the right, where the prover gives something to the verifier, and the verifier gets a random scaler, and then the verifier gives that random scaler back. 176 00:17:20,200 --> 00:17:28,300 And the paper says, well, here we have an interactive protocol, and you make it non-interactive by simply applying the Fiat-Shamir heuristic. 177 00:17:28,300 --> 00:17:30,940 And as an implementer, you're like, cool. 178 00:17:30,940 --> 00:17:33,080 How do I actually do that? 179 00:17:33,240 --> 00:17:42,100 In practice, a lot of people achieve, like, implement the Fiat-Shamir heuristic by simply hashing the things that the prover sends to the verifier. 180 00:17:42,720 --> 00:17:46,360 So that may work with a protocol that's sort of this simple. 181 00:17:46,360 --> 00:17:56,380 But if you have multiple rounds or multiple parties participating, that can actually get really complex and, like, really complicated with some edge cases that you might have to deal with. 182 00:17:56,380 --> 00:17:59,960 So what happens, for instance, if you forget to feed data into the hash? 183 00:18:00,100 --> 00:18:02,800 Or the data is ambiguously encoded? 184 00:18:02,800 --> 00:18:08,060 Or if you have multi-round protocols and you have to somehow hold on to the randomness from a previous round? 185 00:18:08,300 --> 00:18:09,740 What about domain separators? 186 00:18:09,740 --> 00:18:16,820 And there's just a lot of little edge case scenarios that are hard to deal with and you might just get a little bit wrong, which breaks your whole protocol. 187 00:18:18,040 --> 00:18:21,360 So it would be really nice if we didn't have to worry about this. 188 00:18:21,360 --> 00:18:29,040 If somehow you just had, like, a transcript object that held on to all the randomness for you and gave you some randomness back whenever you asked for it. 189 00:18:29,040 --> 00:18:41,460 So like this, if you changed your if you implemented the thing we saw in the first slide, like so, the prover gives the verifier L and R, which in practice when you're making it non-interactive is that you just give the transcript L and R. 190 00:18:41,460 --> 00:18:47,880 And then you get some challenge scaler back, which in practice in code means you just get a challenge scaler from the transcript. 191 00:18:47,940 --> 00:18:49,360 So that would be really nice. 192 00:18:49,360 --> 00:18:57,640 And luckily for us, Henry also implemented Merlin, which is a strobe-based transcript for zero-knowledge proofs. 193 00:18:57,640 --> 00:18:59,400 So this gives us everything we want. 194 00:18:59,400 --> 00:19:07,800 We have message framing, domain separation, composition of proofs, and basically it makes our lives super easy when it comes to dealing with the Fiat-Shamir heuristic. 195 00:19:07,800 --> 00:19:12,080 We sort of treat it as a black box and don't have to worry about what happens behind the scenes. 196 00:19:12,440 --> 00:19:19,000 And the best part about it is that it's more information is available at merlin.cool, so you can learn more about it there. 197 00:19:20,540 --> 00:19:21,100 Awesome. 198 00:19:21,100 --> 00:19:29,920 So now we've sort of learned about the story, you know, how we decided to implement a zero-knowledge proof, why we chose bulletproofs, and what tools we use. 199 00:19:29,920 --> 00:19:32,340 So you're probably itching to see the actual code. 200 00:19:32,340 --> 00:19:33,760 So let's dive into that. 201 00:19:34,080 --> 00:19:37,440 If you feel like following along, the GitHub repo is here. 202 00:19:37,440 --> 00:19:38,560 It's open source. 203 00:19:38,700 --> 00:19:40,720 You can look into it. 204 00:19:42,160 --> 00:19:43,120 Cool. 205 00:19:45,280 --> 00:19:47,400 So let's look at the range proof code. 206 00:19:48,040 --> 00:19:51,520 On the prover side, you have to actually assemble this proof. 207 00:19:51,580 --> 00:20:02,700 And I sort of gave you a little peek into the first few steps of making the proof, to make this A sub L vector, which is the bit vector that represents the V. 208 00:20:02,800 --> 00:20:05,840 So here on the left is an excerpt from the paper. 209 00:20:06,060 --> 00:20:07,440 And this might look familiar. 210 00:20:07,440 --> 00:20:13,440 A sub L is a binary vector such that the inner product of A sub L and the vector 2 to the N equals V. 211 00:20:13,440 --> 00:20:15,660 So that should look familiar from before. 212 00:20:15,760 --> 00:20:18,860 And then you just create A sub R, which is A sub L minus 1. 213 00:20:19,100 --> 00:20:31,740 So on the right-hand side is basically some pseudocode that matches pretty closely with our actual code, except that actual code has a few more optimizations that make it a little bit different, a little harder to read. 214 00:20:31,740 --> 00:20:34,000 So I made it a little nicer here. 215 00:20:34,340 --> 00:20:38,200 And you can follow along with the actual code at these links here. 216 00:20:38,380 --> 00:20:57,000 So on the left, what we do is we sample a random challenge scaler, a random scaler A alpha, and then we make a commitment, big A, using the blinding factor alpha, and taking the multiscalar multiplication of our G vector, which is our sort of base point generators, 217 00:20:57,000 --> 00:21:01,340 and A sub L, and then our H vectors and A sub R. 218 00:21:01,340 --> 00:21:08,400 So effectively what big A does is it commits along with a blinding factor to A sub L and A sub R. 219 00:21:08,400 --> 00:21:26,140 And then on the right-hand side, we can do this really simply using the Ristretto library and its curve and scaler operations to do H times alpha plus MSM, which is multiscalar multiplication of G vector and A sub L, and the multiscalar multiplication of H vector and A sub R. 220 00:21:26,440 --> 00:21:29,480 So hopefully this maps from the paper really well. 221 00:21:30,100 --> 00:21:38,340 And then we do the exact same thing, but with S sub L and S sub R, where S sub L and S sub R are actually blinding factors for A sub L and A sub R. 222 00:21:38,360 --> 00:21:45,600 So we generate those, we generate a blinding factor row, and then we create a big S, which is the commitment to all of those things. 223 00:21:45,940 --> 00:21:46,200 Cool. 224 00:21:46,200 --> 00:21:52,000 And then now we have the actual exciting part, which is that we get to do the Fiat-Shamir heuristic. 225 00:21:52,160 --> 00:21:56,220 So here in the paper, it just says, send A and S to the verifier. 226 00:21:56,300 --> 00:22:05,500 And in practice, what we can do in the code because of our transcript object, thanks to Merlin, is we can just commit the points A and S to the transcript. 227 00:22:05,620 --> 00:22:15,640 And then where we're supposed to, in the interactive protocol, get Y and Z from the verifier, we can now get the challenge scaler for Y and Z from the transcript. 228 00:22:15,640 --> 00:22:19,480 And you can see here that we actually have these domain separators at every one of these steps. 229 00:22:19,480 --> 00:22:25,920 And those domain separators make sure that we're actually getting A and S and not confusing it with any other steps. 230 00:22:27,220 --> 00:22:27,580 Cool. 231 00:22:27,580 --> 00:22:30,100 So that's a little pseudocode. 232 00:22:30,100 --> 00:22:33,900 And hopefully, all of the rest of the library is also about as easy to read. 233 00:22:33,900 --> 00:22:43,800 But I'm going to sort of talk about more high level, how we were able to take advantage of using Rust to get really good performance and really nice code safety. 234 00:22:43,800 --> 00:22:52,680 So one really cool thing that we were able to do was implement session types for an MPC protocol using Rust. 235 00:22:52,780 --> 00:23:00,440 So for context, I mentioned really briefly earlier that you can actually get really good aggregation properties from bullet proofs. 236 00:23:00,440 --> 00:23:19,820 And what that means is if you have N people who all have separate range proofs for their own private secret values, they could come together and without having to trust each other, actually make one aggregated range proof that is significantly smaller and faster to verify than having N separate single range proofs. 237 00:23:19,820 --> 00:23:24,040 So in order to do this, you have to engage in a multiparty computation protocol. 238 00:23:24,160 --> 00:23:27,160 And it looks sort of like this. 239 00:23:27,160 --> 00:23:29,360 You don't have to see the actual party states. 240 00:23:29,360 --> 00:23:31,960 But the state transitions look something like this. 241 00:23:31,960 --> 00:23:39,680 So basically, every party will go through some calculations on their secret value and then make a commitment. 242 00:23:39,680 --> 00:23:46,590 And they'll send this commitment either to a dealer, which is what we have pictured here, or they'll send it to all of the other parties if you don't trust a dealer. 243 00:23:46,710 --> 00:23:52,890 And those other parties will take the commitments and then generate a random challenge scaler and return it to all of the other parties. 244 00:23:52,890 --> 00:23:55,370 And they do this actually three separate times. 245 00:23:55,470 --> 00:24:05,890 So you can see how if we were just using hashing for the Fiat Shamir heuristic, this would actually be extremely complicated to do because you're hashing data from a bunch of different parties over a bunch of different steps. 246 00:24:05,890 --> 00:24:08,850 But with the transcript protocol, this is actually really efficient. 247 00:24:08,950 --> 00:24:20,330 But going back to my main point, because this is a multiparty competition protocol and you're actually doing multiple party transitions, it's really , really important that you get these transitions in the right order. 248 00:24:20,330 --> 00:24:37,830 So for example, if you were to call the same function on the same party state twice, that would actually break the protocol and you might actually be able to leak secrets from the party by doing that because you're doing a different challenge over the same secrets. 249 00:24:37,870 --> 00:24:43,090 So you have to make sure that you can only transition from one party state to the next and not repeat a party state. 250 00:24:43,090 --> 00:24:48,650 And the way that we make sure of this using Rust is by taking advantage of their memory management model. 251 00:24:48,690 --> 00:24:58,950 So every party transition actually is a function on a party state where the function takes ownership of the party state and then consumes it and generates the next party state. 252 00:24:58,950 --> 00:25:04,070 So you're physically not able to call the same function on a state twice. 253 00:25:04,130 --> 00:25:12,150 So I thought that was a really cool thing that you could do with Rust and basically guarantee that you don't abuse this multiparty competition protocol. 254 00:25:12,570 --> 00:25:14,870 So that's one fun thing. 255 00:25:15,290 --> 00:25:23,510 Another thing that we can do in Rust is make optimizations based on the fact that Rust iterators are lazy and zero cost. 256 00:25:23,850 --> 00:25:33,050 So I mentioned earlier that you can do multi-scalar multiplications of a large generator vector and a large scalar vector. 257 00:25:33,050 --> 00:25:34,850 And that basically looks like this down here. 258 00:25:34,850 --> 00:25:38,450 Q equals some scalar times a generator, another scalar times a generator. 259 00:25:38,450 --> 00:25:40,810 And you can chain these up. 260 00:25:40,850 --> 00:26:02,390 So if your iterators are lazy and zero cost, what you can do is any time that you have a multi-scalar multiplication, especially for the verification equation, instead of just doing that multi-scalar multiplication in place, what you can do is add all of the generators and add all of the scalars to increasingly large set of iterators. 261 00:26:02,390 --> 00:26:13,390 And then only at the very, very end do you do one massive multi-scalar multiplication using all of the generators, sorry, all of the points or generators and all of the scalars from the entire calculation. 262 00:26:13,390 --> 00:26:15,190 And this is actually way more efficient. 263 00:26:15,470 --> 00:26:21,290 Both because you don't have to do extra allocations or manage temporaries for those iterators. 264 00:26:21,290 --> 00:26:32,670 And because the backend we use, Ristretto library, has speedups such that if you have a lot of generators and a lot of scalars, it is able to take advantage of some performance properties. 265 00:26:32,870 --> 00:26:38,370 So the end result of all of this is that we have extremely good performance. 266 00:26:38,370 --> 00:26:44,630 So for a 64-bit range proof, we're able to do verification in less than one millisecond. 267 00:26:44,830 --> 00:26:50,170 And with IFMA, three times faster than LibSecP and seven times faster than Monero. 268 00:26:50,170 --> 00:26:55,450 So basically the state-of-the-art range proof out there that we know of. 269 00:26:55,670 --> 00:26:56,950 So that's pretty awesome. 270 00:26:56,950 --> 00:27:07,010 And this performance is a result of, as I mentioned, using Rust, using the iterators, using the Ristretto prime order group and a lot of other little performance tweaks that we did here and there. 271 00:27:07,010 --> 00:27:09,010 So I think this is really awesome. 272 00:27:09,970 --> 00:27:10,530 All right. 273 00:27:10,530 --> 00:27:13,790 So we have implemented this range proof. 274 00:27:13,790 --> 00:27:17,430 You're like, cool, now you can do confidential transactions on the blockchain. 275 00:27:17,430 --> 00:27:19,610 Now we can have private blockchain transactions. 276 00:27:19,610 --> 00:27:20,710 So that's pretty cool. 277 00:27:20,710 --> 00:27:22,390 But we didn't want to stop there. 278 00:27:22,390 --> 00:27:23,670 We're sort of on a roll. 279 00:27:23,670 --> 00:27:27,290 And so what we did is we actually kept building this protocol out. 280 00:27:27,310 --> 00:27:42,750 So what I mentioned early in the talk is that Bulletproofs actually allows not only for, you know, one specific proof type, the range proof, but it actually allows for the ability to make a proof over any set of arbitrary statements. 281 00:27:42,750 --> 00:27:47,970 And so we wanted to make a way for people to make their own statements. 282 00:27:47,970 --> 00:27:49,670 And the statement could be a range proof. 283 00:27:49,670 --> 00:27:56,770 It could be a proof that a loan has been repaid while keeping the loan amount and the percentage amount private. 284 00:27:56,770 --> 00:28:00,230 It could be basically whatever statement you want to make a proof over. 285 00:28:00,230 --> 00:28:07,450 And so we made this constraint system API for fully programmable proofs over the Bulletproofs protocol. 286 00:28:08,370 --> 00:28:12,830 And what that looks like is you can add multiplicative constraints. 287 00:28:12,830 --> 00:28:15,570 So you have variables X, Y, and Z. 288 00:28:15,570 --> 00:28:19,990 And you want to prove that X times Y equals Z while keeping all three of those variables secret. 289 00:28:19,990 --> 00:28:28,810 And you can also add linear constraints, which is basically a combination of secret variables with clear text weights being equal to zero. 290 00:28:28,830 --> 00:28:39,150 So these are the two things that we added to the API to allow you to build constraint systems, which can represent your arbitrary statements. 291 00:28:39,150 --> 00:28:45,150 But you might be asking why constraint systems want to use these to program your arbitrary statements. 292 00:28:45,150 --> 00:28:50,870 And that's because a constraint system can actually represent any efficiently verifiable program. 293 00:28:50,930 --> 00:28:53,710 Anything that is NP complete, for that matter. 294 00:28:54,150 --> 00:29:00,430 So as long as you can express something as a program, you can also express it as a constraint system. 295 00:29:00,430 --> 00:29:06,510 And then you can make a constraint system proof that all of the constraints are actually satisfied by your secret inputs. 296 00:29:07,030 --> 00:29:14,490 So that seems like a very complicated way of saying basically you can code whatever proof you want using this API. 297 00:29:14,670 --> 00:29:17,770 So we made that API, and we wrote up some more notes on that. 298 00:29:18,510 --> 00:29:22,370 And then we... the whole reason we wrote this API was because we wanted to use it. 299 00:29:22,370 --> 00:29:27,330 And one of the reasons that we wanted to use it was to build this confidential assets protocol. 300 00:29:27,330 --> 00:29:43,090 So the key difference between a confidential transaction protocol, which is what we talked about, what I talked about earlier, where you just have, you know, your input and output amounts as secret values, is that in a confidential assets protocol, not only are your input and output amounts secret, 301 00:29:43,090 --> 00:29:45,430 but your asset types are also secret. 302 00:29:45,610 --> 00:29:58,290 So a confidential assets protocol is actually a lot more complicated to build than a confidential transactions protocol, because you can't just add up all of the sums of all of the different asset types, because that would be revealing what the asset types are. 303 00:29:58,290 --> 00:30:03,230 So there's a little bit more finesse that we have to do to build a confidential assets protocol. 304 00:30:04,270 --> 00:30:13,590 And there's sort of four building blocks of this, and each of those building blocks is actually a combination of constraints that went into the constraint system. 305 00:30:13,670 --> 00:30:22,570 So you might be familiar with the furthest right one, the range proof, that basically just takes all of the constraints that we used to build the range proof in the first place that I talked about earlier. 306 00:30:22,610 --> 00:30:33,610 But there's also a shuffle, we call them gadgets, a shuffle gadget, which proves that your inputs and outputs to the gadget are a valid reordering of each other. 307 00:30:33,910 --> 00:30:41,250 We also have a merge and split gadget, and those basically either merge two items into one or leave them unaltered. 308 00:30:41,250 --> 00:30:46,990 And from a verifier's perspective, you can't actually tell whether those things were merged or whether they're left alone. 309 00:30:47,590 --> 00:30:50,110 You only know that one of those two happened. 310 00:30:50,410 --> 00:30:54,630 So we use these building blocks to make a cloak transaction. 311 00:30:55,070 --> 00:31:11,630 What that means is that we have these inputs, we have these outputs, and we apply all of these gadgets to the inputs, such that if these gadgets were applied correctly, then the whole proof will verify correctly, and then you know that the inputs and outputs are actually valid reorderings of each other. 312 00:31:12,030 --> 00:31:15,970 So let's just walk through that, just to give you an idea what that means. 313 00:31:16,730 --> 00:31:20,450 So here your inputs are $5, 3 yen, and $4. 314 00:31:20,630 --> 00:31:24,870 First you want to group the assets together, the asset types together. 315 00:31:24,870 --> 00:31:31,070 So here you're shuffling them, and you want to group the $5 and $4 together and the 3 yen at the very end. 316 00:31:31,850 --> 00:31:35,790 As a reminder, the verifier doesn't know exactly how you're ordering this. 317 00:31:35,790 --> 00:31:40,870 All the verifier can see is that you're doing a valid shuffle or that you're not doing a valid shuffle. 318 00:31:40,910 --> 00:31:46,630 So here this is a valid shuffle, and then we want to merge all of the things that are of the same asset type together. 319 00:31:46,630 --> 00:31:49,110 So $5 and $4 added to $9. 320 00:31:49,870 --> 00:31:55,930 Then we shuffle again, and we move all of the non-zero types to the top of each asset group. 321 00:31:55,930 --> 00:32:00,670 So $9 gets moved to the top, while $0 gets moved to the bottom of the asset group. 322 00:32:01,090 --> 00:32:05,630 Then we split the asset types into whatever output amounts we want. 323 00:32:05,630 --> 00:32:09,110 So if we look to the right, we actually want an output amount of $6 and $3. 324 00:32:09,110 --> 00:32:13,510 So we split $9 into $6 and $3, and we keep 3 yen the same. 325 00:32:13,990 --> 00:32:20,030 And then lastly, we do one shuffle just to make sure that we have a random ordering for the output. 326 00:32:20,110 --> 00:32:24,510 And then we add a range check to make sure that none of these values are negative. 327 00:32:24,710 --> 00:32:40,350 And then basically, as long as you make a proof that all of these operations were valid operations that satisfy the constraints in each of these gadgets, then the verifier can look at this proof, and without knowing what any of the inputs, any of the outputs, 328 00:32:40,350 --> 00:32:48,690 or any of the intermediate values are, they can actually say, yes, indeed, I am fairly sure that this is a valid confidential assets transaction. 329 00:32:48,690 --> 00:32:53,670 You did not, in fact, create extra assets, destroy assets, or anything like that. 330 00:32:53,670 --> 00:33:01,510 And to a verifier, transactions of the same size, meaning the same number of inputs and the same number of outputs, are indistinguishable from each other. 331 00:33:01,590 --> 00:33:02,550 So that's the whole point. 332 00:33:02,550 --> 00:33:07,710 We want to make sure that we can keep these transactions actually confidential. 333 00:33:08,110 --> 00:33:19,450 So the spec and code for this is online, and this is something that has actually been inherited by Stellar, the Stellar Development Foundation, and they're working on integrating this into Stellar. 334 00:33:19,450 --> 00:33:21,050 So that's pretty exciting. 335 00:33:21,870 --> 00:33:31,210 We now have the ability to do a confidential assets transaction with Bulletproofs without using Trusted Setup, which is something that is fairly interesting. 336 00:33:32,050 --> 00:33:48,770 And then, if you're still with me, the last sort of crazy thing was that we wanted to make a smart contract language where people who don't understand how constraints work, don't really care about a multiplicative constraint or a linear constraint, but want to write smart contracts, 337 00:33:48,770 --> 00:33:55,850 are able to use this smart contract language to write contracts that compile down to zero-knowledge proofs. 338 00:33:55,930 --> 00:34:16,270 So I'm near the end of my talk time, and I want to leave time for questions, but basically this was a whole separate talk that we gave at a conference recently about how we were able to make a zero-knowledge smart contract language using Bulletproofs that's a lot more user-friendly than building constraints by hand. 339 00:34:16,610 --> 00:34:17,010 Cool. 340 00:34:17,010 --> 00:34:19,930 So that's my talk. 341 00:34:19,930 --> 00:34:25,550 Once again, a lot of this stuff I worked on with Henry and Oleg at Chain and Interstellar. 342 00:34:25,550 --> 00:34:31,910 They're both super great, and Oleg is actually carrying on a lot of this work at the Stellar Development Foundation. 343 00:34:32,170 --> 00:34:49,490 And I also want to say thanks to George and Deirdre, who persuaded me to submit this talk, and George actually gave a very similar talk to this called Implementing an Elliptic Curve, How to Implement Curve 255.19 in Go at this exact village a few years ago. 344 00:34:49,490 --> 00:34:51,850 So I modeled a lot of my talk off of his talk. 345 00:34:52,570 --> 00:34:54,350 So those are my thanks. 346 00:34:54,350 --> 00:34:57,890 And for further reading, we have of course you should read the Bulletproofs paper. 347 00:34:57,890 --> 00:35:02,650 It's still a really good paper even though it doesn't explain the intermediate steps they take to get to their conclusion. 348 00:35:03,430 --> 00:35:15,470 You can check out our open source GitHub repo for implementation, and then you can follow along with our math here, where we write up how you actually get from step one to step two. 349 00:35:15,510 --> 00:35:25,830 And then lastly, if you want to refer to any of these slides, my slide deck is just at speakerdeck.com slash Kathy Yun, so that you can find all of these URLs in the future. 350 00:35:25,910 --> 00:35:31,490 And I am on Twitter if you want to shoot me any questions or just find me in person later. 351 00:35:31,910 --> 00:35:34,110 That is my talk. 352 00:35:39,790 --> 00:35:43,730 And questions at the mic in the center aisle if you have questions. 353 00:35:43,730 --> 00:35:48,430 We do have a cutoff, and so please walk up to the mic. 354 00:35:48,990 --> 00:35:52,290 And if there are any mobility issues, I'll always walk the mic over. 355 00:35:53,950 --> 00:35:55,070 Hi, thank you. 356 00:35:55,070 --> 00:36:00,250 I'm wondering if these validations, are these compile time or run time? 357 00:36:01,190 --> 00:36:05,930 So you're talking about the verification of proofs? 358 00:36:06,050 --> 00:36:08,330 Yeah, well, the components that you were showing. 359 00:36:08,330 --> 00:36:13,130 Oh, it's able to verify that this component actually does a valid shuffle or a valid merge, these kinds of things. 360 00:36:13,130 --> 00:36:14,110 Because it's really interesting. 361 00:36:14,110 --> 00:36:19,490 It's like you've kind of implemented a really cool like dependent types kind of thing or validation. 362 00:36:19,770 --> 00:36:24,010 And with Rust being a nice strongly typed language, that's kind of neat. 363 00:36:24,010 --> 00:36:28,570 Like bringing the types maybe into a higher level of being able to assert certain things. 364 00:36:28,570 --> 00:36:34,750 Yeah, so you're talking about basically for these gadgets, whether these are compile time. 365 00:36:34,750 --> 00:36:46,290 So basically whenever we build one of these gadgets, what we do is actually we use the constraint system API to like hand select, like here we want one more multiplier, one more linear constraint. 366 00:36:46,290 --> 00:36:49,650 And so that's all done basically at compile time. 367 00:36:50,230 --> 00:36:53,730 And yeah, does that answer your question? 368 00:36:53,730 --> 00:36:57,230 So we basically like do this by hand for Cloak. 369 00:36:57,410 --> 00:37:01,590 But then in ZKVM, you can actually do this with like certain opcodes. 370 00:37:02,090 --> 00:37:06,470 And that gets sort of like compiled down to these multiplicative and linear constraints. 371 00:37:06,470 --> 00:37:08,850 And those then form a proof. 372 00:37:08,850 --> 00:37:09,490 Yeah, I think it's great. 373 00:37:09,490 --> 00:37:16,010 Because also like lots of these blockchain languages, like a theory, whatever, like, they're like super, they seem super untyped and uncheckable. 374 00:37:16,010 --> 00:37:20,750 And so it's nice to see that we're using these modern developments to do this kind of work. 375 00:37:20,750 --> 00:37:21,430 Cool. 376 00:37:21,430 --> 00:37:22,470 Yeah, thanks. 377 00:37:22,650 --> 00:37:37,610 And if you want to learn more about our smart contract language, like philosophy, actually, we built ZKVM, which is like the zero knowledge virtual machine, based a lot on our design for TXVM, which is our original smart contract language that wasn't zero knowledge. 378 00:37:37,610 --> 00:37:39,450 And that was a transaction virtual machine. 379 00:37:39,550 --> 00:37:51,590 And that, like had in mind a lot of the things that you mentioned about like strongly typed, not being able to shoot yourself in the foot by accidentally like dropping a contract, things like that, having deterministic output, for instance. 380 00:37:51,690 --> 00:37:55,210 So we had a lot of lessons learned from developing TXVM. 381 00:37:57,170 --> 00:37:58,590 Anybody else? 382 00:38:01,170 --> 00:38:01,870 All right. 383 00:38:01,870 --> 00:38:02,770 Thank you very much. 384 00:38:02,770 --> 00:38:03,090 Great. 385 00:38:03,090 --> 00:38:03,890 Thank you.