Skip to content

Commit

Permalink
Update to cleaned up go-ipa APIs (#387)
Browse files Browse the repository at this point in the history
* update to tentative go-ipa and fixes

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* remove proxy decls

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* mod: use latest go-ipa

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

---------

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign authored Sep 4, 2023
1 parent bcdda2e commit 300c55f
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 112 deletions.
3 changes: 2 additions & 1 deletion conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"sort"

"github.com/crate-crypto/go-ipa/banderwagon"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func BatchNewLeafNode(nodesValues []BatchNewLeafNodeData) ([]LeafNode, error) {
c1c2frs[2*i], c1c2frs[2*i+1] = new(Fr), new(Fr)
}

toFrMultiple(c1c2frs, c1c2points)
banderwagon.BatchMapToScalarField(c1c2frs, c1c2points)

var poly [NodeWidth]Fr
poly[0].SetUint64(1)
Expand Down
2 changes: 1 addition & 1 deletion empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (n Empty) Commit() *Point {

func (Empty) Commitment() *Point {
var id Point
id.Identity()
id.SetIdentity()
return &id
}

Expand Down
22 changes: 12 additions & 10 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ package verkle
import (
"errors"
"fmt"

"github.com/crate-crypto/go-ipa/banderwagon"
)

var (
Expand All @@ -51,9 +53,9 @@ const (
leafSteamOffset = nodeTypeOffset + nodeTypeSize
leafBitlistOffset = leafSteamOffset + StemSize
leafCommitmentOffset = leafBitlistOffset + bitlistSize
leafC1CommitmentOffset = leafCommitmentOffset + SerializedPointUncompressedSize
leafC2CommitmentOffset = leafC1CommitmentOffset + SerializedPointUncompressedSize
leafChildrenOffset = leafC2CommitmentOffset + SerializedPointUncompressedSize
leafC1CommitmentOffset = leafCommitmentOffset + banderwagon.UncompressedSize
leafC2CommitmentOffset = leafC1CommitmentOffset + banderwagon.UncompressedSize
leafChildrenOffset = leafC2CommitmentOffset + banderwagon.UncompressedSize
)

func bit(bitlist []byte, nr int) bool {
Expand All @@ -71,7 +73,7 @@ var errSerializedPayloadTooShort = errors.New("verkle payload is too short")
// - Leaf nodes: <nodeType><stem><bitlist><comm><c1comm><c2comm><children...>
func ParseNode(serializedNode []byte, depth byte) (VerkleNode, error) {
// Check that the length of the serialized node is at least the smallest possible serialized node.
if len(serializedNode) < nodeTypeSize+SerializedPointUncompressedSize {
if len(serializedNode) < nodeTypeSize+banderwagon.UncompressedSize {
return nil, errSerializedPayloadTooShort
}

Expand Down Expand Up @@ -102,14 +104,14 @@ func parseLeafNode(serialized []byte, depth byte) (VerkleNode, error) {
ln.setDepth(depth)
ln.c1 = new(Point)

// Sanity check that we have at least 3*SerializedPointUncompressedSize bytes left in the serialized payload.
if len(serialized[leafCommitmentOffset:]) < 3*SerializedPointUncompressedSize {
return nil, fmt.Errorf("leaf node commitments are not the correct size, expected at least %d, got %d", 3*SerializedPointUncompressedSize, len(serialized[leafC1CommitmentOffset:]))
// Sanity check that we have at least 3*banderwagon.UncompressedSize bytes left in the serialized payload.
if len(serialized[leafCommitmentOffset:]) < 3*banderwagon.UncompressedSize {
return nil, fmt.Errorf("leaf node commitments are not the correct size, expected at least %d, got %d", 3*banderwagon.UncompressedSize, len(serialized[leafC1CommitmentOffset:]))
}

ln.c1.SetBytesUncompressed(serialized[leafC1CommitmentOffset:leafC1CommitmentOffset+SerializedPointUncompressedSize], true)
ln.c1.SetBytesUncompressed(serialized[leafC1CommitmentOffset:leafC1CommitmentOffset+banderwagon.UncompressedSize], true)
ln.c2 = new(Point)
ln.c2.SetBytesUncompressed(serialized[leafC2CommitmentOffset:leafC2CommitmentOffset+SerializedPointUncompressedSize], true)
ln.c2.SetBytesUncompressed(serialized[leafC2CommitmentOffset:leafC2CommitmentOffset+banderwagon.UncompressedSize], true)
ln.commitment = new(Point)
ln.commitment.SetBytesUncompressed(serialized[leafCommitmentOffset:leafC1CommitmentOffset], true)
return ln, nil
Expand Down Expand Up @@ -138,7 +140,7 @@ func CreateInternalNode(bitlist []byte, raw []byte, depth byte) (*InternalNode,
}
}
node.depth = depth
if len(raw) != SerializedPointUncompressedSize {
if len(raw) != banderwagon.UncompressedSize {
return nil, ErrInvalidNodeEncoding
}

Expand Down
10 changes: 7 additions & 3 deletions encoding_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package verkle

import "testing"
import (
"testing"

"github.com/crate-crypto/go-ipa/banderwagon"
)

func TestParseNodeEmptyPayload(t *testing.T) {
t.Parallel()
Expand All @@ -25,8 +29,8 @@ func TestLeafStemLength(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(ser) != nodeTypeSize+StemSize+bitlistSize+3*SerializedPointUncompressedSize {
t.Fatalf("invalid serialization when the stem is longer than 31 bytes: %x (%d bytes != %d)", ser, len(ser), nodeTypeSize+StemSize+bitlistSize+2*SerializedPointUncompressedSize)
if len(ser) != nodeTypeSize+StemSize+bitlistSize+3*banderwagon.UncompressedSize {
t.Fatalf("invalid serialization when the stem is longer than 31 bytes: %x (%d bytes != %d)", ser, len(ser), nodeTypeSize+StemSize+bitlistSize+2*banderwagon.UncompressedSize)
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/gballet/go-verkle
go 1.19

require (
github.com/crate-crypto/go-ipa v0.0.0-20230831204142-34060f3f415e
github.com/crate-crypto/go-ipa v0.0.0-20230904185759-9f7637e8ddd0
golang.org/x/sync v0.1.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/Yj
github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
github.com/consensys/gnark-crypto v0.11.2 h1:GJjjtWJ+db1xGao7vTsOgAOGgjfPe7eRGPL+xxMX0qE=
github.com/consensys/gnark-crypto v0.11.2/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY=
github.com/crate-crypto/go-ipa v0.0.0-20230831204142-34060f3f415e h1:M2BwoAiEw0Y77ornI/hCE4Ny8zzV25lES2GmS4hYBFQ=
github.com/crate-crypto/go-ipa v0.0.0-20230831204142-34060f3f415e/go.mod h1:7fZtshzGQ3dxVpDpF51K9mX8oziq8Xd5AoM/UT9fF5o=
github.com/crate-crypto/go-ipa v0.0.0-20230904185759-9f7637e8ddd0 h1:MztzKYOxMeC8HlWGXvq2wizas+QT0FgITjGThfmbh/0=
github.com/crate-crypto/go-ipa v0.0.0-20230904185759-9f7637e8ddd0/go.mod h1:7fZtshzGQ3dxVpDpF51K9mX8oziq8Xd5AoM/UT9fF5o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
Expand Down
27 changes: 1 addition & 26 deletions ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,16 @@ package verkle
import (
"errors"

"github.com/crate-crypto/go-ipa/bandersnatch/fr"
"github.com/crate-crypto/go-ipa/banderwagon"
)

type (
Fr = fr.Element
Fr = banderwagon.Fr
Point = banderwagon.Element
SerializedPoint = []byte
SerializedPointCompressed = []byte
)

const (
SerializedPointUncompressedSize = 64
)

func CopyFr(dst, src *Fr) {
copy(dst[:], src[:])
}

func CopyPoint(dst, src *Point) {
dst.Set(src)
}

func toFr(fr *Fr, p *Point) {
p.MapToScalarField(fr)
}

func toFrMultiple(res []*Fr, ps []*Point) {
banderwagon.MultiMapToScalarField(res, ps)
}

func FromLEBytes(fr *Fr, data []byte) error {
if len(data) > 32 {
return errors.New("data is too long")
Expand All @@ -81,7 +60,3 @@ func FromBytes(fr *Fr, data []byte) {
copy(aligned[32-len(data):], data)
fr.SetBytes(aligned[:])
}

func Equal(self *Point, other *Point) bool {
return other.Equal(self)
}
2 changes: 1 addition & 1 deletion proof_ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func DeserializeProof(vp *VerkleProof, statediff StateDiff) (*Proof, error) {
commitments = make([]*Point, len(vp.CommitmentsByPath))
for i, commitmentBytes := range vp.CommitmentsByPath {
var commitment Point
if err := commitment.SetBytesTrusted(commitmentBytes[:]); err != nil {
if err := commitment.SetBytesUnsafe(commitmentBytes[:]); err != nil {
return nil, err
}
commitments[i] = &commitment
Expand Down
14 changes: 7 additions & 7 deletions proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,16 +628,16 @@ func TestStatelessDeserialize(t *testing.T) {
t.Fatal(err)
}

if !Equal(droot.Commit(), root.Commitment()) {
if !droot.Commit().Equal(root.Commitment()) {
t.Log(ToDot(droot), ToDot(root))
t.Fatalf("differing root commitments %x != %x", droot.Commitment().Bytes(), root.Commitment().Bytes())
}

if !Equal(droot.(*InternalNode).children[0].(*LeafNode).commitment, root.(*InternalNode).children[0].Commit()) {
if !droot.(*InternalNode).children[0].(*LeafNode).commitment.Equal(root.(*InternalNode).children[0].Commit()) {
t.Fatal("differing commitment for child #0")
}

if !Equal(droot.(*InternalNode).children[64].Commit(), root.(*InternalNode).children[64].Commit()) {
if !droot.(*InternalNode).children[64].Commit().Equal(root.(*InternalNode).children[64].Commit()) {
t.Fatal("differing commitment for child #64")
}
}
Expand Down Expand Up @@ -667,10 +667,10 @@ func TestStatelessDeserializeMissingChildNode(t *testing.T) {
t.Fatal(err)
}

if !Equal(droot.Commit(), root.Commit()) {
if !droot.Commit().Equal(root.Commit()) {
t.Fatal("differing root commitments")
}
if !Equal(droot.(*InternalNode).children[0].Commit(), root.(*InternalNode).children[0].Commit()) {
if !droot.(*InternalNode).children[0].Commit().Equal(root.(*InternalNode).children[0].Commit()) {
t.Fatal("differing commitment for child #0")
}

Expand Down Expand Up @@ -705,11 +705,11 @@ func TestStatelessDeserializeDepth2(t *testing.T) {
t.Fatal(err)
}

if !Equal(droot.Commit(), root.Commit()) {
if !droot.Commit().Equal(root.Commit()) {
t.Fatal("differing root commitments")
}

if !Equal(droot.(*InternalNode).children[0].Commit(), root.(*InternalNode).children[0].Commit()) {
if !droot.(*InternalNode).children[0].Commit().Equal(root.(*InternalNode).children[0].Commit()) {
t.Fatal("differing commitment for child #0")
}
}
Loading

0 comments on commit 300c55f

Please sign in to comment.