Skip to content

Commit

Permalink
Add ecdsa adaptor support in sys
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Apr 8, 2021
1 parent 7637c2e commit 37a831d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions secp256k1-zkp-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn main() {
.define("ENABLE_MODULE_SURJECTIONPROOF", Some("1"))
.define("ENABLE_MODULE_GENERATOR", Some("1"))
.define("ENABLE_MODULE_RANGEPROOF", Some("1"))
.define("ENABLE_MODULE_ECDSA_ADAPTOR", Some("1"))
.define("ECMULT_GEN_PREC_BITS", Some("4"))
// TODO these three should be changed to use libgmp, at least until secp PR 290 is merged
.define("USE_NUM_NONE", Some("1"))
Expand Down
93 changes: 92 additions & 1 deletion secp256k1-zkp-sys/src/zkp.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use core::{fmt, hash};
use {types::*, Context, PublicKey};
use {types::*, Context, PublicKey, Signature};

/// Rangeproof maximum length
pub const RANGEPROOF_MAX_LENGTH: size_t = 5134;
pub const ECDSA_ADAPTOR_SIGNATURE_LENGTH: size_t = 162;

extern "C" {
#[cfg_attr(
Expand Down Expand Up @@ -278,6 +279,61 @@ extern "C" {
output: *mut PublicKey,
bytes: *const c_uchar,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_2_0_nonce_function_ecdsa_adaptor"
)]
pub static secp256k1_nonce_function_ecdsa_adaptor: EcdsaAdaptorNonceFn;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_2_0_ecdsa_adaptor_encrypt"
)]
pub fn secp256k1_ecdsa_adaptor_encrypt(
cx: *const Context,
adaptor_sig162: *mut EcdsaAdaptorSignature,
seckey32: *const c_uchar,
enckey: *const PublicKey,
msg32: *const c_uchar,
noncefp: EcdsaAdaptorNonceFn,
ndata: *mut c_void,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_2_0_ecdsa_adaptor_verify"
)]
pub fn secp256k1_ecdsa_adaptor_verify(
cx: *const Context,
adaptor_sig162: *const EcdsaAdaptorSignature,
pubkey: *const PublicKey,
msg32: *const c_uchar,
enckey: *const PublicKey,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_2_0_ecdsa_adaptor_decrypt"
)]
pub fn secp256k1_ecdsa_adaptor_decrypt(
cx: *const Context,
sig: *mut Signature,
deckey32: *const c_uchar,
adaptor_sig162: *const EcdsaAdaptorSignature,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_2_0_ecdsa_adaptor_recover"
)]
pub fn secp256k1_ecdsa_adaptor_recover(
cx: *const Context,
deckey32: *mut c_uchar,
sig: *const Signature,
adaptor_sig162: *const EcdsaAdaptorSignature,
enckey: *const PublicKey,
) -> c_int;
}

#[repr(C)]
Expand Down Expand Up @@ -407,3 +463,38 @@ impl hash::Hash for PedersenCommitment {
state.write(&self.0)
}
}

/// Same as secp256k1_nonce_function_hardened with the exception of using the
/// compressed 33-byte encoding for the pubkey argument.
pub type EcdsaAdaptorNonceFn = Option<
unsafe extern "C" fn(
nonce32: *mut c_uchar,
msg32: *const c_uchar,
key32: *const c_uchar,
pk33: *const c_uchar,
algo: *const c_uchar,
algo_len: size_t,
data: *mut c_void,
) -> c_int,
>;

#[repr(C)]
pub struct EcdsaAdaptorSignature([u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH]);
impl_array_newtype!(EcdsaAdaptorSignature, u8, ECDSA_ADAPTOR_SIGNATURE_LENGTH);
impl_raw_debug!(EcdsaAdaptorSignature);

impl From<[u8; 162]> for EcdsaAdaptorSignature {
fn from(bytes: [u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH]) -> Self {
EcdsaAdaptorSignature(bytes)
}
}

impl EcdsaAdaptorSignature {
pub fn new() -> EcdsaAdaptorSignature {
EcdsaAdaptorSignature([0; ECDSA_ADAPTOR_SIGNATURE_LENGTH])
}

pub fn as_bytes(&self) -> &[u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH] {
&self.0
}
}

0 comments on commit 37a831d

Please sign in to comment.