-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSapling.swift
More file actions
361 lines (312 loc) · 11.4 KB
/
Sapling.swift
File metadata and controls
361 lines (312 loc) · 11.4 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Sapling.swift
// Vendored from https://github.com/madfish-solutions/madfish-sapling (packages/sapling-ios/Sources/Sapling/Sapling.swift)
// Provides a Swift wrapper around the SaplingFFI C bindings.
import SaplingFFI
public struct Sapling {
public init() {}
// MARK: Authorizing Key
public func getProofAuthorizingKey(from spendingKey: ExtendedSpendingKey) throws -> ProofAuthorizingKey {
var keyCount = 0
guard let key = c_pak_from_xsk(spendingKey, spendingKey.count, &keyCount)?.toArray(count: keyCount) else {
throw Error.deriveKeyFailed
}
return key
}
// MARK: Commitment
public func verifyCommitment(_ commitment: [UInt8], to address: Address, forValue value: UInt64, with rcm: Rcm) throws -> Bool {
var cmuCount = 0
guard let cmu = c_compute_cmu(address, address.count, value, rcm, rcm.count, &cmuCount)?.toArray(count: cmuCount) else {
throw Error.computeCommitmentFailed
}
return commitment == cmu
}
// MARK: Init
public func initParameters(spend spendParameters: [UInt8], output outputParameters: [UInt8]) throws {
guard c_init_params(spendParameters, spendParameters.count, outputParameters, outputParameters.count) else {
throw Error.initParametersFailed
}
}
// MARK: Key Agreement
public func keyAgreement(p: [UInt8], sk: [UInt8]) throws -> [UInt8] {
var kaCount = 0
guard let ka = c_key_agreement(p, p.count, sk, sk.count, &kaCount)?.toArray(count: kaCount) else {
throw Error.createKeyAgreementFailed
}
return ka
}
// MARK: Merkle Tree
public func merkleHash(ofDepth depth: Int, lhs: [UInt8], rhs: [UInt8]) throws -> [UInt8] {
var merkleHashCount = 0
guard let merkleHash = c_merkle_hash(depth, lhs, lhs.count, rhs, rhs.count, &merkleHashCount)?.toArray(count: merkleHashCount) else {
throw Error.createMerkleHashFailed
}
return merkleHash
}
// MARK: Nullifier
public func computeNullifier(using viewingKey: ExtendedFullViewingKey, to address: Address, forValue value: UInt64, with rcm: Rcm, at position: UInt64) throws -> [UInt8] {
var nullifierCount = 0
guard let nullifier = c_compute_nullifier_with_xfvk(
viewingKey, viewingKey.count,
address, address.count,
value,
rcm, rcm.count,
position,
&nullifierCount
)?.toArray(count: nullifierCount) else {
throw Error.computeNullifierFailed
}
return nullifier
}
// MARK: Output Description
public func prepareOutputDescription(
with context: Context,
using viewingKey: ExtendedFullViewingKey,
to address: Address,
withRcm rcm: Rcm,
ofValue value: UInt64
) throws -> [UInt8] {
var descriptionCount = 0
guard let description = c_output_description_from_xfvk(context, viewingKey, viewingKey.count, address, address.count, rcm, rcm.count, value, &descriptionCount)?.toArray(count: descriptionCount) else {
throw Error.outputDescriptionFailed
}
return description
}
public func preparePartialOutputDescription(
with context: Context,
to address: Address,
withRcm rcm: Rcm,
withEsk esk: Esk,
ofValue value: UInt64
) throws -> [UInt8] {
var descriptionCount = 0
guard let description = c_partial_output_description(
context,
address, address.count,
rcm, rcm.count,
esk, esk.count,
value,
&descriptionCount
)?.toArray(count: descriptionCount) else {
throw Error.outputDescriptionFailed
}
return description
}
public func deriveEpkFromEsk(diversifier: Diversifier, esk: Esk) throws -> [UInt8] {
var epkCount = 0
guard let epk = c_derive_epk_from_esk(diversifier, diversifier.count, esk, esk.count, &epkCount)?.toArray(count: epkCount) else {
throw Error.deriveKeyFailed
}
return epk
}
// MARK: Payment Address
public func getPaymentAddress(from viewingKey: ExtendedFullViewingKey, at index: Index) throws -> [UInt8] {
var addressCount = 0
guard let address = c_payment_address_from_xfvk(viewingKey, viewingKey.count, index, index.count, &addressCount)?.toArray(count: addressCount) else {
throw Error.createAddressFailed
}
return address
}
public func getNextPaymentAddress(from viewingKey: ExtendedFullViewingKey, lastAt index: Index) throws -> [UInt8] {
var addressCount = 0
guard let address = c_next_payment_address_from_xfvk(viewingKey, viewingKey.count, index, index.count, &addressCount)?.toArray(count: addressCount) else {
throw Error.createAddressFailed
}
return address
}
public func getRawPaymentAddress(from incomingViewingKey: IncomingViewingKey, with diversifier: Diversifier) throws -> [UInt8] {
var addressCount = 0
guard let address = c_payment_address_from_ivk(
incomingViewingKey, incomingViewingKey.count,
diversifier, diversifier.count,
&addressCount
)?.toArray(count: addressCount) else {
throw Error.createAddressFailed
}
return address
}
public func getDiversifier(fromRaw rawAddress: Address) throws -> [UInt8] {
var diversifierCount = 0
guard let diversifier = c_diversifier_from_payment_address(rawAddress, rawAddress.count, &diversifierCount)?.toArray(count: diversifierCount) else {
throw Error.extractFromAddressFailed
}
return diversifier
}
public func getPkd(fromRaw rawAddress: Address) throws -> [UInt8] {
var pkdCount = 0
guard let pkd = c_pkd_from_payment_address(rawAddress, rawAddress.count, &pkdCount)?.toArray(count: pkdCount) else {
throw Error.extractFromAddressFailed
}
return pkd
}
// MARK: Proving Context
public func initProvingContext() throws -> Context {
guard let context = c_init_proving_context() else {
throw Error.initProvingContextFailed
}
return context
}
public func dropProvingContext(_ context: Context) {
c_drop_proving_context(context)
}
// MARK: Rand
public func randR() throws -> [UInt8] {
var rCount = 0
guard let r = c_rand_r(&rCount)?.toArray(count: rCount) else {
throw Error.createRandomScalarFailed
}
return r
}
// MARK: Signature
public func createBindingSignature(with context: Context, balance: Int64, sighash: [UInt8]) throws -> [UInt8] {
var signatureCount = 0
guard let signature = c_binding_signature(context, balance, sighash, sighash.count, &signatureCount)?.toArray(count: signatureCount) else {
throw Error.createBindingSignatureFailed
}
return signature
}
// MARK: Spend Description
public func prepareSpendDescriptionWithSpendingKey(
with context: Context,
using spendingKey: ExtendedSpendingKey,
to address: Address,
withRcm rcm: Rcm,
withAr ar: Ar,
ofValue value: UInt64,
withAnchor anchor: Anchor,
at merklePath: MerklePath
) throws -> [UInt8] {
var descriptionCount = 0
guard let description = c_spend_description_from_xsk(
context,
spendingKey, spendingKey.count,
address, address.count,
rcm, rcm.count,
ar, ar.count,
value,
anchor, anchor.count,
merklePath, merklePath.count,
&descriptionCount
)?.toArray(count: descriptionCount) else {
throw Error.spendDescriptionFailed
}
return description
}
public func prepareSpendDescriptionWithAuthorizingKey(
with context: Context,
using authorizingKey: ProofAuthorizingKey,
to address: Address,
withRcm rcm: Rcm,
withAr ar: Ar,
ofValue value: UInt64,
withAnchor anchor: Anchor,
at merklePath: MerklePath
) throws -> [UInt8] {
var descriptionCount = 0
guard let description = c_spend_description_from_pak(
context,
authorizingKey, authorizingKey.count,
address, address.count,
rcm, rcm.count,
ar, ar.count,
value,
anchor, anchor.count,
merklePath, merklePath.count,
&descriptionCount
)?.toArray(count: descriptionCount) else {
throw Error.spendDescriptionFailed
}
return description
}
public func signSpendDescription(_ spendDescription: [UInt8], using spendingKey: ExtendedSpendingKey, with ar: Ar, sighash: [UInt8]) throws -> [UInt8] {
var signedDescriptionCount = 0
guard let signedDescription = c_sign_spend_description_with_xsk(
spendDescription, spendDescription.count,
spendingKey, spendingKey.count,
ar, ar.count,
sighash, sighash.count,
&signedDescriptionCount
)?.toArray(count: signedDescriptionCount) else {
throw Error.spendDescriptionFailed
}
return signedDescription
}
// MARK: Spending Key
public func getExtendedSpendingKey(from seed: Seed, derivationPath: String) throws -> ExtendedSpendingKey {
var keyCount = 0
return try derivationPath.withCString { derivationPath in
guard let key = c_xsk(seed, seed.count, derivationPath, &keyCount)?.toArray(count: keyCount) else {
throw Error.deriveKeyFailed
}
return key
}
}
// MARK: Viewing Key
public func getExtendedFullViewingKey(from seed: Seed, derivationPath: String) throws -> ExtendedFullViewingKey {
var keyCount = 0
return try derivationPath.withCString { derivationPath in
guard let key = c_xfvk(seed, seed.count, derivationPath, &keyCount)?.toArray(count: keyCount) else {
throw Error.deriveKeyFailed
}
return key
}
}
public func getExtendedFullViewingKey(from spendingKey: ExtendedSpendingKey) throws -> ExtendedFullViewingKey {
var keyCount = 0
guard let key = c_xfvk_from_xsk(spendingKey, spendingKey.count, &keyCount)?.toArray(count: keyCount) else {
throw Error.deriveKeyFailed
}
return key
}
public func getOutgoingViewingKey(from viewingKey: ExtendedFullViewingKey) throws -> OutgoingViewingKey {
var keyCount = 0
guard let key = c_ovk_from_xfvk(viewingKey, viewingKey.count, &keyCount)?.toArray(count: keyCount) else {
throw Error.deriveKeyFailed
}
return key
}
public func getIncomingViewingKey(from viewingKey: ExtendedFullViewingKey) throws -> IncomingViewingKey {
var keyCount = 0
guard let key = c_xfvk_to_ivk(viewingKey, viewingKey.count, &keyCount)?.toArray(count: keyCount) else {
throw Error.deriveKeyFailed
}
return key
}
public typealias Context = UnsafeMutableRawPointer
public typealias Seed = [UInt8]
public typealias ExtendedSpendingKey = [UInt8]
public typealias ExtendedFullViewingKey = [UInt8]
public typealias IncomingViewingKey = [UInt8]
public typealias OutgoingViewingKey = [UInt8]
public typealias ProofAuthorizingKey = [UInt8]
public typealias Address = [UInt8]
public typealias Diversifier = [UInt8]
public typealias Index = [UInt8]
public typealias Rcm = [UInt8]
public typealias Esk = [UInt8]
public typealias Ar = [UInt8]
public typealias Anchor = [UInt8]
public typealias MerklePath = [UInt8]
public enum Error: Swift.Error {
case computeCommitmentFailed
case initParametersFailed
case createKeyAgreementFailed
case createMerkleHashFailed
case computeNullifierFailed
case outputDescriptionFailed
case createAddressFailed
case extractFromAddressFailed
case initProvingContextFailed
case createRandomScalarFailed
case createBindingSignatureFailed
case spendDescriptionFailed
case deriveKeyFailed
}
}
extension UnsafeMutablePointer where Pointee == UInt8 {
func toArray(count: Int) -> [UInt8] {
let bytes = Array(UnsafeBufferPointer(start: self, count: count))
deinitialize(count: count)
deallocate()
return bytes
}
}