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