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

Eliminate 2 allocations per Pedersen call #371

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions ecc/stark-curve/pedersen-hash/pedersen_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ func PedersenArray(elems ...*fp.Element) *fp.Element {
func Pedersen(a *fp.Element, b *fp.Element) *fp.Element {

result := new(starkcurve.G1Jac).Set(&shiftPoint)
result.AddAssign(processElement(a, &p0, &p1))
result.AddAssign(processElement(b, &p2, &p3))

var point starkcurve.G1Jac
result.AddAssign(processElement(a, &p0, &p1, &point))
result.AddAssign(processElement(b, &p2, &p3, &point))

// recover the affine x coordinate
var x fp.Element
Expand All @@ -71,17 +73,17 @@ func Pedersen(a *fp.Element, b *fp.Element) *fp.Element {
return &x
}

func processElement(a *fp.Element, p1 *starkcurve.G1Jac, p2 *starkcurve.G1Jac) *starkcurve.G1Jac {
func processElement(a *fp.Element, p1 *starkcurve.G1Jac, p2 *starkcurve.G1Jac, res *starkcurve.G1Jac) *starkcurve.G1Jac {
var bigInt big.Int
var aBytes [32]byte
a.BigInt(&bigInt).FillBytes(aBytes[:])

highPart := bigInt.SetUint64(uint64(aBytes[0])) // The top nibble (bits 249-252)
lowPart := aBytes[1:] // Zero-out the top nibble (bits 249-252)

m := new(starkcurve.G1Jac).ScalarMultiplication(p2, highPart)
res.ScalarMultiplication(p2, highPart)

var n starkcurve.G1Jac
n.ScalarMultiplication(p1, bigInt.SetBytes(lowPart))
return m.AddAssign(&n)
return res.AddAssign(&n)
}