-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
592403e
bandersnatch: add extended normalized point
jsign 40a8ff7
precomp: use extended normalized points and optimized formula
jsign 0e450b1
add comment
jsign 6676c28
bandersnatch: improve PointExtendedFromProj
jsign 6a385c1
fix comment link
jsign 9d4221b
improve naming
jsign File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
"github.com/crate-crypto/go-ipa/bandersnatch/fp" | ||
) | ||
|
||
var CurveParams = gnarkbandersnatch.GetEdwardsCurve() | ||
|
||
type PointAffine = gnarkbandersnatch.PointAffine | ||
type PointProj = gnarkbandersnatch.PointProj | ||
type PointExtended = gnarkbandersnatch.PointExtended | ||
|
||
var Identity = PointProj{ | ||
X: fp.Zero(), | ||
Y: fp.One(), | ||
Z: fp.One(), | ||
} | ||
|
||
var IdentityExt = PointExtendedFromProj(&Identity) | ||
|
||
// Reads an uncompressed affine point | ||
// Point is not guaranteed to be in the prime subgroup | ||
func ReadUncompressedPoint(r io.Reader) (PointAffine, error) { | ||
|
@@ -92,3 +96,62 @@ func computeY(x *fp.Element, choose_largest bool) *fp.Element { | |
return sqrtY.Neg(sqrtY) | ||
} | ||
} | ||
|
||
// PointExtendedFromProj converts a point in projective coordinates to extended coordinates. | ||
func PointExtendedFromProj(p *PointProj) PointExtended { | ||
var pzinv fp.Element | ||
pzinv.Inverse(&p.Z) | ||
var z fp.Element | ||
z.Mul(&p.X, &p.Y).Mul(&z, &pzinv) | ||
return PointExtended{ | ||
X: p.X, | ||
Y: p.Y, | ||
Z: p.Z, | ||
T: z, | ||
} | ||
} | ||
kevaundray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// PointExtendedNormalized is an extended point which is normalized. | ||
// i.e: Z=1. We store it this way to save 32 bytes per point in memory. | ||
type PointExtendedNormalized struct { | ||
X, Y, T gnarkfr.Element | ||
} | ||
kevaundray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Neg computes p = -p1 | ||
func (p *PointExtendedNormalized) Neg(p1 *PointExtendedNormalized) *PointExtendedNormalized { | ||
p.X.Neg(&p1.X) | ||
p.Y = p1.Y | ||
p.T.Neg(&p1.T) | ||
return p | ||
} | ||
kevaundray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// ExtendedAddNormalized computes p = p1 + p2. | ||
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-madd-2008-hwcd | ||
func ExtendedAddNormalized(p, p1 *PointExtended, p2 *PointExtendedNormalized) *gnarkbandersnatch.PointExtended { | ||
kevaundray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var A, B, C, D, E, F, G, H, tmp gnarkfr.Element | ||
A.Mul(&p1.X, &p2.X) | ||
B.Mul(&p1.Y, &p2.Y) | ||
C.Mul(&p1.T, &p2.T).Mul(&C, &CurveParams.D) | ||
D.Set(&p1.Z) | ||
kevaundray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tmp.Add(&p1.X, &p1.Y) | ||
E.Add(&p2.X, &p2.Y). | ||
Mul(&E, &tmp). | ||
Sub(&E, &A). | ||
Sub(&E, &B) | ||
F.Sub(&D, &C) | ||
G.Add(&D, &C) | ||
H.Set(&A) | ||
|
||
// mulBy5(&H) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
kevaundray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
H.Sub(&B, &H) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for making this match the referenced link including the letters used :) |
||
|
||
p.X.Mul(&E, &F) | ||
p.Y.Mul(&G, &H) | ||
p.T.Mul(&E, &H) | ||
p.Z.Mul(&F, &G) | ||
|
||
return p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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