-
Notifications
You must be signed in to change notification settings - Fork 14
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
precomp: use normalized extended points #59
Conversation
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
62a8645
to
9d4221b
Compare
@kevaundray, this should be ready to review. I've run it in the new Kaustinen which has >30k blocks and a node with this version has synced correctly, so looks good. |
G.Add(&D, &C) | ||
H.Set(&A) | ||
|
||
// mulBy5(&H) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'm guessing this comment can be removed or put one line down
H.Neg(&H) | ||
gnarkfr.MulBy5(&H) | ||
|
||
H.Sub(&B, &H) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making this match the referenced link including the letters used :)
@@ -5,20 +5,24 @@ import ( | |||
"io" | |||
|
|||
gnarkbandersnatch "github.com/consensys/gnark-crypto/ecc/bls12-381/bandersnatch" | |||
gnarkfr "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: note that although this is gnarkfr (bls12-381), its fp for bandersnatch, so maybe gnarkfp or something along those lines would be less confusing since we use fp already in this file
Thanks to Gottfried Herold for suggesting this change in a chat we had some time ago.
This PR improves the performance of our fixed-basis MSM algorithm, which is used for most heavy cryptography operations (e.g: tree key hashing, vector commitments, etc.).
The idea is simple: use Extended points additions instead of Projective mixed additions when we aggregate the precomputed points. That saves finite-field muls, thus improving performance. It has a 50% memory cost, but considering some improvements we did a while ago (and other ideas that we can still use), it feels justified for this speedup.
The path to doing this was longer than expected since I had to do some things before to be ready for the change:
The bottom line is that we’re saving two multiplications per group operation. (Rather than one that we expected in our chat with Gottfried).
Benchmarks
AMD Ryzen 7 3800XT 8-Core Processor:
This is an
amd64
processor, so it uses gnark-crypto assembly for bigints ops.Rock5B (this machine has another version of the stat-compare, so you might see some diffs in magnitude symbols):
This is an
arm
machine, so it doesn’t use assembly (and it has a less powerful clock).The speedups in both cases are the same, which makes sense since this optimization mostly avoids work and is independent of whatever implementation of bigint is being used.
Finer details
So, it turns out that I still have to include a further optimized extended point formula optimized for the case when the second point has
Z==1
, which is the case for our precomputed points. That allows us to save one extra mul from the amount used by gnark-crypto. (More about this in PR comments).Also, I had to create a further “extended point” flavor, which I name “normalized”, since our precomputed points would have
Z==1
, so it doesn’t make sense to use gnark-crypto structs which would waste 8 bytes per precomputed point. This makes the precomp tables size to increase 50% (~110MiB) rather than 100% (~220MiB).At some point, we can chat with the gnark team if it makes sense to add a new method with this optimized formula when
Z==1
; or add an extraif
in the current procedure to avoid the addition. (In the latter, we’d have to double-check if the potential branch misprediction… but I doubt that might be relevant; we can measure).TODO: