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: Integrate PeerDASKZG library #6396

Open
wants to merge 21 commits into
base: kzgpeerdas
Choose a base branch
from

Conversation

kevaundray
Copy link
Contributor

Currently in DRAFT as theres quite a bit of experimentation happening.

This is a PR to integrate the rust peerdas-kzg library into nimbus. The branch being used to make quick changes is here: crate-crypto/rust-eth-kzg#44

@@ -0,0 +1,57 @@
#!/bin/bash
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the rust library, we call ./scripts/compile.sh and it will compile the rust code for the host platform and copy the static libs into the nim directory. From thereon, you can call the nim code as if it were regular nim code. This script:

  • clones the rust code
  • calls ./scripts/compile.sh
  • Copies the nim directory to the destination folder
  • deletes the rust code since we no longer need it

@@ -12,6 +12,7 @@ import
std/json,
yaml,
kzg4844/kzg_ex,
../../../vendor/nimpeerdaskzg/nim_peerdas_kzg/nim_peerdas_kzg,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A hack right now, to reference the peerdas-kzg lib

@@ -60,12 +64,13 @@ proc runBlobToKzgCommitmentTest(suiteName, suitePath, path: string) =
if blob.isNone:
check output.kind == JNull
else:
let commitment = blobToKzgCommitment(blob.get)
var blobObject = Blob(bytes: blob.get)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not happy with needing this; will modify the API so that this can take types like array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that the c-kzg library also needed this: #6403

Not sure why it errors on the rust, pre gcc-14 but not on the c-kzg lib pre gcc-14

check:
if commitment.isErr:
output.kind == JNull
else:
commitment.get == fromHex[48](output.getStr).get
commitment.get.bytes == fromHex[48](output.getStr).get
Copy link
Contributor Author

@kevaundray kevaundray Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -60,12 +64,13 @@ proc runBlobToKzgCommitmentTest(suiteName, suitePath, path: string) =
if blob.isNone:
check output.kind == JNull
else:
let commitment = blobToKzgCommitment(blob.get)
var blobObject = Blob(bytes: blob.get)
let commitment = ctx.blobToKzgCommitment(blobObject)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as a reference, if this branch is to be merged into stable, we probably do not want to modify this method since its used in 4844

block:
ctx = newKZGCtx()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be modified in the future to allow one to say how many threads the library should use

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, Nimbus except for a couple specific cryptography libraries doesn't use threads -- would these threads be wholly contained within the KZG library?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep they would be wholly contained in the cryptography library, you can set the number to 1 to use a single thread -- I can also look into making it a compilation flag in the future

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine, then -- we also have

numThreads* {.
defaultValue: 0,
desc: "Number of worker threads (\"0\" = use as many threads as there are CPU cores available)"
name: "num-threads" .}: int

already to configure the number of threads to be modified by the end-user, so could potentially hook into that.

It's already used for BLS signature verification threads, though, so one would have to consider how to keep it from becoming a misleading or double-counted setting

@kevaundray
Copy link
Contributor Author

After running the build script, kzg tests are currently being ran with ./env.sh nim c -r tests/consensus_spec/test_fixture_kzg.nim

@kevaundray
Copy link
Contributor Author

Most of the skeleton is now here, setting to ready to get feedback

@kevaundray kevaundray marked this pull request as ready for review July 2, 2024 14:03
Copy link

github-actions bot commented Jul 2, 2024

Unit Test Results

         9 files    1 337 suites   25m 57s ⏱️
  5 111 tests   4 763 ✔️ 348 💤 0
21 456 runs  21 052 ✔️ 404 💤 0

Results for commit 1a3ea46.

♻️ This comment has been updated with latest results.

beacon_chain/kzg.nim Outdated Show resolved Hide resolved
beacon_chain/kzg.nim Outdated Show resolved Hide resolved
build_peerdas_lib.sh Outdated Show resolved Hide resolved
.github/workflows/ci.yml Outdated Show resolved Hide resolved
build_peerdas_lib.sh Outdated Show resolved Hide resolved
beacon_chain/kzg.nim Outdated Show resolved Hide resolved
build_peerdas_lib.sh Outdated Show resolved Hide resolved
@kevaundray
Copy link
Contributor Author

kevaundray commented Jul 3, 2024

Updated the script to not build the java bindings package -- this requires javac to generate a java header file

For windows, it seems to be having trouble picking up the linker -- will try to reproduce this environment

@kevaundray
Copy link
Contributor Author

Currently going to work on a different strategy to allow an easier way to integrate and release the library

@kevaundray
Copy link
Contributor Author

@tersec can you re-run CI please?

@tersec
Copy link
Contributor

tersec commented Jul 15, 2024

@tersec can you re-run CI please?

done

@kevaundray
Copy link
Contributor Author

The error on linux seems to stem from the fact that linux has a different stack limit than the others -- The place where it is happening is not somewhere this PR has modified, so unclear why it did not pop up in the parent branch.

The reason why it is happening, is possibly due to ComputeCellsAndProofs method computing 819232 + 12848 = 268KB onto the stack for each call. If this is the case, I don't think this should be fixed in this PR as its already present on the parent branch and not related to integration

kevaundray and others added 2 commits July 16, 2024 14:26
Co-authored-by: Agnish Ghosh <[email protected]>
Co-authored-by: Agnish Ghosh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants