Skip to content

Commit

Permalink
update to use latest kzg lib (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-bayardo committed Nov 9, 2022
1 parent eb1b85d commit 4bcfcb3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 62 deletions.
62 changes: 12 additions & 50 deletions beacon-chain/core/blob/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func ValidateBlobsSidecar(slot types.Slot, root [32]byte, commitments [][]byte,
return err
}

aggregatedPoly, err := vectorLinComb(sidecar.Blobs, rPowers)
aggregatedPoly, err := polyLinComb(sidecar.Blobs, rPowers)
if err != nil {
return err
}
Expand All @@ -130,7 +130,7 @@ func ValidateBlobsSidecar(slot types.Slot, root [32]byte, commitments [][]byte,
var y bls.Fr
EvaluatePolyInEvaluationForm(&y, aggregatedPoly, &x)

b, err := verifyKZGProof(aggregatedPolyCommitment, &x, &y, sidecar.AggregatedProof)
b, err := kzg.VerifyKZGProof(bytesutil.ToBytes48(aggregatedPolyCommitment), &x, &y, bytesutil.ToBytes48(sidecar.AggregatedProof))
if err != nil {
return err
}
Expand Down Expand Up @@ -190,36 +190,20 @@ func linComb(commitments [][]byte, scalars []bls.Fr) ([]byte, error) {
return bls.ToCompressedG1(r), nil
}

// vectorLinComb implements the function vector_lincomb from the EIP-4844 spec
func vectorLinComb(blobs []*v1.Blob, scalars []bls.Fr) ([]bls.Fr, error) {
r := make([]bls.Fr, params.FieldElementsPerBlob)
x := bls.Fr{}
var fe [32]byte
feSlice := fe[:] // create a slice that is backed by a tmp [32]byte array
for v := range blobs { // iterate over blobs
blob := blobs[v].Blob
if len(blob) != params.FieldElementsPerBlob {
return nil, errors.New("blob is the wrong size")
}
for i := 0; i < params.FieldElementsPerBlob; i++ { // iterate over a blob's field elements
// copy the []byte field element from the blob into the tmp [32]byte array and then
// convert to bls.Fr.
copy(feSlice, blob[i])
ok := bls.FrFrom32(&x, fe)
func polyLinComb(blobs []*v1.Blob, scalars []bls.Fr) ([]bls.Fr, error) {
out := make([][]bls.Fr, len(blobs))
for i := range blobs {
r := make([]bls.Fr, params.FieldElementsPerBlob)
blob := blobs[i].Blob
for j, fe := range blob {
ok := bls.FrFrom32(&r[j], bytesutil.ToBytes32(fe))
if !ok {
return nil, errors.New("couldn't convert blob data to field element")
return nil, errors.New("invalid value in blob")
}
bls.MulModFr(&x, &scalars[v], &x)
bls.AddModFr(&r[i], &r[i], &x)
}
out[i] = r
}
return r, nil
}

func blsModInv(out *big.Int, x *big.Int) {
if len(x.Bits()) != 0 { // if non-zero
out.ModInverse(x, &blsModulus)
}
return bls.PolyLinComb(out, scalars)
}

// Evaluate a polynomial (in evaluation form) at an arbitrary point `x`
Expand All @@ -228,20 +212,6 @@ func EvaluatePolyInEvaluationForm(yFr *bls.Fr, poly []bls.Fr, x *bls.Fr) {
bls.EvaluatePolyInEvaluationForm(yFr, poly, x, rootsOfUnity, 0)
}

// verifyKZGProof implements verify_kzg_proof from the EIP-4844 spec
func verifyKZGProof(polynomialKZG []byte, x *bls.Fr, y *bls.Fr, quotientKZG []byte) (bool, error) {
commitment, err := bls.FromCompressedG1(polynomialKZG)
if err != nil {
return false, err
}
proof, err := bls.FromCompressedG1(quotientKZG)
if err != nil {
return false, err
}
// TODO: have an implementation independent of geth's
return kzg.VerifyKzgProof(commitment, x, y, proof), nil
}

// hashToFr interprets hash value v as a little endian integer, and converts it to a BLS field
// element after modding it with the BLS modulus.
func hashToFr(fr *bls.Fr, v [32]byte) *bls.Fr {
Expand Down Expand Up @@ -269,11 +239,3 @@ func bigToFr(fr *bls.Fr, b *big.Int) *bls.Fr {
bls.SetFr(fr, b.String())
return fr
}

// frToBig converts BLS field element fr into a bignum, storing it in b and returning it.
func frToBig(b *big.Int, fr *bls.Fr) *big.Int {
// TODO: Conversion currently relies the string representation as an intermediary. Submit a PR
// to protolambda/go-kzg enabling something more efficient.
b.SetString(bls.FrStr(fr), 10)
return b
}
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/emicklei/dot v0.11.0
github.com/ethereum/go-ethereum v1.10.23
github.com/ferranbt/fastssz v0.0.0-20210526181520-7df50c8568f8
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5
github.com/fsnotify/fsnotify v1.5.4
github.com/ghodss/yaml v1.0.0
Expand Down Expand Up @@ -62,7 +61,7 @@ require (
github.com/prometheus/client_golang v1.12.2
github.com/prometheus/client_model v0.2.0
github.com/prometheus/prom2json v1.3.0
github.com/protolambda/go-kzg v0.0.0-20221025081131-f3a74d3b1d0c
github.com/protolambda/go-kzg v0.0.0-20221108193918-c6d0faa55038
github.com/protolambda/ztyp v0.2.1
github.com/prysmaticlabs/fastssz v0.0.0-20220628121656-93dfe28febab
github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7
Expand All @@ -78,7 +77,7 @@ require (
github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e
github.com/trailofbits/go-mutexasserts v0.0.0-20200708152505-19999e7d3cef
github.com/tyler-smith/go-bip39 v1.1.0
github.com/urfave/cli/v2 v2.10.2
github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa
github.com/uudashr/gocognit v1.0.5
github.com/wealdtech/go-bytesutil v1.1.1
github.com/wealdtech/go-eth2-util v1.6.3
Expand Down Expand Up @@ -125,6 +124,7 @@ require (
github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf // indirect
github.com/edsrzf/mmap-go v1.0.0 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/ferranbt/fastssz v0.0.0-20210526181520-7df50c8568f8 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-playground/locales v0.14.0 // indirect
Expand All @@ -139,6 +139,7 @@ require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/graph-gophers/graphql-go v1.3.0 // indirect
github.com/hashicorp/go-bexpr v0.1.10 // indirect
github.com/holiman/big v0.0.0-20221017200358-a027dc42d04e // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/goevmlab v0.0.0-20211215113238-06157bc85f7d // indirect
github.com/huin/goupnp v1.0.3 // indirect
Expand Down Expand Up @@ -255,7 +256,7 @@ require (
github.com/peterh/liner v1.2.0 // indirect
github.com/prometheus/tsdb v0.10.0 // indirect
github.com/prysmaticlabs/gohashtree v0.0.2-alpha
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect
google.golang.org/api v0.34.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
k8s.io/klog/v2 v2.3.0 // indirect
Expand All @@ -268,4 +269,4 @@ replace github.com/json-iterator/go => github.com/prestonvanloon/go v1.1.7-0.201
replace github.com/grpc-ecosystem/grpc-gateway/v2 => github.com/prysmaticlabs/grpc-gateway/v2 v2.3.1-0.20220721162526-0d1c40b5f064

// For eip-4844 types
replace github.com/ethereum/go-ethereum => github.com/mdehoog/go-ethereum v1.10.19-0.20221025212535-8925aee77ccf
replace github.com/ethereum/go-ethereum => github.com/mdehoog/go-ethereum v1.10.19-0.20221109152946-e8e85fe80ef8
21 changes: 14 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/crate-crypto/go-ipa v0.0.0-20220523130400-f11357ae11c7/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
Expand Down Expand Up @@ -322,6 +323,7 @@ github.com/garyburd/redigo v1.6.0/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 h1:f6D9Hr8xV8uYKlyuj8XIruxlh9WjVjdh1gIicAS7ays=
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/gballet/go-verkle v0.0.0-20220902153445-097bd83b7732/go.mod h1:o/XfIXWi4/GqbQirfRm5uTbXMG5NpqxkxblnbZ+QM9I=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
Expand Down Expand Up @@ -560,6 +562,8 @@ github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/J
github.com/herumi/bls-eth-go-binary v0.0.0-20210130185500-57372fb27371/go.mod h1:luAnRm3OsMQeokhGzpYmc0ZKwawY7o87PUEP11Z7r7U=
github.com/herumi/bls-eth-go-binary v1.28.1 h1:fcIZ48y5EE9973k05XjE8+P3YiQgjZz4JI/YabAm8KA=
github.com/herumi/bls-eth-go-binary v1.28.1/go.mod h1:luAnRm3OsMQeokhGzpYmc0ZKwawY7o87PUEP11Z7r7U=
github.com/holiman/big v0.0.0-20221017200358-a027dc42d04e h1:pIYdhNkDh+YENVNi3gto8n9hAmRxKxoar0iE6BLucjw=
github.com/holiman/big v0.0.0-20221017200358-a027dc42d04e/go.mod h1:j9cQbcqHQujT0oKJ38PylVfqohClLr3CvDC+Qcg+lhU=
github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA=
github.com/holiman/goevmlab v0.0.0-20210406174504-acc14986d1a1/go.mod h1:UuoGSTwXQ2lt339kIt7dkwgdi/dc4bh0BEdQDWRMDDQ=
Expand Down Expand Up @@ -861,8 +865,8 @@ github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsO
github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mdehoog/go-ethereum v1.10.19-0.20221025212535-8925aee77ccf h1:ERmH7q3eETG8aYoEa62/uRRQheRH0kwywtVnCSWlbfE=
github.com/mdehoog/go-ethereum v1.10.19-0.20221025212535-8925aee77ccf/go.mod h1:5R3M8hUv088dPxUf7EKzVArHvJ4L8FF7pQDsddAHTII=
github.com/mdehoog/go-ethereum v1.10.19-0.20221109152946-e8e85fe80ef8 h1:6aHTJvy185FYSgFI5H/9wyIBDxN8xFkY/3MklzLD+Ig=
github.com/mdehoog/go-ethereum v1.10.19-0.20221109152946-e8e85fe80ef8/go.mod h1:g+vX1+faxlr/KM1C0wybtfdcH5aPghqJAfpLGAn+Kbk=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
Expand Down Expand Up @@ -1129,8 +1133,8 @@ github.com/prometheus/prom2json v1.3.0/go.mod h1:rMN7m0ApCowcoDlypBHlkNbp5eJQf/+
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/prometheus/tsdb v0.10.0 h1:If5rVCMTp6W2SiRAQFlbpJNgVlgMEd+U2GZckwK38ic=
github.com/prometheus/tsdb v0.10.0/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4=
github.com/protolambda/go-kzg v0.0.0-20221025081131-f3a74d3b1d0c h1:9PBJD0rbR2dsgzUoi7GGbCJ3PZssM2eK//tHaAVS5Ds=
github.com/protolambda/go-kzg v0.0.0-20221025081131-f3a74d3b1d0c/go.mod h1:9Lk5SNX/61jq4lbyYcxCvOeldv4xEUAO6QMp/Fd5ALw=
github.com/protolambda/go-kzg v0.0.0-20221108193918-c6d0faa55038 h1:oTNfeC6fvE/m3iT62hR+umMXjdjtnhCQ6ez9Kl4Gwu0=
github.com/protolambda/go-kzg v0.0.0-20221108193918-c6d0faa55038/go.mod h1:9Lk5SNX/61jq4lbyYcxCvOeldv4xEUAO6QMp/Fd5ALw=
github.com/protolambda/ztyp v0.2.1 h1:+rfw75/Zh8EopNlG652TGDXlLgJflj6XWxJ9yCVpJws=
github.com/protolambda/ztyp v0.2.1/go.mod h1:9bYgKGqg3wJqT9ac1gI2hnVb0STQq7p/1lapqrqY1dU=
github.com/prysmaticlabs/fastssz v0.0.0-20220628121656-93dfe28febab h1:Y3PcvUrnneMWLuypZpwPz8P70/DQsz6KgV9JveKpyZs=
Expand Down Expand Up @@ -1297,8 +1301,8 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/urfave/cli/v2 v2.10.2 h1:x3p8awjp/2arX+Nl/G2040AZpOCHS/eMJJ1/a+mye4Y=
github.com/urfave/cli/v2 v2.10.2/go.mod h1:f8iq5LtQ/bLxafbdBSLPPNsgaW0l/2fYYEHhAyPlwvo=
github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa h1:5SqCsI/2Qya2bCzK15ozrqo2sZxkh0FHynJZOTVoV6Q=
github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI=
github.com/uudashr/gocognit v1.0.5 h1:rrSex7oHr3/pPLQ0xoWq108XMU8s678FJcQ+aSfOHa4=
github.com/uudashr/gocognit v1.0.5/go.mod h1:wgYz0mitoKOTysqxTDMOUXg+Jb5SvtihkfmugIZYpEA=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
Expand Down Expand Up @@ -1656,15 +1660,18 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211214234402-4825e8c3871d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 h1:OK7RB6t2WQX54srQQYSXMW8dF5C6/8+oA/s5QBmmto4=
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
Expand Down

0 comments on commit 4bcfcb3

Please sign in to comment.