Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sapling] Use ternary Merkle tree for note commitments #2651

Closed
daira opened this issue Oct 4, 2017 · 3 comments
Closed

[Sapling] Use ternary Merkle tree for note commitments #2651

daira opened this issue Oct 4, 2017 · 3 comments
Labels
A-circuit Area: zk-SNARK circuits A-consensus Area: Consensus rules A-crypto Area: Cryptography I-performance Problems and improvements with respect to performance M-requires-nu A network upgrade is required to implement this. not in Sapling NU1-sapling Network upgrade: Sapling-specific tasks protocol spec

Comments

@daira
Copy link
Contributor

daira commented Oct 4, 2017

In #2173 it was suggested to implement a ternary Merkle tree by making use of the chaining variable of SHA256Compress. Since we will not be using SHA256Compress for Sapling, and since the discussion on #2173 was specific to SHA256Compress, I decided to open this new ticket for a similar idea applied to Pedersen hashes (#2234).

@daira daira added A-consensus Area: Consensus rules A-crypto Area: Cryptography M-requires-nu A network upgrade is required to implement this. I-performance Problems and improvements with respect to performance protocol spec NU1-sapling Network upgrade: Sapling-specific tasks A-circuit Area: zk-SNARK circuits labels Oct 4, 2017
@daira
Copy link
Contributor Author

daira commented Oct 4, 2017

The cost of a Pedersen hash that compresses 3-to-1 is slightly less than 1.5 times the cost of one that compresses 2-to-1, because the comparison of the hash from the previous layer with the field order is not needed. Also, a 3-to-1 hash is effectively worth log2(3) ~= 1.585 of the 2-to-1 hashes, in terms of how much it expands the supported number of leaves (in this case, note commitments).

Specifically, a 3-to-1 hash costs 3*255 + 1 + 176 + (765/5) * 7 + (765/5 - 2) * 7 + 6 = 3076 constraints (about 1.460 times the cost of a 2-to-1 hash). So the overall cost improvement is a factor of around 1.585/1.460 which is 1.0856, i.e. a 7.89% improvement. Alternatively for the same cost, we can support 1.460 times fewer layers, but at the cost of the current binary depth of 29 this works out to around 5.58 times more leaves. In fact this is quantised so you would probably use 20 ternary layers, for a 0.68% cost increase but 6.49 times more leaves.

This has to be set against the complexity of implementing and specifying a ternary Merkle tree. It seems questionable whether the cost improvement is worth the complexity, but let's consider it anyway.

@daira
Copy link
Contributor Author

daira commented Oct 4, 2017

If we want a Pareto improvement, 19 ternary layers gives 2.16 times as many leaves for a 4.35% cost improvement over 29 binary layers.

(Of course this is just the cost of the Merkle tree, so the cost improvement for the whole circuit or circuits will be less.)

@daira daira added this to Discussion in Sapling Protocol Upgrade Oct 5, 2017
@daira
Copy link
Contributor Author

daira commented Nov 9, 2017

After the Pedersen hash improvements here, a 2-to-1 hash costs 1779 constraints, and a 3-to-1 hash costs 3 * 255 + 1 + 1 * 7 + 190 * 4 + (191 - 1) * 6 + 4 = 2677 constraints.

This puts a 3-to-1 hash at 1.505 times the constraint cost of a 2-to-1 hash. The difference from 1.460 calculated above is explained mainly by the overhead due to the range check having been removed. This change makes it less favourable to use a ternary hash. There is still an overall cost improvement of around 1.585/1.505 which is 1.0533, i.e. a 5.06% improvement. In this case 19 ternary layers would give 2.16 times as many leaves for a 1.41% cost improvement over 29 binary layers.

This seems hardly worth the complexity, so I will close this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-circuit Area: zk-SNARK circuits A-consensus Area: Consensus rules A-crypto Area: Cryptography I-performance Problems and improvements with respect to performance M-requires-nu A network upgrade is required to implement this. not in Sapling NU1-sapling Network upgrade: Sapling-specific tasks protocol spec
Projects
None yet
Development

No branches or pull requests

1 participant