1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
33#![warn(missing_docs)]
34#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
35
36pub extern crate secp256k1_zkp_sys;
38pub use secp256k1_zkp_sys as ffi;
40
41extern crate secp256k1;
42
43#[cfg(feature = "hashes")]
44pub use secp256k1::hashes;
45#[cfg(feature = "actual-rand")]
47pub extern crate actual_rand as rand;
48#[cfg(feature = "serde")]
50pub extern crate actual_serde as serde;
51#[cfg(any(test, feature = "std"))]
52extern crate core;
53#[cfg(all(test, feature = "serde"))]
54extern crate serde_test;
55#[cfg(all(test, target_arch = "wasm32"))]
56#[macro_use]
57extern crate wasm_bindgen_test;
58
59use core::{fmt, str};
60
61pub use secp256k1::constants;
62pub use secp256k1::ecdh;
63pub use secp256k1::ecdsa;
64pub use secp256k1::schnorr;
65
66pub use crate::{PublicKey, SecretKey};
67
68pub use secp256k1::*;
69
70#[cfg(feature = "serde")]
71mod serde_util;
72mod zkp;
73pub use crate::zkp::*;
74
75pub use secp256k1::Error as UpstreamError;
76
77#[derive(Copy, PartialEq, Eq, Clone, Debug)]
79pub enum Error {
80    Upstream(UpstreamError),
82    CannotProveSurjection,
84    InvalidSurjectionProof,
86    InvalidPedersenCommitment,
88    CannotMakeRangeProof,
90    InvalidRangeProof,
92    InvalidGenerator,
94    InvalidTweakLength,
96    TweakOutOfBounds,
98    InvalidEcdsaAdaptorSignature,
100    CannotDecryptAdaptorSignature,
102    CannotRecoverAdaptorSecret,
104    CannotVerifyAdaptorSignature,
106    InvalidWhitelistSignature,
108    InvalidPakList,
110    CannotCreateWhitelistSignature,
112    InvalidWhitelistProof,
114}
115
116impl fmt::Display for Error {
118    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
119        let str = match *self {
120            Error::CannotProveSurjection => "failed to prove surjection",
121            Error::InvalidSurjectionProof => "malformed surjection proof",
122            Error::InvalidPedersenCommitment => "malformed pedersen commitment",
123            Error::CannotMakeRangeProof => "failed to generate range proof",
124            Error::InvalidRangeProof => "failed to verify range proof",
125            Error::InvalidGenerator => "malformed generator",
126            Error::InvalidEcdsaAdaptorSignature => "malformed ecdsa adaptor signature",
127            Error::CannotDecryptAdaptorSignature => "failed to decrypt adaptor signature",
128            Error::CannotRecoverAdaptorSecret => "failed to recover adaptor secret",
129            Error::CannotVerifyAdaptorSignature => "failed to verify adaptor signature",
130            Error::Upstream(inner) => return write!(f, "{}", inner),
131            Error::InvalidTweakLength => "Tweak must of size 32",
132            Error::TweakOutOfBounds => "Tweak must be less than secp curve order",
133            Error::InvalidWhitelistSignature => "malformed whitelist signature",
134            Error::InvalidPakList => "invalid PAK list",
135            Error::CannotCreateWhitelistSignature => {
136                "cannot create whitelist signature with the given data"
137            }
138            Error::InvalidWhitelistProof => {
139                "given whitelist signature doesn't correctly prove inclusion in the whitelist"
140            }
141        };
142
143        f.write_str(str)
144    }
145}
146
147#[cfg(feature = "std")]
148impl std::error::Error for Error {}
149
150impl From<UpstreamError> for Error {
151    fn from(e: UpstreamError) -> Self {
152        Error::Upstream(e)
153    }
154}
155
156fn from_hex(hex: &str, target: &mut [u8]) -> Result<usize, ()> {
160    if hex.len() % 2 == 1 || hex.len() > target.len() * 2 {
161        return Err(());
162    }
163
164    let mut b = 0;
165    let mut idx = 0;
166    for c in hex.bytes() {
167        b <<= 4;
168        match c {
169            b'A'..=b'F' => b |= c - b'A' + 10,
170            b'a'..=b'f' => b |= c - b'a' + 10,
171            b'0'..=b'9' => b |= c - b'0',
172            _ => return Err(()),
173        }
174        if (idx & 1) == 1 {
175            target[idx / 2] = b;
176            b = 0;
177        }
178        idx += 1;
179    }
180    Ok(idx / 2)
181}