Skip to content

Commit

Permalink
add MapToMultiScalarField (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray authored Nov 10, 2022
1 parent e868de2 commit a661476
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
27 changes: 18 additions & 9 deletions banderwagon/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ func (p Element) mapToBaseField() fp.Element {
return res
}

// computes X/Y and returns the byte representation
func (p Element) MapToBaseFieldBytes() [sizePointCompressed]byte {
func (p Element) MapToScalarField() fr.Element {
basefield := p.mapToBaseField()
return basefield.BytesLE()
baseFieldBytes := basefield.BytesLE()

var res fr.Element
res.SetBytesLE(baseFieldBytes[:])

return res
}

func MultiMapToBaseFieldBytes(elements []*Element) [][sizePointCompressed]byte {
// Maps each point to a field element in the scalar field
func MultiMapToScalarField(elements []*Element) []fr.Element {
// Collect all y co-ordinates
var ys []fp.Element
for i := 0; i < int(len(elements)); i++ {
Expand All @@ -138,17 +143,21 @@ func MultiMapToBaseFieldBytes(elements []*Element) [][sizePointCompressed]byte {
// Invert y co-ordinates
yInvs := fp.BatchInvert(ys)

var serialisedPoints [][sizePointCompressed]byte
var scalars []fr.Element

// Multiply x by yInv
for i := 0; i < int(len(elements)); i++ {
var res fp.Element
var mappedElement fp.Element

mappedElement.Mul(&elements[i].inner.X, &yInvs[i])
byts := mappedElement.BytesLE()

res.Mul(&elements[i].inner.X, &yInvs[i])
serialisedPoints = append(serialisedPoints, res.BytesLE())
var res fr.Element
res.SetBytesLE(byts[:])
scalars = append(scalars, res)
}

return serialisedPoints
return scalars

}

Expand Down
18 changes: 9 additions & 9 deletions banderwagon/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,18 @@ func TestMultiMapToBaseField(t *testing.T) {
B.Double(&Generator)
B.Double(&B)

expected_serialised_a := A.MapToBaseFieldBytes()
expected_serialised_b := B.MapToBaseFieldBytes()
expected_a := A.MapToScalarField()
expected_b := B.MapToScalarField()

serialised_points := MultiMapToBaseFieldBytes([]*Element{&A, &B})
scalars := MultiMapToScalarField([]*Element{&A, &B})

got_serialised_a := serialised_points[0]
got_serialised_b := serialised_points[1]
if expected_serialised_a != got_serialised_a {
panic("expected serialised point of A is incorrect ")
got_a := scalars[0]
got_b := scalars[1]
if expected_a != got_a {
panic("expected scalar for point `A` is incorrect ")
}

if expected_serialised_b != got_serialised_b {
panic("expected serialised point of B is incorrect ")
if expected_b != got_b {
panic("expected scalar for point `A` is incorrect ")
}
}

0 comments on commit a661476

Please sign in to comment.