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

Verkle Trees: Faster subgroup checks for Banderwagon via vartime Legendre symbol #419

Open
mratsim opened this issue Jul 5, 2024 · 0 comments

Comments

@mratsim
Copy link
Owner

mratsim commented Jul 5, 2024

Overview

This is a followup to #236 and #354.

For Ethereum Verkle tries, we will likely deserialize a massive number of elliptic curve points, especially during sync.

Subgroup checks are slow, for Banderwagon this is due to an isSquare check.

func isInSubgroup*(P: EC_TwEdw_Aff[Fp[Banderwagon]]): SecretBool =
## Checks if the point is in the quotient subgroup
## The group law does not change because what we quotiented by was a subgroup.
## These are still points on the bandersnatch curve and form a group under point addition.
##
## This is to be used to check if the point lies in the Banderwagon
## while importing a point from serialized bytes
var t{.noInit.}: typeof(P).F
var one{.noInit.}: typeof(P).F
one.setOne()
t.setZero()
# Compute 1 - aX^2 and check its legendre symbol
t.square(P.x)
t *= Banderwagon.getCoefA()
t.diff(one, t)
return t.isSquare()

Looking at the benchmarks on a 7840U, isSquare takes 1089ns.
image

And non-subgroup checked deserialization adds 1800ns of overhead
image

We can reduce the gap by implementing a faster isSquare.

For Ethereum Verkle Tries we don't need the constant-time property. As modular inversion and isSquare use a very similar algorithm, we can expect a conservative 27% perf improvement by adding a useVartime: static bool parameter.

Implementation

Both modular inverse constant-time, vartime and legendre symbole constant-time are in the following file:
https://github.com/mratsim/constantine/blob/0fe6bbc/constantine/math/arithmetic/limbs_exgcd.nim

The legendre symbol constant-time needs to be adaptaed in a similar manner to how modular inverse was adapted for vartime evaluation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant