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

Remove dependency on curv in multisig #1819

Merged
merged 6 commits into from
Jun 27, 2022
Merged

Remove dependency on curv in multisig #1819

merged 6 commits into from
Jun 27, 2022

Conversation

msgmaxim
Copy link
Contributor

@msgmaxim msgmaxim commented Jun 21, 2022

closes @1648

Instead of depending on the wrapper from kzen-curv, we now use primitives from secp256k1 directly. See #1648 for more context. Apart from reducing multisig's memory footprint (I measured a 25% reduction), it is nice to remove this somewhat obscure dependency and gain more control over the implementation (and it better matches what we did for curve25519).

Some other improvements:

  • Scalar::from_bytes is now Scalar::from_bytes_mod_order, which better conveys its expected behaviour. curve25519's implementation is the same, but secp256k1's was wrong as it didn't do modulo reduction (it would result in a crash with a probability of ~1/(2^128), so negligible). Now fixed.

  • From<u32> for Scalar used to create a temporary bigint and doing reduction modulo curve order, which was wasteful as u32 is always smaller and can be trivially converted to Scalar directly, which is the change I made here.

Note to reviewers: secp255k1.rs (the old file, had a typo) and secp256k1.rs (the new file) are different enough that it makes more sense to review the new file "from scratch" (of course referencing the old file can occasionally be helpful).

// Wrapping in `Option` to make it easier to keep track
// of "zero" scalars which often need special treatment
#[derive(Clone, Debug, PartialEq)]
pub struct Scalar(Option<SK>);
Copy link
Contributor

@AlastairHolmes AlastairHolmes Jun 23, 2022

Choose a reason for hiding this comment

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

curv's impl wraps this Option in a Zeroize, wouldn't we like to do the same?

Copy link
Contributor

Choose a reason for hiding this comment

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

Without this we will not zeroize on drop

Copy link
Contributor Author

@msgmaxim msgmaxim Jun 24, 2022

Choose a reason for hiding this comment

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

Actually, we do zeroize on drop. See the drop implementation in derive_scalar_impls. Adding another level of inderection creates problems like requiring SK to be ZeroizeOnDrop (which is probably why curv had to use a newtype rather than an alias for SK, as it doesn't implement it natively). Considering how easy it is to implement ourselves, I don't think it is worth it.

Comment on lines 196 to 202
let x_bytes = x.to_bytes_be();
let mut array = [0u8; SECRET_KEY_SIZE];
array[SECRET_KEY_SIZE - x_bytes.len()..].copy_from_slice(&x_bytes);

// Safe because `x` is within the group
// and `array` is correct size by construction
Scalar(Some(SK::from_slice(&array).unwrap()))
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 it would be better to factor this part out into a function and inline the rest of this function (As in the invert function the BigUint you pass to this function cannot be zero (As I understand things))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you suggesting changing from_reduced_bigint to from_reduced_non_zero_bigint? I agree that the inverse shouldn't produce zero, but I don't think it is a big deal if we check anyway and have a slightly nicer name.

@msgmaxim msgmaxim merged commit 6ae54a1 into develop Jun 27, 2022
@msgmaxim msgmaxim deleted the chore/remove-curv branch June 27, 2022 00:46
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.

3 participants