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

Add support for EIP-7594 #452

Merged
merged 246 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 233 commits
Commits
Show all changes
246 commits
Select commit Hold shift + click to select a range
d2a97c1
Add initial implementation of DAS functions
jtraglia Nov 14, 2023
dba45a0
Add rust bindings for DAS functions
jtraglia Nov 15, 2023
0d1875a
Refactor names for types/consts
jtraglia Nov 15, 2023
a8e4785
For go bindings, use heap and pointers
jtraglia Nov 17, 2023
224ecfe
Add verify_sample_proof_batch function
jtraglia Nov 30, 2023
7eb54ce
Fix a few nits in new function
jtraglia Nov 30, 2023
7604da9
Add more comments
jtraglia Nov 30, 2023
4a20215
Add new batch func to Rust bindings
jtraglia Nov 30, 2023
9c1d3f2
Add multi blob test for batch func
jtraglia Nov 30, 2023
4cd978e
Start to add java bindings
jtraglia Nov 30, 2023
0acf0d9
Add batch verification method to java bindings
jtraglia Nov 30, 2023
eeb8afc
Use more complex sample structure
jtraglia Dec 1, 2023
455e106
Update go bindings
jtraglia Dec 1, 2023
7e0ef29
Clean up go bindings a little
jtraglia Dec 1, 2023
5a085e8
Delete 2d functions
jtraglia Dec 1, 2023
d974692
Update java bindings
jtraglia Dec 4, 2023
c841e0b
Use appropriate style in java bindings
jtraglia Dec 4, 2023
c6dba3b
Remove references to BLOB_COUNT
jtraglia Dec 4, 2023
0ee25f5
Add samplesToBlob and recoverSamples to java bindings
jtraglia Dec 4, 2023
fbf580b
Improve samples_to_blob
jtraglia Dec 4, 2023
f40f937
Replace calloc with JNI allocation
jtraglia Dec 5, 2023
d7a932c
Rename two functions
jtraglia Dec 5, 2023
579ebb9
Rename SAMPLE_SIZE to FIELD_ELEMENTS_PER_SAMPLE
jtraglia Dec 5, 2023
488d4e0
Update rust bindings
jtraglia Dec 5, 2023
e262a66
Fix comment
jtraglia Dec 5, 2023
3484da2
Use fast g1 lincomb implementation
jtraglia Dec 6, 2023
a758b25
Rename sample to cell
jtraglia Jan 10, 2024
802ee97
Fix comment indentations for cell params
jtraglia Jan 10, 2024
68ee8e2
Add pub methods for cell and export das items (#386)
jacobkaufmann Jan 23, 2024
e9c6801
Add benchmark for verifying columns
jtraglia Jan 25, 2024
fdf5928
Remove complex cell type
jtraglia Jan 26, 2024
e6ab04f
Add helper functions for Cell
jtraglia Jan 26, 2024
0c06fcf
Remove reference to old BLOB_COUNT variable
jtraglia Jan 29, 2024
e36de87
Make Cell::new const
jtraglia Jan 29, 2024
0999734
Add func to profile compute_cells_and_proofs
jtraglia Jan 29, 2024
a190ea3
Change scale factor from 5 to 7
jtraglia Jan 31, 2024
ae99048
Check that recovered cells match input cells
jtraglia Jan 31, 2024
9a1cb0a
Add methods to cell to retrieve byte representations (#387)
jacobkaufmann Jan 31, 2024
68dbb3a
Correct offset for cell 2D array construction (#388)
jacobkaufmann Jan 31, 2024
7f1ed38
Rename DATA_POINTS_PER_BLOB -> FIELD_ELEMENTS_PER_EXT_BLOB
jtraglia Feb 1, 2024
da392b0
Add BITS_PER_FIELD_ELEMENT constant
jtraglia Feb 2, 2024
1a64692
Revert back to simpler cells_to_blob impl
jtraglia Feb 2, 2024
dd6f2fe
Add compute_cells() func to bindings
jtraglia Feb 2, 2024
be1437e
Go back to non-pointer params in Go bindings
jtraglia Feb 2, 2024
f40b78c
Fix minor typo
jtraglia Feb 2, 2024
7122cae
Replace multiple memory allocs with one
jtraglia Feb 2, 2024
5a2656d
Refactor fk20_multi_da_opt
jtraglia Feb 2, 2024
1bee14a
Speed up proof generation by 2x
jtraglia Feb 14, 2024
e2a96a9
Clean things up a little
jtraglia Feb 14, 2024
22d4729
Fix some minor mistakes dealing with refactor
jtraglia Feb 20, 2024
8f240b7
Use fixed-based MSM for proof generation
jtraglia Feb 21, 2024
f686360
Merge branch 'main' into das
jtraglia Feb 27, 2024
2b70d2b
Compute random challenge for verify cell proof batch
jtraglia Feb 27, 2024
c03a5ab
Filter out zero points in g1_lincomb_fast
jtraglia Feb 27, 2024
78d258c
Use bool array to check if values are used
jtraglia Mar 5, 2024
cdd59c4
Fix minor typo
jtraglia Mar 5, 2024
c2cf698
Allocate polys & use consistent ordering
jtraglia Mar 5, 2024
24d793e
Merge remote-tracking branch 'origin/main' into das
jtraglia Mar 21, 2024
146bf44
Add node.js bindings for DAS functions
jtraglia Mar 21, 2024
4114989
Remove duplicate constant
jtraglia Mar 21, 2024
955ec20
Add das functions to c_kzg_4844_jni.h
jtraglia Apr 9, 2024
f5388a4
Use verifyCellProofBatch domain from spec
jtraglia Apr 9, 2024
1124ee9
Rename rowIds & columnIds to *Indices to match spec
jtraglia Apr 9, 2024
7027b08
Rename recover_cells to recover_polynomial
jtraglia Apr 16, 2024
beaba1a
Fix bug in new g1_lincomb_fast
jtraglia Apr 18, 2024
ab56ac6
Add initial set of peerdas reference tests
jtraglia Apr 18, 2024
585c6c8
Fix benchmarks in go bindings
jtraglia Apr 19, 2024
4ba3962
Add VerifyRows benchmark
jtraglia Apr 19, 2024
ea0319a
Rename recover_polynomial to recover_all_cells
jtraglia Apr 22, 2024
13be436
Rename CELLS_PER_BLOB to CELLS_PER_EXT_BLOB
jtraglia Apr 22, 2024
c350122
Update reference tests
jtraglia Apr 24, 2024
57b94f4
Make some minor fixups in function comments
jtraglia Apr 24, 2024
4532f1e
Merge remote-tracking branch 'origin/main' into das
jtraglia Apr 24, 2024
40faae4
Clean up verify_kzg_proof_multi_impl
jtraglia Apr 24, 2024
5ddb74b
Add new reference tests for Rust bindings
jtraglia Apr 24, 2024
d3bc458
Remove old rust test
jtraglia Apr 24, 2024
6928688
Merge remote-tracking branch 'origin/main' into das
jtraglia Apr 25, 2024
44d4306
Start to add das funcs to python bindings
jtraglia Apr 25, 2024
21d4ca9
Add python bindings for compute_cells*
jtraglia Apr 25, 2024
42bac4b
Add python bindings for verify_cell_proof*
jtraglia Apr 25, 2024
0e3332f
Address a few minor nits
jtraglia Apr 25, 2024
7ec9687
Run das reference tests on java bindings
jtraglia Apr 25, 2024
17a6feb
Run das reference tests on nodejs bindings
jtraglia Apr 25, 2024
439426a
Begin to add c# das bindings
jtraglia Apr 25, 2024
6e4ea90
Add compute methods for c#
jtraglia Apr 25, 2024
1dafe1b
Add recoverAllCells method for C#
jtraglia Apr 26, 2024
cd87686
Update reference tests
jtraglia Apr 26, 2024
16ebed4
Rename "proof" to "kzg_proof" in das functions
jtraglia Apr 26, 2024
7bb4294
Add extra null checks in free_trusted_setup
jtraglia Apr 26, 2024
18755b6
Fix typo (ftt -> fft)
jtraglia Apr 26, 2024
d18672d
Begin to add das funcs to nim bindings
jtraglia Apr 26, 2024
cb93b3a
Add other das functions for nim bindings
jtraglia Apr 26, 2024
5279a94
Update uppercase constants
jtraglia Apr 26, 2024
ddc9f2b
Fix memleak in poly_lagrange_to_monomial
jtraglia Apr 26, 2024
70850f7
Add function doc and remove poly_monomial_to_lagrange
jtraglia Apr 26, 2024
0ce3208
Convert interpolation_poly from poly_t to fr_t*
jtraglia Apr 26, 2024
7307014
Add explicit duplication check in recover_all_cells
jtraglia Apr 27, 2024
3901e79
Improve scale_poly helper funcs
jtraglia Apr 27, 2024
74ddae0
Revert scale factor swap
jtraglia Apr 27, 2024
e08f22e
Use g1_lincomb_naive in verify_kzg_proof_batch
jtraglia Apr 29, 2024
783ad62
Simplify fft_g1_fast with iterative implementation
jtraglia May 3, 2024
902ff30
Revert "Simplify fft_g1_fast with iterative implementation"
jtraglia May 3, 2024
955ad4d
Remove da_using_fk20_multi and use less poly_t
jtraglia May 3, 2024
27690a2
Use more FIELD_ELEMENTS_PER_EXT_BLOB
jtraglia May 3, 2024
433398a
Compute fixed-base MSM scratch size in init_fk20_multi_settings
jtraglia May 3, 2024
43f12d6
Clean up function docs
jtraglia May 3, 2024
6ec2927
Add inf check to fft_g1 to save 1ms
jtraglia May 3, 2024
7cafa95
Merge remote-tracking branch 'origin/main' into das
jtraglia May 6, 2024
eadfcb2
[nodejs] Update docs & assign alpha version (#421)
matthewkeil May 6, 2024
26d3b41
Update versions in preparation for release (#428)
jtraglia May 7, 2024
f5e4b03
Make precomputation optional/configurable
jtraglia May 13, 2024
9d86b54
Address asn feedback
jtraglia May 14, 2024
315b3cc
Change recover_all_cells from Vec to fixed array
jtraglia May 14, 2024
0c5bb7f
Box recover_all_cells return
jtraglia May 14, 2024
114fa03
Skip vector allocation, only do fixed array
jtraglia May 14, 2024
20fcc40
Add null check for 2d coeffs before freeing
jtraglia May 15, 2024
4ad278f
Add parallel bench test
jtraglia May 15, 2024
dd940f6
Add cellsToBlob & computeCells to Nim bindings
jtraglia May 17, 2024
4f1172d
Clean up some function docs
jtraglia May 23, 2024
b718e64
Add G1 points in monomial form to the trusted setup
jtraglia May 24, 2024
ca21314
Move no-recovery-needed check further down
jtraglia May 24, 2024
88da8da
Rename g1/g2 fields in trusted setup
jtraglia May 24, 2024
f859573
Fix proof computation bug with other FIELD_ELEMENTS_PER_CELL configs
jtraglia May 24, 2024
5c78a21
Begin to add rust fuzzing
jtraglia May 29, 2024
f5a2dbc
Add differential fuzzer with peerdas-kzg
jtraglia May 29, 2024
4f5ee50
Add more differential fuzzers
jtraglia May 29, 2024
1c966f1
Create corpus dir if it doesn't exist
jtraglia May 29, 2024
56d6d11
Fix typo in corpus name
jtraglia May 29, 2024
87ed99f
Add more eip7594 fuzzers
jtraglia May 30, 2024
b00a366
Remove duplicate allocation
jtraglia May 30, 2024
e0fe6b9
Change recover_all_cells to recover_cells_and_kzg_proofs
jtraglia May 30, 2024
43d7681
Check ctx in computeCellsAndProofs and recoverProofs
jtraglia May 30, 2024
622b2e4
Add RecoverCellsAndKZGProofs func for Go
jtraglia May 31, 2024
62c39e7
Remove unused variables
jtraglia Jun 3, 2024
1f57f65
Clean up recover_all_cells_impl some
jtraglia Jun 3, 2024
bf6347d
Do some more clean up
jtraglia Jun 3, 2024
d1aa2da
Various clean up
jtraglia Jun 3, 2024
0b050e1
[nim] Correct params for verifyProof (#433)
agnxsh Jun 4, 2024
cfe301a
Move coset_for_cell
jtraglia Jun 4, 2024
4b0760f
Add recover_cells_and_kzg_proofs to Rust bindings
jtraglia Jun 4, 2024
1c165f8
Add recover_cells_and_kzg_proofs to Python bindings
jtraglia Jun 4, 2024
3046d04
Add recover_cells_and_kzg_proofs to Java bindings
jtraglia Jun 4, 2024
0437d05
Replace some uint64_t with size_t
jtraglia Jun 4, 2024
6731352
Add recover_cells_and_kzg_proofs to Node bindings
jtraglia Jun 4, 2024
b6b5a6e
Add recover_cells_and_kzg_proofs for Nim bindings
jtraglia Jun 4, 2024
8a9e975
Merge remote-tracking branch 'origin/main' into das
jtraglia Jun 5, 2024
230e13b
Quick & dirty fix for cell fromHex in Nim
jtraglia Jun 5, 2024
5f3f724
Convert Cell to flat array of bytes
jtraglia Jun 5, 2024
b14c403
Remove to_bytes() usages in generate-fuzz-corpus code
jtraglia Jun 5, 2024
8c2c552
Fix Go's Cell.UnmarshalText function
jtraglia Jun 5, 2024
1665d50
Do not expose all constants
jtraglia Jun 6, 2024
e644ce9
Remove old references to recover_all_cells in C
jtraglia Jun 6, 2024
d36cb68
Update rustfuzz
jtraglia Jun 6, 2024
c7bc312
Clean up Rust bindings
jtraglia Jun 6, 2024
3cf5ab2
Add new recover_cells_and_kzg_proofs tests
jtraglia Jun 10, 2024
c5554ed
Remove cells_to_blob
jtraglia Jun 10, 2024
e028648
Update c# reference tests
jtraglia Jun 10, 2024
585b941
Update java reference tests
jtraglia Jun 10, 2024
d4e4f4a
Update python reference tests
jtraglia Jun 10, 2024
8f1cd28
Update nodejs reference tests
jtraglia Jun 10, 2024
908bc98
Update nim reference tests
jtraglia Jun 10, 2024
fc73ccd
Delete some recover_all_cells bindings
jtraglia Jun 10, 2024
69e4abc
Update rust reference tests
jtraglia Jun 10, 2024
d556fc3
Remove compute_cells
jtraglia Jun 10, 2024
08d7273
Specify precompute in base_fuzz.h (#435)
asanso Jun 11, 2024
8dbab5c
Make updates to rustfuzz
jtraglia Jun 11, 2024
4bc5fe0
Add new Rust files
jtraglia Jun 11, 2024
5499c69
Make CELLS_PER_EXT_BLOB in Java protected
jtraglia Jun 11, 2024
3343d0f
some minor improvements of comments
b-wagn Jun 18, 2024
e24e206
Improve documentation of verify_cell_kzg_proof
b-wagn Jun 19, 2024
304bf66
Improve documentation and variable names in verify_kzg_proof_multi_impl
b-wagn Jun 19, 2024
85d7374
Change vanishing poly name to Z(X) + wrap lines in comments later
b-wagn Jun 19, 2024
3f4a7a4
Merge pull request #441 from b-wagn/das-doc-improve
asn-d6 Jun 20, 2024
a9ba548
[rust] Change load_trusted_setup to take flat byte arrays (#440)
asn-d6 Jun 25, 2024
141c873
In recover_cells_and_kzg_proofs, always do FK20 (#442)
asn-d6 Jun 25, 2024
61ce67e
Run C auto-formatter & fix some nits
jtraglia Jun 25, 2024
3b091bd
Update project README
jtraglia Jun 25, 2024
d5ebc50
Add 4844 functions to rustfuzz
jtraglia Jun 25, 2024
427157d
Fix documentation formatting (#446)
b-wagn Jun 26, 2024
9af0971
Remove proofs param from recover_cells_and_kzg_proofs
jtraglia Jun 26, 2024
6647c10
Replace fuzz with rustfuzz
jtraglia Jun 26, 2024
99f13fe
Add new fuzz directory
jtraglia Jun 26, 2024
690c343
Update copyright years
jtraglia Jun 26, 2024
23c16f2
Update ref tests & rename cell id to cell index
jtraglia Jun 26, 2024
47bb90d
Remove unused commitment filtering
jtraglia Jun 27, 2024
7a00fa7
Run cargo update in fuzz
jtraglia Jun 27, 2024
13060e1
Change fuzz corpus path from rustfuzz to fuzz
jtraglia Jun 27, 2024
289635d
Add profiling funcs for 7594 methods
jtraglia Jun 27, 2024
6c89e8f
Start to fuzz against Constantine
jtraglia Jun 28, 2024
8714b80
Fuzz 4844 funcs against Constantine
jtraglia Jun 28, 2024
0b121ef
Rid ourselves of the CHECK macro
jtraglia Jul 1, 2024
eee74a3
Fix various documentation nits
jtraglia Jul 1, 2024
d176593
DAS: Implement coset FFT and use it around the codebase (#449)
asn-d6 Jul 6, 2024
5e4be92
Remove idea of point from load_trusted_setup params
jtraglia Jul 8, 2024
5ce1d4f
In batch cell verification, take commitment for each cell (#451)
jtraglia Jul 11, 2024
8758b3d
Remove verify_cell_kzg_proof
jtraglia Jul 11, 2024
f8e768b
Remove verify_cell_kzg_proof tests
jtraglia Jul 11, 2024
51e9ee3
Rename column_indices to cell_indices
jtraglia Jul 11, 2024
7354c6a
Don't use `tmp` variable in `c_kzg_calloc`
asn-d6 Jul 11, 2024
1846247
Fix Fiat-Shamir computation in verify_cell_kzg_proof_batch()
asn-d6 Jul 11, 2024
4b6cc41
Fix function docs
jtraglia Jul 11, 2024
ff2008d
Move batch cell verification helper funcs up
jtraglia Jul 11, 2024
dced524
Clean up compute_r_powers_for_verify_cell_kzg_proof_batch
jtraglia Jul 11, 2024
6c80d2c
Revert "Clean up compute_r_powers_for_verify_cell_kzg_proof_batch"
jtraglia Jul 11, 2024
fa1ec0f
Use consistent ordering of new EIP-7594 funcs
jtraglia Jul 11, 2024
ab15763
Reorder fuzzers
jtraglia Jul 11, 2024
13768f4
Rename "cas" to "caps"
jtraglia Jul 11, 2024
8e5f51a
Clean up java bindings
jtraglia Jul 11, 2024
a00850f
Remove ComputeCellsTest
jtraglia Jul 11, 2024
d4cf8b7
Remove G*Data types
jtraglia Jul 11, 2024
b6c4e3d
Fix some nits
jtraglia Jul 11, 2024
7ca7a26
Merge remote-tracking branch 'origin/main' into das
jtraglia Jul 12, 2024
4a2824d
Revert changes to poly_to_kzg_commitment
jtraglia Jul 12, 2024
1254856
Revert accidental blst downgrade
jtraglia Jul 12, 2024
ae214c2
Add newline to trusted setup
jtraglia Jul 12, 2024
d7dc5f4
Replace min with MIN macro to fix name conflict
jtraglia Jul 12, 2024
136f781
Add missing constants to Rust benchmarks
jtraglia Jul 12, 2024
2c451a6
Replace __SIZEOF_POINTER__ with sizeof(void *)
jtraglia Jul 12, 2024
44f47b0
Add debug prints to C# test
jtraglia Jul 12, 2024
4da0c65
Add new functions to csharp/ckzg.def
jtraglia Jul 12, 2024
5e51630
Add debug print for C#'s TestRecoverCellsAndKzgProofs
jtraglia Jul 12, 2024
ee1b77d
Change param types for recover from byte * to byte*
jtraglia Jul 12, 2024
bad7bdf
Add debug prints
jtraglia Jul 12, 2024
91bad45
Add more info to the exception
jtraglia Jul 12, 2024
43ba901
Change ulong * to ulong*
jtraglia Jul 12, 2024
d7c5056
Add more debug prints
jtraglia Jul 12, 2024
ecc33f7
Use ulong instead of int
jtraglia Jul 12, 2024
455bb22
Remove debug prints
jtraglia Jul 12, 2024
1296b34
Use TestCaseSource for new C# tests
jtraglia Jul 12, 2024
57db1db
Improve function docs for C and C#
jtraglia Jul 12, 2024
50fd406
Clarify doc comment
jtraglia Jul 12, 2024
38e3a00
Replace cellCount with len(cells)
jtraglia Jul 12, 2024
e781b8b
Remove unused include in Java bindings
jtraglia Jul 12, 2024
742f6a8
Add C test for verify_cell_kzg_proof_batch
jtraglia Jul 15, 2024
7af67a4
Clean up C tests some more
jtraglia Jul 15, 2024
e689135
Make CellsPerExtBlob in Go bindings public
jtraglia Jul 15, 2024
0e753e2
Revert "Make CellsPerExtBlob in Go bindings public"
jtraglia Jul 15, 2024
ce7c857
Merge remote-tracking branch 'origin/main' into das
jtraglia Jul 17, 2024
4694410
Revert fuzz changes
jtraglia Jul 17, 2024
e7da294
Revert changes to Rust bindings for new fuzzer
jtraglia Jul 17, 2024
07923d0
Revert test_files.iter() to test_files
jtraglia Jul 17, 2024
b805483
g1_lincomb_fast(): Simplify point at infinity weed out (#454)
asn-d6 Jul 18, 2024
08fba8b
Fix formatting
jtraglia Jul 18, 2024
7a99c25
Apply review feedback to Nim's load_trusted_setup
jtraglia Jul 18, 2024
0c96ac1
Make some constants public again
jtraglia Jul 18, 2024
9990d88
Merge branch 'main' into das
jtraglia Jul 18, 2024
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
434 changes: 210 additions & 224 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ serde = ["dep:serde"]
generate-bindings = ["dep:bindgen"]
ethereum_kzg_settings = ["dep:once_cell"]

# Enable this feature when running the tests to generate the fuzzing corpus.
# This converts the yaml reference tests into a binary form for the fuzzer.
generate-fuzz-corpus = []

# This is a standalone feature so that crates that disable default features can
# enable blst/portable without having to add it as a dependency
portable = ["blst/portable"]
Expand All @@ -36,6 +40,7 @@ portable = ["blst/portable"]
no-threads = ["blst/no-threads"]

[dependencies]
arbitrary = { version = "1", features = ["derive"] }
blst = { version = "0.3.11", default-features = false }
hex = { version = "0.4.2", default-features = false, features = ["alloc"] }
libc = { version = "0.2", default-features = false }
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A minimal implementation of the [Polynomial
Commitments](https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/polynomial-commitments.md)
API for EIP-4844, written in C.
API for EIP-4844 and EIP-7594, written in C.

## Bindings

Expand All @@ -27,13 +27,21 @@ The C-KZG-4844 library provides implementations of the public KZG functions that
are defined in the Polynomial Commitments specification. The aim is to align
these functions as closely as possible with the specification.

For EIP-4844:

- `blob_to_kzg_commitment`
- `compute_kzg_proof`
- `compute_blob_kzg_proof`
- `verify_kzg_proof`
- `verify_blob_kzg_proof`
- `verify_blob_kzg_proof_batch`

For EIP-7594:

- `compute_cells_and_kzg_proofs`
- `recover_cells_and_kzg_proofs`
- `verify_cell_kzg_proof_batch`

This library also provides functions for loading and freeing the trusted setup,
which are not defined in the specification. These functions are intended to be
executed once during the initialization process. As the name suggests, the
Expand Down
15 changes: 13 additions & 2 deletions bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static IntPtr LoadNativeLibrary(Assembly _, string path)
}

[DllImport("ckzg", EntryPoint = "load_trusted_setup_wrap")]
private static extern IntPtr InternalLoadTrustedSetup(string filename);
private static extern IntPtr InternalLoadTrustedSetup(string filename, ulong precompute);

[DllImport("ckzg", EntryPoint = "free_trusted_setup_wrap", CallingConvention = CallingConvention.Cdecl)]
private static extern void InternalFreeTrustedSetup(IntPtr ts);
Expand All @@ -58,7 +58,18 @@ private static extern unsafe KzgResult VerifyBlobKzgProof(out bool result, byte*

[DllImport("ckzg", EntryPoint = "verify_blob_kzg_proof_batch", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult VerifyBlobKzgProofBatch(out bool result, byte* blobs, byte* commitments,
byte* proofs, int count, IntPtr ts);
byte* proofs, ulong count, IntPtr ts);

[DllImport("ckzg", EntryPoint = "compute_cells_and_kzg_proofs", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult ComputeCellsAndKzgProofs(byte* cells, byte* proofs, byte* blob, IntPtr ts);

[DllImport("ckzg", EntryPoint = "recover_cells_and_kzg_proofs", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult RecoverCellsAndKzgProofs(byte* recovered_cells, byte* recovered_proofs,
ulong* cell_indices, byte* cells, ulong num_cells, IntPtr ts);

[DllImport("ckzg", EntryPoint = "verify_cell_kzg_proof_batch", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult VerifyCellKzgProofBatch(out bool result, byte* commitments,
ulong* cell_indices, byte* cells, byte* proofs, ulong num_cells, IntPtr ts);

private enum KzgResult
{
Expand Down
4 changes: 2 additions & 2 deletions bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
<PropertyGroup>
<Authors>Ethereum Foundation</Authors>
<Copyright>Ethereum Foundation</Copyright>
<Description>The C# bindings for the Polynomial Commitments API library for EIP-4844</Description>
<Description>The C# bindings for the Polynomial Commitments API library for EIP-4844 and EIP-7594</Description>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<PackageId>Ckzg.Bindings</PackageId>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>c-kzg eip-4844</PackageTags>
<PackageTags>c-kzg eip-4844 eip-7594</PackageTags>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/ethereum/c-kzg-4844</RepositoryUrl>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
122 changes: 113 additions & 9 deletions bindings/csharp/Ckzg.Bindings/Ckzg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@ namespace Ckzg;

public static partial class Ckzg
{
public const int BytesPerFieldElement = 32;
public const int FieldElementsPerBlob = 4096;
public const int BytesPerBlob = BytesPerFieldElement * FieldElementsPerBlob;

// Private constants.
private const int BytesPerFieldElement = 32;
private const int CellsPerExtBlob = 128;
private const int FieldElementsPerBlob = 4096;
private const int FieldElementsPerCell = 64;

// Public constants.
public const int BytesPerBlob = FieldElementsPerBlob * BytesPerFieldElement;
public const int BytesPerCell = FieldElementsPerCell * BytesPerFieldElement;
public const int BytesPerCommitment = 48;
public const int BytesPerProof = 48;

/// <summary>
/// Loads trusted setup settings from file.
/// </summary>
/// <param name="filename">Settings file path</param>
/// <param name="precompute">Configurable value between 0-15</param>
/// <exception cref="ArgumentException">Thrown when the file path is not correct</exception>
/// <exception cref="InvalidOperationException">Thrown when unable to load the setup</exception>
/// <returns>Trusted setup settings as a pointer</returns>
public static IntPtr LoadTrustedSetup(string filepath)
public static IntPtr LoadTrustedSetup(string filepath, int precompute)
{
if (!File.Exists(filepath)) throw new ArgumentException("Trusted setup file does not exist", nameof(filepath));

IntPtr ckzgSetup = InternalLoadTrustedSetup(filepath);
IntPtr ckzgSetup = InternalLoadTrustedSetup(filepath, (ulong)precompute);

if (ckzgSetup == IntPtr.Zero) throw new InvalidOperationException("Unable to load trusted setup");
return ckzgSetup;
Expand All @@ -30,7 +38,6 @@ public static IntPtr LoadTrustedSetup(string filepath)
/// </summary>
/// <param name="ckzgSetup">Trusted setup settings</param>
/// <exception cref="ArgumentException">Thrown when settings are not correct</exception>

public static void FreeTrustedSetup(IntPtr ckzgSetup)
{
ThrowOnUninitializedTrustedSetup(ckzgSetup);
Expand All @@ -49,8 +56,8 @@ public static void FreeTrustedSetup(IntPtr ckzgSetup)
public static unsafe void BlobToKzgCommitment(Span<byte> commitment, ReadOnlySpan<byte> blob, IntPtr ckzgSetup)
{
ThrowOnUninitializedTrustedSetup(ckzgSetup);
ThrowOnInvalidLength(blob, nameof(blob), BytesPerBlob);
ThrowOnInvalidLength(commitment, nameof(commitment), BytesPerCommitment);
ThrowOnInvalidLength(blob, nameof(blob), BytesPerBlob);

fixed (byte* commitmentPtr = commitment, blobPtr = blob)
{
Expand Down Expand Up @@ -191,12 +198,103 @@ public static unsafe bool VerifyBlobKzgProofBatch(ReadOnlySpan<byte> blobs, Read
fixed (byte* blobsPtr = blobs, commitmentsPtr = commitments, proofsPtr = proofs)
{
KzgResult kzgResult =
VerifyBlobKzgProofBatch(out var result, blobsPtr, commitmentsPtr, proofsPtr, count, ckzgSetup);
VerifyBlobKzgProofBatch(out var result, blobsPtr, commitmentsPtr, proofsPtr, (ulong)count, ckzgSetup);
ThrowOnError(kzgResult);
return result;
}
}

/// <summary>
/// Given a blob, get all of its cells and proofs.
/// </summary>
/// <param name="cells">Cells as a flattened byte array</param>
/// <param name="proofs">Proofs as a flattened byte array</param>
/// <param name="blob">Blob bytes</param>
/// <param name="ckzgSetup">Trusted setup settings</param>
/// <exception cref="ArgumentException">Thrown when length of an argument is not correct or settings are not correct</exception>
/// <exception cref="ApplicationException">Thrown when the library returns unexpected Error code</exception>
/// <exception cref="InsufficientMemoryException">Thrown when the library has no enough memory to process</exception>
public static unsafe void ComputeCellsAndKzgProofs(Span<byte> cells, Span<byte> proofs, ReadOnlySpan<byte> blob,
IntPtr ckzgSetup)
{
ThrowOnUninitializedTrustedSetup(ckzgSetup);
ThrowOnInvalidLength(cells, nameof(cells), BytesPerCell * CellsPerExtBlob);
ThrowOnInvalidLength(proofs, nameof(proofs), BytesPerProof * CellsPerExtBlob);
ThrowOnInvalidLength(blob, nameof(blob), BytesPerBlob);

fixed (byte* cellsPtr = cells, proofsPtr = proofs, blobPtr = blob)
{
KzgResult result = ComputeCellsAndKzgProofs(cellsPtr, proofsPtr, blobPtr, ckzgSetup);
ThrowOnError(result);
}
}

/// <summary>
/// Given some cells for a blob, recover all cells/proofs.
/// </summary>
/// <param name="recoveredCells">Recovered cells as a flattened byte array</param>
/// <param name="recoveredProofs">Recovered proofs as a flattened byte array</param>
/// <param name="cellIndices">Cell indices as a flattened ulong array</param>
/// <param name="cells">Cells as a flattened byte array</param>
/// <param name="numCells">The number of cells provided</param>
/// <param name="ckzgSetup">Trusted setup settings</param>
/// <exception cref="ArgumentException">Thrown when length of an argument is not correct or settings are not correct</exception>
/// <exception cref="ApplicationException">Thrown when the library returns unexpected Error code</exception>
/// <exception cref="InsufficientMemoryException">Thrown when the library has no enough memory to process</exception>
public static unsafe void RecoverCellsAndKzgProofs(Span<byte> recoveredCells, Span<byte> recoveredProofs,
ReadOnlySpan<ulong> cellIndices, ReadOnlySpan<byte> cells, int numCells, IntPtr ckzgSetup)
{
ThrowOnUninitializedTrustedSetup(ckzgSetup);
ThrowOnInvalidLength(recoveredCells, nameof(recoveredCells), BytesPerCell * CellsPerExtBlob);
ThrowOnInvalidLength(recoveredProofs, nameof(recoveredProofs), BytesPerProof * CellsPerExtBlob);
ThrowOnInvalidLength(cellIndices, nameof(cellIndices), numCells);
ThrowOnInvalidLength(cells, nameof(cells), BytesPerCell * numCells);

fixed (byte* recoveredCellsPtr = recoveredCells, recoveredProofsPtr = recoveredProofs, cellsPtr = cells)
{
fixed(ulong* cellIndicesPtr = cellIndices)
{
KzgResult result = RecoverCellsAndKzgProofs(recoveredCellsPtr, recoveredProofsPtr, cellIndicesPtr,
cellsPtr, (ulong)numCells, ckzgSetup);
ThrowOnError(result);
}
}
}

/// <summary>
/// Given some cells, verify that their proofs are valid.
/// </summary>
/// <param name="commitmentsBytes">The commitments associated with the rows</param>
/// <param name="cellIndices">Cell indices as a flattened ulong array</param>
/// <param name="cells">Cells as a flattened byte array</param>
/// <param name="proofsBytes">Proofs as a flattened byte array</param>
/// <param name="numCells">The number of cells provided</param>
/// <param name="ckzgSetup">Trusted setup settings</param>
/// <exception cref="ArgumentException">Thrown when length of an argument is not correct or settings are not correct</exception>
/// <exception cref="ApplicationException">Thrown when the library returns unexpected Error code</exception>
/// <exception cref="InsufficientMemoryException">Thrown when the library has no enough memory to process</exception>
/// <returns>Verification result</returns>
public static unsafe bool VerifyCellKzgProofBatch(ReadOnlySpan<byte> commitments, ReadOnlySpan<ulong> cellIndices,
ReadOnlySpan<byte> cells, ReadOnlySpan<byte> proofs, int numCells, IntPtr ckzgSetup)
{
ThrowOnUninitializedTrustedSetup(ckzgSetup);
ThrowOnInvalidLength(commitments, nameof(commitments), BytesPerCommitment * numCells);
ThrowOnInvalidLength(cellIndices, nameof(cellIndices), numCells);
ThrowOnInvalidLength(cells, nameof(cells), BytesPerCell * numCells);
ThrowOnInvalidLength(proofs, nameof(proofs), BytesPerProof * numCells);

fixed (byte* commitmentsPtr = commitments, cellsPtr = cells, proofsPtr = proofs)
{
fixed (ulong* cellIndicesPtr = cellIndices)
{
KzgResult kzgResult = VerifyCellKzgProofBatch(out var result, commitmentsPtr,
cellIndicesPtr, cellsPtr, proofsPtr, (ulong)numCells, ckzgSetup);
ThrowOnError(kzgResult);
return result;
}
}
}

#region Argument verification helpers
private static void ThrowOnError(KzgResult result)
{
Expand All @@ -220,7 +318,13 @@ private static void ThrowOnUninitializedTrustedSetup(IntPtr ckzgSetup)
private static void ThrowOnInvalidLength(ReadOnlySpan<byte> data, string fieldName, int expectedLength)
{
if (data.Length != expectedLength)
throw new ArgumentException("Invalid data size", fieldName);
throw new ArgumentException($"Invalid data size, got {data.Length}, expected {expectedLength}", fieldName);
}

private static void ThrowOnInvalidLength(ReadOnlySpan<ulong> data, string fieldName, int expectedLength)
{
if (data.Length != expectedLength)
throw new ArgumentException($"Invalid data size, got {data.Length}, expected {expectedLength}", fieldName);
}
#endregion
}
Loading
Loading