2024-02-13 07:04:56 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2024-10-12 18:48:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-02-13 07:04:56 +00:00
|
|
|
"go.uber.org/zap"
|
2024-10-12 18:48:25 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
2024-06-15 16:05:06 +00:00
|
|
|
rbls48581 "source.quilibrium.com/quilibrium/monorepo/bls48581"
|
2024-10-12 18:48:25 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
|
2024-02-13 07:04:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type KZGInclusionProver struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewKZGInclusionProver(logger *zap.Logger) *KZGInclusionProver {
|
|
|
|
return &KZGInclusionProver{
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 18:48:25 +00:00
|
|
|
func (k *KZGInclusionProver) VerifyFrame(
|
|
|
|
frame *protobufs.ClockFrame,
|
|
|
|
) error {
|
|
|
|
aggregateCommitments := [][]byte{}
|
|
|
|
for i := 0; i < (len(frame.Input)-516)/74; i++ {
|
|
|
|
c := frame.Input[516+(i*74) : 516+(i*74)+74]
|
|
|
|
aggregateCommitments = append(aggregateCommitments, c)
|
|
|
|
}
|
2024-06-15 16:05:06 +00:00
|
|
|
|
2024-10-12 18:48:25 +00:00
|
|
|
if len(aggregateCommitments) != len(frame.AggregateProofs) {
|
|
|
|
k.logger.Error(
|
|
|
|
"commit length mismatched proof for frame",
|
|
|
|
zap.Int("commit_length", len(aggregateCommitments)),
|
|
|
|
zap.Int("proof_length", len(frame.AggregateProofs)),
|
|
|
|
)
|
|
|
|
return errors.Wrap(
|
|
|
|
errors.New("commit length mismatched proof for frame"),
|
|
|
|
"verify frame",
|
|
|
|
)
|
|
|
|
}
|
2024-06-15 16:05:06 +00:00
|
|
|
|
2024-10-12 18:48:25 +00:00
|
|
|
for _, proof := range frame.AggregateProofs {
|
|
|
|
commitments := [][]byte{}
|
|
|
|
expand := []byte{}
|
|
|
|
|
|
|
|
for _, commit := range proof.GetInclusionCommitments() {
|
|
|
|
switch commit.TypeUrl {
|
|
|
|
case protobufs.IntrinsicExecutionOutputType:
|
|
|
|
k.logger.Debug("confirming inclusion in aggregate")
|
|
|
|
digest := sha3.NewShake256()
|
|
|
|
_, err := digest.Write(commit.Data)
|
|
|
|
if err != nil {
|
|
|
|
k.logger.Error(
|
|
|
|
"error converting key bundle to polynomial",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
return errors.Wrap(err, "verify frame")
|
|
|
|
}
|
|
|
|
|
|
|
|
expand = make([]byte, 1024)
|
|
|
|
_, err = digest.Read(expand)
|
|
|
|
if err != nil {
|
|
|
|
k.logger.Error(
|
|
|
|
"error converting key bundle to polynomial",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
return errors.Wrap(err, "verify frame")
|
|
|
|
}
|
|
|
|
|
|
|
|
commitments = append(commitments, commit.Commitment)
|
|
|
|
default:
|
|
|
|
return errors.Wrap(errors.New("unsupported"), "verify frame")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(commitments) != 1 {
|
|
|
|
return errors.Wrap(errors.New("unsupported"), "verify frame")
|
|
|
|
}
|
|
|
|
|
|
|
|
result := rbls48581.VerifyRaw(
|
|
|
|
expand,
|
|
|
|
commitments[0],
|
|
|
|
uint64(expand[0]%16),
|
|
|
|
proof.Proof,
|
|
|
|
16,
|
|
|
|
)
|
|
|
|
if !result {
|
|
|
|
k.logger.Error(
|
|
|
|
"could not verify clock frame",
|
|
|
|
zap.Error(errors.New("invalid proof")),
|
|
|
|
)
|
|
|
|
return errors.Wrap(
|
|
|
|
errors.New("invalid proof"),
|
|
|
|
"verify frame",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-06-15 16:05:06 +00:00
|
|
|
|
2024-10-12 18:48:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-02-13 07:04:56 +00:00
|
|
|
|
2024-06-08 11:32:45 +00:00
|
|
|
func (k *KZGInclusionProver) CommitRaw(
|
|
|
|
data []byte,
|
|
|
|
polySize uint64,
|
|
|
|
) ([]byte, error) {
|
2024-06-15 16:05:06 +00:00
|
|
|
return rbls48581.CommitRaw(data, polySize), nil
|
2024-06-08 11:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KZGInclusionProver) ProveRaw(
|
|
|
|
data []byte,
|
|
|
|
index int,
|
|
|
|
polySize uint64,
|
|
|
|
) ([]byte, error) {
|
2024-06-15 16:05:06 +00:00
|
|
|
return rbls48581.ProveRaw(data, uint64(index), polySize), nil
|
2024-06-08 11:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KZGInclusionProver) VerifyRaw(
|
|
|
|
data []byte,
|
|
|
|
commit []byte,
|
|
|
|
index int,
|
|
|
|
proof []byte,
|
|
|
|
polySize uint64,
|
|
|
|
) (bool, error) {
|
2024-06-15 16:05:06 +00:00
|
|
|
return rbls48581.VerifyRaw(data, commit, uint64(index), proof, polySize), nil
|
2024-06-08 11:32:45 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
var _ InclusionProver = (*KZGInclusionProver)(nil)
|