1

我写了一些有效的代码:

use rand::Rng;
use secp256k1::{Secp256k1, SecretKey, PublicKey};

fn main() {
    let secp = Secp256k1::new();

    let seed_a = rand::thread_rng().gen::<[u8; 32]>();
    let mut skey_a = SecretKey::from_slice(&seed_a).expect("32 bytes, within curve order");
    let pkey_a = PublicKey::from_secret_key(&secp, &skey_a);

    println!("skey_a {:?}", skey_a);
    println!("pkey_a {:?}", pkey_a);

    let seed_b = rand::thread_rng().gen::<[u8; 32]>();
    let skey_b = SecretKey::from_slice(&seed_b).expect("32 bytes, within curve order");
    let pkey_b = PublicKey::from_secret_key(&secp, &skey_b);

    println!("skey_b {:?}", skey_b);
    println!("pkey_b {:?}", pkey_b);

    skey_a.add_assign(&seed_b).unwrap(); // there is no plain add, nor an obvious way to get the bytes out of an skey
    let skey_sum = skey_a;
    let pkey_sum_from_skeys = PublicKey::from_secret_key(&secp, &skey_sum);

    println!("skey_sum {:?}", skey_sum);
    println!("pkey_sum_from_skeys {:?}", pkey_sum_from_skeys);

    // calculate the sum of public keys without needing access to secret keys
    let pkey_sum_from_pkeys = pkey_a.combine(&pkey_b).unwrap();

    println!("pkey_sum_from_pkeys {:?}", pkey_sum_from_pkeys);
}

产生以下样本输出:

skey_a SecretKey(ce9b44fedaa9aa82ee394a488df5ac55bdd3bd62c8cae45bebc1c91174fac2c2)
pkey_a PublicKey(c2d220e65b1a612405f7b18aa503132b2f29b9ac993bdcfc65ac3dfec4192856ad5da30b482ccb42c63c809703bb3cfac3644a586c8635f3178462d53e351fdf)
skey_b SecretKey(a8bfd58be7c9e14c27f0e60a00c1320e79698c8747cfa0cca0a2180267efc9ca)
pkey_b PublicKey(de8df52ed48d2c3320c03344a3fe859d61015e5f8d45b0df9aaa8d056c784e7e55a61a53630ee016e0bc8ac21d6ae4cd92e0ef91e74281d9410167b982764a8e)
skey_sum SecretKey(775b1a8ac2738bcf162a30528eb6de657c8e6d036151e4eccc9182870cb44b4b)
pkey_sum_from_skeys PublicKey(f91cf9ee526dab8a955709385c6ae5a7f757cf82278af1d670ab6b33f2f28d8716cbddf253047fa9ff6b152a6a1986213482d9ff6fdfc3883e481d7133d0045e)
pkey_sum_from_pkeys PublicKey(f91cf9ee526dab8a955709385c6ae5a7f757cf82278af1d670ab6b33f2f28d8716cbddf253047fa9ff6b152a6a1986213482d9ff6fdfc3883e481d7133d0045e)

我的问题出在这条线上:

skey_a.add_assign(&seed_b).unwrap();

首先,我必须使用add_assign,因为我找不到普通的非破坏性add.

其次,我无法从中获取字节skey_a,因此我必须使用seed_a从中生成的字节skey_a

这些问题中的任何一个都可以解决吗,或者这是我能用secp256k1板条箱做的最好的事情吗?


尝试记录as_ref()是行不通的。

error[E0599]: no method named `as_ref` found for struct `secp256k1::SecretKey` in the current scope
   --> src/main.rs:127:17
    |
127 |     let b = skey_b.as_ref();
    |                    ^^^^^^ method not found in `secp256k1::SecretKey`
    | 
   ::: /home/chris/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/convert/mod.rs:167:8
    |
167 |     fn as_ref(&self) -> &T;
    |        ------
    |        |
    |        the method is available for `std::boxed::Box<secp256k1::SecretKey>` here
    |        the method is available for `std::sync::Arc<secp256k1::SecretKey>` here
    |        the method is available for `std::rc::Rc<secp256k1::SecretKey>` here

我认为正在进行一些复杂的再出口,我不明白。

4

0 回答 0