This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mopro.swift
866 lines (744 loc) · 27.9 KB
/
mopro.swift
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
// This file was autogenerated by some hot garbage in the `uniffi` crate.
// Trust me, you don't want to mess with it!
// swiftlint:disable all
import Foundation
// Depending on the consumer's build setup, the low-level FFI code
// might be in a separate module, or it might be compiled inline into
// this module. This is a bit of light hackery to work with both.
#if canImport(moproFFI)
import moproFFI
#endif
private extension RustBuffer {
// Allocate a new buffer, copying the contents of a `UInt8` array.
init(bytes: [UInt8]) {
let rbuf = bytes.withUnsafeBufferPointer { ptr in
RustBuffer.from(ptr)
}
self.init(capacity: rbuf.capacity, len: rbuf.len, data: rbuf.data)
}
static func empty() -> RustBuffer {
RustBuffer(capacity: 0, len: 0, data: nil)
}
static func from(_ ptr: UnsafeBufferPointer<UInt8>) -> RustBuffer {
try! rustCall { ffi_mopro_bindings_rustbuffer_from_bytes(ForeignBytes(bufferPointer: ptr), $0) }
}
// Frees the buffer in place.
// The buffer must not be used after this is called.
func deallocate() {
try! rustCall { ffi_mopro_bindings_rustbuffer_free(self, $0) }
}
}
private extension ForeignBytes {
init(bufferPointer: UnsafeBufferPointer<UInt8>) {
self.init(len: Int32(bufferPointer.count), data: bufferPointer.baseAddress)
}
}
// For every type used in the interface, we provide helper methods for conveniently
// lifting and lowering that type from C-compatible data, and for reading and writing
// values of that type in a buffer.
// Helper classes/extensions that don't change.
// Someday, this will be in a library of its own.
private extension Data {
init(rustBuffer: RustBuffer) {
// TODO: This copies the buffer. Can we read directly from a
// Rust buffer?
self.init(bytes: rustBuffer.data!, count: Int(rustBuffer.len))
}
}
// Define reader functionality. Normally this would be defined in a class or
// struct, but we use standalone functions instead in order to make external
// types work.
//
// With external types, one swift source file needs to be able to call the read
// method on another source file's FfiConverter, but then what visibility
// should Reader have?
// - If Reader is fileprivate, then this means the read() must also
// be fileprivate, which doesn't work with external types.
// - If Reader is internal/public, we'll get compile errors since both source
// files will try define the same type.
//
// Instead, the read() method and these helper functions input a tuple of data
private func createReader(data: Data) -> (data: Data, offset: Data.Index) {
(data: data, offset: 0)
}
// Reads an integer at the current offset, in big-endian order, and advances
// the offset on success. Throws if reading the integer would move the
// offset past the end of the buffer.
private func readInt<T: FixedWidthInteger>(_ reader: inout (data: Data, offset: Data.Index)) throws -> T {
let range = reader.offset ..< reader.offset + MemoryLayout<T>.size
guard reader.data.count >= range.upperBound else {
throw UniffiInternalError.bufferOverflow
}
if T.self == UInt8.self {
let value = reader.data[reader.offset]
reader.offset += 1
return value as! T
}
var value: T = 0
let _ = withUnsafeMutableBytes(of: &value) { reader.data.copyBytes(to: $0, from: range) }
reader.offset = range.upperBound
return value.bigEndian
}
// Reads an arbitrary number of bytes, to be used to read
// raw bytes, this is useful when lifting strings
private func readBytes(_ reader: inout (data: Data, offset: Data.Index), count: Int) throws -> [UInt8] {
let range = reader.offset ..< (reader.offset + count)
guard reader.data.count >= range.upperBound else {
throw UniffiInternalError.bufferOverflow
}
var value = [UInt8](repeating: 0, count: count)
value.withUnsafeMutableBufferPointer { buffer in
reader.data.copyBytes(to: buffer, from: range)
}
reader.offset = range.upperBound
return value
}
// Reads a float at the current offset.
private func readFloat(_ reader: inout (data: Data, offset: Data.Index)) throws -> Float {
return try Float(bitPattern: readInt(&reader))
}
// Reads a float at the current offset.
private func readDouble(_ reader: inout (data: Data, offset: Data.Index)) throws -> Double {
return try Double(bitPattern: readInt(&reader))
}
// Indicates if the offset has reached the end of the buffer.
private func hasRemaining(_ reader: (data: Data, offset: Data.Index)) -> Bool {
return reader.offset < reader.data.count
}
// Define writer functionality. Normally this would be defined in a class or
// struct, but we use standalone functions instead in order to make external
// types work. See the above discussion on Readers for details.
private func createWriter() -> [UInt8] {
return []
}
private func writeBytes<S>(_ writer: inout [UInt8], _ byteArr: S) where S: Sequence, S.Element == UInt8 {
writer.append(contentsOf: byteArr)
}
// Writes an integer in big-endian order.
//
// Warning: make sure what you are trying to write
// is in the correct type!
private func writeInt<T: FixedWidthInteger>(_ writer: inout [UInt8], _ value: T) {
var value = value.bigEndian
withUnsafeBytes(of: &value) { writer.append(contentsOf: $0) }
}
private func writeFloat(_ writer: inout [UInt8], _ value: Float) {
writeInt(&writer, value.bitPattern)
}
private func writeDouble(_ writer: inout [UInt8], _ value: Double) {
writeInt(&writer, value.bitPattern)
}
// Protocol for types that transfer other types across the FFI. This is
// analogous to the Rust trait of the same name.
private protocol FfiConverter {
associatedtype FfiType
associatedtype SwiftType
static func lift(_ value: FfiType) throws -> SwiftType
static func lower(_ value: SwiftType) -> FfiType
static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType
static func write(_ value: SwiftType, into buf: inout [UInt8])
}
// Types conforming to `Primitive` pass themselves directly over the FFI.
private protocol FfiConverterPrimitive: FfiConverter where FfiType == SwiftType {}
extension FfiConverterPrimitive {
public static func lift(_ value: FfiType) throws -> SwiftType {
return value
}
public static func lower(_ value: SwiftType) -> FfiType {
return value
}
}
// Types conforming to `FfiConverterRustBuffer` lift and lower into a `RustBuffer`.
// Used for complex types where it's hard to write a custom lift/lower.
private protocol FfiConverterRustBuffer: FfiConverter where FfiType == RustBuffer {}
extension FfiConverterRustBuffer {
public static func lift(_ buf: RustBuffer) throws -> SwiftType {
var reader = createReader(data: Data(rustBuffer: buf))
let value = try read(from: &reader)
if hasRemaining(reader) {
throw UniffiInternalError.incompleteData
}
buf.deallocate()
return value
}
public static func lower(_ value: SwiftType) -> RustBuffer {
var writer = createWriter()
write(value, into: &writer)
return RustBuffer(bytes: writer)
}
}
// An error type for FFI errors. These errors occur at the UniFFI level, not
// the library level.
private enum UniffiInternalError: LocalizedError {
case bufferOverflow
case incompleteData
case unexpectedOptionalTag
case unexpectedEnumCase
case unexpectedNullPointer
case unexpectedRustCallStatusCode
case unexpectedRustCallError
case unexpectedStaleHandle
case rustPanic(_ message: String)
public var errorDescription: String? {
switch self {
case .bufferOverflow: return "Reading the requested value would read past the end of the buffer"
case .incompleteData: return "The buffer still has data after lifting its containing value"
case .unexpectedOptionalTag: return "Unexpected optional tag; should be 0 or 1"
case .unexpectedEnumCase: return "Raw enum value doesn't match any cases"
case .unexpectedNullPointer: return "Raw pointer value was null"
case .unexpectedRustCallStatusCode: return "Unexpected RustCallStatus code"
case .unexpectedRustCallError: return "CALL_ERROR but no errorClass specified"
case .unexpectedStaleHandle: return "The object in the handle map has been dropped already"
case let .rustPanic(message): return message
}
}
}
private extension NSLock {
func withLock<T>(f: () throws -> T) rethrows -> T {
lock()
defer { self.unlock() }
return try f()
}
}
private let CALL_SUCCESS: Int8 = 0
private let CALL_ERROR: Int8 = 1
private let CALL_UNEXPECTED_ERROR: Int8 = 2
private let CALL_CANCELLED: Int8 = 3
private extension RustCallStatus {
init() {
self.init(
code: CALL_SUCCESS,
errorBuf: RustBuffer(
capacity: 0,
len: 0,
data: nil
)
)
}
}
private func rustCall<T>(_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
let neverThrow: ((RustBuffer) throws -> Never)? = nil
return try makeRustCall(callback, errorHandler: neverThrow)
}
private func rustCallWithError<T, E: Swift.Error>(
_ errorHandler: @escaping (RustBuffer) throws -> E,
_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T
) throws -> T {
try makeRustCall(callback, errorHandler: errorHandler)
}
private func makeRustCall<T, E: Swift.Error>(
_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T,
errorHandler: ((RustBuffer) throws -> E)?
) throws -> T {
uniffiEnsureInitialized()
var callStatus = RustCallStatus()
let returnedVal = callback(&callStatus)
try uniffiCheckCallStatus(callStatus: callStatus, errorHandler: errorHandler)
return returnedVal
}
private func uniffiCheckCallStatus<E: Swift.Error>(
callStatus: RustCallStatus,
errorHandler: ((RustBuffer) throws -> E)?
) throws {
switch callStatus.code {
case CALL_SUCCESS:
return
case CALL_ERROR:
if let errorHandler = errorHandler {
throw try errorHandler(callStatus.errorBuf)
} else {
callStatus.errorBuf.deallocate()
throw UniffiInternalError.unexpectedRustCallError
}
case CALL_UNEXPECTED_ERROR:
// When the rust code sees a panic, it tries to construct a RustBuffer
// with the message. But if that code panics, then it just sends back
// an empty buffer.
if callStatus.errorBuf.len > 0 {
throw try UniffiInternalError.rustPanic(FfiConverterString.lift(callStatus.errorBuf))
} else {
callStatus.errorBuf.deallocate()
throw UniffiInternalError.rustPanic("Rust panic")
}
case CALL_CANCELLED:
fatalError("Cancellation not supported yet")
default:
throw UniffiInternalError.unexpectedRustCallStatusCode
}
}
private func uniffiTraitInterfaceCall<T>(
callStatus: UnsafeMutablePointer<RustCallStatus>,
makeCall: () throws -> T,
writeReturn: (T) -> Void
) {
do {
try writeReturn(makeCall())
} catch {
callStatus.pointee.code = CALL_UNEXPECTED_ERROR
callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
}
}
private func uniffiTraitInterfaceCallWithError<T, E>(
callStatus: UnsafeMutablePointer<RustCallStatus>,
makeCall: () throws -> T,
writeReturn: (T) -> Void,
lowerError: (E) -> RustBuffer
) {
do {
try writeReturn(makeCall())
} catch let error as E {
callStatus.pointee.code = CALL_ERROR
callStatus.pointee.errorBuf = lowerError(error)
} catch {
callStatus.pointee.code = CALL_UNEXPECTED_ERROR
callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
}
}
private class UniffiHandleMap<T> {
private var map: [UInt64: T] = [:]
private let lock = NSLock()
private var currentHandle: UInt64 = 1
func insert(obj: T) -> UInt64 {
lock.withLock {
let handle = currentHandle
currentHandle += 1
map[handle] = obj
return handle
}
}
func get(handle: UInt64) throws -> T {
try lock.withLock {
guard let obj = map[handle] else {
throw UniffiInternalError.unexpectedStaleHandle
}
return obj
}
}
@discardableResult
func remove(handle: UInt64) throws -> T {
try lock.withLock {
guard let obj = map.removeValue(forKey: handle) else {
throw UniffiInternalError.unexpectedStaleHandle
}
return obj
}
}
var count: Int {
map.count
}
}
// Public interface members begin here.
private struct FfiConverterBool: FfiConverter {
typealias FfiType = Int8
typealias SwiftType = Bool
public static func lift(_ value: Int8) throws -> Bool {
return value != 0
}
public static func lower(_ value: Bool) -> Int8 {
return value ? 1 : 0
}
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Bool {
return try lift(readInt(&buf))
}
public static func write(_ value: Bool, into buf: inout [UInt8]) {
writeInt(&buf, lower(value))
}
}
private struct FfiConverterString: FfiConverter {
typealias SwiftType = String
typealias FfiType = RustBuffer
public static func lift(_ value: RustBuffer) throws -> String {
defer {
value.deallocate()
}
if value.data == nil {
return String()
}
let bytes = UnsafeBufferPointer<UInt8>(start: value.data!, count: Int(value.len))
return String(bytes: bytes, encoding: String.Encoding.utf8)!
}
public static func lower(_ value: String) -> RustBuffer {
return value.utf8CString.withUnsafeBufferPointer { ptr in
// The swift string gives us int8_t, we want uint8_t.
ptr.withMemoryRebound(to: UInt8.self) { ptr in
// The swift string gives us a trailing null byte, we don't want it.
let buf = UnsafeBufferPointer(rebasing: ptr.prefix(upTo: ptr.count - 1))
return RustBuffer.from(buf)
}
}
}
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> String {
let len: Int32 = try readInt(&buf)
return try String(bytes: readBytes(&buf, count: Int(len)), encoding: String.Encoding.utf8)!
}
public static func write(_ value: String, into buf: inout [UInt8]) {
let len = Int32(value.utf8.count)
writeInt(&buf, len)
writeBytes(&buf, value.utf8)
}
}
private struct FfiConverterData: FfiConverterRustBuffer {
typealias SwiftType = Data
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Data {
let len: Int32 = try readInt(&buf)
return try Data(readBytes(&buf, count: Int(len)))
}
public static func write(_ value: Data, into buf: inout [UInt8]) {
let len = Int32(value.count)
writeInt(&buf, len)
writeBytes(&buf, value)
}
}
public struct G1 {
public var x: String
public var y: String
// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(x: String, y: String) {
self.x = x
self.y = y
}
}
extension G1: Equatable, Hashable {
public static func == (lhs: G1, rhs: G1) -> Bool {
if lhs.x != rhs.x {
return false
}
if lhs.y != rhs.y {
return false
}
return true
}
public func hash(into hasher: inout Hasher) {
hasher.combine(x)
hasher.combine(y)
}
}
public struct FfiConverterTypeG1: FfiConverterRustBuffer {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> G1 {
return
try G1(
x: FfiConverterString.read(from: &buf),
y: FfiConverterString.read(from: &buf)
)
}
public static func write(_ value: G1, into buf: inout [UInt8]) {
FfiConverterString.write(value.x, into: &buf)
FfiConverterString.write(value.y, into: &buf)
}
}
public func FfiConverterTypeG1_lift(_ buf: RustBuffer) throws -> G1 {
return try FfiConverterTypeG1.lift(buf)
}
public func FfiConverterTypeG1_lower(_ value: G1) -> RustBuffer {
return FfiConverterTypeG1.lower(value)
}
public struct G2 {
public var x: [String]
public var y: [String]
// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(x: [String], y: [String]) {
self.x = x
self.y = y
}
}
extension G2: Equatable, Hashable {
public static func == (lhs: G2, rhs: G2) -> Bool {
if lhs.x != rhs.x {
return false
}
if lhs.y != rhs.y {
return false
}
return true
}
public func hash(into hasher: inout Hasher) {
hasher.combine(x)
hasher.combine(y)
}
}
public struct FfiConverterTypeG2: FfiConverterRustBuffer {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> G2 {
return
try G2(
x: FfiConverterSequenceString.read(from: &buf),
y: FfiConverterSequenceString.read(from: &buf)
)
}
public static func write(_ value: G2, into buf: inout [UInt8]) {
FfiConverterSequenceString.write(value.x, into: &buf)
FfiConverterSequenceString.write(value.y, into: &buf)
}
}
public func FfiConverterTypeG2_lift(_ buf: RustBuffer) throws -> G2 {
return try FfiConverterTypeG2.lift(buf)
}
public func FfiConverterTypeG2_lower(_ value: G2) -> RustBuffer {
return FfiConverterTypeG2.lower(value)
}
public struct GenerateProofResult {
public var proof: Data
public var inputs: Data
// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(proof: Data, inputs: Data) {
self.proof = proof
self.inputs = inputs
}
}
extension GenerateProofResult: Equatable, Hashable {
public static func == (lhs: GenerateProofResult, rhs: GenerateProofResult) -> Bool {
if lhs.proof != rhs.proof {
return false
}
if lhs.inputs != rhs.inputs {
return false
}
return true
}
public func hash(into hasher: inout Hasher) {
hasher.combine(proof)
hasher.combine(inputs)
}
}
public struct FfiConverterTypeGenerateProofResult: FfiConverterRustBuffer {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> GenerateProofResult {
return
try GenerateProofResult(
proof: FfiConverterData.read(from: &buf),
inputs: FfiConverterData.read(from: &buf)
)
}
public static func write(_ value: GenerateProofResult, into buf: inout [UInt8]) {
FfiConverterData.write(value.proof, into: &buf)
FfiConverterData.write(value.inputs, into: &buf)
}
}
public func FfiConverterTypeGenerateProofResult_lift(_ buf: RustBuffer) throws -> GenerateProofResult {
return try FfiConverterTypeGenerateProofResult.lift(buf)
}
public func FfiConverterTypeGenerateProofResult_lower(_ value: GenerateProofResult) -> RustBuffer {
return FfiConverterTypeGenerateProofResult.lower(value)
}
public struct ProofCalldata {
public var a: G1
public var b: G2
public var c: G1
// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(a: G1, b: G2, c: G1) {
self.a = a
self.b = b
self.c = c
}
}
extension ProofCalldata: Equatable, Hashable {
public static func == (lhs: ProofCalldata, rhs: ProofCalldata) -> Bool {
if lhs.a != rhs.a {
return false
}
if lhs.b != rhs.b {
return false
}
if lhs.c != rhs.c {
return false
}
return true
}
public func hash(into hasher: inout Hasher) {
hasher.combine(a)
hasher.combine(b)
hasher.combine(c)
}
}
public struct FfiConverterTypeProofCalldata: FfiConverterRustBuffer {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ProofCalldata {
return
try ProofCalldata(
a: FfiConverterTypeG1.read(from: &buf),
b: FfiConverterTypeG2.read(from: &buf),
c: FfiConverterTypeG1.read(from: &buf)
)
}
public static func write(_ value: ProofCalldata, into buf: inout [UInt8]) {
FfiConverterTypeG1.write(value.a, into: &buf)
FfiConverterTypeG2.write(value.b, into: &buf)
FfiConverterTypeG1.write(value.c, into: &buf)
}
}
public func FfiConverterTypeProofCalldata_lift(_ buf: RustBuffer) throws -> ProofCalldata {
return try FfiConverterTypeProofCalldata.lift(buf)
}
public func FfiConverterTypeProofCalldata_lower(_ value: ProofCalldata) -> RustBuffer {
return FfiConverterTypeProofCalldata.lower(value)
}
public enum MoproError {
case CircomError(message: String)
case Halo2Error(message: String)
}
public struct FfiConverterTypeMoproError: FfiConverterRustBuffer {
typealias SwiftType = MoproError
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> MoproError {
let variant: Int32 = try readInt(&buf)
switch variant {
case 1: return try .CircomError(
message: FfiConverterString.read(from: &buf)
)
case 2: return try .Halo2Error(
message: FfiConverterString.read(from: &buf)
)
default: throw UniffiInternalError.unexpectedEnumCase
}
}
public static func write(_ value: MoproError, into buf: inout [UInt8]) {
switch value {
case .CircomError(_ /* message is ignored*/ ):
writeInt(&buf, Int32(1))
case .Halo2Error(_ /* message is ignored*/ ):
writeInt(&buf, Int32(2))
}
}
}
extension MoproError: Equatable, Hashable {}
extension MoproError: Foundation.LocalizedError {
public var errorDescription: String? {
String(reflecting: self)
}
}
private struct FfiConverterSequenceString: FfiConverterRustBuffer {
typealias SwiftType = [String]
public static func write(_ value: [String], into buf: inout [UInt8]) {
let len = Int32(value.count)
writeInt(&buf, len)
for item in value {
FfiConverterString.write(item, into: &buf)
}
}
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [String] {
let len: Int32 = try readInt(&buf)
var seq = [String]()
seq.reserveCapacity(Int(len))
for _ in 0 ..< len {
try seq.append(FfiConverterString.read(from: &buf))
}
return seq
}
}
private struct FfiConverterDictionaryStringSequenceString: FfiConverterRustBuffer {
public static func write(_ value: [String: [String]], into buf: inout [UInt8]) {
let len = Int32(value.count)
writeInt(&buf, len)
for (key, value) in value {
FfiConverterString.write(key, into: &buf)
FfiConverterSequenceString.write(value, into: &buf)
}
}
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [String: [String]] {
let len: Int32 = try readInt(&buf)
var dict = [String: [String]]()
dict.reserveCapacity(Int(len))
for _ in 0 ..< len {
let key = try FfiConverterString.read(from: &buf)
let value = try FfiConverterSequenceString.read(from: &buf)
dict[key] = value
}
return dict
}
}
public func generateCircomProof(zkeyPath: String, circuitInputs: [String: [String]]) throws -> GenerateProofResult {
return try FfiConverterTypeGenerateProofResult.lift(rustCallWithError(FfiConverterTypeMoproError.lift) {
uniffi_mopro_bindings_fn_func_generate_circom_proof(
FfiConverterString.lower(zkeyPath),
FfiConverterDictionaryStringSequenceString.lower(circuitInputs), $0
)
})
}
public func generateHalo2Proof(srsPath: String, pkPath: String, circuitInputs: [String: [String]]) throws -> GenerateProofResult {
return try FfiConverterTypeGenerateProofResult.lift(rustCallWithError(FfiConverterTypeMoproError.lift) {
uniffi_mopro_bindings_fn_func_generate_halo2_proof(
FfiConverterString.lower(srsPath),
FfiConverterString.lower(pkPath),
FfiConverterDictionaryStringSequenceString.lower(circuitInputs), $0
)
})
}
public func toEthereumInputs(inputs: Data) -> [String] {
return try! FfiConverterSequenceString.lift(try! rustCall {
uniffi_mopro_bindings_fn_func_to_ethereum_inputs(
FfiConverterData.lower(inputs), $0
)
})
}
public func toEthereumProof(proof: Data) -> ProofCalldata {
return try! FfiConverterTypeProofCalldata.lift(try! rustCall {
uniffi_mopro_bindings_fn_func_to_ethereum_proof(
FfiConverterData.lower(proof), $0
)
})
}
public func verifyCircomProof(zkeyPath: String, proof: Data, publicInput: Data) throws -> Bool {
return try FfiConverterBool.lift(rustCallWithError(FfiConverterTypeMoproError.lift) {
uniffi_mopro_bindings_fn_func_verify_circom_proof(
FfiConverterString.lower(zkeyPath),
FfiConverterData.lower(proof),
FfiConverterData.lower(publicInput), $0
)
})
}
public func verifyHalo2Proof(srsPath: String, vkPath: String, proof: Data, publicInput: Data) throws -> Bool {
return try FfiConverterBool.lift(rustCallWithError(FfiConverterTypeMoproError.lift) {
uniffi_mopro_bindings_fn_func_verify_halo2_proof(
FfiConverterString.lower(srsPath),
FfiConverterString.lower(vkPath),
FfiConverterData.lower(proof),
FfiConverterData.lower(publicInput), $0
)
})
}
private enum InitializationResult {
case ok
case contractVersionMismatch
case apiChecksumMismatch
}
// Use a global variable to perform the versioning checks. Swift ensures that
// the code inside is only computed once.
private var initializationResult: InitializationResult = {
// Get the bindings contract version from our ComponentInterface
let bindings_contract_version = 26
// Get the scaffolding contract version by calling the into the dylib
let scaffolding_contract_version = ffi_mopro_bindings_uniffi_contract_version()
if bindings_contract_version != scaffolding_contract_version {
return InitializationResult.contractVersionMismatch
}
if uniffi_mopro_bindings_checksum_func_generate_circom_proof() != 54365 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_mopro_bindings_checksum_func_generate_halo2_proof() != 3963 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_mopro_bindings_checksum_func_to_ethereum_inputs() != 64747 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_mopro_bindings_checksum_func_to_ethereum_proof() != 64531 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_mopro_bindings_checksum_func_verify_circom_proof() != 46591 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_mopro_bindings_checksum_func_verify_halo2_proof() != 56128 {
return InitializationResult.apiChecksumMismatch
}
return InitializationResult.ok
}()
private func uniffiEnsureInitialized() {
switch initializationResult {
case .ok:
break
case .contractVersionMismatch:
fatalError("UniFFI contract version mismatch: try cleaning and rebuilding your project")
case .apiChecksumMismatch:
fatalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
}
// swiftlint:enable all