Skip to content

Commit

Permalink
Migrates codebase from bandersnatch to banderwagon (#215)
Browse files Browse the repository at this point in the history
* migrate to banderwagon

* temp - comment out the hardcoded test vector

* fix toFr

* generate new precomp file for tests to pass

* toFr

- Fixes toFr to be consistent with Rust

* uncomment test

* use SetBytesLe

* FromLEBytes: right-pad to a 32-byte integer

* rename hash to commitment

Co-authored-by: Guillaume Ballet <[email protected]>
  • Loading branch information
kevaundray and gballet authored May 18, 2022
1 parent ab3868e commit 8b21c55
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- restore_cache:
keys:
- v1-pkg-cache
- srs-precomp
- srs-precomp-banderwagon

- run: wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s latest

Expand All @@ -32,7 +32,7 @@ jobs:
- "/go/pkg"

- save_cache: # Save the precomputed data in the cache
key: srs-precomp
key: srs-precomp-banderwagon
paths:
- "./precomp"

Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module github.com/gballet/go-verkle

go 1.17

require (
github.com/crate-crypto/go-ipa v0.0.0-20220309173511-816621cb2ec4
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 // indirect
)
require github.com/crate-crypto/go-ipa v0.0.0-20220518143002-bb5c76ff150e

require golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect
33 changes: 7 additions & 26 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
github.com/crate-crypto/go-ipa v0.0.0-20211031211114-01ccaafc10b5 h1:MDNhjbPBtRebozlc2eGDjdJUTAEkkuUBHNfkd+TLVZ8=
github.com/crate-crypto/go-ipa v0.0.0-20211031211114-01ccaafc10b5/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/crate-crypto/go-ipa v0.0.0-20211107182441-1aeb67f49de7 h1:P6yxenBOOu4RF26bRLiWG+zrwQ1kPAaz71sDmX6b76I=
github.com/crate-crypto/go-ipa v0.0.0-20211107182441-1aeb67f49de7/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/crate-crypto/go-ipa v0.0.0-20211223165939-ab3f49447206 h1:nMTTM1b+4WtWrb43nmTUV/FJz7M0M2g5fioLmd4QzUQ=
github.com/crate-crypto/go-ipa v0.0.0-20211223165939-ab3f49447206/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/crate-crypto/go-ipa v0.0.0-20220114181434-991b62f9b1da h1:2luwsOSyUPVE67DmD7s2nKM+JbxaRuOCu6Y82qBTdnI=
github.com/crate-crypto/go-ipa v0.0.0-20220114181434-991b62f9b1da/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/crate-crypto/go-ipa v0.0.0-20220120174240-fe21866d2ad5 h1:BsLconJDsODyRO72xjBHhxZKPCuY/kBgZdPeV4GFNlU=
github.com/crate-crypto/go-ipa v0.0.0-20220120174240-fe21866d2ad5/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/crate-crypto/go-ipa v0.0.0-20220309173511-816621cb2ec4 h1:6KIFkDoBRVfE2I4cUt2wUuejBs+ReEe0cNQnTLs8AwE=
github.com/crate-crypto/go-ipa v0.0.0-20220309173511-816621cb2ec4/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/crate-crypto/go-ipa v0.0.0-20220518141538-8a09157db00a h1:vlXTVchD2qxDlrCtVQZOyYThbI5rVuLmZgXpMf7C7wM=
github.com/crate-crypto/go-ipa v0.0.0-20220518141538-8a09157db00a/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/crate-crypto/go-ipa v0.0.0-20220518143002-bb5c76ff150e h1:lerU+Bwfo7izG4zhMOT+hYFVic+3uN6sgSikUEysL2U=
github.com/crate-crypto/go-ipa v0.0.0-20220518143002-bb5c76ff150e/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211023085530-d6a326fbbf70 h1:SeSEfdIxyvwGJliREIJhRPPXvW6sDlLT+UQ3B0hD0NA=
golang.org/x/sys v0.0.0-20211023085530-d6a326fbbf70/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211031064116-611d5d643895 h1:iaNpwpnrgL5jzWS0vCNnfa8HqzxveCFpFx3uC/X4Tps=
golang.org/x/sys v0.0.0-20211031064116-611d5d643895/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211107104306-e0b2ad06fe42 h1:G2DDmludOQZoWbpCr7OKDxnl478ZBGMcOhrv+ooX/Q4=
golang.org/x/sys v0.0.0-20211107104306-e0b2ad06fe42/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7 h1:BXxu8t6QN0G1uff4bzZzSkpsax8+ALqTGUtz08QrV00=
golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 h1:8IVLkfbr2cLhv0a/vKq4UFUcJym8RmDoDboxCFWEjYE=
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e h1:w36l2Uw3dRan1K3TyXriXvY+6T56GNmlKGcqiQUJDfM=
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13 changes: 6 additions & 7 deletions ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
package verkle

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

type Fr = fr.Element
type Point = bandersnatch.PointAffine
type Point = banderwagon.Element

func CopyFr(dst, src *Fr) {
copy(dst[:], src[:])
Expand All @@ -43,16 +43,15 @@ func CopyPoint(dst, src *Point) {
}

func toFr(fr *Fr, p *Point) {
bytes := p.Bytes()
baseField := p.MapToBaseField()
bytes := (&baseField).BytesLE()
fr.SetBytesLE(bytes[:])
}

func FromLEBytes(fr *Fr, data []byte) {
var aligned [32]byte
for i := range data {
aligned[31-i] = data[i]
}
fr.SetBytes(aligned[:])
copy(aligned[:len(data)], data)
fr.SetBytesLE(aligned[:])
}

func StemFromBytes(fr *Fr, data []byte) {
Expand Down
3 changes: 2 additions & 1 deletion proof_ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ func DeserializeProof(proofSerialized []byte, keyvals []KeyValuePair) (*Proof, e
if err := binary.Read(reader, binary.LittleEndian, commitmentBytes); err != nil {
return nil, err
}
if err := commitment.Unmarshal(commitmentBytes); err != nil {

if err := commitment.SetBytes(commitmentBytes); err != nil {
return nil, err
}

Expand Down
24 changes: 24 additions & 0 deletions tree_ipa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"encoding/hex"
"fmt"
"testing"

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

var identity *Point
Expand Down Expand Up @@ -210,3 +212,25 @@ func TestEmptyTrie(t *testing.T) {
t.Fatalf("invalid root commitment %v != %v", comm, identity)
}
}

func TestGroupToField(t *testing.T) {
point := banderwagon.Generator
var v Fr
toFr(&v, &point)
bytes := v.BytesLE()
hexStr := hex.EncodeToString(bytes[:])
if hexStr != "d1e7de2aaea9603d5bc6c208d319596376556ecd8336671ba7670c2139772d14" {
t.Fatalf("group to field not working")
}
}

func TestPaddingInFromLEBytes(t *testing.T) {
var fr1, fr2 Fr
FromLEBytes(&fr1, ffx32KeyTest[:16])
key, _ := hex.DecodeString("ffffffffffffffffffffffffffffffff00000000000000000000000000000000")
FromLEBytes(&fr2, key)

if !fr1.Equal(&fr2) {
t.Fatal("byte alignment")
}
}
11 changes: 5 additions & 6 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ var (
{197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
testAccountRootHashRust, _ = hex.DecodeString("551dbfb30ba563ce071cd5ea69b1c28467c018f740d86cf9828c4cc32c4b0305")
testAccountRootCommRust, _ = hex.DecodeString("10ed89d89047bb168baa4e69b8607e260049e928ddbcb2fdd23ea0f4182b1f8a")
)

func TestWithRustCompatibility(t *testing.T) {
Expand All @@ -943,10 +943,9 @@ func TestWithRustCompatibility(t *testing.T) {
t.Fatalf("error inserting: %v", err)
}
}
var hash Fr
toFr(&hash, root.ComputeCommitment())
hashBytes := hash.BytesLE()
if !bytes.Equal(hashBytes[:], testAccountRootHashRust) {
t.Fatalf("rust and golang impl are not compatible rust=%x, go=%x", testAccountRootHashRust, hashBytes)

commBytes := root.ComputeCommitment().Bytes()
if !bytes.Equal(commBytes[:], testAccountRootCommRust) {
t.Fatalf("rust and golang impl are not compatible rust=%x, go=%x", testAccountRootCommRust, commBytes)
}
}

0 comments on commit 8b21c55

Please sign in to comment.