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

chore!: Update test vectors #94

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
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
67 changes: 48 additions & 19 deletions api_eip7594.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,10 @@ func (ctx *Context) computeCellsAndKZGProofsFromPolyCoeff(polyCoeff []fr.Element
return Cells, serializedProofs, nil
}

func (ctx *Context) RecoverCellsAndComputeKZGProofs(cellIDs []uint64, cells []*Cell, _proofs []KZGProof, numGoRoutines int) ([CellsPerExtBlob]*Cell, [CellsPerExtBlob]KZGProof, error) {
// Check each proof can be deserialized
// TODO: This gets removed when we update the specs.
for _, proof := range _proofs {
_, err := DeserializeKZGProof(proof)
if err != nil {
return [CellsPerExtBlob]*Cell{}, [CellsPerExtBlob]KZGProof{}, err
}
}

func (ctx *Context) RecoverCellsAndComputeKZGProofs(cellIDs []uint64, cells []*Cell, numGoRoutines int) ([CellsPerExtBlob]*Cell, [CellsPerExtBlob]KZGProof, error) {
if len(cellIDs) != len(cells) {
return [CellsPerExtBlob]*Cell{}, [CellsPerExtBlob]KZGProof{}, ErrNumCellIDsNotEqualNumCells
}
if len(cellIDs) != len(_proofs) {
return [CellsPerExtBlob]*Cell{}, [CellsPerExtBlob]KZGProof{}, ErrNumCellIDsNotEqualNumProofs
}

// Check that the cell Ids are unique
if !isUniqueUint64(cellIDs) {
Expand Down Expand Up @@ -126,10 +114,12 @@ func (ctx *Context) RecoverCellsAndComputeKZGProofs(cellIDs []uint64, cells []*C
return ctx.computeCellsAndKZGProofsFromPolyCoeff(polyCoeff, numGoRoutines)
}

func (ctx *Context) VerifyCellKZGProofBatch(rowCommitments []KZGCommitment, rowIndices, columnIndices []uint64, cells []*Cell, proofs []KZGProof) error {
func (ctx *Context) VerifyCellKZGProofBatch(commitments []KZGCommitment, cellIndices []uint64, cells []*Cell, proofs []KZGProof) error {
rowCommitments, rowIndices := deduplicateKZGCommitments(commitments)

// Check that all components in the batch have the same size, expect the rowCommitments
batchSize := len(rowIndices)
lengthsAreEqual := batchSize == len(columnIndices) && batchSize == len(cells) && batchSize == len(proofs)
lengthsAreEqual := batchSize == len(cellIndices) && batchSize == len(cells) && batchSize == len(proofs)
if !lengthsAreEqual {
return ErrBatchLengthCheck
}
Expand All @@ -145,19 +135,19 @@ func (ctx *Context) VerifyCellKZGProofBatch(rowCommitments []KZGCommitment, rowI
}
}

for _, cellIndex := range columnIndices {
for _, cellIndex := range cellIndices {
if cellIndex >= CellsPerExtBlob {
return ErrInvalidCellID
}
}

commitments := make([]bls12381.G1Affine, len(rowCommitments))
commitmentsG1 := make([]bls12381.G1Affine, len(rowCommitments))
for i := 0; i < len(rowCommitments); i++ {
comm, err := DeserializeKZGCommitment(rowCommitments[i])
if err != nil {
return err
}
commitments[i] = comm
commitmentsG1[i] = comm
}
proofsG1 := make([]bls12381.G1Affine, len(proofs))
for i := 0; i < len(proofs); i++ {
Expand All @@ -175,7 +165,7 @@ func (ctx *Context) VerifyCellKZGProofBatch(rowCommitments []KZGCommitment, rowI
}
cosetsEvals[i] = cosetEvals
}
return kzgmulti.VerifyMultiPointKZGProofBatch(commitments, rowIndices, columnIndices, proofsG1, cosetsEvals, ctx.openKey7594)
return kzgmulti.VerifyMultiPointKZGProofBatch(commitmentsG1, rowIndices, cellIndices, proofsG1, cosetsEvals, ctx.openKey7594)
}

// isUniqueUint64 returns true if the slices contains no duplicate elements
Expand All @@ -194,3 +184,42 @@ func isUniqueUint64(slice []uint64) bool {
// All elements are unique
return true
}

// deduplicateCommitments takes a slice of KZGCommitments and returns two slices:
// a deduplicated slice of KZGCommitments and a slice of indices.
//
// Each index in the slice of indices corresponds to the position of the
// commitment in the deduplicated slice.
// When coupled with the deduplicated commitments, one is able to reconstruct
// the input duplicated commitments slice.
//
// Note: This function assumes that KZGCommitment is comparable (i.e., can be used as a map key).
// If KZGCommitment is not directly comparable, you may need to implement a custom key function.
func deduplicateKZGCommitments(original []KZGCommitment) ([]KZGCommitment, []uint64) {
deduplicatedCommitments := make(map[KZGCommitment]uint64)

// First pass: build the map and count unique elements
for _, comm := range original {
if _, exists := deduplicatedCommitments[comm]; !exists {
// Assign an index to a commitment, the first time we see it
deduplicatedCommitments[comm] = uint64(len(deduplicatedCommitments))
}
}

deduplicated := make([]KZGCommitment, len(deduplicatedCommitments))
indices := make([]uint64, len(original))

// Second pass: build both deduplicated and indices slices
for i, comm := range original {
// Get the unique index for this commitment
index := deduplicatedCommitments[comm]
// Add the index into the indices slice
indices[i] = index
// Add the commitment to the deduplicated slice
// If the commitment has already been seen, then
// this just overwrites it with the same parameter.
deduplicated[index] = comm
}

return deduplicated, indices
}
27 changes: 9 additions & 18 deletions consensus_specs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,10 @@ func TestComputeCellsAndKZGProofs(t *testing.T) {
func TestVerifyCellKZGProofBatch(t *testing.T) {
type Test struct {
Input struct {
RowCommitments []string `yaml:"row_commitments"`
RowIndices []uint64 `yaml:"row_indices"`
ColumnIndices []uint64 `yaml:"column_indices"`
Cells []string `yaml:"cells"`
Proofs []string `yaml:"proofs"`
Commitments []string `yaml:"commitments"`
CellIndices []uint64 `yaml:"cell_indices"`
Cells []string `yaml:"cells"`
Proofs []string `yaml:"proofs"`
}
Output *bool `yaml:"output"`
}
Expand All @@ -430,14 +429,13 @@ func TestVerifyCellKZGProofBatch(t *testing.T) {
require.NoError(t, err)
testCaseValid := test.Output != nil

rowCommitments, err := HexStrArrToCommitments(test.Input.RowCommitments)
commitments, err := HexStrArrToCommitments(test.Input.Commitments)
if err != nil {
require.False(t, testCaseValid)
return
}

rowIndices := test.Input.RowIndices
columnIndices := test.Input.ColumnIndices
cellIndices := test.Input.CellIndices

cells, err := hexStrArrToCells(test.Input.Cells)
if err != nil {
Expand All @@ -449,7 +447,7 @@ func TestVerifyCellKZGProofBatch(t *testing.T) {
require.False(t, testCaseValid)
return
}
err = ctx.VerifyCellKZGProofBatch(rowCommitments, rowIndices, columnIndices, cells, proofs)
err = ctx.VerifyCellKZGProofBatch(commitments, cellIndices, cells, proofs)
// Test specifically distinguish between the test failing
// because of the pairing check and failing because of
// validation errors on the input
Expand All @@ -468,9 +466,8 @@ func TestVerifyCellKZGProofBatch(t *testing.T) {
func TestRecoverCellsAndKZGProofs(t *testing.T) {
type Test struct {
Input struct {
CellIds []uint64 `yaml:"cell_ids"`
CellIds []uint64 `yaml:"cell_indices"`
Cells []string `yaml:"cells"`
Proofs []string `yaml:"proofs"`
}
Output *[][]string `yaml:"output"`
}
Expand All @@ -497,13 +494,7 @@ func TestRecoverCellsAndKZGProofs(t *testing.T) {
return
}

proofs, err := HexStrArrToProofs(test.Input.Proofs)
if err != nil {
require.False(t, testCaseValid)
return
}

recoveredCells, recoveredProofs, err := ctx.RecoverCellsAndComputeKZGProofs(cellIds, cells, proofs, 0)
recoveredCells, recoveredProofs, err := ctx.RecoverCellsAndComputeKZGProofs(cellIds, cells, 0)

if err == nil {
require.NotNil(t, test.Output)
Expand Down
1 change: 0 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var (
ErrInvalidRowIndex = errors.New("row index should be less than the number of row commitments")

ErrNumCellIDsNotEqualNumCells = errors.New("number of cell IDs should be equal to the number of cells")
ErrNumCellIDsNotEqualNumProofs = errors.New("number of cell IDs should be equal to the number of proofs")
ErrCellIDsNotUnique = errors.New("cell IDs are not unique")
ErrFoundInvalidCellID = errors.New("cell ID should be less than CellsPerExtBlob")
ErrNotEnoughCellsForReconstruction = errors.New("not enough cells to perform reconstruction")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,4 @@
input:
cell_ids: []
cell_indices: []
cells: []
proofs: ['0x856f877b601e53c697b800a551e05c3c08a24446c369e0a3e5fc6360b9a60c7d6ecdf48f0733d5d2d8dc7b8adec05c14',
'0xa8a513370308ada3c5c0d169ac818fb4bbba63c7b94bc938646d01628a834585faca904d584ef1c95ac4ea7e82548084',
'0x8ccb917c3fc26831eb1b9fd5abf8433bd73938254a7be9b51ba9f11fae645297be749e29eceb822c0548cecdd7c199d3',
'0x91867a8c1ddb4f589055c3675b312345943b58f7ef3c83c1da3e86c4fc3dc6266ce03ff7ae0819ab8ff1d45a318d96c5',
'0x8a518167aacaa864d40ceca03b0fcc6d7ef4cf5f8288be6dc72218b102287a0cf30a2fcb02cd021c04fb0d0b0a83c0af',
'0x98897d6e757448dbc85c461ff290681fd1988700e7f460e0e8deb794f8834ad28d2ba09eaccc31547cb9501ee2162c9a',
'0xae4b59ec979cd7365a546a101f184b4862e635c4efaa553f0f40e9657bb503b71bbc45f2b929fcf0cf1b094b47f4970a',
'0xa90b575d63e061c140a103c3b4af5a492c3aa82697f0c39eecb0ca2c5b093c734f2f60850febc131dd126ede9f491746',
'0xa77ab7289d0ab9de5c9b259ad9dcd92edc31fabfcb87d283e1ceccf926d486cd2f76aa8c8ead61d14a758ac2996ee665',
'0x8ce451a00bfe3ec4cf4131232c3162ba95c35355e7663e7038cd14faf18004d62518771fb40c9ced924bc8a19ea7caaa',
'0x98a0a12747907bbe648924c4ff3ff96393067d9841787750b17e6efd4497103164008d16b8da3ec3baa578e6a5baf931',
'0xb0624dc03276c1ccc609d31d9186a7a4aa07a2d989c5b4a190550381ce22e6fa4ea573195f4352883c6f07ed26640e7b',
'0x894212d3d32f89fa54414a4216af07ed2855fb524e784b664b963ce820ad039e9c2d7bf23a4304369b66fa23019c472b',
'0xa06de152b3b95142c56c6b54beadb5b0dd022ce3c137dee048586388878cd99e63535720e1ed0a6b8538a974505a22af',
'0x913faf9aef9c323754242f9ee5bbf0463d91e45a193877ffc1dea103c1fef57c5511a42386329667ef74e6ad17050cb7',
'0x954da4a93bcfa42bca12a224b1dc1cbd09dbc69eef78be6ae1eaa2746a143a17d2420655c30df10009d0a971730d2514',
'0x85ba052815fbf5ae0093dac1777483363cccb3025823f95764f6b4b0dbf233ca17fb87484181cd1f5520a5465da64560',
'0xa66f8a1a7f6d5e7e0ae3f7ef693bd72265f905d0b6c2d18f7f7665872e9830bc265ca6e74d1523c116e1146f7b0b7210',
'0xab7cf2ff2f9133196f5e38ec73556bd4e104ac46a2891384c778d9a987d73674d762250fa32aa8d6ea9d460030805804',
'0xb8ac106b4b52e097d399c059afec1b2eee26dd2c048898de96306a9ac0950263c1b33f78120c4e7bb25acb20e205d34f',
'0x8b5b5fd74f5aa52371d3baf99925ee6b02252ed13c4b9b71dcd34c3874100827257d5335362eb0efc7649486ed3069cc',
'0xa52cb3acb341c84f812cebb449fc0333af6f80e7e12e93c506edbf7227f6ae3de81232e4ee43a60e331c2a65aef13740',
'0x8e5799b7e283afd93db86c0c5604ded7825c146116b0739663bb61aff77ce1cb6d7cac454ddbf4bf4bb40b8c77573410',
'0xb2d3e7f405e8c7d05aeef18a863c17178c12dc495a8cf5d49918b814c185872e1a28cd75be09ac140a4c4c35c74eace5',
'0xb6b2c01e63852d0adfe5647bc54c944cfb5de144b8540b22ae0116df0db0575457edfc6bd5a8224865c768b7cb8ea757',
'0x81154afa1a253717bd96e200798ba1debd048a2649595ff2b3fa7caebb42fb12d54d3d6baff9f9166d5396f61f5be1fa',
'0x845b4cee082a6726505d107ea74408988b59815cb81c5c3909aec9abe5047247f100d85dbecf067239835fcefc28eae7',
'0xa986f604ff0845783923804063059b7cab4c5c6c6762be1905eef4928274e62406c5d3c449dc66448f20d9b67f265159',
'0xb4eb51865dc543805645c37bdd6c93bff5c499777da14d38b09ab3f7d10a2e8d07d06a1d8cc306ec42a858d0fb002eaa',
'0xa57c956d6d1a3fa707c134a83525b4d618fbbc4811281805be4ba8263f1e64122cbab27357666e0bbf137f1ea8b986d5',
'0xb0f6c87e877e84a9290d4f4418368101b6ec6d6cf3e3778ab655fc321ef18a00be7793ece4b9e11d065fdf85f7acf5c6',
'0x8d03c06619e40ba2d5bddd6416d9a1aeb89f6b79b58d865c6df2e1e097bac654b15940bf07e61d1388c7eb25eec5af47',
'0x834b5758b3b5ad698dca5e1bb94aecc515f94029e652b2ea620f15f10d71f50ab4102788a87475f80cd0fc2a4410a18b',
'0xafae1b4c97f937bc7be16ee960f2236a4e0ba8950776d46e671ecb611dbd74701354ec6406c2aa3321566764545ff903',
'0x9773f71018cd418e25e399469cb70dfe61f12ab51e99cb1fe4c48ba52bf87685f852cc67c8567f922e2f64c6643f8b7d',
'0x99a4bc8590a1fdd356bd3c2e2e861656a95e2602799fe41de986db553a2324249f0a1d970859f10f9eef5da3d55172b3',
'0x96d88091bbeed32afe6dd57064443f84f68a149742d82e04a06246cc01cf2dcdc140eb1b6791ad5f3c50b71098cc00f6',
'0x90af5d6bf3295cab831adb7ee3eaf8c6d7be7d96049cbd08c2f69ba16840335f82d8d311c0ff3158f31b35c6628f4a5c',
'0x935c2b4765e167ac6f4c8b12567d2a06981249e73c5017f5e2f4499b7566368c245d96f595192c2817a70b73cf5541fc',
'0xb813321b004a8d61bade0b6db24e8deee66d3afb3add92de0db2af84b6ed7943e2bf16ee0399aab4e596cb41625adbf4',
'0x8be915e559c8dfc2d038c05e531407ebeaa2dcd353c094125b1fdee43c5bd52e45db4625422906725f3283fd7bee8219',
'0xa702ca1150319b857c4ef74529879f92d3ab7a0994217c3033a0c1972836394acbf37cd9a03554a81801260b0965a59a',
'0x84e81333fb2951d37949ff00276466af5a7b650495c46839c59fc6b1d2816c9a1aa8b9dd425831450432d6a16bf3a4e5',
'0x846987315511db7d48e14737f0e6757f3879fb78b4418afc6ea3b1835e2f22515b101626a2e48f3596757912f467311a',
'0xa34a846a6d035f91faa8862ee7247ea90904cdc6cd4c8a12a008bfafd3f9cecd6c22a60883396c23c7a30c78d5430e7f',
'0xb0036a3b9a4c729b05e9d6d0699d1a52f1abce39de3114b1ce3460212c7a1ffa7d03ab58c22ba39a21f437b0cd43a5f9',
'0xb95068e6ff89eb10bb42310239f0d2d9de6b3d013808d92213c821fdd8545bdc100e8792572ef95feb59e6727b5b3de0',
'0x92232a0cccf5596ff238c10b6e91f9fe504a17c95a7a5fe9347f2887411b81a87c98b70759bf6c43f99b75f2c07f487e',
'0x88b18a943b9f94c9aa36c54487cb6d61c20615c24f5b9ff290054ff166fed069eb199c70202b6138171d16b1f58c20f3',
'0x906844d90d45ec71fbcf3d1cdccf828c987422897aa3bc2d8bcc0ed5b82f0964ce970331ce518236573f9c0be9cf4899',
'0xb02f3778a503ffcd14c7cdb0cfdb272ed86be60a0c82e86630733d7ad5fce1487fd22f8f2b96ed5301e72b3936a1ba9a',
'0xb37ddabd4229b3fb542ba036ecc97166aee6a42f2c10038d45c29bb525b7e2997d7db99fe1e18ff2b25f6830cf349bcf',
'0xacde7374aaa997b38d22765942af651e09fc4b988487dd3ad8f0889d038c2e3b40be1ae037afcc7654453d3879eebea4',
'0xa462e3c847629046405023e26bb3c0cc5e118a1ce29709daf2ab959c5fcabc217cf1edfbb554cd9566600787d1375450',
'0xa1e055c5ee7df7ad88e454057b53fbf1515504e6d449cd0d3c0d990c130e6b66412e87ff9a98d002fca09e925c248fb5',
'0x835f2430a26c4d4722ef2c0ba5f28cf256e93476232ea70912ed0fbc7e4595a9cefeb3c46ae5c7fd487a2f5860ad5356',
'0x8744ee96c94dfcc3cede0b010826ab4786cf8e0a043b95e5473072b28bc0d00eaf27539338553496bf17f85368ebfe37',
'0x9956527b54adf5c8314c848d3feccd642b0918af728c62e4b9be1e5c694d04fa671ce388248ba2e6e370cfe0f912c1aa',
'0xa50cb643b571c99652336b346af6ad08ed263e9d945230b09f56850282a5a235eb55fc6af39f361124511f6fc475e532',
'0xb33bf855b49a914d246c496db45fc523b1a8843c615f48a50c44dcee41065a73d488ff485b937b0d7cf649c336c7630a',
'0xa2bdf1f45bc061fd32537a4b1f3e3572753555de2cc0943c5ac7e505065764530250f82dd9445cc8320fb480b5600ada',
'0x84e3c4ee40c3af8960fbc683e27384c2ae687a92be6a1f60c29615d6f472b2b7fe2f8d53d786bfb7eb45ca8f87ffcb78',
'0xabd1aaa5a47fda35d500696235b4f730e60558d5e940eb290c47e782eca210d7f45bb49dda504ac0a89cb1064e4f8010',
'0xa120734a9c46e069c1602accffee4ab627758657466557046e8ae1e9e901e8c41638bf637b72b428bf3077c719778222']
output: null
Loading
Loading