Skip to content

Commit

Permalink
refactor Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Dec 13, 2023
1 parent daa1c17 commit 9ecbf15
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
10 changes: 9 additions & 1 deletion core/encoding/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,20 @@ func (e *Encoder) UniversalVerifySubBatch(params core.EncodingParams, samplesCor
samples := make([]kzgEncoder.Sample, len(samplesCore))

for i, sc := range samplesCore {
x, err := encoder.GetLeadingCosetIndex(
uint64(sc.AssignmentIndex),
encParams.NumChunks,
)
if err != nil {
return err
}

sample := kzgEncoder.Sample{
Commitment: *sc.Commitment.G1Point,
Proof: sc.Chunk.Proof,
RowIndex: sc.BlobIndex,
Coeffs: sc.Chunk.Coeffs,
X: sc.AssignmentIndex,
X: uint(x),
}
samples[i] = sample
}
Expand Down
14 changes: 5 additions & 9 deletions core/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func NewChunkValidator(enc Encoder, asgn AssignmentCoordinator, cst ChainState,
}
}

// preprocessBlob for each Quorum
func (v *chunkValidator) preprocessBlob(quorumHeader *BlobQuorumInfo, blob *BlobMessage, operatorState *OperatorState) ([]*Chunk, *Assignment, *EncodingParams, error) {
func (v *chunkValidator) validateBlobQuorum(quorumHeader *BlobQuorumInfo, blob *BlobMessage, operatorState *OperatorState) ([]*Chunk, *Assignment, *EncodingParams, error) {
if quorumHeader.AdversaryThreshold >= quorumHeader.QuorumThreshold {
return nil, nil, nil, errors.New("invalid header: quorum threshold does not exceed adversary threshold")
}
Expand Down Expand Up @@ -109,7 +108,7 @@ func (v *chunkValidator) ValidateBlob(blob *BlobMessage, operatorState *Operator

for _, quorumHeader := range blob.BlobHeader.QuorumInfos {
// preprocess validation info
chunks, assignment, params, err := v.preprocessBlob(quorumHeader, blob, operatorState)
chunks, assignment, params, err := v.validateBlobQuorum(quorumHeader, blob, operatorState)
if err == ErrBlobQuorumSkip {
continue
} else if err != nil {
Expand Down Expand Up @@ -139,14 +138,12 @@ func (v *chunkValidator) ValidateBatch(blobs []*BlobMessage, operatorState *Oper
return errors.New("number of bundles does not match number of quorums")
}

// Validate the blob length

// Saved for the blob length validation
blobCommitmentList[k] = blob.BlobHeader.BlobCommitments

// for each quorum
for _, quorumHeader := range blob.BlobHeader.QuorumInfos {
// Check if the operator is a member of the quorum
chunks, assignment, params, err := v.preprocessBlob(quorumHeader, blob, operatorState)
chunks, assignment, params, err := v.validateBlobQuorum(quorumHeader, blob, operatorState)
if err == ErrBlobQuorumSkip {
continue
} else if err != nil {
Expand Down Expand Up @@ -185,8 +182,7 @@ func (v *chunkValidator) ValidateBatch(blobs []*BlobMessage, operatorState *Oper
}

// Parallelize the universal verification for each subBatch
numSubBatch := len(subBatchMap)
numResult := numSubBatch + len(blobCommitmentList)
numResult := len(subBatchMap) + len(blobCommitmentList)
// create a channel to accept results, we don't use stop
out := make(chan error, numResult)

Expand Down
17 changes: 4 additions & 13 deletions pkg/encoding/kzgEncoder/multiframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type Sample struct {
Proof bls.G1Point
RowIndex int // corresponds to a row in the verification matrix
Coeffs []bls.Fr
X uint // X is the same assignment index of chunk in EigenDa
X uint // X is the evaluating index which corresponds to the leading coset
}

// generate a random value using Fiat Shamir transform
// we can also pseudo randomness generated locally, but we have to ensure no adv can manipulate it
// we can also pseudo randomness generated locally, but we have to ensure no adversary can manipulate it
// Hashing everything takes about 1ms, so Fiat Shamir transform does not incur much cost
func GenRandomness(samples []Sample) (bls.Fr, error) {
var buffer bytes.Buffer
Expand Down Expand Up @@ -117,18 +117,9 @@ func genRhsG1(samples []Sample, randomsFr []bls.Fr, m int, params rs.EncodingPar
leadingDs := make([]bls.Fr, n)

for k := 0; k < n; k++ {
// It is important to obtain the leading coset index here
// As the params from the eigenda Core might not have NumChunks be the power of 2
x, err := rs.GetLeadingCosetIndex(
uint64(samples[k].X),
params.NumChunks,
)
if err != nil {
return nil, err
}

// got the leading coset field element
h := ks.ExpandedRootsOfUnity[x]
h := ks.ExpandedRootsOfUnity[samples[k].X]
var hPow bls.Fr
bls.CopyFr(&hPow, &bls.ONE)

Expand Down Expand Up @@ -167,7 +158,7 @@ func (group *KzgEncoderGroup) UniversalVerify(params rs.EncodingParams, samples
for i, s := range samples {
if s.RowIndex >= m {
fmt.Printf("sample %v has %v Row, but there are only %v blobs\n", i, s.RowIndex, m)
return errors.New("sample.Row and numBlob is inconsistent")
return errors.New("sample.RowIndex and numBlob are inconsistent")
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/encoding/kzgEncoder/multiframe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestUniversalVerify(t *testing.T) {
Proof: f.Proof,
RowIndex: z,
Coeffs: f.Coeffs,
X: uint(i),
X: uint(q),
}
samples = append(samples, sample)
}
Expand Down

0 comments on commit 9ecbf15

Please sign in to comment.