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

Implement no_std support #280

Merged
merged 40 commits into from
Sep 28, 2019
Merged

Conversation

xoloki
Copy link
Contributor

@xoloki xoloki commented Jul 22, 2019

This change implements no_std support for the bulletproofs crate. The changes are mostly mechanical. This is the culmination several PRs worth of work, with changes to curve25519 and merlin crates already merged and released.

It was necessary to make minor modifications to some APIs, particularly in regards to RNGs. As discussed by @isislovecruft in #275, the APIs in this change are backwards compatible for those building the crate in std mode. So current users of the bulletproofs crate will see no change. Users wishing to build bulletproofs in no_std mode will need to call the new *_with_rng prove and verfiy functions, and pass in a RNG.

This change is based on the main branch rather than the develop branch. This is because the main branch in the dalek repo has several changes that are not in the develop branch. This made rebasing my change on the upstream develop branch to be problematic.

@oleganza
Copy link
Collaborator

This looks cool. Will check in more detail tomorrow.

@hdevalence
Copy link
Contributor

It looks like one of the CI failures is just because of rustfmt changes that haven't been applied to the codebase yet; I can make a separate change that fixes them so that this branch can be merged cleanly on top of the fixed style.

@arthurgreef
Copy link

Hi - I pulled the branch but I cannot bet a clean build. Can you provide me with you build command? Is is cargo build --no-default-features --features alloc? Thanks.

@xoloki
Copy link
Contributor Author

xoloki commented Jul 24, 2019

Hi - I pulled the branch but I cannot bet a clean build. Can you provide me with you build command? Is is cargo build --no-default-features --features alloc? Thanks.

I pushed a change to the branch that forces the rand dependency to 0.6, which appears to fix this. Your cargo build command will now work.

I was testing the no_std support in an application that used bulletproofs in no_std mode, so it is likely that some other dependency was already requesting 0.6 and cargo's dependency resolution was never actually using a 0.5 rand.

@xoloki
Copy link
Contributor Author

xoloki commented Jul 24, 2019

It looks like one of the CI failures is just because of rustfmt changes that haven't been applied to the codebase yet; I can make a separate change that fixes them so that this branch can be merged cleanly on top of the fixed style.

The badly formatted code was all in places that I changed, so I went ahead and fixed my branch @hdevalence

@xoloki
Copy link
Contributor Author

xoloki commented Jul 24, 2019

All checks are now passing.

@arthurgreef
Copy link

arthurgreef commented Jul 25, 2019

We are using cargo-nono to see if the libraries reference std and are noticing that the dependencies do. I that expected? Thanks.

curve25519-dalek: FAILURE

  • Source code contains an explicit use std:: statement.
    --> src/backend/vector/ifma/edwards.rs
  • Source code contains an explicit use std:: statement.
    --> src/prelude.rs
    clear_on_drop: SUCCESS
    subtle: SUCCESS
    byteorder: SUCCESS
    digest: FAILURE
  • Source code contains an explicit use std:: statement

@hdevalence
Copy link
Contributor

Re: curve25519-dalek failure, it might be because the version with no_std fixes hasn't been released yet, I can check and do a new release if not. There's a security audit soon-to-be-released for these libraries with some minor issues so we might batch no_std fixes into those fixes instead of doing two at once.

I updated the develop branch so there should not be any more divergence between the main and develop branches.

@hdevalence
Copy link
Contributor

Hi all, recent changes to Rust nightlies broke the build (by changing the path root for doc(include) path specifiers), so we had to update the develop branch and release 1.0.4 which works on current nightlies. The current develop branch is now back in sync with the main branch, is ready for more changes to be merged in, and won't have any more drastic changes (except to the R1CS code).

…ve Ristretto and Scalar serialization; convert to and from byte arrays when dealing with serialized data structures
… serializing any curve points or scalars directly
…since RangeProof supports it natively with to/from_bytes()
…s as _with_rng, then creating a thread_rng in the compatibility functions in std mode
…erence to the wrapped functions in the convenience wrappers' block comments
@xoloki
Copy link
Contributor Author

xoloki commented Aug 7, 2019

BTW the CI failures appear to be spurious, it was just failure to connect to github:

fatal: unable to access 'https://github.com/dalek-cryptography/bulletproofs.git/': Failed to connect to github.com port 443: Connection timed out
The command "eval git clone --depth=50 https://github.com/dalek-cryptography/bulletproofs.git dalek-cryptography/bulletproofs " failed. Retrying, 2 of 3.

@xoloki
Copy link
Contributor Author

xoloki commented Aug 12, 2019

Okay @hdevalence I got rid of the alloc feature and just used alloc everywhere. Building without default features just works now.

Let me know if you want me to do anything else.

@xoloki
Copy link
Contributor Author

xoloki commented Aug 12, 2019

All tests passing now @hdevalence

@@ -295,15 +302,19 @@ impl<'a, 'b> DealerAwaitingProofShares<'a, 'b> {
/// performing local aggregation,
/// [`receive_trusted_shares`](DealerAwaitingProofShares::receive_trusted_shares)
/// saves time by skipping verification of the aggregated proof.
pub fn receive_shares(mut self, proof_shares: &[ProofShare]) -> Result<RangeProof, MPCError> {
pub fn receive_shares<T: RngCore + CryptoRng>(
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this needs to be a new receive_shares_with_rng function, because receive_shares is part of the public API and can't change without a major version bump.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll rework these so the API doesn't change in std mode.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks! Sorry again for missing this on the first pass of review 😞

@@ -68,20 +70,18 @@ pub struct PartyAwaitingPosition<'a> {
impl<'a> PartyAwaitingPosition<'a> {
/// Assigns a position in the aggregated proof to this party,
/// allowing the party to commit to the bits of their value.
pub fn assign_position(
pub fn assign_position<T: RngCore + CryptoRng>(
Copy link
Contributor

Choose a reason for hiding this comment

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

similarly here, assign_position is a stable API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do.

@@ -155,12 +155,11 @@ pub struct PartyAwaitingBitChallenge<'a> {
impl<'a> PartyAwaitingBitChallenge<'a> {
/// Receive a [`BitChallenge`] from the dealer and use it to
/// compute commitments to the party's polynomial coefficients.
pub fn apply_challenge(
pub fn apply_challenge<T: RngCore + CryptoRng>(
Copy link
Contributor

Choose a reason for hiding this comment

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

apply_challenge is also stable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll make these changes today.

@hdevalence
Copy link
Contributor

Hey @xoloki, this looks great, I would like to merge it, thanks for all this work!

I was doing a final pass over the combined changes and I spotted something I should have noticed before, but didn't: the functions in the aggregation module that were changed to take RNG parameters are part of the public API of the crate, so they can't be changed without breaking stability. I'm sorry I didn't spot it before and that there's another round of back-and-forth, but I think that that is the only thing left.

@xoloki
Copy link
Contributor Author

xoloki commented Aug 13, 2019

No worries, thanks for all your help getting this in shape.

@xoloki
Copy link
Contributor Author

xoloki commented Aug 14, 2019

Ok @hdevalence I fixed the public API to be backwards compatible in std mode, and renamed the nostd public API elements _with_rng.

Let me know if you need any more changes.

@@ -64,7 +68,7 @@ pub fn exp_iter(x: Scalar) -> ScalarExp {
pub fn add_vec(a: &[Scalar], b: &[Scalar]) -> Vec<Scalar> {
if a.len() != b.len() {
// throw some error
println!("lengths of vectors don't match for vector addition");
//println!("lengths of vectors don't match for vector addition");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe add panic?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can probably clean this up in a different PR

singleparty_create_and_verify_helper(64, 8);
// XXX disable this test since it's failing with
// 'attempt to negate with overflow'
//singleparty_create_and_verify_helper(64, 8);
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems kind of worrying (in the sense that it may point at a problem in the rest of the source code) -- is it reproducible? I tried to reproduce on my machine but I wasn't able to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Weird, I thought I reverted this. It was failing locally, but not remotely in CI.

Reverted now.

@hdevalence
Copy link
Contributor

I have a cherry-pick'able commit on a copy of this branch (hdevalence@4191781 ) that undoes the changes to the test code, so that the test code continues to use the previous aggregation API. This way we test that the previous API still works correctly.

@xoloki
Copy link
Contributor Author

xoloki commented Aug 14, 2019

I have a cherry-pick'able commit on a copy of this branch (hdevalence@4191781 ) that undoes the changes to the test code, so that the test code continues to use the previous aggregation API. This way we test that the previous API still works correctly.

The cherry has been picked, thanks!

@xoloki
Copy link
Contributor Author

xoloki commented Sep 12, 2019

Hi @hdevalence, is there anything else you need from me? It would be super helpful if we can get this merged by the end of the month, our auditors would be much happier reviewing an upstream release rather than a fork and a PR.

@hdevalence
Copy link
Contributor

Hey @xoloki -- I was on vacation so I missed this; I'll try to get this merged before the end of the week!

@xoloki
Copy link
Contributor Author

xoloki commented Sep 23, 2019

Excellent, thanks @hdevalence!

@hdevalence
Copy link
Contributor

Planning to get this merged this afternoon!

Copy link
Contributor

@hdevalence hdevalence left a comment

Choose a reason for hiding this comment

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

LGTM!

@hdevalence hdevalence merged commit 426c87a into dalek-cryptography:develop Sep 28, 2019
@hdevalence
Copy link
Contributor

Hey @xoloki, I think I'm out of time to do a release for this today but I will do it at the beginning of next week. Sorry that this took so long and thanks so much for your work on this :)

@xoloki
Copy link
Contributor Author

xoloki commented Sep 28, 2019

Thanks for the merge! Next week is totally fine for a release.

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.

5 participants