-
Notifications
You must be signed in to change notification settings - Fork 72
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
Serializing SecretKey to bytes #104
Comments
A eip-2333 specifies big endian blst uses big endian threshold_crypto uses bincode defaults to little endian (see Options docs) but it seems most common for bls12-381 implementations to specify big endian. Some code examples to illustrate where this may cause grief: threshold_crypto SecretKey.reveal(): println!("sk.reveal: {:?}", sk.reveal());
// sk.reveal: "SecretKey(Fr(0x408157791befddd702672dcfcfc99da3512f9c0ea818890fcb6ab749580ef2cf")) threshold_crypto using default bincode: let skb = bincode::serialize(&SerdeSecret(&sk)).unwrap();
println!("threshold_crypto bincode: {:?}", hex::encode(skb));
// threshold_crypto bincode: "cff20e5849b76acb0f8918a80e9c2f51a39dc9cfcf2d6702d7ddef1b79578140"
// this is little endian, reversing the bytes gives same as sk.reveal() threshold_crypto using big_endian bincode v1.2.1 (bincode config changes in later versions): let skb = bincode::config().big_endian().serialize(&SerdeSecret(&sk)).unwrap();
println!("threshold_crypto bincode big_endian: {:?}", hex::encode(skb));
// threshold_crypto bincode big_endian: "cb6ab749580ef2cf512f9c0ea818890f02672dcfcfc99da3408157791befddd7"
// it becomes clearer what is going on when splitting into 4x u64 as per the underlying data type
// cb6ab749580ef2cf 512f9c0ea818890f 02672dcfcfc99da3 408157791befddd7 blst (which is explicitly big endian via blst_bendian_from_scalar): println!("blst: {:?}", hex::encode(blst_sk.to_bytes());
// blst: "408157791befddd702672dcfcfc99da3512f9c0ea818890fcb6ab749580ef2cf" It seems like a edit: code to generate above values can be found at https://github.com/iancoleman/bls_interop |
+1. I just came here looking for a to_bytes() method myself. It is asymmetric that PublicKey has a to_bytes() method, but there is no equivalent for SecretKey/Share. |
Converting a SecretKey to json gives
[u64; 4]
Javascript will happily but incorrectly parse this (javascript uses f64 for all numbers):
Being able to convert to bytes would be handy in these sorts of cases, maybe
to_bytes
andfrom_bytes
method?Here's a SecretKey method that works for converting to bytes:
But I'm not sure if this logic is best to be in the SecretKey impl or if it's something that should be added to serde_impl.rs, since it depends on how much the library wants to insulate the secret key and how it would achieve that.
The text was updated successfully, but these errors were encountered: