-
Notifications
You must be signed in to change notification settings - Fork 54
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
deneb: implement poly comm spec #225
Merged
ralexstokes
merged 25 commits into
ralexstokes:main
from
EchoAlice:polynomial_commitments
Oct 12, 2023
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
1f19c68
Create initial polynomial commitments spec
EchoAlice 976ac3f
Implement blob_to_kzg_commitment() leveraging C-KZG lib. Remove old …
EchoAlice e6ad0ca
Implement compute_kzg_proof()
EchoAlice 7943c25
Return proof *and* evaluation of polynomial at 'z' for compute_kzg_pr…
EchoAlice c61c333
Implement compute_blob_kzg_proof
EchoAlice ab8d032
Iterate on Alex's feedback.
EchoAlice 6bfda99
Implement remaining interface functions from C-KZG
EchoAlice f376160
Remove unnecessary comment
EchoAlice 61bc784
Create ProofAndEvaluation struct. Simplify verify...batch() logic
EchoAlice 1add26d
Reexport KzgSettings
EchoAlice 0aa5fd2
Remove private Deneb item
EchoAlice e78d66d
Fix botched rebase
EchoAlice 88758e2
Leverage Blob type and Bytes32 struct from repository. Change argumen…
EchoAlice 99f62e5
Use custom KzgCommitment and KzgProof types as arguments to relevant …
EchoAlice f45f5ea
Create type alias' with custom type `Bytes48`. Remove unused consts
EchoAlice 8c78fe6
Fix bogus fn outputs
EchoAlice 91b5672
Implement more idiomatic return types for functions.
EchoAlice 351bcb6
WIP - Iteration over Alex's feedback
EchoAlice f391e4d
Convert `.unwrap()`s to `.expect()`s and iterate on other feedback (WIP)
EchoAlice aac268b
Remove unnecessary type alias. Refactor result logic
EchoAlice 5bf73fe
Create custom Error type so we don't leak c_kzg info from our module
EchoAlice 5556b2e
Apply suggestions from code review
ralexstokes 16886a4
Create Error enum that handles all cases for errors to return from ou…
EchoAlice 0842b88
Move features
EchoAlice b164746
Apply suggestions from code review
ralexstokes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use crate::{deneb::blob_sidecar::Blob, primitives::Bytes32, ssz::prelude::*}; | ||
pub use c_kzg::KzgSettings; | ||
use thiserror::Error; | ||
|
||
pub const BYTES_PER_FIELD_ELEMENT: usize = 32; | ||
pub const BYTES_PER_COMMITMENT: usize = 48; | ||
pub const BYTES_PER_PROOF: usize = 48; | ||
|
||
pub type VersionedHash = Bytes32; | ||
pub type FieldElement = Bytes32; | ||
pub type KzgCommitment = ByteVector<BYTES_PER_COMMITMENT>; | ||
pub type KzgProof = ByteVector<BYTES_PER_PROOF>; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
CKzg(#[from] c_kzg::Error), | ||
#[error("proof verification failed")] | ||
InvalidProof, | ||
} | ||
|
||
EchoAlice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub struct ProofAndEvaluation { | ||
pub proof: KzgProof, | ||
pub evaluation: FieldElement, | ||
} | ||
|
||
pub fn blob_to_kzg_commitment<const BYTES_PER_BLOB: usize>( | ||
blob: &Blob<BYTES_PER_BLOB>, | ||
kzg_settings: &KzgSettings, | ||
) -> Result<KzgCommitment, Error> { | ||
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?; | ||
|
||
let commitment = c_kzg::KzgCommitment::blob_to_kzg_commitment(&blob, kzg_settings)?; | ||
let inner = KzgCommitment::try_from(commitment.to_bytes().as_slice()).expect("correct size"); | ||
Ok(inner) | ||
} | ||
|
||
pub fn compute_kzg_proof<const BYTES_PER_BLOB: usize>( | ||
blob: &Blob<BYTES_PER_BLOB>, | ||
evaluation_point: &FieldElement, | ||
kzg_settings: &KzgSettings, | ||
) -> Result<ProofAndEvaluation, Error> { | ||
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?; | ||
let evaluation_point = c_kzg::Bytes32::from_bytes(evaluation_point.as_ref())?; | ||
|
||
let (proof, evaluation) = | ||
c_kzg::KzgProof::compute_kzg_proof(&blob, &evaluation_point, kzg_settings)?; | ||
let proof = KzgProof::try_from(proof.to_bytes().as_ref()).expect("correct size"); | ||
let evaluation = FieldElement::try_from(evaluation.as_slice()).expect("correct size"); | ||
|
||
let result = ProofAndEvaluation { proof, evaluation }; | ||
Ok(result) | ||
} | ||
|
||
pub fn compute_blob_kzg_proof<const BYTES_PER_BLOB: usize>( | ||
blob: &Blob<BYTES_PER_BLOB>, | ||
commitment: &KzgCommitment, | ||
kzg_settings: &KzgSettings, | ||
) -> Result<KzgProof, Error> { | ||
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?; | ||
let commitment = c_kzg::Bytes48::from_bytes(commitment.as_ref()).expect("correct size"); | ||
|
||
let proof = c_kzg::KzgProof::compute_blob_kzg_proof(&blob, &commitment, kzg_settings)?; | ||
|
||
Ok(KzgProof::try_from(proof.to_bytes().as_ref()).expect("input is correct size")) | ||
} | ||
|
||
pub fn verify_kzg_proof( | ||
commitment: &KzgCommitment, | ||
evaluation_point: &FieldElement, | ||
result_point: &FieldElement, | ||
proof: &KzgProof, | ||
kzg_settings: &KzgSettings, | ||
) -> Result<(), Error> { | ||
let evaluation_point = c_kzg::Bytes32::from_bytes(evaluation_point.as_ref())?; | ||
let result_point = c_kzg::Bytes32::from_bytes(result_point.as_ref())?; | ||
let commitment = c_kzg::Bytes48::from_bytes(commitment.as_ref()).expect("correct size"); | ||
let proof = c_kzg::Bytes48::from_bytes(proof.as_ref()).expect("correct size"); | ||
|
||
let res = c_kzg::KzgProof::verify_kzg_proof( | ||
&commitment, | ||
&evaluation_point, | ||
&result_point, | ||
&proof, | ||
kzg_settings, | ||
)?; | ||
|
||
res.then_some(()).ok_or(Error::InvalidProof) | ||
EchoAlice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
pub fn verify_blob_kzg_proof<const BYTES_PER_BLOB: usize>( | ||
blob: &Blob<BYTES_PER_BLOB>, | ||
commitment: &KzgCommitment, | ||
proof: &KzgProof, | ||
kzg_settings: &KzgSettings, | ||
) -> Result<(), Error> { | ||
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?; | ||
let commitment = c_kzg::Bytes48::from_bytes(commitment.as_ref()).unwrap(); | ||
let proof = c_kzg::Bytes48::from_bytes(proof.as_ref()).unwrap(); | ||
|
||
let res = c_kzg::KzgProof::verify_blob_kzg_proof(&blob, &commitment, &proof, kzg_settings)?; | ||
|
||
res.then_some(()).ok_or(Error::InvalidProof) | ||
} | ||
|
||
pub fn verify_blob_kzg_proof_batch<const BYTES_PER_BLOB: usize>( | ||
blobs: &[Blob<BYTES_PER_BLOB>], | ||
commitments: &[KzgCommitment], | ||
proofs: &[KzgProof], | ||
kzg_settings: &KzgSettings, | ||
) -> Result<(), Error> { | ||
let mut c_kzg_blobs = Vec::with_capacity(blobs.len()); | ||
let mut c_kzg_commitments = Vec::with_capacity(commitments.len()); | ||
let mut c_kzg_proofs = Vec::with_capacity(proofs.len()); | ||
|
||
for blob in blobs { | ||
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?; | ||
c_kzg_blobs.push(blob); | ||
} | ||
for commitment in commitments { | ||
let commitment = c_kzg::Bytes48::from_bytes(commitment.as_ref()).unwrap(); | ||
c_kzg_commitments.push(commitment); | ||
} | ||
for proof in proofs { | ||
let proof = c_kzg::Bytes48::from_bytes(proof.as_ref()).unwrap(); | ||
c_kzg_proofs.push(proof); | ||
} | ||
|
||
let res = c_kzg::KzgProof::verify_blob_kzg_proof_batch( | ||
&c_kzg_blobs, | ||
&c_kzg_commitments, | ||
&c_kzg_proofs, | ||
kzg_settings, | ||
)?; | ||
|
||
res.then_some(()).ok_or(Error::InvalidProof) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to be less sloppy next time