This is the first in a series of articles that attempt to explain Lichen at a level that someone competent in Bitcoin tech, but maybe not in cryptography generally, could understand.
Caveat: this post explains how Lichen uses BABE; it is different in some small technical details, and also in application, from the paper’s own description.
BABE is a very specific construct to do a very specific thing. It ties together 4 things:
Bitcoin Script and transaction mechanics
Verifying a Groth16 zero knowledge proof
Garbled circuits
Witness encryption
Why do we even want to tie those together? We want to be able to gate a Bitcoin transaction (and so, payment) behind the condition: “is some incredibly complicated program output “true”?”
Why “incredibly complicated”? Because we want to gate the Bitcoin payment behind something sophisticated like “Are the ecash notes you’re claiming you hold actually valid?” - that’s complicated in that it may involve checking things like digital signatures and other proofs, things like do you own a certain key, does the sum of the values of the notes add up to X, and a ton of other things. The complication could also be checking an SPV proof from another blockchain. There’s a huge list, but the point is that any such check is not just thousands of lines of code, but more importantly, it’s code that Bitcoin Script has no hope in hell of verifying.
The zero knowledge proof as a black-box.
We choose the Groth16 ZKP system for specific reasons that we can elide for now. I’m going to avoid explaining it to you and see how far we can get. The proof is short, 3 curve points (think public keys); you just have to believe that with a bunch of computation, I can create that proof if I have the valid “witness” - the witness is what you feed into the program, if it’s a correct witness then the proof will return “true” if it’s valid. The simple mental model to keep in mind is: a public key is like a program and the valid witness is the private key. If you have that you can make the proof (in that case, a signature) validate.
OK so the Groth16 ZKP looks like A,B,C, three points. All good so far. But what those A, B, C are will be random every time you prove. So you can’t lock a bitcoin payment behind that string; you don’t know what it is (even worse, these curve points are not on Bitcoin’s secp256k1 curve, but, on a different one, so Bitcoin has no idea what you’re talking about; to Bitcoin, it’s just a string).
Magically, that string is always the same length and structure no matter how complicated the program is. If it’s a Sudoku puzzle and the witness is a valid solution, then it doesn’t matter if the sudoku is 5 x 5 or 1billion x 1billion, it’s still just A,B,C.
Witness Encryption, the dumb type
Witness Encryption is a very powerful idea: that you could decrypt a secret, not if you own a private key, but if you know a witness - in exactly the sense I described above. You know a Sudoku solution, you know a satisfying set of inputs to a super-complex program.
To do this in general is exceptionally hard. Look at the well-publicized work this year of the alloc-init team on PIPESv2 and note how they are struggling to get a solution that involves less than hundreds of terabytes of ciphertext.
But what is less well known is there is a dumb, simple version of witness encryption for certain types of relationships involving elliptic curve pairings (section 1.3.2 of the BABE paper explains it, with the history).
Pairings
Even this small corner could take you months or years of study, but luckily, you can just think of pairings as a black box, too: they’re something that let one do multiplicative actions on your curve points in the same way as we do additive actions when we do tweak. Think taproot or Silent Payments or several similar ideas, like:
P=Q+jG
… where if you know the private key of Q you also know the private key of P, as long as you know j. This is additive: you are adding the secrets of two points to get the secret for their sum “on the curve”. With a pairing operation defined for two curves, you can do multiplicative operations “on the curve(s)” too:
Don’t worry what e as a function actually is, nor where we get these groups G1,G2,GT (T stand for ‘target’, i.e. output, group). Like I said, black box; these things just happen to allow these rules.
Now, this idea of pairings is super-powerful, because you can suddenly do multiplication as well as addition. That’s why there’s so many modern crypto constructions that rely on it. And the “dumb witness encryption” I just mentioned is one of those. It’s been demonstrated in a few scenarios but here’s where it would work for verifying a Groth16 ZKP, if it did - but it doesn’t!
To verify e(A,B)+e(C,x)=e(y,z), which is roughly what the verification equation for a Groth16 ZKP is (think of x,y,z as some constants rather than variables), you could apply a “blinding factor” r, a bit like how you can blind a Schnorr signature:
re(A,B)+re(C,x)=re(y,z)
Here’s the trick. Look at the right hand side. y and z are fixed, public points: they’re part of the verifying key, the “public key” of the program in our analogy. And r is a secret that I chose. So I can compute re(y,z) myself; it’s just e(ry,z). I hash that, use it as the key to encrypt some secret message m, publish the ciphertext, and throw r away.
Who else can compute re(y,z)? Not just anyone. Knowing y and z doesn’t help without r; getting there would be like getting a private key from a public key. But someone with a valid proof can get there from the other side of the equation, if I give them a hint. Pretend for a moment the equation was just
e(C,x)=e(y,z)
I publish rx alongside the ciphertext. Then whoever holds a valid C computes
e(C,rx)=re(C,x)=re(y,z)
which is exactly my key. Someone with an invalid C computes the same expression and gets garbage. No proof, no key; proof, key. That’s witness encryption: being able to decrypt is the same thing as knowing a valid proof, which is the same thing as knowing a valid witness.
Now think about why this is so attractive for Bitcoin. Make the secret message m the preimage of a hash, and put that hash in a script: “this output can be spent by whoever reveals the preimage of H”. Bitcoin can check that easily. And only someone with a valid proof can learn the preimage. We’d have put a zero knowledge proof verifier into Bitcoin with nothing more than a hashlock.
The blinding trick. The encryptor reaches the key from the right hand side, because it knows r; a prover reaches it from the left hand side, but only with a valid proof.
But it doesn’t work for Groth16 as it stands, and the reason is that first term, re(A,B). To compute it, the prover needs e(rA,B) or e(A,rB): someone has to have multiplied A or B by r. I can’t publish rA or rB as hints, because A and B don’t exist yet when I encrypt; they’re the proof, and remember, they’re random every time it’s made. The term e(C,x) was fine because x is fixed, so I could hide r in it in advance. In e(A,B)both sides come from the proof, so there’s nowhere to put r. And I can’t hand out r itself, because then anyone could compute re(y,z) directly, proof or no proof.
So the dumb witness encryption works for pairing equations where every pairing has at least one fixed side. Groth16’s verification multiplies two parts of the proof together, and that one product is all that stands in the way.
The details: the real Groth16 equation
The verifier accepts a proof (A,B,C) if
e(A,B)=e(α,β)+e(L,γ)+e(C,δ)
where α,β,γ,δ are points of the verifying key, and L is a fixed combination of points weighted by the program’s public inputs. So in the simplified form above, x=−δ, and e(y,z) stands for e(α,β)+e(L,γ). That isn’t a single pairing, but it doesn’t need to be: it’s one fixed value in the target group, and that’s all the encryptor needs.
Note though that it’s only fixed once the public inputs are. The encryptor has to know them when it encrypts, so in BABE the statement being proven, public inputs included, is fixed when the contract is set up.
What we’re missing is a way for the prover to get rA, their own A multiplied by my secret r, without ever learning r. That’s a very particular calculation: take an input point and multiply it by a secret constant. Much simpler than verifying a whole proof, but still something nobody can do with a secret they don’t know. Unless I give them a machine that does it for them, with r sealed inside, that works on whatever input they feed it and reveals nothing but the output.
That machine is a garbled circuit.
Garbled circuits and cut and choose
This is another complex piece of machinery; but at least it’s something that’s been known in cryptography since the 1980s. People don’t tend to use it much, or at least, they didn’t in the past, because it tends to be pretty big and unwieldy. Think of our program as a circuit, written out with binary gates (AND, OR and so on) - what a garbled circuit does is basically encrypts the input wires and then all the possible output wires - so for example, 4 output wires for the truth table of an AND gate. To get the decryption of the output wire, you need the decryption of the input wires. And the idea is that you will get the correct final output of the circuit decrypted from being given the decryptions of your chosen input wires, the inputs you feed into the program. Because of this it’s easy to see why this will tend to be extremely big. Encryptions, essentially, of every possible calculation the circuit could do.
One garbled AND gate. Each wire gets two random labels, standing for 0 and 1. Holding one label for a and one for b, you can open exactly one row, which gives you one label for c, the right one, and nothing else.
The good news and the bad news. The good news is, the “witness encryptor” from the previous section, knowing the secret value r, can use a garbled circuit to give the output value rA to the future witness decryptor, by garbling the circuit that does the elliptic curve scalar multiplication. You know those textbook elliptic curve calculations on the coordinates xA,yA and xB,yB of two points A and B that we all know and love?
That’s C=A+B, where m is the slope of the line through A and B. To double A, the slope is the tangent’s instead: m=3xA2/2yA. Note the divisions: each one is expensive inside a circuit.
… well, but we also have to use the famous double and add technique: we do the above but we double: we add A to A to double it, then repeat, until we have to do an extra addition at the end. Like getting 9 by doing 1+1, then 2+2, then 4+4 and finally adding 1, instead of adding 1 nine times. By the way, BABE actually avoids doing double and add, but, later.
So that, apparently, was the good news. We can put scalar multiplication in a circuit. The bad news is: how does the decryptor know that the encryptor encrypted the garbled circuit correctly? Maybe he encrypted an entirely different circuit? For that we need…
Cut and choose
This isn’t the only way, but perhaps the standard way, to deal with the so-called “malicious setting” of garbled circuits. You want to know that the circuit was garbled correctly before you commit money to a Bitcoin contract that depends on you being able to decrypt the garbled circuit. The trick is, you make about 100 such circuits; you have the encryptor commit to all of them. Then you run say 70 of them, that you, the decryptor, randomly request: you say to the encryptor (the ‘garbler’), give me the decryptions of the input wires, you open all the wires and check they encode the circuit correctly. Then you may (this depends on yet anther trick) be able to be confident, by statistics, that some of the unopened, still untouched garbled circuits are honestly constructed. That’s cut and choose, and it can be used in a wide class of cryptography protocols where the internal structure can’t be known to have the right form, so you have to test it like this.
Cut and choose with BABE’s numbers: 78 garbled circuits, of which the evaluator keeps 10, chosen at random. The garbler only gets away with cheating if all 10 kept are bad and all 68 opened are good: odds of 1 in (1078), about 2−40.
Blow up in size; connecting the instances to bring it down.
This blog is already getting long, so I’ll briefly describe this part. Suppose each garbled circuit is 100MB (the ones we use in BABE are much smaller, thankfully) and then we do this cut and choose and to be confident that one randomly chosen one is right, we might need 2000. Suddenly we might be looking at gigabytes and gigabytes of, not only data, but transferred data. Yuck. But there is a trick identified in Glock by Liam Eagen that vastly helps: using Shamir secret sharing and more specifically what’s called verifiable secret sharing, you can make it so that once a certain number of circuits n are opened, it only needs a single extra secret (per digit of your input string, which here is A, remember: the decryptor is going to get their secret by having the circuit spit out rA for the A they chose) to be revealed, to unlock all of the remaining garbled circuit outputs. And this means that you only need one of the previously-unopened set to be valid, which helps the statistics a lot. This way, you need perhaps 80 garbled circuit instances instead of say 2000.
The secret sharing in miniature: a cubic, instead of BABE’s polynomials of degree 68. The opened circuits give away their shares (black), which fit many curves. The one secret revealed on chain, f(0), pins the curve down, and with it the shares of every kept circuit.
In BABE, concretely, this means the cut and choose protocol only needs to transfer about 250MB of data, which is small enough that you can even do it in an app on a phone. Quite an achievement.
Technically, this trick is not part of BABE itself, though it’s referred to as an idea in the paper. The main part of BABE is the remaining section, and it’s the most technical, so I’ll be even more vague here!
Making the circuit small: BABE’s main trick.
Calculating a scalar multiplication inside the garbled circuit could be quite inefficient, even if it’s only one.
Aside: I guess I didn’t explain what BABE is avoiding: remember, we’re trying to have the witness encryption output the secret if A,B,C is a valid proof, and not otherwise. The obvious way do that would be to have the garbled circuit run through the full Groth16 verification equation. It turns out that that is a much more expensive piece of calculation than elliptic curve operations (additions, multiplications), for technical reasons. So that even though you only have to do 3 pairings calculations (three of those e() function evaluations we discussed), it would be orders of magnitude more expensive than just doing r×A, as we are choosing to do, in this BABE construct.
The obvious way is to garble double-and-add: about 254 rounds of doubling and adding on the curve’s coordinates, each round a pile of field multiplications built out of AND and XOR gates, each round depending on the one before. That would be enormous. BABE avoids it with two observations.
First: take the doubling out of the circuit. Write r in binary, r=∑i2iri, each ri being 0 or 1. Then
rA=i∑2i(riA)
If the circuit just handed the evaluator each riA, she could do that weighted sum herself, in the clear. But it would give r away: each riA is either A or nothing, so she’d read the bits of r straight off. So the garbler hides each one behind a random point ρi:
Qi=riA+ρi
The ρi are random, except that they’re chosen to cancel out when weighted, ∑i2iρi=0 (pick all but one at random, then solve for the last). Each Qi on its own is just a random-looking point, so it says nothing about ri. But all together:
i∑2iQi=i∑2iriA+i∑2iρi=rA
So the evaluator does the doubling and adding herself. For each bit of r the circuit produces one point, either A+ρi or just ρi (because bits are 0 or 1). So at most a single addition, of A to a point the garbler fixed in advance. And all 254 of them are independent of each other.
A toy r=181: eight bits instead of 254. Each bit gives one point, A+ρi or just ρi, so at most one addition each, all independent of each other. The doubling happens outside the circuit, in the weighted sum.
Second: adding a fixed point is a linear function. Take the textbook point addition formula, for A=(x,y) plus a point the garbler knows, and write it in projective coordinates, which avoid the division. The coordinates of the result turn out to be just weighted sums of a handful of numbers computed from A:
φ(A)=(1,x,y,x2,y2,xy)
This is given the obscure name “feature vector” for reasons that escape me. A nice trick here: because the Weierstrass formula here is y2=x3+3 (it’s not secp256k1 but similar! BN254 iirc), you can take cubic powers of x and translate them into quadratic terms, so this vector only takes the terms up to quadratic in the variables (x,y).
The weights depend on ρi and on the bit ri, so only the garbler knows them. But the features themselves are the same for every bit of r. So the circuit splits into two very unequal parts:
A part made of ordinary gates, run once: take A’s coordinates as bits, check that A really is a point on the curve, and compute x2, y2 and xy. Three multiplications, not thousands.
For each bit of r, a weighted sum of the feature vector elements, with secret weights. That’s the cheapest thing there is to garble, because it needs no gates at all. Write each feature in binary too. For each bit of each feature, the garbler provides one encrypted number: decrypted with that bit’s label, it gives the weight times the bit’s value, plus a mask. The evaluator decrypts whichever one her label opens and adds everything up; the masks are arranged to cancel in the total.
So it’s bit decomposition twice over: r into bits, so that each step is one addition rather than a chain of doublings; and the features into bits, so that each of those additions is just a sum of numbers the evaluator adds up.
The details: which features each coordinate uses
The result is in Jacobian coordinates (X,Y,Z), which stand for the affine point (X/Z2,Y/Z3). Adding A to a fixed point, each coordinate needs only some of the features (BABE’s Lemma 5): X uses 1,x,y,x2; Y uses 1,y,x2,y2,xy; and Z uses only 1,x.
Jacobian coordinates aren’t unique: (λ2X,λ3Y,λZ) is the same point for any nonzero λ. The garbler scales each Qi by its own random λi, so that the representation the evaluator sees is random too, not just the point.
The “encrypted number” for a feature bit u, with labels L0 and L1, is a single field element c=D+H(L0)−H(L1), where D is the weight. The evaluator holding Lu computes H(Lu)+uc, which is Du+H(L0): the weight times the bit, masked by H(L0). The garbler subtracts all those masks in a public constant term, so the sum comes out exact.
None of this needs to hide A: it’s posted on chain anyway, so the gate part can be garbled “privacy-free”, which is cheaper again. The only secrets are r, in the weights, and the labels, which make sure the evaluator can only decrypt for the A that was actually posted.
Conclusion
You get a 2 party protocol: one side hides a secret that controls a bitcoin output. The other side wants that output and therefore that secret (in fact the roles can be reversed; the ZKP can be used to prevent spending, but that’s a sidetrack). It ends up in an onchain interaction:
One side is required to Lamport-sign the bits of its chosen proof-point A.
The other side can only challenge by then posting signatures that complete earlier adaptor signatures for each of the chosen bits of A: that completes the cut and choose trick we described above: these are secret shares that unlock all of the remaining garbled circuit instances.
Given those adaptor secrets, the first party can now evaluate at least one honestly created garbled circuit and get the desired result rA. From that it can do the “dumb witness encryption” because it has B,C to go with A that do fit the Groth16 verification equation, and it gets the desired secret value that allows it to spend (or prevent spending) in the remainder of the presigned transaction tree.
The dispute on chain. Whoever fails to move in time loses.