]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Add Scalar and SharedSecret type mappings for new secp256k1 types
authorMatt Corallo <git@bluematt.me>
Fri, 23 Sep 2022 20:47:17 +0000 (20:47 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 25 Sep 2022 13:54:12 +0000 (13:54 +0000)
c-bindings-gen/src/types.rs
lightning-c-bindings/src/c_types/mod.rs

index 5fd874d3308ce04e362729d97dd13729115459e4..6089d6467626e2ef767a6f88117d5a291349b446 100644 (file)
@@ -936,6 +936,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature"),
                        "bitcoin::secp256k1::SecretKey" if is_ref  => Some("*const [u8; 32]"),
                        "bitcoin::secp256k1::SecretKey" if !is_ref => Some("crate::c_types::SecretKey"),
+                       "bitcoin::secp256k1::Scalar" if is_ref  => Some("*const crate::c_types::BigEndianScalar"),
+                       "bitcoin::secp256k1::Scalar" if !is_ref => Some("crate::c_types::BigEndianScalar"),
+                       "bitcoin::secp256k1::ecdh::SharedSecret" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
+
                        "bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some("crate::c_types::derived::CVec_u8Z"),
                        "bitcoin::blockdata::transaction::OutPoint" => Some("crate::lightning::chain::transaction::OutPoint"),
@@ -1021,6 +1025,9 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(""),
                        "bitcoin::secp256k1::SecretKey" if is_ref => Some("&::bitcoin::secp256k1::SecretKey::from_slice(&unsafe { *"),
                        "bitcoin::secp256k1::SecretKey" if !is_ref => Some(""),
+                       "bitcoin::secp256k1::Scalar" if !is_ref => Some(""),
+                       "bitcoin::secp256k1::ecdh::SharedSecret" if !is_ref => Some("::bitcoin::secp256k1::ecdh::SharedSecret::from_bytes("),
+
                        "bitcoin::blockdata::script::Script" if is_ref => Some("&::bitcoin::blockdata::script::Script::from(Vec::from("),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some("::bitcoin::blockdata::script::Script::from("),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("&"),
@@ -1104,6 +1111,9 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(".into_rust()"),
                        "bitcoin::secp256k1::SecretKey" if !is_ref => Some(".into_rust()"),
                        "bitcoin::secp256k1::SecretKey" if is_ref => Some("}[..]).unwrap()"),
+                       "bitcoin::secp256k1::Scalar" if !is_ref => Some(".into_rust()"),
+                       "bitcoin::secp256k1::ecdh::SharedSecret" if !is_ref => Some(".data)"),
+
                        "bitcoin::blockdata::script::Script" if is_ref => Some(".to_slice()))"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_rust())"),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(".into_bitcoin()"),
@@ -1196,6 +1206,9 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature::from_rust(&"),
                        "bitcoin::secp256k1::SecretKey" if is_ref => Some(""),
                        "bitcoin::secp256k1::SecretKey" if !is_ref => Some("crate::c_types::SecretKey::from_rust("),
+                       "bitcoin::secp256k1::Scalar" if !is_ref => Some("crate::c_types::BigEndianScalar::from_rust("),
+                       "bitcoin::secp256k1::ecdh::SharedSecret" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
+
                        "bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice::from_slice(&"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(""),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("crate::c_types::Transaction::from_bitcoin("),
@@ -1273,6 +1286,9 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(")"),
                        "bitcoin::secp256k1::SecretKey" if !is_ref => Some(")"),
                        "bitcoin::secp256k1::SecretKey" if is_ref => Some(".as_ref()"),
+                       "bitcoin::secp256k1::Scalar" if !is_ref => Some(")"),
+                       "bitcoin::secp256k1::ecdh::SharedSecret" if !is_ref => Some(".secret_bytes() }"),
+
                        "bitcoin::blockdata::script::Script" if is_ref => Some("[..])"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_bytes().into()"),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(")"),
index 3b574073fcdd23fd100e1dcf05bf7e120159098d..0c46581f20266e5abe23dfab1e1d151de0d2b40d 100644 (file)
@@ -11,6 +11,7 @@ use bitcoin::secp256k1::ecdsa::Signature as SecpSignature;
 use bitcoin::secp256k1::Error as SecpError;
 use bitcoin::secp256k1::ecdsa::RecoveryId;
 use bitcoin::secp256k1::ecdsa::RecoverableSignature as SecpRecoverableSignature;
+use bitcoin::secp256k1::Scalar as SecpScalar;
 use bitcoin::bech32;
 use bitcoin::util::address;
 
@@ -149,6 +150,22 @@ impl RecoverableSignature {
        }
 }
 
+#[repr(C)]
+#[derive(Clone)]
+/// Represents a scalar value between zero and the secp256k1 curve order, in big endian.
+pub struct BigEndianScalar {
+       /// The bytes of the scalar value.
+       pub big_endian_bytes: [u8; 32],
+}
+impl BigEndianScalar {
+       pub(crate) fn from_rust(scalar: &SecpScalar) -> Self {
+               Self { big_endian_bytes: scalar.to_be_bytes() }
+       }
+       pub(crate) fn into_rust(&self) -> SecpScalar {
+               SecpScalar::from_be_bytes(self.big_endian_bytes).expect("Scalar greater than the curve order")
+       }
+}
+
 #[repr(C)]
 #[derive(Copy, Clone)]
 /// Represents an error returned from libsecp256k1 during validation of some secp256k1 data