-
Notifications
You must be signed in to change notification settings - Fork 8
/
lib.go
716 lines (598 loc) · 20.7 KB
/
lib.go
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
// A golang wrapper for challenge-bypass-ristretto
//
// NOTE all structs wrap raw C FFI pointers to memory allocated in rust.
//
// Finalizers to deallocate memory are assigned immediately following
// internal calls to any rust functions which allocate memory.
//
// As a result it is CRITICAL that you DO NOT COPY ANY STRUCT from this package.
//
// To help enforce this behavior, a "noCopy" struct has been embedded which will cause a
// `go vet` error if copied. Do yourself a favor and set up your favorite meta linter early.
package challengebypassristrettoffi
/*
#cgo LDFLAGS: -L target/x86_64-unknown-linux-musl/debug -lchallenge_bypass_ristretto_ffi
#include "src/lib.h"
*/
import "C"
import (
"runtime"
"unsafe"
"github.com/pkg/errors"
)
// noCopy is embedded into structs which must not be copied.
// Copying a struct with embedded noCopy will result in a `go vet` error.
//
// See https://github.com/golang/go/issues/8005
type noCopy struct{}
func (*noCopy) Lock() {}
func (*noCopy) Unlock() {}
func wrapLastError(msg string) error {
orig := C.last_error_message()
if orig == nil {
return errors.New(msg)
}
defer C.c_char_destroy(orig)
return errors.Wrap(errors.New(C.GoString(orig)), msg)
}
// TokenPreimage is a slice of bytes which can be hashed to a `RistrettoPoint`.
type TokenPreimage struct {
raw *C.C_TokenPreimage
noCopy noCopy
}
func tokenPreimageFinalizer(t *TokenPreimage) {
C.token_preimage_destroy(t.raw)
}
// MarshalText marshalls the token preimage into text.
func (t *TokenPreimage) MarshalText() ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
encoded := C.token_preimage_encode_base64(t.raw)
if encoded == nil {
return nil, wrapLastError("Failed to encode token preimage")
}
defer C.c_char_destroy(encoded)
return []byte(C.GoString(encoded)), nil
}
// UnmarshalText unmarshalls the token preimage from text.
func (t *TokenPreimage) UnmarshalText(bytes []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.token_preimage_decode_base64((*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return wrapLastError("Failed to decode token preimage")
}
*t = TokenPreimage{raw: raw}
runtime.SetFinalizer(t, tokenPreimageFinalizer)
return nil
}
// Token consists of a randomly chosen preimage and blinding factor.
type Token struct {
raw *C.C_Token
noCopy noCopy
}
func tokenFinalizer(t *Token) {
C.token_destroy(t.raw)
t.raw = nil
}
// RandomToken generates a new random `Token` using the os random number generator.
func RandomToken() (*Token, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.token_random()
if raw == nil {
return nil, wrapLastError("Failed to generate token")
}
tok := &Token{raw: raw}
runtime.SetFinalizer(tok, tokenFinalizer)
return tok, nil
}
// Blind the Token, returning a BlindedToken to be sent to the server.
func (t *Token) Blind() *BlindedToken {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.token_blind(t.raw)
if raw == nil {
panic(wrapLastError("Failed to blind token"))
}
tok := &BlindedToken{raw: raw}
runtime.SetFinalizer(tok, blindedTokenFinalizer)
return tok
}
// MarshalText marshalls the token into text.
func (t *Token) MarshalText() ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
encoded := C.token_encode_base64(t.raw)
if encoded == nil {
return nil, wrapLastError("Failed to encode token")
}
defer C.c_char_destroy(encoded)
return []byte(C.GoString(encoded)), nil
}
// UnmarshalText unmarshalls the token from text.
func (t *Token) UnmarshalText(bytes []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.token_decode_base64((*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return wrapLastError("Failed to decode token")
}
*t = Token{raw: raw}
runtime.SetFinalizer(t, tokenFinalizer)
return nil
}
// BlindedToken is sent to the server for signing.
type BlindedToken struct {
raw *C.C_BlindedToken
noCopy noCopy
}
func blindedTokenFinalizer(t *BlindedToken) {
C.blinded_token_destroy(t.raw)
t.raw = nil
}
// MarshalText marshalls the blinded token into text.
func (t *BlindedToken) MarshalText() ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
encoded := C.blinded_token_encode_base64(t.raw)
if encoded == nil {
return nil, wrapLastError("Failed to encode blinded token")
}
defer C.c_char_destroy(encoded)
return []byte(C.GoString(encoded)), nil
}
// UnmarshalText unmarshalls the blinded token from text.
func (t *BlindedToken) UnmarshalText(bytes []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.blinded_token_decode_base64((*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return wrapLastError("Failed to decoded blinded token")
}
*t = BlindedToken{raw: raw}
runtime.SetFinalizer(t, blindedTokenFinalizer)
return nil
}
//SignedToken is the result of signing a BlindedToken.
type SignedToken struct {
raw *C.C_SignedToken
noCopy noCopy
}
func signedTokenFinalizer(t *SignedToken) {
C.signed_token_destroy(t.raw)
t.raw = nil
}
// MarshalText marshalls the signed token into text.
func (t *SignedToken) MarshalText() ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
encoded := C.signed_token_encode_base64(t.raw)
if encoded == nil {
return nil, wrapLastError("Failed to encode signed token")
}
defer C.c_char_destroy(encoded)
return []byte(C.GoString(encoded)), nil
}
// UnmarshalText unmarshalls the signed token from text.
func (t *SignedToken) UnmarshalText(bytes []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.signed_token_decode_base64((*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return wrapLastError("Failed to decode signed token")
}
*t = SignedToken{raw: raw}
runtime.SetFinalizer(t, signedTokenFinalizer)
return nil
}
// SigningKey is used to sign a BlindedToken and verify an UnblindedToken.
//
// This is a server secret and should NEVER be revealed to the client.
type SigningKey struct {
raw *C.C_SigningKey
noCopy noCopy
}
func signingKeyFinalizer(k *SigningKey) {
C.signing_key_destroy(k.raw)
k.raw = nil
}
// Sign the provided BlindedToken.
func (k *SigningKey) Sign(t *BlindedToken) (*SignedToken, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.signing_key_sign(k.raw, t.raw)
if raw == nil {
return nil, wrapLastError("Failed to sign token")
}
tok := &SignedToken{raw: raw}
runtime.SetFinalizer(tok, signedTokenFinalizer)
return tok, nil
}
// RederiveUnblindedToken via the token preimage of the provided UnblindedToken
func (k *SigningKey) RederiveUnblindedToken(t *TokenPreimage) *UnblindedToken {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.signing_key_rederive_unblinded_token(k.raw, t.raw)
if raw == nil {
panic(wrapLastError("Failed to rederive unblinded token"))
}
tok := &UnblindedToken{raw: raw}
runtime.SetFinalizer(tok, unblindedTokenFinalizer)
return tok
}
// RandomSigningKey generates a new random `SigningKey` using the os random number generator.
func RandomSigningKey() (*SigningKey, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.signing_key_random()
if raw == nil {
return nil, wrapLastError("Failed to generate signing key")
}
key := &SigningKey{raw: raw}
runtime.SetFinalizer(key, signingKeyFinalizer)
return key, nil
}
// MarshalText marshalls the signing key into text.
func (k *SigningKey) MarshalText() ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
encoded := C.signing_key_encode_base64(k.raw)
if encoded == nil {
return nil, wrapLastError("Failed to encode signing key")
}
defer C.c_char_destroy(encoded)
return []byte(C.GoString(encoded)), nil
}
// UnmarshalText unmarshalls the signing key from text.
func (k *SigningKey) UnmarshalText(bytes []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.signing_key_decode_base64((*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return wrapLastError("Failed to decode signing key")
}
*k = SigningKey{raw: raw}
runtime.SetFinalizer(k, signingKeyFinalizer)
return nil
}
// PublicKey returns the public key associated with this SigningKey
func (k *SigningKey) PublicKey() *PublicKey {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.signing_key_get_public_key(k.raw)
if raw == nil {
panic(wrapLastError("Failed to get public key for signing key"))
}
pub := &PublicKey{raw: raw}
runtime.SetFinalizer(pub, publicKeyFinalizer)
return pub
}
// UnblindedToken is the result of unblinding a SignedToken.
type UnblindedToken struct {
raw *C.C_UnblindedToken
noCopy noCopy
}
func unblindedTokenFinalizer(t *UnblindedToken) {
C.unblinded_token_destroy(t.raw)
t.raw = nil
}
// DeriveVerificationKey for this particular UnblindedToken
func (t *UnblindedToken) DeriveVerificationKey() *VerificationKey {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.unblinded_token_derive_verification_key_sha512(t.raw)
if raw == nil {
panic(wrapLastError("Failed to derive verification key"))
}
key := &VerificationKey{raw: raw}
runtime.SetFinalizer(key, verificationKeyFinalizer)
return key
}
// Preimage returns the TokenPreimage for this particular UnblindedToken
func (t *UnblindedToken) Preimage() *TokenPreimage {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.unblinded_token_preimage(t.raw)
if raw == nil {
panic(wrapLastError("Failed to get token preimage for unblinded token"))
}
tok := &TokenPreimage{raw: raw}
runtime.SetFinalizer(tok, tokenPreimageFinalizer)
return tok
}
// MarshalText marshalls the unblinded token into text.
func (t *UnblindedToken) MarshalText() ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
encoded := C.unblinded_token_encode_base64(t.raw)
if encoded == nil {
return nil, wrapLastError("Failed to encode unblinded token")
}
defer C.c_char_destroy(encoded)
return []byte(C.GoString(encoded)), nil
}
// UnmarshalText unmarshalls the unblinded token from text.
func (t *UnblindedToken) UnmarshalText(bytes []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.unblinded_token_decode_base64((*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return wrapLastError("Failed to decode unblinded token")
}
*t = UnblindedToken{raw: raw}
runtime.SetFinalizer(t, unblindedTokenFinalizer)
return nil
}
// VerificationKey is the shared key for proving / verifying the validity of an UnblindedToken.
type VerificationKey struct {
raw *C.C_VerificationKey
noCopy noCopy
}
func verificationKeyFinalizer(k *VerificationKey) {
C.verification_key_destroy(k.raw)
k.raw = nil
}
// Sign a message, producing a VerificationSignature
func (k *VerificationKey) Sign(message string) (*VerificationSignature, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
bytes := []byte(message)
raw := C.verification_key_sign_sha512(k.raw, (*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return nil, wrapLastError("Failed to sign message")
}
sig := &VerificationSignature{raw: raw}
runtime.SetFinalizer(sig, verificationSignatureFinalizer)
return sig, nil
}
// Verify that the signature of a message matches the provided `VerificationSignature`
func (k *VerificationKey) Verify(sig *VerificationSignature, message string) (bool, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
bytes := []byte(message)
result := C.verification_key_invalid_sha512(k.raw, sig.raw, (*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if result < 0 {
return false, wrapLastError("Failed to verify message signature")
}
return result == 0, nil
}
// VerificationSignature which can be verified given the VerificationKey and message
type VerificationSignature struct {
raw *C.C_VerificationSignature
noCopy noCopy
}
func verificationSignatureFinalizer(s *VerificationSignature) {
C.verification_signature_destroy(s.raw)
s.raw = nil
}
// MarshalText marshalls the verification signature into text.
func (t *VerificationSignature) MarshalText() ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
encoded := C.verification_signature_encode_base64(t.raw)
if encoded == nil {
return nil, wrapLastError("Failed to encode verification signature")
}
defer C.c_char_destroy(encoded)
return []byte(C.GoString(encoded)), nil
}
// UnmarshalText unmarshalls the unblinded token from text.
func (t *VerificationSignature) UnmarshalText(bytes []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.verification_signature_decode_base64((*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return wrapLastError("Failed to decode verification signature")
}
*t = VerificationSignature{raw: raw}
runtime.SetFinalizer(t, verificationSignatureFinalizer)
return nil
}
// PublicKey is a committment by the server to a particular SigningKey.
type PublicKey struct {
raw *C.C_PublicKey
noCopy noCopy
}
func publicKeyFinalizer(k *PublicKey) {
C.public_key_destroy(k.raw)
k.raw = nil
}
// MarshalText marshalls the verification signature into text.
func (t *PublicKey) MarshalText() ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
encoded := C.public_key_encode_base64(t.raw)
if encoded == nil {
return nil, wrapLastError("Failed to encode public key")
}
defer C.c_char_destroy(encoded)
return []byte(C.GoString(encoded)), nil
}
// UnmarshalText unmarshalls the unblinded token from text.
func (t *PublicKey) UnmarshalText(bytes []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.public_key_decode_base64((*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return wrapLastError("Failed to decode public key")
}
*t = PublicKey{raw: raw}
runtime.SetFinalizer(t, publicKeyFinalizer)
return nil
}
// DLEQProof shows a point was signed by the same signing key as a particular PublicKey
type DLEQProof struct {
raw *C.C_DLEQProof
noCopy noCopy
}
func dleqProofFinalizer(p *DLEQProof) {
C.dleq_proof_destroy(p.raw)
p.raw = nil
}
// NewDLEQProof showing SignedToken is the result of signing BlindedToken with the given SigningKey
func NewDLEQProof(blindedToken *BlindedToken, signedToken *SignedToken, key *SigningKey) (*DLEQProof, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.dleq_proof_new(blindedToken.raw, signedToken.raw, key.raw)
if raw == nil {
return nil, wrapLastError("Failed to create new DLEQ proof")
}
proof := &DLEQProof{raw: raw}
runtime.SetFinalizer(proof, dleqProofFinalizer)
return proof, nil
}
// Verify that the DLEQProof shows the SignedToken is BlindedToken signed by the same SigningKey as PublicKey
func (proof *DLEQProof) Verify(blindedToken *BlindedToken, signedToken *SignedToken, publicKey *PublicKey) (bool, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
result := C.dleq_proof_invalid(proof.raw, blindedToken.raw, signedToken.raw, publicKey.raw)
if result < 0 {
return false, wrapLastError("Failed to verify DLEQ proof")
}
return result == 0, nil
}
// MarshalText marshalls the verification signature into text.
func (proof *DLEQProof) MarshalText() ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
encoded := C.dleq_proof_encode_base64(proof.raw)
if encoded == nil {
return nil, wrapLastError("Failed to encode DLEQ proof")
}
defer C.c_char_destroy(encoded)
return []byte(C.GoString(encoded)), nil
}
// UnmarshalText unmarshalls the unblinded token from text.
func (proof *DLEQProof) UnmarshalText(bytes []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.dleq_proof_decode_base64((*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return wrapLastError("Failed to decode DLEQ proof")
}
*proof = DLEQProof{raw: raw}
runtime.SetFinalizer(proof, dleqProofFinalizer)
return nil
}
// BatchDLEQProof shows many points were signed by the same signing key as a particular PublicKey
type BatchDLEQProof struct {
raw *C.C_BatchDLEQProof
noCopy noCopy
}
func batchDleqProofFinalizer(p *BatchDLEQProof) {
C.batch_dleq_proof_destroy(p.raw)
p.raw = nil
}
// NewBatchDLEQProof showing each SignedToken is the result of signing the corresponding BlindedToken with the
// given SigningKey
func NewBatchDLEQProof(blindedTokens []*BlindedToken, signedTokens []*SignedToken, key *SigningKey) (*BatchDLEQProof, error) {
if len(blindedTokens) != len(signedTokens) {
return nil, errors.New("Length of blinded and signed tokens must match")
}
cBlindedTokens := make([]*C.C_BlindedToken, len(blindedTokens), len(blindedTokens))
for k, v := range blindedTokens {
cBlindedTokens[k] = v.raw
}
cSignedTokens := make([]*C.C_SignedToken, len(signedTokens), len(signedTokens))
for k, v := range signedTokens {
cSignedTokens[k] = v.raw
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.batch_dleq_proof_new(
(**C.C_BlindedToken)(unsafe.Pointer(&cBlindedTokens[0])),
(**C.C_SignedToken)(unsafe.Pointer(&cSignedTokens[0])),
C.int(len(cBlindedTokens)), key.raw)
if raw == nil {
return nil, wrapLastError("Failed to create new DLEQ proof")
}
proof := &BatchDLEQProof{raw: raw}
runtime.SetFinalizer(proof, batchDleqProofFinalizer)
return proof, nil
}
// Verify that the BatchDLEQProof shows each SignedToken is a BlindedToken signed by the same SigningKey as PublicKey
func (proof *BatchDLEQProof) Verify(blindedTokens []*BlindedToken, signedTokens []*SignedToken, publicKey *PublicKey) (bool, error) {
if len(blindedTokens) != len(signedTokens) {
return false, errors.New("Blinded tokens and signed tokens must have same length")
}
cBlindedTokens := make([]*C.C_BlindedToken, len(blindedTokens), len(blindedTokens))
for k, v := range blindedTokens {
cBlindedTokens[k] = v.raw
}
cSignedTokens := make([]*C.C_SignedToken, len(signedTokens), len(signedTokens))
for k, v := range signedTokens {
cSignedTokens[k] = v.raw
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
result := C.batch_dleq_proof_invalid(proof.raw,
(**C.C_BlindedToken)(unsafe.Pointer(&cBlindedTokens[0])),
(**C.C_SignedToken)(unsafe.Pointer(&cSignedTokens[0])),
C.int(len(cBlindedTokens)), publicKey.raw)
if result < 0 {
return false, wrapLastError("Error duing batch DLEQ proof verification")
}
return result == 0, nil
}
// VerifyAndUnblind each SignedToken if the BatchDLEQProof is valid
func (proof *BatchDLEQProof) VerifyAndUnblind(tokens []*Token, blindedTokens []*BlindedToken, signedTokens []*SignedToken, publicKey *PublicKey) ([]*UnblindedToken, error) {
if len(tokens) != len(signedTokens) || len(blindedTokens) != len(signedTokens) {
return nil, errors.New("Blinded tokens and signed tokens must have same length")
}
cTokens := make([]*C.C_Token, len(tokens), len(tokens))
for k, v := range tokens {
cTokens[k] = v.raw
}
cBlindedTokens := make([]*C.C_BlindedToken, len(blindedTokens), len(blindedTokens))
for k, v := range blindedTokens {
cBlindedTokens[k] = v.raw
}
cSignedTokens := make([]*C.C_SignedToken, len(signedTokens), len(signedTokens))
for k, v := range signedTokens {
cSignedTokens[k] = v.raw
}
cUnblindedTokens := make([]*C.C_UnblindedToken, len(tokens), len(tokens))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
result := C.batch_dleq_proof_invalid_or_unblind(proof.raw,
(**C.C_Token)(unsafe.Pointer(&cTokens[0])),
(**C.C_BlindedToken)(unsafe.Pointer(&cBlindedTokens[0])),
(**C.C_SignedToken)(unsafe.Pointer(&cSignedTokens[0])),
(**C.C_UnblindedToken)(unsafe.Pointer(&cUnblindedTokens[0])),
C.int(len(cBlindedTokens)), publicKey.raw)
if result < 0 {
return nil, wrapLastError("Error duing batch DLEQ proof verification")
} else if result > 0 {
return nil, errors.New("Invalid proof")
}
unblindedTokens := make([]*UnblindedToken, len(tokens), len(tokens))
for k, v := range cUnblindedTokens {
unblindedTokens[k] = &UnblindedToken{raw: v}
runtime.SetFinalizer(unblindedTokens[k], unblindedTokenFinalizer)
}
return unblindedTokens, nil
}
// MarshalText marshalls the verification signature into text.
func (proof *BatchDLEQProof) MarshalText() ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
encoded := C.batch_dleq_proof_encode_base64(proof.raw)
if encoded == nil {
return nil, wrapLastError("Failed to encode batch DLEQ proof")
}
defer C.c_char_destroy(encoded)
return []byte(C.GoString(encoded)), nil
}
// UnmarshalText unmarshalls the unblinded token from text.
func (proof *BatchDLEQProof) UnmarshalText(bytes []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
raw := C.batch_dleq_proof_decode_base64((*C.uint8_t)(&bytes[0]), C.size_t(len(bytes)))
if raw == nil {
return wrapLastError("Failed to decode batch DLEQ proof")
}
*proof = BatchDLEQProof{raw: raw}
runtime.SetFinalizer(proof, batchDleqProofFinalizer)
return nil
}