Comments (10)
I fixed /src/elliptic/curves/secp256_k1.rs#L317
public key length is only 33 or 65.
How about this? Secp256k1 require prefix 2 or 3 to decide odd or even.
fn from_bytes(bytes: &[u8]) -> Result<Secp256k1Point, ErrorKey> {
if bytes.len() < 1 {
return Err(ErrorKey::InvalidPublicKey);
}
let prefix = bytes[0];
let byte_len = bytes.len();
if byte_len == 33 && (prefix == 2 || prefix == 3) {
let result = PK::from_slice(bytes[..33]);
let test = result.map(|pk| Secp256k1Point {
purpose: "random",
ge: pk,
});
test.map_err(|_err| ErrorKey::InvalidPublicKey)
} else if byte_len == 65 && prefix == 4 {
let result = PK::from_slice(bytes[..65]);
let test = result.map(|pk| Secp256k1Point {
purpose: "random",
ge: pk,
});
test.map_err(|_err| ErrorKey::InvalidPublicKey)
} else {
Err(ErrorKey::InvalidPublicKey)
}
}
from curv.
Hi, @namuyan. A few comments:
- you have a serialize and deserialize implemented for
GE
. please try to use this (in your example you use the serialize ofPublicKey
which is the secp256k1 lib serialize). - There's one more way, see this comment : https://github.com/KZen-networks/curv/blob/master/src/elliptic/curves/secp256_k1.rs#L294. It will work but I do not encourage using it (or at least let me know what computation you want to do to make sure its fine)
from_bytes
can take any random number of bytes, and make a point out of it, not used as deserilizer. For other elliptic curves there are algorithms to generate random uniformly distributed point from random bytes. in secp256k1 this is the best we get.- having said that, I tend to be inclined to move to something like you suggested. Will have to think about it more.
Please let me know if (1) or (2) works for your application
from curv.
Thank you for comment!
I just want simple and short bytes formatted public key, like Bitcoin use.
pub fn public_from_bytes(bytes: &[u8]) -> Result<Secp256k1Point, ErrorKey> {
if bytes.len() < 32 {
return Err(ErrorKey::InvalidPublicKey);
}
let prefix = bytes[0];
let byte_len = bytes.len();
let public =
if byte_len == 33 && (prefix == 2 || prefix == 3) {
PK::from_slice(bytes).map_err(|_err| ErrorKey::InvalidPublicKey)
} else if byte_len == 65 && prefix == 4 {
PK::from_slice(bytes).map_err(|_err| ErrorKey::InvalidPublicKey)
} else {
Err(ErrorKey::InvalidPublicKey)
}?;
Secp256k1Point::from_bytes(&public.serialize_uncompressed()[1..])
}
from curv.
If you use it for ecdsa than my option 2 should work for you with the compressed format:
- to serielize - call
bytes_compressed_to_big_int
to get aBigInt
from your point (you can transform bigint to bytes and bytes to bigint easily) - to deserialize -
/// 1) convert BigInt::to_vec
/// 2) remove first byte [1..33]
/// 3) call from_bytes
let me know if you have any problem
from curv.
Thank you for reply.
Prefix of compressed public key is important, I cannot recover same Y point (odd or even?
from curv.
That's true in the general case, but in the case of ECDSA it doesn't matter. you can verify ecdsa even with public key and R with wrong parity.
from curv.
I try to use pubic key without prefix, but often verification failed.
Next, I checked your aggregation signature test code by point2point
.
use curv::{BigInt,GE};
use curv::elliptic::curves::traits::ECPoint;
use curv::arithmetic::traits::Converter;
fn point2point(p: &GE) -> GE {
let tmp = p.bytes_compressed_to_big_int();
let tmp = BigInt::to_vec(&tmp);
GE::from_bytes(&tmp[1..]).unwrap()
}
point2point wrapper covert public key with prefix 2 or 3 to prefix 2.
#[cfg(test)]
mod tests {
use super::point2point;
use curv::BigInt;
use curv::GE;
use protocols::aggsig::{verify, verify_partial, EphemeralKey, KeyAgg, KeyPair};
extern crate hex;
use curv::elliptic::curves::traits::*;
#[test]
fn test_multiparty_signing_for_two_parties() {
let is_musig = true;
let message: [u8; 4] = [79, 77, 69, 82];
// round 0: generate signing keys
let party1_key = KeyPair::create();
let party2_key = KeyPair::create();
// round 1: send commitments to ephemeral public keys
let party1_ephemeral_key = EphemeralKey::create();
let party2_ephemeral_key = EphemeralKey::create();
let party1_commitment = &party1_ephemeral_key.commitment;
let party2_commitment = &party2_ephemeral_key.commitment;
// round 2: send ephemeral public keys and check commitments
// p1 release R1' and p2 test com(R1') = com(R1):
assert!(EphemeralKey::test_com(
&point2point(&party2_ephemeral_key.keypair.public_key),
&party2_ephemeral_key.blind_factor,
party2_commitment
));
// p2 release R2' and p1 test com(R2') = com(R2):
assert!(EphemeralKey::test_com(
&point2point(&party1_ephemeral_key.keypair.public_key),
&party1_ephemeral_key.blind_factor,
party1_commitment
));
// compute apk:
let mut pks: Vec<GE> = Vec::new();
pks.push(party1_key.public_key.clone());
pks.push(party2_key.public_key.clone());
let party1_key_agg = KeyAgg::key_aggregation_n(&pks, 0);
let party2_key_agg = KeyAgg::key_aggregation_n(&pks, 1);
assert_eq!(party1_key_agg.apk, party2_key_agg.apk);
// compute R' = R1+R2:
let party1_r_tag = EphemeralKey::add_ephemeral_pub_keys(
&point2point(&party1_ephemeral_key.keypair.public_key),
&point2point(&party2_ephemeral_key.keypair.public_key),
);
let party2_r_tag = EphemeralKey::add_ephemeral_pub_keys(
&point2point(&party1_ephemeral_key.keypair.public_key),
&point2point(&party2_ephemeral_key.keypair.public_key),
);
assert_eq!(party1_r_tag, party2_r_tag);
// compute c = H0(Rtag || apk || message)
let party1_h_0 =
EphemeralKey::hash_0(&party1_r_tag, &party1_key_agg.apk, &message, is_musig);
let party2_h_0 =
EphemeralKey::hash_0(&party2_r_tag, &party2_key_agg.apk, &message, is_musig);
assert_eq!(party1_h_0, party2_h_0);
// compute partial signature s_i and send to the other party:
let s1 = EphemeralKey::sign(
&party1_ephemeral_key,
&party1_h_0,
&party1_key,
&party1_key_agg.hash,
);
let s2 = EphemeralKey::sign(
&party2_ephemeral_key,
&party2_h_0,
&party2_key,
&party2_key_agg.hash,
);
let r = party1_ephemeral_key.keypair.public_key.x_coor().unwrap();
assert!(verify_partial(
&ECScalar::from(&s1),
&r,
&ECScalar::from(&party1_h_0),
&ECScalar::from(&party1_key_agg.hash),
&party1_key.public_key
)
.is_ok());
// signature s:
let (r, s) = EphemeralKey::add_signature_parts(s1, &s2, &party1_r_tag);
// verify:
assert!(verify(&s, &r, &point2point(&party1_key_agg.apk), &message, is_musig,).is_ok())
}
}
Is this code use prefix on process? or is something wrong with my idea?
from curv.
Hi @namuyan, if I understand correctly, you are trying point2point
on Schnorr signature. Therefore it make sense that you will fail sometimes (with wrong public key). I wrote before that only if you use ECDSA it is safe to use point2point
. In your case I suggest to use the serialize
and deserialize
methods (option 1) that are using non compressed points, or to add a serialize/deserialize option for compress - if you do, I think it might be a good addition to the library.
from curv.
Thank you for advice.
I understand that Schnorr signature require correct Y point. I decide to use compressed public key with prefix. Addition to it, I will marge key meta info (is_musig, etc). code
from curv.
I agree that Schnorr requires correct Y coordinate.
Thank you for the discussion.
feel free to open a PR with your changes
from curv.
Related Issues (20)
- Update `hmac` dependency HOT 1
- Suboptimal serialization of certain structs HOT 4
- Feature request: serialization in v0.7 HOT 1
- suggested additions to Polynomial HOT 2
- Zero Polynomial degree is returning 0
- Implement Zeroize for `Point<E>`
- ed25519::GE fails in serialization and deserialization HOT 10
- Serialization doesn't work with bincode
- Build error: could not find `traits` in `super` HOT 4
- Update crate to latest revision of source code. HOT 1
- generate pk should know the curve
- Bump p256 to 0.11.1 HOT 3
- Incorrect documentation for `check_point_order_equals_group_order` HOT 2
- Updating `curv` dependency in `kzen-paillier`
- Choices between G1Point and Point<Bls12_381_1> in v0.10.0
- Add support for RustCrypto's crypto-bigint HOT 3
- Why use a point as generator instead of a number in feldman vss?
- Deserialization fails for `Secp256k1Scalar`
- Is pairing still considered experimental?
- Build failing on rust 1.73.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from curv.