-
Notifications
You must be signed in to change notification settings - Fork 4
/
fabmorph.go
973 lines (777 loc) · 28.6 KB
/
fabmorph.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
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
//////////////////////////////////////
// THE PAILLIER EXPERIMENT ////
//////////////////////////////////////
/*
This is the fabmorph, a chaincode that runs in Hyperledger Fabric 1.4.
He was created as part of the Paillier Experiment. You can invoke its methods
to send an encrypted energy measurement record to the ledger, or to retrieve such
information. It can also implement accumalative sums with the measurements using
Paillier cryptosystem. Please check the code to find more information about how
use it.
@author: Wilson S. Melo Jr.
@date: Apr/2019
*/
package main
import (
//the majority of the imports are trivial...
"bytes"
"fmt"
"strconv"
"strings"
"crypto/rand"
"math/big"
"errors"
"io"
"time"
"encoding/json"
//these imports are for Hyperledger Fabric interface
"github.com/hyperledger/fabric/core/chaincode/shim"
sc "github.com/hyperledger/fabric/protos/peer"
)
////////////////////////////////////////
//////// GOMORPH INSERTION CODE ////////
////////////////////////////////////////
/* All the following functions belong to the Gomorph Library. We insert all the code into the chaincode
main package because it was easier than make Hyperledger Fabric to accept external packages.
Gomorph is available at https://github.com/radicalrafi/gomorph.
Paillier cryptosystem implementation
Provides primitives for Public & Private Key Generation / Encryption / Decryption
Provides Functions to operate on the Cyphertext according to Paillier algorithm
@author: radicalrafi
@license: Apache 2.0
*/
/* The Paillier crypto system picks two keys p & q and denotes n = p*q
Messages have to be in the ring Z/nZ (integers modulo n)
Therefore a Message can't be bigger than n
*/
var ErrLongMessage = errors.New("Gaillier Error #1: Message is too long for The Public-Key Size \n Message should be smaller than Key size you choose")
//constants
var one = big.NewInt(1)
//Key structs
type PubKey struct {
KeyLen int
N *big.Int //n = p*q (where p & q are two primes)
G *big.Int //g random integer in Z\*\n^2
Nsq *big.Int //N^2
}
type PrivKey struct {
KeyLen int
PubKey
L *big.Int //lcm((p-1)*(q-1))
U *big.Int //L^-1 modulo n mu = U = (L(g^L mod N^2)^-1)
}
func GenerateKeyPair(random io.Reader, bits int) (*PubKey, *PrivKey, error) {
p, err := rand.Prime(random, bits/2)
if err != nil {
return nil, nil, err
}
q, err := rand.Prime(random, bits/2)
if err != nil {
return nil, nil, err
}
//N = p*q
n := new(big.Int).Mul(p, q)
nSq := new(big.Int).Mul(n, n)
g := new(big.Int).Add(n, one)
//p-1
pMin := new(big.Int).Sub(p, one)
//q-1
qMin := new(big.Int).Sub(q, one)
//(p-1)*(q-1)
l := new(big.Int).Mul(pMin, qMin)
//l^-1 mod n
u := new(big.Int).ModInverse(l, n)
pub := &PubKey{KeyLen: bits, N: n, Nsq: nSq, G: g}
return pub, &PrivKey{PubKey: *pub, KeyLen: bits, L: l, U: u}, nil
}
/*
Encrypt :function to encrypt the message into a paillier cipher text
using the following rule :
cipher = g^m * r^n mod n^2
* r is random integer such as 0 <= r <= n
* m is the message
*/
func Encrypt(pubkey *PubKey, message []byte) ([]byte, error) {
r, err := rand.Prime(rand.Reader, pubkey.KeyLen)
if err != nil {
return nil, err
}
m := new(big.Int).SetBytes(message)
if pubkey.N.Cmp(m) < 1 {
return nil, ErrLongMessage
}
//c = g^m * r^nmod n^2
//g^m
gm := new(big.Int).Exp(pubkey.G, m, pubkey.Nsq)
//r^n
rn := new(big.Int).Exp(r, pubkey.N, pubkey.Nsq)
//prod = g^m * r^n
prod := new(big.Int).Mul(gm, rn)
c := new(big.Int).Mod(prod, pubkey.Nsq)
return c.Bytes(), nil
}
/*
Decrypts a given ciphertext following the rule:
m = L(c^lambda mod n^2).mu mod n
* lambda : L
* mu : U
*/
func Decrypt(privkey *PrivKey, cipher []byte) ([]byte, error) {
c := new(big.Int).SetBytes(cipher)
if privkey.Nsq.Cmp(c) < 1 {
return nil, ErrLongMessage
}
//c^l mod n^2
a := new(big.Int).Exp(c, privkey.L, privkey.Nsq)
//L(x) = x-1 / n we compute L(a)
l := new(big.Int).Div(new(big.Int).Sub(a, one), privkey.N)
//computing m
m := new(big.Int).Mod(new(big.Int).Mul(l, privkey.U), privkey.N)
return m.Bytes(), nil
}
/*
Homomorphic Properties of Paillier Cryptosystem
* The product of two ciphers decrypts to the sum of the plain text
* The product of a cipher with a non-cipher raising g will decrypt to their sum
* A Cipher raised to a non-cipher decrypts to their product
* Any cipher raised to an integer k will decrypt to the product of the deciphered and k
*/
//Add two ciphers together
func Add(pubkey *PubKey, c1, c2 []byte) []byte {
a := new(big.Int).SetBytes(c1)
b := new(big.Int).SetBytes(c2)
// a * b mod n^²
res := new(big.Int).Mod(new(big.Int).Mul(a, b), pubkey.Nsq)
return res.Bytes()
}
//Add a constant & a cipher
func AddConstant(pubkey *PubKey, cipher, constant []byte) []byte {
c := new(big.Int).SetBytes(cipher)
k := new(big.Int).SetBytes(constant)
//result = c * g^k mod n^2
res := new(big.Int).Mod(
new(big.Int).Mul(c, new(big.Int).Exp(pubkey.G, k, pubkey.Nsq)), pubkey.Nsq)
return res.Bytes()
}
//Multiplication by a constant integer
func Mul(pubkey *PubKey, cipher, constant []byte) []byte {
c := new(big.Int).SetBytes(cipher)
k := new(big.Int).SetBytes(constant)
//res = c^k mod n^2
res := new(big.Int).Exp(c, k, pubkey.Nsq)
return res.Bytes()
}
////////////////////////////////////////
//////// GOMORPH CODE ENDS HERE ////////
////////////////////////////////////////
//////////////////////////////////////
//////// FABMORPH - CHAINCODE ////////
//////////////////////////////////////
/* All the following functions are used to implemetn fabmorph chaincode. This chaincode
basically works with 2 scenarios:
1) A measurement instrument sends measures in plain text to the blockchain. The smartcontract
applies homomorphic cryptography and stores encrypted measures. The client can recover it by
reading the encrypted value and decrypting locally.
2) A measurement instrument sends encrypted measures. The smartcontract applies homomorphic
operations to obtain the (encrypted) total consumption value and to store it in the ledger.
The client can it by reading the encrypted value and decrypting locally.
@author: Wilson S. Melo Jr.
@date: Apr/2019
*/
/*
* This is an auxiliar function to convert the Pallier public key struct into a string.
* It is necessary to lead with the object in Fabric transactions.
* The key in string format will have the 4 fields in the following order: KeyLen, N, G, Nsq
*/
func PubKey2String(pubkey *PubKey) (string){
return strconv.Itoa(pubkey.KeyLen) + "," + pubkey.N.String() + "," + pubkey.G.String() + "," + pubkey.Nsq.String()
}
/*
* This is an auxiliar function to convert a string key into the Pallier public key struct.
* It is necessary to lead with the object in Fabric transactions.
* The key in string format MUST be formated with 4 comma-separate substrings, that correspond to the
* respective fields: KeyLen, N, G, Nsq.
* Example:
* "512,9317708529424897702020382930116702407110920461631622943398410342176567591986029379418525445868266
642035599843718252107477949966673217711651921453503121487,9317708529424897702020382930116702407110920
46163162294339841034217656759198602937941852544586826664203559984371825210747794996667321771165192145
3503121488,868196922393174897253161831767710200925181500218099202121385823388772389324857444497325510
95045029334111747236626667783012791667259632343428969631355566392144138916566553732073336450909522598
17833785123113998363351341374266805275045098714823061322751114829098157258427086799436922320373510493
4252452681091169"
* which means:
KeyLen = 512
N = 9317708529424897702020382930116702407110920461631622943398410342176567591986029379418525445868266
642035599843718252107477949966673217711651921453503121487
G = 9317708529424897702020382930116702407110920461631622943398410342176567591986029379418525445868266
642035599843718252107477949966673217711651921453503121488
Nsq = 86819692239317489725316183176771020092518150021809920212138582338877238932485744449732551095045
02933411174723662666778301279166725963234342896963135556639214413891656655373207333645090952259817833
78512311399836335134137426680527504509871482306132275111482909815725842708679943692232037351049342524
52681091169
*/
func String2PubKey(pubkey string) (*PubKey){
//extract tokens from pubkey string to a vector
strvec := strings.Split(pubkey, ",")
if len(strvec) != 4 {
return nil
}
//get the key lenght
l, err := strconv.Atoi(strvec[0]);
n, res := new(big.Int).SetString(strvec[1],10)
g, res := new(big.Int).SetString(strvec[2],10)
nSq, res := new(big.Int).SetString(strvec[3],10)
if err == nil && res {
//compose public key using values informed in the string pubkey
return &PubKey{KeyLen: l, N: n, G: g, Nsq: nSq}
} else {
return nil
}
}
/*
* This is an auxiliar function that encapsulates de conversion of a vetor of bytes
* representing a big.Int number into a literal transcription in string format.
*/
func Bytes2String(bcipher []byte) (string){
//creates an auxiliar big.Int to manipulate the string
cipherInt := new(big.Int).SetBytes(bcipher)
return cipherInt.String()
}
/*
* This is an auxiliar function that encapsulates de conversion of a string with a
* literal representation of a big.Int number (base 10) into a vector of bytes in the
* format required by Gomorph library.
*/
func String2Bytes(cipher string) ([]byte){
//creates an auxiliar big.Int to manipulate the string
cipherInt, err := new(big.Int).SetString(cipher,10)
if err {
return cipherInt.Bytes()
} else {
return nil
}
}
/*
* SmartContract defines the chaincode base structure. All the methods are implemented to
* return a SmartContrac type.
*/
type SmartContract struct {
}
/*
Meter constitutes our key|value struct (digital asset) and implements a single
record to manage the
meter public key and measures. All blockchain transactions operates with this type.
IMPORTANT: all the field names must start with upper case
*/
type Meter struct {
PublicKey string `json:"publickey"`
PlainMeasure int64 `json:"plainmeasure"`
EncrypMeasure string `json:"encrypmeasure"`
}
/*
* The Init method is called when the Smart Contract "fabmorph" is instantiated.
* Best practice is to have any Ledger initialization in separate function.
* Note that chaincode upgrade also calls this function to reset
* or to migrate data, so be careful to avoid a scenario where you
* inadvertently clobber your ledger's data!
*/
func (s *SmartContract) Init(stub shim.ChaincodeStubInterface) sc.Response {
return shim.Success(nil)
}
/*
* The invoke function is called on each transaction invoking the chaincode. It
* follows a structure of switching calls, so each valid feature need to
* have a proper entry-point.
*/
func (s *SmartContract) Invoke(stub shim.ChaincodeStubInterface) sc.Response {
// extract the function name and args from the transaction proposal
fn, args := stub.GetFunctionAndParameters()
//implements a switch for each acceptable function
if fn == "registerMeter" {
//registers a new meter into the ledger
return s.registerMeter(stub, args)
} else if fn == "insertMeasurement" {
//inserts a measurement which increases the meter consumption counter. The measurement
return s.insertMeasurement(stub, args)
} else if fn == "insertPlainTextMeasurement" {
//inserts a measurement which increases the meter consumption counter. The measurement
return s.insertPlainTextMeasurement(stub, args)
} else if fn == "getConsumption" {
//retrieves the accumulated consumption
return s.getConsumption(stub, args)
} else if fn == "sleepTest" {
//retrieves the accumulated consumption
return s.sleepTest(stub, args)
} else if fn == "countHistory" {
//look for a specific fill up record and brings its changing history
return s.countHistory(stub, args)
} else if fn == "countLedger" {
//look for a specific fill up record and brings its changing history
return s.countLedger(stub)
} else if fn == "queryLedger" {
//execute a CouchDB query, args must include query expression
return s.queryLedger(stub, args)
}
//function fn not implemented, notify error
return shim.Error("Chaincode do not support this function")
}
/*
SmartContract::registerMeter(...)
Does the register of a new meter into the ledger. The meter is the base of the key|value structure.
The key constitutes the meter ID. If a cryptographic public key is informed, it indicates that
the meter accumulates its measurements using homomorphic cryptography.
The vector args[] must contain two parameters:
- args[0] - meter ID
- args[1] - the Paillier public key associated with the meter
- args[2] - (optional) initial encrypted measurement value.
*/
func (s *SmartContract) registerMeter(stub shim.ChaincodeStubInterface, args []string) sc.Response {
//validate args vector lenght
if !(len(args) == 2 || len(args) == 3) {
return shim.Error("It was expected the parameters: <meter id> <public key> [encrypted inital consumption]")
}
//gets the parameters associated with the meter ID and the public key (in string format)
meterid := args[0]
strpubkey := args[1]
//create a var to store the encrypted initial consumption
encrypMeasureString := ""
//tests if the encrypted inital consumption was provided
if len(args) == 3 {
//initiates the value of the encrypted measurement
encrypMeasureString = args[2]
}
fmt.Println("Value of encrypted measurement: (", encrypMeasureString, ")")
//defines the initial consumption as zero
plainMeasure := new(big.Int).SetInt64(0)
//test if strpubkey is a empty string
if len(strpubkey) > 0 {
//there is a public key, so we need to encrypt the measurement
pubkey := String2PubKey(strpubkey)
if pubkey != nil && encrypMeasureString == "" {
//encrypts the initial consumption using initial value in plainMeasure
encrypMeasure, err := Encrypt(pubkey, plainMeasure.Bytes())
//test if encryptation had success
if err != nil {
return shim.Error("Error on encrypting with the informed public key")
}
encrypMeasureString = Bytes2String(encrypMeasure)
fmt.Println("I encrypted the measurement: (", encrypMeasureString, ")")
}
}
//creates the meter record
var meter = Meter{PublicKey: strpubkey, PlainMeasure: plainMeasure.Int64(), EncrypMeasure: encrypMeasureString}
//encapsulates meter in a JSON structure
meterAsBytes, _ := json.Marshal(meter)
//registers meter in the ledger
stub.PutState(meterid, meterAsBytes)
//loging...
fmt.Println("Registering meter: ", meter)
//notify procedure success
return shim.Success(nil)
}
/*
This method implements the insertion of encrypted measurements in the blockchain.
The encryptation must uses the same public key configured to the meter.
Notice that the informed measurement will be added (accumulated) to the the previous
encrypted measurement consumption information.
The vector args[] must contain two parameters:
- args[0] - meter ID
- args[1] - the encrypted measurement, in a string representing a big int number.
*/
func (s *SmartContract) insertMeasurement(stub shim.ChaincodeStubInterface, args []string) sc.Response {
//validate args vector lenght
if len(args) != 2 {
return shim.Error("It was expected 2 parameter: <meter ID> <measurement>")
}
//gets the parameter associated with the meter ID and the incremental measurement
meterid := args[0]
//try to convert the informed measurement into the format []byte, required by Gomorph
measurement := String2Bytes(args[1])
//check if we have success
if measurement == nil {
//measurement is not a proper number
return shim.Error("Error on veryfing measurement, it is not a proper input")
}
//retrive meter record
meterAsBytes, err := stub.GetState(meterid)
//test if we receive a valid meter ID
if err != nil || meterAsBytes == nil {
return shim.Error("Error on retrieving meter ID register")
}
//creates Meter struct to manipulate returned bytes
MyMeter := Meter{}
//convert bytes into a Meter object
json.Unmarshal(meterAsBytes, &MyMeter)
//convert meter public key to the format expected by Gomorph
pubkey := String2PubKey(MyMeter.PublicKey)
//compute the new measurement value by using homomorphic adding constant property
newEncrypMeasure := Add(pubkey, String2Bytes(MyMeter.EncrypMeasure), measurement)
//update encrypted measure cipher
MyMeter.EncrypMeasure = Bytes2String(newEncrypMeasure);
//encapsulates meter back into the JSON structure
newMeterAsBytes, _ := json.Marshal(MyMeter)
//update meter state in the ledger
stub.PutState(meterid, newMeterAsBytes)
//loging...
fmt.Println("Updating meter consumption:", MyMeter)
//notify procedure success
return shim.Success(nil)
}
/*
This method enables the insertion of measurements as plaintext information.
Its main intent is to provide a comparing in terms of performance with the
insertMeasurement() method.
The vector args[] must contain two parameters:
- args[0] - meter ID
- args[1] - the measurement value (as integer, float type is not supported)
*/
func (s *SmartContract) insertPlainTextMeasurement(stub shim.ChaincodeStubInterface, args []string) sc.Response {
//validate args vector lenght
if len(args) != 2 {
return shim.Error("It was expected 2 parameter: <meter ID> <measurement>")
}
//gets the parameter associated with the meter ID and the incremental measurement
meterid := args[0]
//try to convert the informed measurement into the format []byte, required by Gomorph
measurement, err := strconv.ParseInt(args[1], 10, 64)
//check if we have success
if err != nil {
//measurement is not a proper number
return shim.Error("Error on veryfing measurement, it is not a proper int64 input")
}
//retrive meter record
meterAsBytes, err := stub.GetState(meterid)
//test if we receive a valid meter ID
if err != nil || meterAsBytes == nil {
return shim.Error("Error on retrieving meter ID register")
}
//creates Meter struct to manipulate returned bytes
MyMeter := Meter{}
//convert bytes into a Meter object
json.Unmarshal(meterAsBytes, &MyMeter)
//update the plaintext measurement
MyMeter.PlainMeasure += measurement;
//encapsulates meter back into the JSON structure
newMeterAsBytes, _ := json.Marshal(MyMeter)
//update meter state in the ledger
stub.PutState(meterid, newMeterAsBytes)
//loging...
fmt.Println("Updating meter plaintext consumption:", MyMeter)
//notify procedure success
return shim.Success(nil)
}
/*
This method enables retrieves the consumption information from the ledger.
It returns a vector of bytes with the meter asset in JSON format.
The vector args[] must contain two parameters:
- args[0] - meter ID
*/
func (s *SmartContract) getConsumption(stub shim.ChaincodeStubInterface, args []string) sc.Response {
//validate args vector lenght
if len(args) != 1 {
return shim.Error("It was expected 1 parameter: <meter ID>")
}
//gets the parameter associated with the meter ID and the incremental measurement
meterid := args[0]
//retrive meter record
meterAsBytes, err := stub.GetState(meterid)
//test if we receive a valid meter ID
if err != nil || meterAsBytes == nil {
return shim.Error("Error on retrieving meter ID register")
}
//return payload with bytes related to the meter state
return shim.Success(meterAsBytes)
}
/*
This method is a dummy test that makes the endorser "sleep" for some seconds.
It is usefull to check either the sleeptime affects the performance of concurrent
transactions.
- args[0] - sleeptime (in seconds)
*/
func (s *SmartContract) sleepTest(stub shim.ChaincodeStubInterface, args []string) sc.Response {
//validate args vector lenght
if len(args) != 1 {
return shim.Error("It was expected 1 parameter: <sleeptime>")
}
//gets the parameter associated with the meter ID and the incremental measurement
sleeptime, err := strconv.Atoi(args[0])
//test if we receive a valid meter ID
if err != nil {
return shim.Error("Error on retrieving sleep time")
}
//tests if sleeptime is a valid value
if sleeptime > 0 {
//stops during sleeptime seconds
time.Sleep(time.Duration(sleeptime) * time.Second)
}
//return payload with bytes related to the meter state
return shim.Success(nil)
}
/*
This method brings the changing history of a specific meter asset. It can be useful to
query all the changes that happened with a meter value.
- args[0] - asset key (or meter ID)
*/
func (s *SmartContract) queryHistory(stub shim.ChaincodeStubInterface, args []string) sc.Response {
//validate args vector lenght
if len(args) != 1 {
return shim.Error("It was expected 1 parameter: <key>")
}
historyIer, err := stub.GetHistoryForKey(args[0])
//verifies if the history exists
if err != nil {
//fmt.Println(errMsg)
return shim.Error("Fail on getting ledger history")
}
// buffer is a JSON array containing records
var buffer bytes.Buffer
var counter = 0
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for historyIer.HasNext() {
//increments iterator
queryResponse, err := historyIer.Next()
if err != nil {
return shim.Error(err.Error())
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
//generates a formated result
buffer.WriteString("{\"Value\":")
buffer.WriteString("\"")
// Record is a JSON object, so we write as-is
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("\"")
buffer.WriteString(", \"Counter\":")
buffer.WriteString(strconv.Itoa(counter))
//buffer.WriteString(queryResponse.Timestamp)
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
//increases counter
counter++
}
buffer.WriteString("]")
historyIer.Close()
//loging...
fmt.Printf("Consulting ledger history, found %d\n records", counter)
//notify procedure success
return shim.Success(buffer.Bytes())
}
/*
This method brings the number of times that a meter asset was modified in the ledger.
It performs faster than queryHistory() method once it does not retrive any information,
it only counts the changes.
- args[0] - asset key (or meter ID)
*/
func (s *SmartContract) countHistory(stub shim.ChaincodeStubInterface, args []string) sc.Response {
//validate args vector lenght
if len(args) != 1 {
return shim.Error("It was expected 1 parameter: <key>")
}
historyIer, err := stub.GetHistoryForKey(args[0])
//verifies if the history exists
if err != nil {
//fmt.Println(errMsg)
return shim.Error("Fail on getting ledger history")
}
//creates a counter
var counter int64
counter = 0
for historyIer.HasNext() {
//increments iterator
_, err := historyIer.Next()
if err != nil {
return shim.Error(err.Error())
}
//increases counter
counter++
fmt.Printf("Consulting ledger history, found %d\n records", counter)
}
// buffer is a JSON array containing records
var buffer bytes.Buffer
buffer.WriteString("[")
buffer.WriteString("\"Counter\":")
buffer.WriteString(strconv.FormatInt(counter, 10))
buffer.WriteString("]")
historyIer.Close()
//loging...
fmt.Printf("Consulting ledger history, found %d\n records", counter)
//notify procedure success
return shim.Success(buffer.Bytes())
}
/*
This method counts the total of well succeeded transactions in the ledger.
*/
func (s *SmartContract) countLedger(stub shim.ChaincodeStubInterface) sc.Response {
//use a range of keys, assuming that the max key value is 999999,
resultsIterator, err := stub.GetStateByRange("0", "999999")
if err != nil {
return shim.Error(err.Error())
}
//defer iterator closes at the end of the function
defer resultsIterator.Close()
//creates a counter
var counter int64
var keys int64
counter = 0
keys = 0
//the interator checks all the valid keys
for resultsIterator.HasNext() {
//increments iterator
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
//busca historico da proxima key
historyIer, err := stub.GetHistoryForKey(queryResponse.Key)
//verifies if the history exists
if err != nil {
//fmt.Println(errMsg)
return shim.Error(err.Error())
}
defer historyIer.Close()
for historyIer.HasNext() {
//increments iterator
_, err := historyIer.Next()
if err != nil {
return shim.Error(err.Error())
}
//increases counter
counter++
}
fmt.Printf("Consulting ledger history, found key %d\n", queryResponse.Key)
keys++
}
// buffer is a JSON array containing records
var buffer bytes.Buffer
buffer.WriteString("[")
buffer.WriteString("\"Counter\":")
buffer.WriteString(strconv.FormatInt(counter, 10))
buffer.WriteString("\"Keys\":")
buffer.WriteString(strconv.FormatInt(keys, 10))
buffer.WriteString("]")
//loging...
fmt.Printf("Consulting ledger history, found %d transactions in %d keys\n", counter, keys)
//notify procedure success
return shim.Success(buffer.Bytes())
}
/*
This method executes a free query on the ledger, returning a vector of meter assets.
The query string must be a query expression supported by CouchDB servers.
- args[0] - query string.
*/
func (s *SmartContract) queryLedger(stub shim.ChaincodeStubInterface, args []string) sc.Response {
//validate args vector lenght
if len(args) != 1 {
return shim.Error("It was expected 1 parameter: <query string>")
}
//using auxiliar variable
queryString := args[0]
//loging...
fmt.Printf("Executing the following query: %s\n", queryString)
//try to execute query and obtain records iterator
resultsIterator, err := stub.GetQueryResult(queryString)
//test if iterator is valid
if err != nil {
return shim.Error(err.Error())
}
//defer iterator closes at the end of the function
defer resultsIterator.Close()
// buffer is a JSON array containing QueryRecords
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
//increments iterator
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
//generates a formated result
buffer.WriteString("{\"Key\":")
buffer.WriteString("\"")
buffer.WriteString(queryResponse.Key)
buffer.WriteString("\"")
buffer.WriteString(", \"Record\":")
// Record is a JSON object, so we write as-is
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
//loging...
fmt.Printf("Obtained the following fill up records: %s\n", buffer.String())
//notify procedure success
return shim.Success(buffer.Bytes())
}
/*
This method enables simple tests with the Paillier cryptosystem. Make yourserf
confortable to modify it as much as necessary.
*/
func TestPaillier() {
//set two big.Int number that will be the operands for homomorphic computing
case1 := new(big.Int).SetInt64(667) //you can assign any number
case2 := new(big.Int).SetInt64(971) //you can assign any number
//generate a Paillier key pair (remember of informing the correct size, e.g., 2048)
pub, priv, err := GenerateKeyPair(rand.Reader, 2048)
//assert the method execution
if err != nil {
fmt.Printf("Error Generating Keypair")
} else {
fmt.Printf("Key pair generate with success...\n")
}
//encrypt the operands
fmt.Printf("Starting encryption:\n")
encCase1, err1 := Encrypt(pub, case1.Bytes())
encCase2, err2 := Encrypt(pub, case2.Bytes())
//assert the method execution
if err1 != nil || err2 != nil {
fmt.Printf("Error Encrypting Integers")
} else {
fmt.Printf("%v -> %v\n", case1, encCase1)
fmt.Printf("%v -> %v\n", case2, encCase2)
}
//test Paillier addition
fmt.Printf("Testing the addition...\n")
res := Add(pub, encCase1, encCase2)
//log result
fmt.Printf("We got: %v\n",res)
//set the expected sum value
corr := case1.Add(case1,case2)
//decripts the sum result
fmt.Printf("Testing decryption...\n")
decRes, err := Decrypt(priv, res)
//assert the result
if err != nil {
fmt.Printf("Failed to Decrypt Result got %v want %v with Error : %v", decRes, corr, err)
}
//decryption succeeds, check the result
resB := new(big.Int).SetBytes(decRes)
//assert the result
if resB.Cmp(corr) != 0 {
fmt.Printf("Failed to Add two ciphers got %v want %v", resB, corr)
} else {
fmt.Printf("Decryption is correct, all the tests performed with success!\n")
}
}
/*
* The main function starts up the chaincode in the container during instantiate
*/
func main() {
////////////////////////////////////////////////////////
// USE THIS BLOCK TO COMPILE THE CHAINCODE
if err := shim.Start(new(SmartContract)); err != nil {
fmt.Printf("Error starting SmartContract chaincode: %s\n", err)
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// USE THIS BLOCK TO PERFORM ANY TEST WITH PAILLIER
//TestPaillier()
////////////////////////////////////////////////////////
}