Skip to content
LearnBitcoin

ResearchBitcoin Dev Archive lightning-dev list

Better privacy with SNARKs

14 messages, Nov 17 to Dec 2, 2015. Started by Anthony Towns.

Anthony Towns (7), Rusty Russell (3), Mats Jerratsch (2), Andrew Miller, Andrew Miller

Permalink First message in the Wayback Machine

Anthony Towns permalink original

Hi all,

An obvious privacy limitation with lightning is that even with onion routing, differents hops can be associated as being part of the same transaction due to sharing a common R value. So if you see a HTLC from Alice to Bob, paying $5 to Bob on receipt of R where SHA(R)=12345..; and you see another HTLC from Carol to Dave, paying $4.95 to Bob on receipt of R under the same condition, SHA(R)=12345..., then you know it's part of the same transaction.

If you could change R at each step in the route, this would go away, improving payment anonymity and making it harder to attack the system in general.

That's hard, because as a forwarding node, if you receive a HTLC payable on R1, then to send a HTLC payable on R2, you need to be able to calculate R1 from R2 or you'll be out of pocket. But you also can't be able to calculate R1 *without* R2, or you could just rip off whoever's making the payment. And, of course you have to know SHA(R2) to forward the payment at all. And if you only know SHA(R1) and SHA(R2) it's hard to say anything at all about R1 and R2 because cryptographic hash functions are designed to make any structural relationships go away.

BUT! I think the magic of SNARKs [0] lets you do this!

With a SNARK, you can "prove" that you have some secrets (ie, R1 and R2) that satisfy some programmable condition (ie, SHA(R1)=H1 and SHA(R2)=H2 and R1=R2 XOR X), based on public inputs (H1, H2 and X), without revealing those secrets.

I think that's pretty safe, because if you receive an HTLC asking for a preimage for H1, along with instructions in the onion saying ask Bob for a preimage for H2, and here's X and a proof, then either:

 - your forwarded HTLC will fail, and everything's fine
 - you'll receive R2, calculate R1=R2 XOR X and see SHA(R1)=H1 as
   expected, and everything's fine
 - you'll receive R2, calculate R1=R2 XOR X and see SHA(R1) != H1,
   which is only possible if the cryptography behind SNARKs are broken
 - you'll receive RX, such that H2=SHA(RX) but RX being too
   long or too short. If SNARKs aren't broken, this means that you know
   R2alt and someone else knows R2 that are different but hash to the
   same value, meaning SHA has been broken.

It seems like there are research-level tools out there that actually make this practical to try out. I've had a go at implementing this using snarkfront [1]. Using it looks like:

1) initial setup of proof/verification keys

$ ./test_lightning -m keygen > keygen.txt # global setup

2) generate a proof, using a 32 byte secret, and XOR key (64 hex digits)
 $ SECRET="the quick brown fox jumps lazily"
 $ XOR=$(echo "she sells sea shells" | sha256sum | head -c64)
 $ cat keygen.txt |
     ./test_lightning -m proof -s "$SECRET" -x "$XOR" > proof.txt
   m: proof.
   f: .
   b: .
   x: 5677d356ccd6ff29b296a697791d109a1703557ecbbcf52b4f38d4b680858912.
   F: 74686520717569636b2062726f776e20666f78206a756d7073206c617a696c79
   B: 221fb676bda3964ad9b6c4e5166a7eba716c2d5ea1c9985b3c18b8d7faece56b
   #F: ae4d48f71fdb6f74149fab591e88f2cc07d4e696968def1aa7ca1e07096c5b85
   #B: 166e4f9b8ec5895e870f0f0508327d73ba9ad9af4e9841599bafb1bf55c8a245
   generate proof
   (6) ..................................................
   (5) ..................................................
   (4) ..................................................
   (3) ..................................................
   (2) ..................................................
   (1) ..................................................
3) Verify the proof:
 $ F=ae4d48f71fdb6f74149fab591e88f2cc07d4e696968def1aa7ca1e07096c5b85
 $ B=166e4f9b8ec5895e870f0f0508327d73ba9ad9af4e9841599bafb1bf55c8a245
 $ cat keygen.txt proof.txt |
     ./test_lightning -m verify -h "$F" -b "$B" -x "$XOR"
   m: verify.
   f: ae4d48f71fdb6f74149fab591e88f2cc07d4e696968def1aa7ca1e07096c5b85.
   b: 166e4f9b8ec5895e870f0f0508327d73ba9ad9af4e9841599bafb1bf55c8a245.
   x: 5677d356ccd6ff29b296a697791d109a1703557ecbbcf52b4f38d4b680858912.
   verify proof (6) (5) (4) (3) (2) (1)
   proof is verified
4) Verify it doesn't report a valid proof with different inputs:
  $ cat keygen.txt proof.txt |
     ./test_lightning -m verify -h "$B" -b "$F" -x "$XOR"
   m: verify.
   f: 166e4f9b8ec5895e870f0f0508327d73ba9ad9af4e9841599bafb1bf55c8a245.
   b: ae4d48f71fdb6f74149fab591e88f2cc07d4e696968def1aa7ca1e07096c5b85.
   x: 5677d356ccd6ff29b296a697791d109a1703557ecbbcf52b4f38d4b680858912.
   verify proof (6) (5) (4) (3) (2)
   proof is rejected

Some results:

 * the proof/verification key data take up about 100MB -- in theory
   one set of this data can be used by everyone; the only catch is
   that everyone has to trust that nobody has kept the original random
   numbers used to generate it.
 * proof/verification key data takes about a minute to generate,
   and about 650MB of RAM.
 * the proof data itself (which would need to be sent to the node that's
   going to switch R's) is just 864 bytes; so it'd use up about 5 hops
   worth of onion routing at 192B per hop -- in a 4096 byte packet eg,
   you could have four hops, changing R each time; or you could have 9
   hops, changing R only three times.
 * generating the proof data for a given R1,X pair takes about 10
   seconds, and 260MB of RAM
 * verifying the proof is quick-ish -- it takes 0.5s on my laptop,
   and uses about 150MB of RAM.

For comparison, that last point makes a SNARK verification 500+ times more expensive than an ECDH operation. If I got my maths right, you can translate 3c for a linode CPU-hour into 2.5 satoshi for a linode CPU-second (at $338/BTC), so you're probably looking at a minimum fee of a few satoshi per SNARK verification, but that's still pretty okay for transactions of 500 satoshi or more, ie anything more than about a fifth of a US cent.

The 10s proof generation time is probably more of a limitation -- though you could generate them in advance easily enough and just store them until you need to use them, which would avoid lag being a problem at least. But even then it's still essentially adding up to 30c of additional costs to your transaction (ie 10s cpu time valued at up to 3c/s), which probably isn't worthwhile for transactions smaller than a dollar or two.

A drawback is that you'd either (a) have to do all this on the merchant's side (not just sending SHA(R) to whoever wants to pay you, but sending SHA(R1), SHA(R2), SHA(R3), SHA(R4), X12, X23, X34, and three proofs, which would be pretty painful; or (b) you'd have to generate all the R secrets as a consumer, and you wouldn't get to use the fact that you know R as evidence that you paid the merchant.

Anyway, it's obviously not ready for prime time today: SNARKs are still pretty new as a concept; I'm definitely not familiar enough with SNARK theory to be sure I'm not misusing the concept somehow; snarkfront may not have implemented the theory fully correctly; and I might not have captured everything I needed to in order for my "proof" to actually say what I want it to. So not a great idea to use this to protect real money today.

But still, this seems like it's not all /that/ far from being practical, and if the crypto's not fundamentally broken, seems like it goes a long way to filling in the biggest privacy hole in lightning today [3]...

Code is at https://github.com/ajtowns/snarkfront/ or more directly at: https://github.com/ajtowns/snarkfront/blob/lightning-sha/test_lightning.cpp

Cheers, aj

[0] https://tahoe-lafs.org/trac/tahoe-lafs/wiki/SNARKs
[1] https://github.com/jancarlsson/snarkfront
[2] This would also improve privacy/anonymity for other applications of
    HTLCs, such as atomic swaps across chains:
     1 bitcoin, payable on R1 + Alice's sig or timeout + Bob's sig
     100 litecoin, payable on R2 + Robert's sig or timeout + Ally's sig
    Alice and Bob communicate privately, agreeing to trade 1 BTC for 100
    litecoin and revealing their aliases Robert and Ally; Alice generates
    R1, R2, and reveals SHA(R1), SHA(R2), R1^R2 and the SNARK proof and
    publishes the litecoin payment. Bob verifies the proof, and publishes
    the bitcoin payment. Alice claims the bitcoin payment, revealing R1;
    Bob calculates R2 and claims the litecoin payment. The swap can take
    place trustlessly because Bob knows the only way Alice can claim his
    bitcoin is by revealing enough info so he can claim the corresponding
    litecoin. But there isn't any on-chain information linking the two
    transactions, because R1 and R2 are independent (and could even be
    using different hash functions as well as different preimages).
    After the funds have been claimed, the private communication is
    also completely deniable, since anyone could generate R1^R2 and a
    corresponding SNARK proof just using the info on the blockchain.
Mats Jerratsch in reply to Anthony Towns permalink original

Interesting, thanks for the write-up Anthony!

Indeed, if we can somehow prove that R1 = R2 XOR X without revealing R1 or R2, we can switch R in between the routing an arbitrary amount of time. This would solve the last obvious privacy attack vector that we have currently.

I feel like the way ZKPs are constructed, one has to be absolutely certain everything is perfectly implemented to actually work out the way we want it.

Quoted text (6 lines)
> A drawback is that you'd either (a) have to do all this on the merchant's
> side (not just sending SHA(R) to whoever wants to pay you, but sending
> SHA(R1), SHA(R2), SHA(R3), SHA(R4), X12, X23, X34, and three proofs,
> which would be pretty painful; or (b) you'd have to generate all the
> R secrets as a consumer, and you wouldn't get to use the fact that you
> know R as evidence that you paid the merchant.

I don't think (a) would be too much of a problem. I am playing around with the idea of having a messaging system over the same route as the actual payment. The data that needs to be communicated over regular channels (QR, ...) would be limited to a rendezvous node and the encrypted route from that node to the receiver. The sender can contact the receiver over that route and include the encrypted route back for further communications.

I also think that - given how young SNARKs still are - efficiency will further be improved. Especially generation and verification of the proof, such that it is no longer a major cost factor.

---------------

After a night of sleep and some reassurance with sipa, I thought about something similar but with EC keys, that will allow us to do the same, but without SNARKS.

If we would switch from preimage-hash verification to privatekey-publickey, we can use the arithmetic operations inherited from the elliptic curve field.

Assume two keypairs, K1(Q, q) and K2(R, r). Further we have a scalar p, such that

r = p * q

and

R = r * G = ( p * q ) * G = p * ( q * G ) = p * Q.

You can therefore give someone Q, R and p and he is able to verify that above conditions indeed holds true. For a sufficiently large p it is not possible to derive that relationship within reasonable time without p. If he ever gets to know q, he is able to directly compute r as well.

So instead of making a payment towards a hash, we make a payment towards a public key. If we ever get to know the private key, the payment is deemed settled.

This will allow for following construction:

(1) Bob calculates a key pair he wants to receive a payment on (2) Bob will give the public key Q to Alice (3) Alice calculates a route, consisting of 10 hops (4) Alice chooses x random large numbers N_x and calculates x public keys, such that

Q_y = Q * N_x * N_x-1 * ... * N_y.

(5) Alice constructs an onion object and includes Q_y together with N_y

Each hop is able to translate the public key towards the corresponding key for the next relay node, only Alice, with the knowledge of the set N is able to relate the payment though.

There is one caveat. We are currently unable to enforce a payment with a priv/pub key pair. We would need a new operator OP_CHECKPRIVPUBKEYPAIR or similar that pops two items from the stack

<Private Key> <Public Key>

and replaces them with true/false. It could also be constructed in a softfork-manner, where the stack is not touched and it only fails in case the key pair is not correct.

Revealing the private key has the same effect as revealing a preimage.

Cheers Mats

Rusty Russell in reply to Mats Jerratsch permalink original

Mats Jerratsch <matsjj at gmail.com> writes:

Quoted text (3 lines)
> After a night of sleep and some reassurance with sipa, I thought about
> something similar but with EC keys, that will allow us to do the same,
> but without SNARKS.

Nice job! I've been saying there might be a way using keypairs, but I'm just not this clever :)

Even then, it took me several reads to make sure I understood your post :)

Quoted text (8 lines)
> If we would switch from preimage-hash verification to
> privatekey-publickey, we can use the arithmetic operations inherited
> from the elliptic curve field.
>
> Assume two keypairs, K1(Q, q) and K2(R, r). Further we have a scalar
> p, such that
>
> r = p * q

Yes, the two private keys are related by p...

Quoted text (3 lines)
> and
>
> R = r * G = ( p * q ) * G = p * ( q * G ) = p * Q.

...And if the two public keys are related by p, we know it's true. Thus we know revealing their privkey will give us our privkey.

Quoted text (3 lines)
> There is one caveat. We are currently unable to enforce a payment with
> a priv/pub key pair. We would need a new operator
> OP_CHECKPRIVPUBKEYPAIR or similar that pops two items from the stack

Or we could do an OP_FFMUL to do multiply over a finite field and check the result, too:

        <G> OP_FFMUL <PUBKEY> OP_EQUAL

With the segregated witness proposal, introducing new opcodes is easy, so maybe someone would find a reason to prefer open-coding it like this?

Cheers, Rusty.

Anthony Towns in reply to Rusty Russell permalink original

On Thu, Nov 19, 2015 at 01:16:57PM +0000, Mats Jerratsch wrote:

Quoted text (1 line)
> Interesting, thanks for the write-up Anthony!

Glad I went ahead and implemented it as a snark before posting the idea or you would have spoiled all the fun I had playing with snarkfront!

Quoted text (3 lines)
> I feel like the way ZKPs are constructed, one has to be absolutely
> certain everything is perfectly implemented to actually work out the
> way we want it.

Yeah. I think you could have a "stop-loss" condition, where any node that gets cheated (ie, they have a proof that R2 gives them and R1 for H1,H2,X; but they have an R2 that doesn't give them R1) can just publish all that evidence, and everyone can immediately stop trusting snarks and so only a small amount of money can actually be stolen from the system. But, yeah.

Quoted text (4 lines)
> ---------------
> After a night of sleep and some reassurance with sipa, I thought about
> something similar but with EC keys, that will allow us to do the same,
> but without SNARKS.

So this is genius! And I swear I would have thought of it myself if I could just get past my mental block on adding opcodes to bitcoin. Honest, guv!

Quoted text (10 lines)
> Assume two keypairs, K1(Q, q) and K2(R, r). Further we have a scalar
> p, such that
> r = p * q
> and
> R = r * G = ( p * q ) * G = p * ( q * G ) = p * Q.
> You can therefore give someone Q, R and p and he is able to verify
> that above conditions indeed holds true. For a sufficiently large p it
> is not possible to derive that relationship within reasonable time
> without p. If he ever gets to know q, he is able to directly compute r
> as well.

Even better, given K1(Q,q) (q the public key provided by the merchant, Q the merchant's secret, used to claim the payment and serving as a receipt), you (as the consumer) can construct r2, .., rN by generating random bits, and then generate

  q1 = q
  q2 = r2*q1
  q3 = r3*q2
  ...
  qN = rN*q[N-1]

and route your HTLC payable to qN at the first hop, q[N-1] at the second all the way to q1 when it gets to the merchant. The merchant reveals Q=Q1, and nodes calculate:

  Q2 = r2*Q1
  Q3 = r3*Q2
  ...
  QN = rN*Q[N-1]

to collect their payments. So the extra burden for keeping the payment private falls entirely on the consumer, rather than the merchant. (Okay, you had this point in your mail already essentially)

Hmm, I'm not sure if you can divide QN by (r2*..*rN) to get back to Q1, but I think you can (the lowercase q's are the points on the elliptic curve where division is hard, but the capital Q's are just numbers in Z_n where n is the order of G on the curve, I think?). If you can, you even get the original receipt/proof of payment!

Oh, _and_ you don't even having to reveal q1/Q to any intermediate hops -- you could just pay to q[N-1] on the final hop and provide rN in the onion message on the final hop. If you do that, nobody but the merchant and the end consumer can calculate the proof of payment.

_And_ I think you could just use SHA(ECDH_SEC || 3) as the r values at
each stage rather than needing any additional entropy, or having to add
any significant data to the onion packets.
Quoted text (5 lines)
> There is one caveat. We are currently unable to enforce a payment with
> a priv/pub key pair. We would need a new operator
> OP_CHECKPRIVPUBKEYPAIR or similar that pops two items from the stack
> <Private Key> <Public Key>
> and replaces them with true/false.

There are a few "generic" crypto ops that might be useful to have; two that come to mind are:

  <point> <multiplicand> <resultpoint> OP_CHECK_SECP256K1_MUL_VERIFY
    (enough for this, maybe lets you do ECDH on the blockchain,
     and allows you to commit to revealing a key on the blockchain)
  <signature> <pubkey> <message> OP_CHECK_INLINE_SIGNATURE_VERIFY
    (this would let you do atomic cross-chain swaps without knowing in
     advance who you're swapping with)

Maybe it'd make sense to combine them into a soft-forkable OP_CRYPTO_OP that pulls the crypto operation id from the stack, then if the operation is known, pulls the operands, and fails the script if the operands don't add up. If the operation is unknown, mark it as non-standard but acceptable for future soft-forkability...

On Fri, Nov 20, 2015 at 12:05:46PM +1030, Rusty Russell wrote:

Quoted text (2 lines)
> With the segregated witness proposal, introducing new opcodes is easy,
> so maybe someone would find a reason to prefer open-coding it like this?

I don't follow how segregated witness makes new opcodes any easier?

Cheers, aj

Anthony Towns in reply to Anthony Towns permalink original

On Fri, Nov 20, 2015 at 05:44:15PM +1000, Anthony Towns wrote:

Quoted text (3 lines)
> Hmm, I'm not sure if you can divide QN by (r2*..*rN) to get back to Q1,
> but I think you can [...] If you can,
> you even get the original receipt/proof of payment!

Yep, this works!

Quoted text (3 lines)
> _And_ I think you could just use SHA(ECDH_SEC || 3) as the r values at
> each stage rather than needing any additional entropy, or having to add
> any significant data to the onion packets.

This doesn't quite, though: if a txn routes from Alice through Bob to Carol, with Alice/Bob's secret being p,P and Bob/Carol's being q,Q, with p = q*r and P = Q*r; Alice has to pass on both p and q; p as part of the HTLC contract, and q inside the onion payload because calculating q=p/r is infeasible unless elliptic curve crypto is broken.

So add an extra 32B of payload to each onion hop if calculating r from the ECDH secret is fine, or 64B of payload if it's not.

Cheers, aj

Rusty Russell in reply to Anthony Towns permalink original

Anthony Towns <aj at erisian.com.au> writes:

Quoted text (5 lines)
> On Fri, Nov 20, 2015 at 12:05:46PM +1030, Rusty Russell wrote:
>> With the segregated witness proposal, introducing new opcodes is easy,
>> so maybe someone would find a reason to prefer open-coding it like this?
>
> I don't follow how segregated witness makes new opcodes any easier?

I didn't either, and that's because it's slightly orthogonal.

The proposal I heard is that the first byte of SW script is a version byte, and if you don't understand that version, the script succeeds.

Cheers, Rusty.

Anthony Towns in reply to Rusty Russell permalink original

On 24 November 2015 1:30:19 pm AEST, Rusty Russell <rusty at rustcorp.com.au> wrote:

Quoted text (13 lines)
>Anthony Towns <aj at erisian.com.au> writes:
>> On Fri, Nov 20, 2015 at 12:05:46PM +1030, Rusty Russell wrote:
>>> With the segregated witness proposal, introducing new opcodes is
>easy,
>>> so maybe someone would find a reason to prefer open-coding it like
>this?
>>
>> I don't follow how segregated witness makes new opcodes any easier?
>
>I didn't either, and that's because it's slightly orthogonal.
>
>The proposal I heard is that the first byte of SW script is a version
>byte, and if you don't understand that version, the script succeeds.

Ah, I see - it doesn't make OP_CHECK*VERIFY any easier then, but adding ops that actually change the contents of the stack becomes a soft fork instead of a hard fork. Pretty neat. Don't think that's needed here though.

Cheers, aj

Andrew Miller in reply to Anthony Towns permalink original

Even though it seems like SNARKs aren't strictly necessary for this application, it's awesome you tried that out!

So, there are a couple of things that seem off to me about those performance estimates. I don't know snarklib/snarkfront well so I'm not sure what would cause this. But checking a single snark proof should take around 10 milliseconds rather than 500 milliseconds. And it should only require like a kilobyte of memory to verify, definitely not 150 MB. Also when you say that the verification/proving key is 100MB, those are two very different keys! The verification key should be extremely small, in the kilobyte range.

When I have some time (hopefully later this week) I'll try to reproduce this experiment using libsnark and write back in this thread. Some students at UMD also have a tool for composing circuits that is (arguably) more user-friendly and complements libsnark, I'm really hoping they release those open source soon!

On Tue, Nov 24, 2015 at 12:45 AM, Anthony Towns <aj at erisian.com.au> wrote:

Quoted text (31 lines)
> On 24 November 2015 1:30:19 pm AEST, Rusty Russell <rusty at rustcorp.com.au>
> wrote:
> >Anthony Towns <aj at erisian.com.au> writes:
> >> On Fri, Nov 20, 2015 at 12:05:46PM +1030, Rusty Russell wrote:
> >>> With the segregated witness proposal, introducing new opcodes is
> >easy,
> >>> so maybe someone would find a reason to prefer open-coding it like
> >this?
> >>
> >> I don't follow how segregated witness makes new opcodes any easier?
> >
> >I didn't either, and that's because it's slightly orthogonal.
> >
> >The proposal I heard is that the first byte of SW script is a version
> >byte, and if you don't understand that version, the script succeeds.
>
> Ah, I see - it doesn't make OP_CHECK*VERIFY any easier then, but adding
> ops that actually change the contents of the stack becomes a soft fork
> instead of a hard fork. Pretty neat. Don't think that's needed here though.
>
> Cheers,
> aj
>
>
> --
> Sent from my phone.
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
>
Anthony Towns in reply to Anthony Towns permalink original

On Fri, Nov 20, 2015 at 05:44:15PM +1000, Anthony Towns wrote:

Quoted text (6 lines)
> > After a night of sleep and some reassurance with sipa, I thought about
> > something similar but with EC keys, that will allow us to do the same,
> > but without SNARKS.
> So this is genius! And I swear I would have thought of it myself if
> I could just get past my mental block on adding opcodes to bitcoin.
> Honest, guv!

And wow, it looks like you can do it without adding opcodes to bitcoin.

Two approaches to forcing someone to reveal the private key corresponding to secp256k1 public key P. Number one, which Greg Maxwell came up with:

  OP_SIZE 57 OP_LESSTHANOREQUAL OP_VERIFY <P> OP_CHECKSIGVERIFY

To satisfy this script, you have to generate a signature with P, that produces <r> and <s> parameters for the signature that have a combined total of 15 leading zero bytes (or more). There is a known <r> value with 11 leading zero bytes though: g^(1/2), so you need to brute force about 2**32=4B <s> parameters to get a valid signature, and that's just modifing the transaction, hashing it, and doing modular arithmetic ops. It might mean paying for a few seconds use of dedicated mining hardware though.

Using that <r> value reveals the secret key p: p = (2s - h)/r (mod O(g)).

If you want to cheat, you can brute force a secret key N with corresponding public key r with as many leading zero bytes as possible. Greg Maxwell thinks grinding r values at a rate 0.08 microseconds per try is practical, so that's ~10e6/second. Doing that on 2000 8-core machines for abut a week gets you an r-value with 7 leading 0-bytes. Getting 8 leading 0-bytes might take 20k machines and four months.

With 7 leading zeroes in r, you still need 8 leading zeroes in s, which would require about 213,000 GH/s worth of mining hardware running for 24 hours to achieve. With 8 leading zeroes in r, you'd only need 7 leading zeroes in s, which you could get in 1 hour with 20GH/s of mining hardware.

The alternative approach, which andytoshi and I came up with independently is a lot more complicated:

  revealP( Q, R, sigA, sigB, sigC ) {
    check_multisig_verify(2, P, R, 2, sigA, sigB); code_separtor();
    check_multisig_verify(2, Q, R, 2, sigA, sigC); code_separtor();
    check_multisig_verify(2, P, Q, 2, sigC, sigB);
  }

If sigA, sigB and sigC all share the same r and SIGHASH settings, coming up with secret keys Q' and R' is straightforward (Q'=P'-(h2-h1)/r, R'=P'-(h2-h3)/r, where h1, h2 and h3 are the transaction hashes for the various steps), and you have two valid sigs by key P with the same r value, letting you calculate P'. If you don't use the same r value, or use different sighash types between the signatures, coming up with valid keys and sigs seems to require doing discrete logs on the elliptic curve, so should be intractable. (In particular, I don't think throwing lots of hashpower at the problem helps at all)

But if you have to drop the transaction to the blockchain, it's six sigops, which combined with the two other signatures an HTLC needs to be usable (one for A on timeout, one for B on success), means a total of 8 sigops per output, which is about equivalent to 400B per output given the relationship between the bytes-per-block and sigops-per-block limits. Yikes. Translating from pseudocode into script is a little hard too.

Quoted text (5 lines)
> > Assume two keypairs, K1(Q, q) and K2(R, r). Further we have a scalar
> > p, such that
> > r = p * q
> > and
> > R = r * G = ( p * q ) * G = p * ( q * G ) = p * Q.

Greg Maxwell also pointed out you can also do this faster while still being secure; assuming Q was the public key from the incoming HTLC, and P is the public key you'll use for the outgoing HTLC, and r is your secret:

p = q + r P = (q+r)*G = q*G + r*G = Q + r*G

Given Q is already known, calculating P just requires multiplying the base point and an addition, which is quicker than multiplying an arbitrary point. And once you find out p, calculating q=p-r is obviously easy.

Cheers, aj

Rusty Russell in reply to Anthony Towns permalink original

Anthony Towns <aj at erisian.com.au> writes:

Quoted text (36 lines)
> On Fri, Nov 20, 2015 at 05:44:15PM +1000, Anthony Towns wrote:
>> > After a night of sleep and some reassurance with sipa, I thought about
>> > something similar but with EC keys, that will allow us to do the same,
>> > but without SNARKS.
>> So this is genius! And I swear I would have thought of it myself if
>> I could just get past my mental block on adding opcodes to bitcoin.
>> Honest, guv!
>
> And wow, it looks like you can do it without adding opcodes to bitcoin.
>
> Two approaches to forcing someone to reveal the private key corresponding
> to secp256k1 public key P. Number one, which Greg Maxwell came up with:
>
>   OP_SIZE 57 OP_LESSTHANOREQUAL OP_VERIFY <P> OP_CHECKSIGVERIFY
>
> To satisfy this script, you have to generate a signature with P, that
> produces <r> and <s> parameters for the signature that have a combined
> total of 15 leading zero bytes (or more). There is a known <r> value with
> 11 leading zero bytes though: g^(1/2), so you need to brute force about
> 2**32=4B <s> parameters to get a valid signature, and that's just modifing
> the transaction, hashing it, and doing modular arithmetic ops.  It might
> mean paying for a few seconds use of dedicated mining hardware though.
>
> Using that <r> value reveals the secret key p: p = (2s - h)/r (mod O(g)).
>
> If you want to cheat, you can brute force a secret key N with
> corresponding public key r with as many leading zero bytes as possible.
> Greg Maxwell thinks grinding r values at a rate 0.08 microseconds per
> try is practical, so that's ~10e6/second. Doing that on 2000 8-core
> machines for abut a week gets you an r-value with 7 leading 0-bytes.
> Getting 8 leading 0-bytes might take 20k machines and four months.
>
> With 7 leading zeroes in r, you still need 8 leading zeroes in s, which
> would require about 213,000 GH/s worth of mining hardware running for 24
> hours to achieve. With 8 leading zeroes in r, you'd only need 7 leading
> zeroes in s, which you could get in 1 hour with 20GH/s of mining hardware.

Yes, this is very clever. But since it's slow, insecure or both, I don't think we should go for it.

Quoted text (10 lines)
> The alternative approach, which andytoshi and I came up with
> independently is a lot more complicated:
>
>   revealP( Q, R, sigA, sigB, sigC ) {
>     check_multisig_verify(2, P, R, 2, sigA, sigB); code_separtor();
>     check_multisig_verify(2, Q, R, 2, sigA, sigC); code_separtor();
>     check_multisig_verify(2, P, Q, 2, sigC, sigB);
>   }
>
> If sigA, sigB and sigC all share the same r and SIGHASH settings,

I don't think this works? We can't provide the signatures in the scriptPubkey, since that requires them signing themselves. We can't have them provide it in the scriptSig, since theres no "do these have the same r value" operator in script. All those ops got disabled :(

Quoted text (16 lines)
>> > Assume two keypairs, K1(Q, q) and K2(R, r). Further we have a scalar
>> > p, such that
>> > r = p * q
>> > and
>> > R = r * G = ( p * q ) * G = p * ( q * G ) = p * Q.
>
> Greg Maxwell also pointed out you can also do this faster while still
> being secure; assuming Q was the public key from the incoming HTLC, and P
> is the public key you'll use for the outgoing HTLC, and r is your secret:
>
>  p = q + r
>  P = (q+r)*G = q*G + r*G = Q + r*G
>
> Given Q is already known, calculating P just requires multiplying the
> base point and an addition, which is quicker than multiplying an arbitrary
> point. And once you find out p, calculating q=p-r is obviously easy.

Yes, this is a nice optimization.

Cheers, Rusty.

Anthony Towns in reply to Rusty Russell permalink original

On Fri, Nov 27, 2015 at 02:25:09PM +1030, Rusty Russell wrote:

Quoted text (11 lines)
> Anthony Towns <aj at erisian.com.au> writes:
> > The alternative approach, which andytoshi and I came up with
> > independently is a lot more complicated:
> >   revealP( Q, R, sigA, sigB, sigC ) {
> >     check_multisig_verify(2, P, R, 2, sigA, sigB); code_separtor();
> >     check_multisig_verify(2, Q, R, 2, sigA, sigC); code_separtor();
> >     check_multisig_verify(2, P, Q, 2, sigC, sigB);
> >   }
> > If sigA, sigB and sigC all share the same r and SIGHASH settings,
> I don't think this works?  We can't provide the signatures in the
> scriptPubkey, since that requires them signing themselves.

The scriptPubkey has the pubkey P, and a whole mess of stack operations to implement the function above; the scriptSig just has Q, R and the three signatures.

Quoted text (3 lines)
> We can't
> have them provide it in the scriptSig, since theres no "do these have
> the same r value" operator in script.

There's six sig ops, but only three different signatures. Getting the various combinations to have the same signature forces the same r value between each of the signatures, without needing a separate op to check it explicitly.

It's mathematically possible to come up with Q, R, sigA, sigB, sigC where sigA.r, sigB.r and sigC.r are all different; but it requires being able to come up with a transaction with a particular hash, or calculating the discrete log of a weird value to do so, so should be safely intractable.

Cheers, aj

Mats Jerratsch in reply to Anthony Towns permalink original

I love how people come up with sneaky solutions to force the other party to reveal the private key somehow. But given that we might have dozens of HTLCs in a commit transactions or also payments that pay to two (or more) payment keys (is this the new term for it now?) we should maybe strive for a more clean solution.

I have summarized most of the information of this discussion and 'handed in' a BIP request to the bitcoin-dev mailing list. If the use case is good enough (and I think it is) and there are little draw backs (can't think of any security-relevant one), there should not be too much resistance.

http://lists.linuxfoundation.org/pipermail/bitcoin-dev/2015-November/011827.html

Cheers, Mats

2015-11-27 5:42 GMT+01:00 Anthony Towns <aj at erisian.com.au>:

Quoted text (38 lines)
> On Fri, Nov 27, 2015 at 02:25:09PM +1030, Rusty Russell wrote:
>> Anthony Towns <aj at erisian.com.au> writes:
>> > The alternative approach, which andytoshi and I came up with
>> > independently is a lot more complicated:
>> >   revealP( Q, R, sigA, sigB, sigC ) {
>> >     check_multisig_verify(2, P, R, 2, sigA, sigB); code_separtor();
>> >     check_multisig_verify(2, Q, R, 2, sigA, sigC); code_separtor();
>> >     check_multisig_verify(2, P, Q, 2, sigC, sigB);
>> >   }
>> > If sigA, sigB and sigC all share the same r and SIGHASH settings,
>> I don't think this works?  We can't provide the signatures in the
>> scriptPubkey, since that requires them signing themselves.
>
> The scriptPubkey has the pubkey P, and a whole mess of stack operations
> to implement the function above; the scriptSig just has Q, R and the
> three signatures.
>
>> We can't
>> have them provide it in the scriptSig, since theres no "do these have
>> the same r value" operator in script.
>
> There's six sig ops, but only three different signatures. Getting the
> various combinations to have the same signature forces the same r value
> between each of the signatures, without needing a separate op to check
> it explicitly.
>
> It's mathematically possible to come up with Q, R, sigA, sigB, sigC where
> sigA.r, sigB.r and sigC.r are all different; but it requires being able
> to come up with a transaction with a particular hash, or calculating the
> discrete log of a weird value to do so, so should be safely intractable.
>
> Cheers,
> aj
>
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
Anthony Towns in reply to Anthony Towns permalink original

On Thu, Nov 26, 2015 at 09:15:16PM +1000, Anthony Towns wrote:

Quoted text (6 lines)
>   revealP( Q, R, sigA, sigB, sigC ) {
>     check_multisig_verify(2, P, R, 2, sigA, sigB); code_separtor();
>     check_multisig_verify(2, Q, R, 2, sigA, sigC); code_separtor();
>     check_multisig_verify(2, P, Q, 2, sigC, sigB);
>   }
> Translating from pseudocode into script is a little hard too.

I think:

  2 5 ROLL 5 ROLL 2 <P> 5 ROLL
  2 8 ROLL 6 PICK 2 5 PICK 11 ROLL
  2 5 PICK 11 PICK 2 10 PICK 5 PICK
  C_MSIG_V CODESEP C_MSIG_V CODESEP C_MSIG_V

might work (signing by providing sig_a, sig_b, sig_c, q_pub, r_pub). That's 34B of operators and 31B for P, for the redeem script, and 3*59B for signatures made using using N=g/2, 2*31B for Q and R, and 5B to push them on the stack, for a total of 65B redeem script and 244B scriptsig. (There's no need to use N=g/2 in this scheme, but since you're intending to reveal P/Q/R, there's no reason not to and it saves a few bytes, so...)

That compares to: "SHA256 <H> EQUALVERIFY" giving a 35B redeem script, and a 33B scriptsig; so the privacy gain would be at a cost of an extra 236B per HTLC per channel, when cleared on the blockchain.

At 40 satoshis per byte, the extra cost per HTLC is 9440 satoshi, which is about 3c per HTLC by my count.

I think a minimal HTLC looks something like:

 IF_DUP IF 1SUB
   IF SHA256 <revoke> EQUALVERIFY ELSE <reveal R> ENDIF <a_pub>
 ELSE
   <t> CLTV <d> CSV <b_pub>
 ENDIF
 CHECKSIG

for the payer's commitment, spendable with:

  <sigA> <revoke secret> 2
  <sigA> <htlc preimage> 1
  <sigB> 0

That's 19 opcodes, 2*33B pubkeys, 1*32B revoke hash, and however many bytes to reveal R. So another 117B, plus another 67B for the signature and branch code. To spend an HTLC, you also need the other bits for a txin, which is 42 bytes:

  42+117+67+35     = 261B  (sha256, refunding HTLC)
  42+117+67+65     = 291B  (ecc, refunding HTLC)
  42+117+67+35+33  = 294B  (sha256, claiming HTLC)
  42+117+67+65+244 = 531B  (ecc, claiming HTLC)

So it's really only an overhead of ~80% when claiming, and ~11% when refunding? So at $350/BTC, 40 satoshi per byte, that's 7.4c for an ECC claim versus 4.1c for a SHA256 claim.

I think you'd want to account for that as "cost of closing a channel is 4c (or 7c) times the number of simultaneous incoming HTLCs I'll accept, plus 4c times the number of simultaneous outgoing HTLCs I'll do".

(Trying to make sure every HTLC pays you at least 4c in fees isn't workable if you want to support really small microtransactions, obviously)

That said, it still seems like you might be better off just forgetting a 2c HTLC as far as the commitment tx goes, if it's going to cost you 4c to collect it. Account for it while the channel's open, sure, and update balances when it goes through, but actually committing to it looks like it might be a net negative... (If you've got some subscription with a miner such that you don't pay per byte for your transactions, that would change things too, I guess)

Cheers, aj

Andrew Miller in reply to Anthony Towns permalink original

So, it turns out that a SNARK is overkill for the application in this thread. It was just a one-off experiment. But the performance estimates seemed pretty far off compared to what’s reported in in the snark science papers. So, Sean (a Zcash engineer, in his “20% fun time”) replicated the experiment from the original post, in libsnark (not snarklib).

First, as a recap, this is the specification of the original post’s SNARK, summarized in Camenisch-Stadler notation: ZkPoK{ (R1, R2): H1 = sha256(R1) and H2 = sha256(R2) and R1 = R2 xor X }

In other words: given a statement H1, H2, and X, you can prove that you know a secret witness R1, and R2, such that the predicate holds (i.e., R1 and R2 are preimages of H1,H2 and are related by X)

This is almost a complete specification of the SNARK: all that’s left is to choose a representation size for R1 and R2…. here these are forced to be 32-byte strings.

Here are the results for a libsnark implementation of this, ordered up to the above spec: https://github.com/ebfull/lightning_circuit/

Most relevant benchmarks: key generation time: 11.6270s proof generation time: 3.0968s verification time: 0.0270s proof size: ~287 bytes proving key size: ~12.85 megabytes verifying key size: ~573 bytes R1CS constraints: 56612 (mostly sha256-related)

The difference in proof time and proving key size (this is ~3x faster to prove, with ~10x less key size) might be explained by libsnark having a more efficient SHA256 implementation. The libsnark one is hand-optimized. But I don’t have any idea why the OP and snarklib reported such large verification keys or memory consumption during verification. The verifier should only have to think of about a kilobyte.

On Tue, Nov 17, 2015 at 4:14 PM, Anthony Towns <aj at erisian.com.au> wrote:

Quoted text (192 lines)
> Hi all,
>
> An obvious privacy limitation with lightning is that even with onion
> routing, differents hops can be associated as being part of the same
> transaction due to sharing a common R value. So if you see a HTLC from
> Alice to Bob, paying $5 to Bob on receipt of R where SHA(R)=12345..;
> and you see another HTLC from Carol to Dave, paying $4.95 to Bob on
> receipt of R under the same condition, SHA(R)=12345..., then you know
> it's part of the same transaction.
>
> If you could change R at each step in the route, this would go away,
> improving payment anonymity and making it harder to attack the system in
> general.
>
> That's hard, because as a forwarding node, if you receive a HTLC payable
> on R1, then to send a HTLC payable on R2, you need to be able to
> calculate R1 from R2 or you'll be out of pocket. But you also can't be
> able to calculate R1 *without* R2, or you could just rip off whoever's
> making the payment. And, of course you have to know SHA(R2) to forward the
> payment at all. And if you only know SHA(R1) and SHA(R2) it's hard to
> say anything at all about R1 and R2 because cryptographic hash functions
> are designed to make any structural relationships go away.
>
> BUT! I think the magic of SNARKs [0] lets you do this!
>
> With a SNARK, you can "prove" that you have some secrets (ie, R1 and R2)
> that satisfy some programmable condition (ie, SHA(R1)=H1 and SHA(R2)=H2
> and R1=R2 XOR X), based on public inputs (H1, H2 and X), without revealing
> those secrets.
>
> I think that's pretty safe, because if you receive an HTLC asking for a
> preimage for H1, along with instructions in the onion saying ask Bob for
> a preimage for H2, and here's X and a proof, then either:
>
>  - your forwarded HTLC will fail, and everything's fine
>
>  - you'll receive R2, calculate R1=R2 XOR X and see SHA(R1)=H1 as
>    expected, and everything's fine
>
>  - you'll receive R2, calculate R1=R2 XOR X and see SHA(R1) != H1,
>    which is only possible if the cryptography behind SNARKs are broken
>
>  - you'll receive RX, such that H2=SHA(RX) but RX being too
>    long or too short. If SNARKs aren't broken, this means that you know
>    R2alt and someone else knows R2 that are different but hash to the
>    same value, meaning SHA has been broken.
>
> It seems like there are research-level tools out there that actually
> make this practical to try out. I've had a go at implementing this using
> snarkfront [1]. Using it looks like:
>
> 1) initial setup of proof/verification keys
>
>  $ ./test_lightning -m keygen > keygen.txt  # global setup
>
> 2) generate a proof, using a 32 byte secret, and XOR key (64 hex digits)
>
>  $ SECRET="the quick brown fox jumps lazily"
>  $ XOR=$(echo "she sells sea shells" | sha256sum | head -c64)
>  $ cat keygen.txt |
>      ./test_lightning -m proof -s "$SECRET" -x "$XOR" > proof.txt
>    m: proof.
>    f: .
>    b: .
>    x: 5677d356ccd6ff29b296a697791d109a1703557ecbbcf52b4f38d4b680858912.
>    F: 74686520717569636b2062726f776e20666f78206a756d7073206c617a696c79
>    B: 221fb676bda3964ad9b6c4e5166a7eba716c2d5ea1c9985b3c18b8d7faece56b
>    #F: ae4d48f71fdb6f74149fab591e88f2cc07d4e696968def1aa7ca1e07096c5b85
>    #B: 166e4f9b8ec5895e870f0f0508327d73ba9ad9af4e9841599bafb1bf55c8a245
>    generate proof
>    (6) ..................................................
>    (5) ..................................................
>    (4) ..................................................
>    (3) ..................................................
>    (2) ..................................................
>    (1) ..................................................
>
> 3) Verify the proof:
>
>  $ F=ae4d48f71fdb6f74149fab591e88f2cc07d4e696968def1aa7ca1e07096c5b85
>  $ B=166e4f9b8ec5895e870f0f0508327d73ba9ad9af4e9841599bafb1bf55c8a245
>  $ cat keygen.txt proof.txt |
>      ./test_lightning -m verify -h "$F" -b "$B" -x "$XOR"
>    m: verify.
>    f: ae4d48f71fdb6f74149fab591e88f2cc07d4e696968def1aa7ca1e07096c5b85.
>    b: 166e4f9b8ec5895e870f0f0508327d73ba9ad9af4e9841599bafb1bf55c8a245.
>    x: 5677d356ccd6ff29b296a697791d109a1703557ecbbcf52b4f38d4b680858912.
>    verify proof (6) (5) (4) (3) (2) (1)
>    proof is verified
>
> 4) Verify it doesn't report a valid proof with different inputs:
>
>   $ cat keygen.txt proof.txt |
>      ./test_lightning -m verify -h "$B" -b "$F" -x "$XOR"
>    m: verify.
>    f: 166e4f9b8ec5895e870f0f0508327d73ba9ad9af4e9841599bafb1bf55c8a245.
>    b: ae4d48f71fdb6f74149fab591e88f2cc07d4e696968def1aa7ca1e07096c5b85.
>    x: 5677d356ccd6ff29b296a697791d109a1703557ecbbcf52b4f38d4b680858912.
>    verify proof (6) (5) (4) (3) (2)
>    proof is rejected
>
> Some results:
>
>  * the proof/verification key data take up about 100MB -- in theory
>    one set of this data can be used by everyone; the only catch is
>    that everyone has to trust that nobody has kept the original random
>    numbers used to generate it.
>
>  * proof/verification key data takes about a minute to generate,
>    and about 650MB of RAM.
>
>  * the proof data itself (which would need to be sent to the node that's
>    going to switch R's) is just 864 bytes; so it'd use up about 5 hops
>    worth of onion routing at 192B per hop -- in a 4096 byte packet eg,
>    you could have four hops, changing R each time; or you could have 9
>    hops, changing R only three times.
>
>  * generating the proof data for a given R1,X pair takes about 10
>    seconds, and 260MB of RAM
>
>  * verifying the proof is quick-ish -- it takes 0.5s on my laptop,
>    and uses about 150MB of RAM.
>
> For comparison, that last point makes a SNARK verification 500+ times
> more expensive than an ECDH operation. If I got my maths right, you
> can translate 3c for a linode CPU-hour into 2.5 satoshi for a linode
> CPU-second (at $338/BTC), so you're probably looking at a minimum fee
> of a few satoshi per SNARK verification, but that's still pretty okay
> for transactions of 500 satoshi or more, ie anything more than about a
> fifth of a US cent.
>
> The 10s proof generation time is probably more of a limitation -- though
> you could generate them in advance easily enough and just store them until
> you need to use them, which would avoid lag being a problem at least. But
> even then it's still essentially adding up to 30c of additional costs to
> your transaction (ie 10s cpu time valued at up to 3c/s), which probably
> isn't worthwhile for transactions smaller than a dollar or two.
>
> A drawback is that you'd either (a) have to do all this on the merchant's
> side (not just sending SHA(R) to whoever wants to pay you, but sending
> SHA(R1), SHA(R2), SHA(R3), SHA(R4), X12, X23, X34, and three proofs,
> which would be pretty painful; or (b) you'd have to generate all the
> R secrets as a consumer, and you wouldn't get to use the fact that you
> know R as evidence that you paid the merchant.
>
> Anyway, it's obviously not ready for prime time today: SNARKs are still
> pretty new as a concept; I'm definitely not familiar enough with SNARK
> theory to be sure I'm not misusing the concept somehow; snarkfront may not
> have implemented the theory fully correctly; and I might not have captured
> everything I needed to in order for my "proof" to actually say what I
> want it to. So not a great idea to use this to protect real money today.
>
> But still, this seems like it's not all /that/ far from being practical,
> and if the crypto's not fundamentally broken, seems like it goes a long
> way to filling in the biggest privacy hole in lightning today [3]...
>
> Code is at https://github.com/ajtowns/snarkfront/ or more directly at:
> https://github.com/ajtowns/snarkfront/blob/lightning-sha/test_lightning.cpp
>
> Cheers,
> aj
>
> [0] https://tahoe-lafs.org/trac/tahoe-lafs/wiki/SNARKs
>
> [1] https://github.com/jancarlsson/snarkfront
>
> [2] This would also improve privacy/anonymity for other applications of
>     HTLCs, such as atomic swaps across chains:
>
>      1 bitcoin, payable on R1 + Alice's sig or timeout + Bob's sig
>      100 litecoin, payable on R2 + Robert's sig or timeout + Ally's sig
>
>     Alice and Bob communicate privately, agreeing to trade 1 BTC for 100
>     litecoin and revealing their aliases Robert and Ally; Alice generates
>     R1, R2, and reveals SHA(R1), SHA(R2), R1^R2 and the SNARK proof and
>     publishes the litecoin payment. Bob verifies the proof, and publishes
>     the bitcoin payment. Alice claims the bitcoin payment, revealing R1;
>     Bob calculates R2 and claims the litecoin payment. The swap can take
>     place trustlessly because Bob knows the only way Alice can claim his
>     bitcoin is by revealing enough info so he can claim the corresponding
>     litecoin. But there isn't any on-chain information linking the two
>     transactions, because R1 and R2 are independent (and could even be
>     using different hash functions as well as different preimages).
>     After the funds have been claimed, the private communication is
>     also completely deniable, since anyone could generate R1^R2 and a
>     corresponding SNARK proof just using the info on the blockchain.
>
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
>