Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

EC math functions #4696

Merged
merged 2 commits into from
Mar 2, 2017
Merged

EC math functions #4696

merged 2 commits into from
Mar 2, 2017

Conversation

svyatonik
Copy link
Collaborator

Will need this for secret store.
There are no tests as these will be very artificial. Like - I do not know the answer, but my functions shows result A, so let it be A. I could add such tests, if you think these would be helpful.

@svyatonik
Copy link
Collaborator Author

svyatonik commented Feb 27, 2017

P.S.: at least these work fine in secretstore (PR is coming)

@rphmeier rphmeier added A0-pleasereview 🤓 Pull request needs code review. M4-core ⛓ Core client code / Rust. labels Feb 27, 2017
use secp256k1::key;

/// Inplace multiply public key by secret key (EC point * scalar)
pub fn public_mul_secret(public: &mut Public, secret: &Secret) -> Result<(), Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

why not to make this methods on corresponding structs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I considered this, but these are very specific (i.e. won't be used by most of users) so I decided that it is better to place math functions in separate module (to avoid garbage in IntelliSense && etc). I could move, if you think it is better

Copy link
Contributor

Choose a reason for hiding this comment

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

i already use them, see extended.rs

Copy link
Contributor

Choose a reason for hiding this comment

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

rather than as methods, it should use std::ops::{Add, Mul} and maybe their _Assign variants. I assume the failure condition is just that the context wasn't created with the correct capabilities. Since our context is global and always created with full capabilities, it should be safe to assert that no errors occur.

Copy link
Contributor

@rphmeier rphmeier Feb 27, 2017

Choose a reason for hiding this comment

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

Failure conditions like invalid scalar/curve point also shouldn't be possible; if they are then it's a bug in the type allowing us to create invalid secrets.

Copy link
Contributor

Choose a reason for hiding this comment

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

in that case, then it's probably best not to use ops, just methods on the type which can error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@NikVolf @rphmeier I have just remembered that Public is alias for H512. I think it is not ok to add EC-specific math to hash type. What do you guys think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

still can move some of functions to Secret:: methods, though

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah
and as far as i remember @debris wanted to wrap H512 to Public the same as H256 wrapped to Secret, so maybe leave it until then

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved appropriate functions to Secret methods


/// Compute power of secret key inplace (secret ^ pow).
/// This function is not intended to be used with large powers.
pub fn secret_pow(secret: &mut Secret, pow: usize) -> Result<(), Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

the code of this function could be improved with match:

match pow {
    0 => {}
    1 => {}
    pow => {}
}
Ok(())

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

}

/// Return base point of secp256k1
pub fn generation_point() -> Public {
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't it the same as taking GENERATOR_X, GENERATOR_Y from here using less cpu cycles?

https://github.com/ethcore/rust-secp256k1/blob/master/src/constants.rs#L53

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cool, but idk how to combine these :) could you point the proper function?

Copy link
Contributor

Choose a reason for hiding this comment

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

something like this

		let mut public_sec_raw = [0u8; 65];
		public_sec_raw[0] = 4;
		public_sec_raw[1..33].copy_from_slice(&GENERATOR_X);
		public_sec_raw[33..65].copy_from_slice(&GENERATOR_Y);
		let public_sec = PublicKey::from_slice(&SECP256K1, &public_sec_raw).map_err(|_| Error::InvalidPoint)?;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Ok(key::PublicKey::from_slice(&SECP256K1, &public_data)?)
}

fn to_secp256k1_secret(secret: &Secret) -> Result<key::SecretKey, Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

could be a consuming method on Secret struct as well

Copy link
Contributor

@rphmeier rphmeier Feb 27, 2017

Choose a reason for hiding this comment

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

or a From implementation (which may already exist?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cool, will add these :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These are inplace methods => consuming does not fits here.
From must not fail. This could fail.

@@ -28,6 +28,7 @@ mod brain;
mod error;
mod keypair;
mod keccak;
pub mod math;
Copy link
Contributor

Choose a reason for hiding this comment

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

style: public modules should be separated from private.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@NikVolf
Copy link
Contributor

NikVolf commented Feb 28, 2017

Any tests against well-known vectors?

@NikVolf NikVolf added A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. and removed A0-pleasereview 🤓 Pull request needs code review. labels Feb 28, 2017
@svyatonik
Copy link
Collaborator Author

svyatonik commented Feb 28, 2017

@NikVolf see first comment. If you have any 'well-known' vector, please share - I'll add

@svyatonik svyatonik added A0-pleasereview 🤓 Pull request needs code review. and removed A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. labels Feb 28, 2017
@NikVolf
Copy link
Contributor

NikVolf commented Feb 28, 2017

Fair enough, we can hardcode some samples later in case to be confident with later changes

@NikVolf NikVolf added A8-looksgood 🦄 Pull request is reviewed well. and removed A0-pleasereview 🤓 Pull request needs code review. labels Feb 28, 2017
@gavofyork gavofyork merged commit cb16882 into master Mar 2, 2017
@gavofyork gavofyork deleted the secretstore_ethkey_math branch March 2, 2017 11:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A8-looksgood 🦄 Pull request is reviewed well. M4-core ⛓ Core client code / Rust.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants