1
1
#![ allow( non_snake_case) ]
2
2
3
- /// Processing bitcoin private keys, public keys
4
- /// Two main tasks are addressed by this module:
5
- /// 1. creating sets of privkey test values and the
6
- /// corresponding pubkey files.
7
- /// 2. handling conversions between WIF, hex BIP340
8
- /// and ark_ec serialization formats.
3
+ /// Processing bitcoin private key files
9
4
10
5
extern crate bulletproofs;
11
6
extern crate merlin;
12
7
extern crate rand;
13
8
14
- use ark_ec:: { AffineRepr , CurveGroup } ;
9
+ use ark_ec:: AffineRepr ;
15
10
use ark_serialize:: CanonicalSerialize ;
16
11
use bitcoin:: secp256k1:: All ;
17
12
use rand:: Rng ;
18
13
use std:: iter:: zip;
19
14
20
15
use std:: error:: Error ;
21
- use std:: ops:: { Mul , Add } ;
22
16
use std:: io:: Write ;
23
17
use ark_ec:: short_weierstrass:: Affine ;
24
18
use ark_secp256k1:: Config as SecpConfig ;
25
- use ark_secp256k1:: Fq as SecpBase ;
26
- use bitcoin:: key:: { Secp256k1 , TapTweak , UntweakedKeypair } ;
27
- use bitcoin:: { PrivateKey , XOnlyPublicKey } ;
19
+ use bitcoin:: key:: Secp256k1 ;
20
+ use bitcoin:: PrivateKey ;
28
21
use crate :: auditor:: get_audit_generators;
29
- use crate :: utils:: * ;
30
- use ark_ff:: BigInteger256 ;
22
+ use crate :: serialization:: { self , * } ;
31
23
32
24
33
25
type F = <ark_secp256k1:: Affine as AffineRepr >:: ScalarField ;
@@ -52,7 +44,7 @@ pub fn create_fake_privkeys_values_files(num_privs: u64,
52
44
& buf, bitcoin:: Network :: Signet ) . unwrap ( ) ;
53
45
let privhex = hex:: encode ( & privk2. to_bytes ( ) ) ;
54
46
55
- let x = decode_hex_le_to_F :: < F > ( & privhex) ;
47
+ let x = decode_hex_le_to_F :: < F > ( & privhex) ? ;
56
48
privkey_wifs. push ( get_wif_from_field_elem ( x) ?) ;
57
49
}
58
50
@@ -121,29 +113,6 @@ output_filename: &str) -> Result<(), Box<dyn Error>> {
121
113
write_file_string ( output_filename, buf) ;
122
114
Ok ( ( ) )
123
115
}
124
- /// Currently not in use:
125
- /// A functoin to write the commitments to a keyset
126
- /// file, but in BIP340 format:
127
- pub fn write_keyset_file_from_commitments_UNUSED ( comms : Vec < Affine < SecpConfig > > ,
128
- output_filename : & str ) -> Result < ( ) , Box < dyn Error > > {
129
- let mut buf: Vec < u8 > = Vec :: new ( ) ;
130
- let mut commslist: Vec < String > = Vec :: new ( ) ;
131
- for comm in comms {
132
- let mut buf2: Vec < u8 > = Vec :: new ( ) ;
133
- comm. serialize_with_mode ( & mut buf2,
134
- ark_serialize:: Compress :: Yes ) ?;
135
- // ignore the final byte which is the sign byte in ark:
136
- let buf3 = & mut buf2[ 0 ..32 ] ;
137
- // ark uses LE, so stop that nonsense:
138
- buf3. reverse ( ) ;
139
- let bin_bip340_pubkey = XOnlyPublicKey :: from_slice ( & buf3) ?. serialize ( ) ;
140
- let hex_bip340_pubkey = hex:: encode ( bin_bip340_pubkey) ;
141
- commslist. push ( hex_bip340_pubkey) ;
142
- }
143
- write ! ( & mut buf, "{}" , commslist. join( " " ) ) ?;
144
- write_file_string ( output_filename, buf) ;
145
- Ok ( ( ) )
146
- }
147
116
148
117
pub fn get_commitments_from_wif_and_sats_file ( privkeys_values_file_loc : & str ,
149
118
secp : & Secp256k1 :: < All > ) -> Result <
@@ -158,60 +127,15 @@ pub fn get_commitments_from_wif_and_sats(privkeys_wif: Vec<String>,
158
127
values : Vec < u64 > , secp : & Secp256k1 < All > )
159
128
-> Result < Vec < Affine < SecpConfig > > , Box < dyn Error > > {
160
129
let ( _, J ) = get_audit_generators ( ) ;
161
- //Form list of commitments as xG + vJ
162
130
let mut comms: Vec < Affine < SecpConfig > > = Vec :: new ( ) ;
163
131
for i in 0 ..privkeys_wif. len ( ) {
164
- let privk2: PrivateKey = PrivateKey :: from_wif (
165
- & privkeys_wif[ i] ) ?;
166
- let untweaked_key_pair: UntweakedKeypair =
167
- UntweakedKeypair :: from_secret_key (
168
- & secp, & privk2. inner ) ;
169
- let tweaked_key_pair = untweaked_key_pair
170
- . tap_tweak ( & secp, None ) ;
171
- let privkey_bytes = tweaked_key_pair
172
- . to_inner ( ) . secret_bytes ( ) ;
173
- let privhex = hex:: encode ( & privkey_bytes) ;
174
- let x: F = decode_hex_le_to_F ( & privhex) ;
175
- let v = F :: from ( values[ i] ) ;
176
- let P : Affine < SecpConfig > ;
177
- ( _, P ) = get_pub_and_flip_if_necessary ( x) ?;
178
- // C = xG + vJ (note *un*blinded)
179
- comms. push ( P . add ( J . mul ( v) ) . into_affine ( ) ) ;
132
+ let ( _, comm) = serialization:: privkey_val_to_taproot_ark_ec (
133
+ & privkeys_wif[ i] , values[ i] , J , secp) ?;
134
+ comms. push ( comm) ;
180
135
}
181
136
Ok ( comms)
182
137
}
183
138
184
- pub fn get_wif_from_field_elem ( x : F ) -> Result < String , Box < dyn Error > > {
185
- let mut buf: Vec < u8 > = Vec :: new ( ) ;
186
- x. serialize_compressed ( & mut buf) ?;
187
- buf. reverse ( ) ;
188
- let privk2: PrivateKey = PrivateKey :: from_slice ( & buf, bitcoin:: Network :: Signet ) . unwrap ( ) ;
189
- return Ok ( privk2. to_wif ( ) ) ;
190
- }
191
-
192
- pub fn get_field_elem_from_wif ( wif : & String ) -> Result < F , Box < dyn Error > > {
193
- let privk = PrivateKey :: from_wif ( wif) ?;
194
- let privkey_bytes = privk. to_bytes ( ) ;
195
- let privhex = hex:: encode ( & privkey_bytes) ;
196
- Ok ( decode_hex_le_to_F :: < F > ( & privhex) )
197
- }
198
-
199
- /// Since we are using BIP340 pubkeys on chain, we must use the correct
200
- /// sign of x, such that the parity of x*G is even.
201
- /// This is because, the public servers/verifiers have no choice
202
- /// but to assume that the public representation (a BIP340 key)
203
- /// corresponds to that parity, when they do the P +vG arithmetic.
204
- pub fn get_pub_and_flip_if_necessary ( mut x : F ) -> Result < ( F , Affine < SecpConfig > ) , Box < dyn Error > > {
205
- let ( G , _) = get_audit_generators ( ) ;
206
- let mut P = G . mul ( x) . into_affine ( ) ;
207
- let yval: SecpBase = * P . y ( ) . unwrap ( ) ;
208
- let yvalint: BigInteger256 = BigInteger256 :: from ( yval) ;
209
- if yvalint. 0 [ 0 ] % 2 == 1 {
210
- x = -x;
211
- P = -P ; }
212
- Ok ( ( x, P ) )
213
- }
214
-
215
139
pub fn get_privkeys_and_values ( fileloc : & str ) -> Result < ( Vec < String > , Vec < u64 > ) , Box < dyn Error > > {
216
140
let ptext = read_file_string ( & fileloc) ?;
217
141
let lines = ptext. lines ( ) ;
0 commit comments