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
#![crate_type = "lib"]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![warn(missing_docs)]
#![cfg_attr(not(test), no_std)]
#![cfg_attr(feature = "dev", allow(unstable_features))]
#![cfg_attr(feature = "dev", feature(plugin))]
#![cfg_attr(feature = "dev", plugin(clippy))]
#[cfg(any(test))]
pub extern crate rand;
#[cfg(feature = "serde")]
pub extern crate serde;
#[cfg(all(feature = "serde", test))]
pub extern crate serde_test;
#[cfg(test)]
extern crate core;
#[cfg(test)]
extern crate secp256k1_zkp_dev;
pub extern crate secp256k1;
#[macro_use]
mod macros;
pub mod ffi;
pub mod schnorrsig;
mod types;
pub struct ScratchSpace {
scratch_space: *mut ffi::ScratchSpace,
}
impl ScratchSpace {
pub fn new<C>(secp256k1: &secp256k1::Secp256k1<C>, max_size: usize) -> ScratchSpace {
unsafe {
let space = ffi::secp256k1_zkp_scratch_space_create(*secp256k1.ctx(), max_size);
ScratchSpace {
scratch_space: space,
}
}
}
fn scratch_space(&self) -> *mut ffi::ScratchSpace {
self.scratch_space
}
}
impl Drop for ScratchSpace {
fn drop(&mut self) {
unsafe {
ffi::secp256k1_zkp_scratch_space_destroy(self.scratch_space);
}
}
}
unsafe impl Send for ScratchSpace {}
unsafe impl Sync for ScratchSpace {}
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)
}
#[cfg(test)]
mod tests {
use super::ScratchSpace;
use secp256k1::Secp256k1;
#[test]
fn scratch_space() {
let s = Secp256k1::new();
{
let scratch_space1 = ScratchSpace::new(&s, 0);
let scratch_space2 = ScratchSpace::new(&s, 100);
let _ = scratch_space1.scratch_space();
let _ = scratch_space2.scratch_space();
}
}
}