Coder Social home page Coder Social logo

Comments (10)

namuyan avatar namuyan commented on August 17, 2024

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.

omershlo avatar omershlo commented on August 17, 2024

Hi, @namuyan. A few comments:

  1. you have a serialize and deserialize implemented for GE. please try to use this (in your example you use the serialize of PublicKey which is the secp256k1 lib serialize).
  2. 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)
  3. 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.
  4. 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.

namuyan avatar namuyan commented on August 17, 2024

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.

omershlo avatar omershlo commented on August 17, 2024

If you use it for ecdsa than my option 2 should work for you with the compressed format:

  1. to serielize - call bytes_compressed_to_big_int to get a BigInt from your point (you can transform bigint to bytes and bytes to bigint easily)
  2. 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.

namuyan avatar namuyan commented on August 17, 2024

Thank you for reply.
Prefix of compressed public key is important, I cannot recover same Y point (odd or even?

from curv.

omershlo avatar omershlo commented on August 17, 2024

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.

namuyan avatar namuyan commented on August 17, 2024

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.

omershlo avatar omershlo commented on August 17, 2024

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.

namuyan avatar namuyan commented on August 17, 2024

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.

omershlo avatar omershlo commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.