-
Notifications
You must be signed in to change notification settings - Fork 1
/
privatekeyshare_test.go
164 lines (136 loc) · 28.7 KB
/
privatekeyshare_test.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
// Copyright 2021 TNO
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dpaillier
import (
"encoding/json"
"math/big"
"testing"
secret "github.com/TNO-MPC/shamir"
"github.com/stretchr/testify/assert"
)
func TestDecryptAfterPartialEncryption(t *testing.T) {
assert := assert.New(t)
secretKeysBytes := []byte(`[{"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"N2":853016260478354226078636382734387040407483519976207003897519695185826844295458904752872432196634911614678402880071483489462359767482094856111017494629029984862571976858728808344815669131649814364057010957088741191437765260452274742317149639176361883554305206397169870207537527962185456313097471325970838952701484521278114949898965507818324377403623610210740344078301138788584064601983534300016953161718488127157329190291664465209493016460012592877278639650922625794222100764169323531527284131449600278216647357151115843444339159035537329817448537933849191386750870880746872188814926547211147876491421956759655212016049,"Nplus1":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675258,"ParticipantIndex":1,"Hi":48938735228481605671506662510450651917225420709088722835831527327036543243711930821915805062356760485474508158912086145271911791041508609452706680749881605333082010585140898510660891757634755899441752457010570897966950967837887383087006028149229701618178026661292554946288902347203397401854638216743948189137442329908420367343706946673826242701381055223695141496310771597082998002652128978175203744487185810158252761746852357550692459267604898195079114769915771876490203867421540523010624150412030944174018880443891846140580169404351465610172768916571430784704146912769822075188808669369608285139381970859914135197778795,"FactorialOfNPart":6,"ThetaInv":192470118236030216420699271576372473137212638181137142016583270329440043672587342881593711669207853964573701325320755613154987300219349078641354360758829353651018562623512001755672067229136891955303338736134388804774653912434049391985550331498968363876200256829543464806284831660336245478255772246612994793426},{"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"N2":853016260478354226078636382734387040407483519976207003897519695185826844295458904752872432196634911614678402880071483489462359767482094856111017494629029984862571976858728808344815669131649814364057010957088741191437765260452274742317149639176361883554305206397169870207537527962185456313097471325970838952701484521278114949898965507818324377403623610210740344078301138788584064601983534300016953161718488127157329190291664465209493016460012592877278639650922625794222100764169323531527284131449600278216647357151115843444339159035537329817448537933849191386750870880746872188814926547211147876491421956759655212016049,"Nplus1":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675258,"ParticipantIndex":2,"Hi":48938735228481605671506662510450651917225420709088722835831527327036543243711930821915805062356760485474508158912086145271911791041508609452706680749881605333082010585140898510660891757634755899441752457010570897966950967837887383087006028149229701618178026661292554946288902347203397401854638216743948189137485448296776514499498081636331574247242734510498473234612884907078921183673112640075390810663175950953375673715009350130951317796393954035799500614761857263374926605380905050967953053023851627037874707923651368741931907289460729988952012611626013906553182932655866806990727945926452154637810872628659404425344728,"FactorialOfNPart":6,"ThetaInv":192470118236030216420699271576372473137212638181137142016583270329440043672587342881593711669207853964573701325320755613154987300219349078641354360758829353651018562623512001755672067229136891955303338736134388804774653912434049391985550331498968363876200256829543464806284831660336245478255772246612994793426},{"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"N2":853016260478354226078636382734387040407483519976207003897519695185826844295458904752872432196634911614678402880071483489462359767482094856111017494629029984862571976858728808344815669131649814364057010957088741191437765260452274742317149639176361883554305206397169870207537527962185456313097471325970838952701484521278114949898965507818324377403623610210740344078301138788584064601983534300016953161718488127157329190291664465209493016460012592877278639650922625794222100764169323531527284131449600278216647357151115843444339159035537329817448537933849191386750870880746872188814926547211147876491421956759655212016049,"Nplus1":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675258,"ParticipantIndex":3,"Hi":48938735228481605671506662510450651917225420709088722835831527327036543243711930821915805062356760485474508158912086145271911791041508609452706680749881605333082010585140898510660891757634755899441752457010570897966950967837887383087006028149229701618178026661292554946288902347203397401854638216743948189137528566685132661655289216598836905793104413797301804972914998217074844364694096301975577876839166091748498585683166342711210176325183009876519886459607942650259649343340269578925281955635672309901730535403410891343283645174569994367731256306680597028402218952541911538792647222483296024136239774397404673652910679,"FactorialOfNPart":6,"ThetaInv":192470118236030216420699271576372473137212638181137142016583270329440043672587342881593711669207853964573701325320755613154987300219349078641354360758829353651018562623512001755672067229136891955303338736134388804774653912434049391985550331498968363876200256829543464806284831660336245478255772246612994793426}]`)
var secretKeys []*PrivateKeyShare
json.Unmarshal(secretKeysBytes, &secretKeys)
plaintext := big.NewInt(3726)
// Check that it works no matter who encrypts things
for i := range secretKeys {
ct := secretKeys[i].PartiallyEncrypt(plaintext)
pd := make([]*PartialDecryption, len(secretKeys))
for j := range secretKeys {
pd[j] = secretKeys[j].PartiallyDecrypt(ct)
}
// Check that it works no matter who decrypts things
for j := range secretKeys {
pt := secretKeys[j].Decrypt(pd)
assert.Equal(plaintext.String(), pt.String(), "Decryption incorrect, encrypter is %d, decrypter is %d", i, j)
}
}
}
func TestDecryptAfterFullEncryption(t *testing.T) {
assert := assert.New(t)
secretKeysBytes := []byte(`[{"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"N2":853016260478354226078636382734387040407483519976207003897519695185826844295458904752872432196634911614678402880071483489462359767482094856111017494629029984862571976858728808344815669131649814364057010957088741191437765260452274742317149639176361883554305206397169870207537527962185456313097471325970838952701484521278114949898965507818324377403623610210740344078301138788584064601983534300016953161718488127157329190291664465209493016460012592877278639650922625794222100764169323531527284131449600278216647357151115843444339159035537329817448537933849191386750870880746872188814926547211147876491421956759655212016049,"Nplus1":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675258,"ParticipantIndex":1,"Hi":48938735228481605671506662510450651917225420709088722835831527327036543243711930821915805062356760485474508158912086145271911791041508609452706680749881605333082010585140898510660891757634755899441752457010570897966950967837887383087006028149229701618178026661292554946288902347203397401854638216743948189137442329908420367343706946673826242701381055223695141496310771597082998002652128978175203744487185810158252761746852357550692459267604898195079114769915771876490203867421540523010624150412030944174018880443891846140580169404351465610172768916571430784704146912769822075188808669369608285139381970859914135197778795,"FactorialOfNPart":6,"ThetaInv":192470118236030216420699271576372473137212638181137142016583270329440043672587342881593711669207853964573701325320755613154987300219349078641354360758829353651018562623512001755672067229136891955303338736134388804774653912434049391985550331498968363876200256829543464806284831660336245478255772246612994793426},{"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"N2":853016260478354226078636382734387040407483519976207003897519695185826844295458904752872432196634911614678402880071483489462359767482094856111017494629029984862571976858728808344815669131649814364057010957088741191437765260452274742317149639176361883554305206397169870207537527962185456313097471325970838952701484521278114949898965507818324377403623610210740344078301138788584064601983534300016953161718488127157329190291664465209493016460012592877278639650922625794222100764169323531527284131449600278216647357151115843444339159035537329817448537933849191386750870880746872188814926547211147876491421956759655212016049,"Nplus1":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675258,"ParticipantIndex":2,"Hi":48938735228481605671506662510450651917225420709088722835831527327036543243711930821915805062356760485474508158912086145271911791041508609452706680749881605333082010585140898510660891757634755899441752457010570897966950967837887383087006028149229701618178026661292554946288902347203397401854638216743948189137485448296776514499498081636331574247242734510498473234612884907078921183673112640075390810663175950953375673715009350130951317796393954035799500614761857263374926605380905050967953053023851627037874707923651368741931907289460729988952012611626013906553182932655866806990727945926452154637810872628659404425344728,"FactorialOfNPart":6,"ThetaInv":192470118236030216420699271576372473137212638181137142016583270329440043672587342881593711669207853964573701325320755613154987300219349078641354360758829353651018562623512001755672067229136891955303338736134388804774653912434049391985550331498968363876200256829543464806284831660336245478255772246612994793426},{"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"N2":853016260478354226078636382734387040407483519976207003897519695185826844295458904752872432196634911614678402880071483489462359767482094856111017494629029984862571976858728808344815669131649814364057010957088741191437765260452274742317149639176361883554305206397169870207537527962185456313097471325970838952701484521278114949898965507818324377403623610210740344078301138788584064601983534300016953161718488127157329190291664465209493016460012592877278639650922625794222100764169323531527284131449600278216647357151115843444339159035537329817448537933849191386750870880746872188814926547211147876491421956759655212016049,"Nplus1":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675258,"ParticipantIndex":3,"Hi":48938735228481605671506662510450651917225420709088722835831527327036543243711930821915805062356760485474508158912086145271911791041508609452706680749881605333082010585140898510660891757634755899441752457010570897966950967837887383087006028149229701618178026661292554946288902347203397401854638216743948189137528566685132661655289216598836905793104413797301804972914998217074844364694096301975577876839166091748498585683166342711210176325183009876519886459607942650259649343340269578925281955635672309901730535403410891343283645174569994367731256306680597028402218952541911538792647222483296024136239774397404673652910679,"FactorialOfNPart":6,"ThetaInv":192470118236030216420699271576372473137212638181137142016583270329440043672587342881593711669207853964573701325320755613154987300219349078641354360758829353651018562623512001755672067229136891955303338736134388804774653912434049391985550331498968363876200256829543464806284831660336245478255772246612994793426}]`)
var secretKeys []*PrivateKeyShare
json.Unmarshal(secretKeysBytes, &secretKeys)
plaintext := big.NewInt(3726)
// Check that it works no matter who encrypts things
for i := range secretKeys {
ct := secretKeys[i].Encrypt(plaintext)
pd := make([]*PartialDecryption, len(secretKeys))
for j := range secretKeys {
pd[j] = secretKeys[j].PartiallyDecrypt(ct)
}
// Check that it works no matter who decrypts things
for j := range secretKeys {
pt := secretKeys[j].Decrypt(pd)
assert.Equal(plaintext.String(), pt.String(), "Decryption incorrect, encrypter is %d, decrypter is %d", i, j)
}
}
}
func TestPartialDecryptionIntegrity(t *testing.T) {
assert := assert.New(t)
secretKeysBytes := []byte(`[{"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"N2":853016260478354226078636382734387040407483519976207003897519695185826844295458904752872432196634911614678402880071483489462359767482094856111017494629029984862571976858728808344815669131649814364057010957088741191437765260452274742317149639176361883554305206397169870207537527962185456313097471325970838952701484521278114949898965507818324377403623610210740344078301138788584064601983534300016953161718488127157329190291664465209493016460012592877278639650922625794222100764169323531527284131449600278216647357151115843444339159035537329817448537933849191386750870880746872188814926547211147876491421956759655212016049,"Nplus1":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675258,"ParticipantIndex":1,"Hi":48938735228481605671506662510450651917225420709088722835831527327036543243711930821915805062356760485474508158912086145271911791041508609452706680749881605333082010585140898510660891757634755899441752457010570897966950967837887383087006028149229701618178026661292554946288902347203397401854638216743948189137442329908420367343706946673826242701381055223695141496310771597082998002652128978175203744487185810158252761746852357550692459267604898195079114769915771876490203867421540523010624150412030944174018880443891846140580169404351465610172768916571430784704146912769822075188808669369608285139381970859914135197778795,"FactorialOfNPart":6,"ThetaInv":192470118236030216420699271576372473137212638181137142016583270329440043672587342881593711669207853964573701325320755613154987300219349078641354360758829353651018562623512001755672067229136891955303338736134388804774653912434049391985550331498968363876200256829543464806284831660336245478255772246612994793426},{"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"N2":853016260478354226078636382734387040407483519976207003897519695185826844295458904752872432196634911614678402880071483489462359767482094856111017494629029984862571976858728808344815669131649814364057010957088741191437765260452274742317149639176361883554305206397169870207537527962185456313097471325970838952701484521278114949898965507818324377403623610210740344078301138788584064601983534300016953161718488127157329190291664465209493016460012592877278639650922625794222100764169323531527284131449600278216647357151115843444339159035537329817448537933849191386750870880746872188814926547211147876491421956759655212016049,"Nplus1":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675258,"ParticipantIndex":2,"Hi":48938735228481605671506662510450651917225420709088722835831527327036543243711930821915805062356760485474508158912086145271911791041508609452706680749881605333082010585140898510660891757634755899441752457010570897966950967837887383087006028149229701618178026661292554946288902347203397401854638216743948189137485448296776514499498081636331574247242734510498473234612884907078921183673112640075390810663175950953375673715009350130951317796393954035799500614761857263374926605380905050967953053023851627037874707923651368741931907289460729988952012611626013906553182932655866806990727945926452154637810872628659404425344728,"FactorialOfNPart":6,"ThetaInv":192470118236030216420699271576372473137212638181137142016583270329440043672587342881593711669207853964573701325320755613154987300219349078641354360758829353651018562623512001755672067229136891955303338736134388804774653912434049391985550331498968363876200256829543464806284831660336245478255772246612994793426},{"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"N2":853016260478354226078636382734387040407483519976207003897519695185826844295458904752872432196634911614678402880071483489462359767482094856111017494629029984862571976858728808344815669131649814364057010957088741191437765260452274742317149639176361883554305206397169870207537527962185456313097471325970838952701484521278114949898965507818324377403623610210740344078301138788584064601983534300016953161718488127157329190291664465209493016460012592877278639650922625794222100764169323531527284131449600278216647357151115843444339159035537329817448537933849191386750870880746872188814926547211147876491421956759655212016049,"Nplus1":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675258,"ParticipantIndex":3,"Hi":48938735228481605671506662510450651917225420709088722835831527327036543243711930821915805062356760485474508158912086145271911791041508609452706680749881605333082010585140898510660891757634755899441752457010570897966950967837887383087006028149229701618178026661292554946288902347203397401854638216743948189137528566685132661655289216598836905793104413797301804972914998217074844364694096301975577876839166091748498585683166342711210176325183009876519886459607942650259649343340269578925281955635672309901730535403410891343283645174569994367731256306680597028402218952541911538792647222483296024136239774397404673652910679,"FactorialOfNPart":6,"ThetaInv":192470118236030216420699271576372473137212638181137142016583270329440043672587342881593711669207853964573701325320755613154987300219349078641354360758829353651018562623512001755672067229136891955303338736134388804774653912434049391985550331498968363876200256829543464806284831660336245478255772246612994793426}]`)
var secretKeys []*PrivateKeyShare
json.Unmarshal(secretKeysBytes, &secretKeys)
// Generate partial decryptions of some ciphertexts
ct := secretKeys[0].Encrypt(big.NewInt(3726))
partials := make([]*PartialDecryption, len(secretKeys))
for j := range secretKeys {
partials[j] = secretKeys[j].PartiallyDecrypt(ct)
}
// Reconstruct "theta" = v * h(0)
vh, err := secret.ShareCombine([]secret.Share{
{Factor: big.NewInt(1), Degree: 2, X: 1, Y: secretKeys[0].Hi},
{Factor: big.NewInt(1), Degree: 2, X: 2, Y: secretKeys[1].Hi},
{Factor: big.NewInt(1), Degree: 2, X: 3, Y: secretKeys[2].Hi},
})
// you get v * v * lambda * beta == v * h(0) for Factor == 1.
// Factor == 6 (== v == n!) divides out one v, et cetera
assert.NoError(err, "Combining v * h(0) from shares h(i)")
// Check if v*h is actually the inverse of thetaInverse
one := big.NewInt(0).Mul(secretKeys[0].ThetaInv, vh)
one.Mod(one, secretKeys[0].PublicKey.N)
assert.Equal("1", one.String(), "Computed theta is not inverse of ThetaInv from key")
// We'll check if the partial decryptions are correct, i.e.
// c^(v*h(0)) == c^(l_i*h(i)) (mod N^2)
// This is the left-hand side:
lefthand := big.NewInt(0).Exp(ct, vh, secretKeys[0].PublicKey.N2)
// Manually reconstruct the right-hand side like .Decrypt does
ctPower := big.NewInt(1)
li := big.NewInt(1)
for _, partial := range partials {
li.Set(secretKeys[0].FactorialOfNPart)
for _, other := range partials {
if partial.I != other.I {
li.Mul(li, big.NewInt(int64(other.I)))
li.Div(li, big.NewInt(int64(other.I-partial.I)))
}
}
li.Div(li, secretKeys[0].FactorialOfNPart)
li.Exp(partial.Ci, li, secretKeys[0].PublicKey.N2)
ctPower.Mul(ctPower, li).Mod(ctPower, secretKeys[0].PublicKey.N2)
}
assert.Equal(lefthand.String(), ctPower.String(), "Reconstruction of c^theta failed")
// Check that decryption works
ctPower.Sub(ctPower, bigOne) // L(x) = (x-1) ...
// We will divide by N, which should be a factor if everything went by the book. Check it.
assert.Equal("0", big.NewInt(0).Mod(ctPower, secretKeys[0].PublicKey.N).String(), "not mod")
ctPower.Div(ctPower, secretKeys[0].PublicKey.N) // ... / N
ctPower.Mul(ctPower, secretKeys[0].ThetaInv) // m = L(ctPower) * ThetaInv ...
ctPower.Mod(ctPower, secretKeys[0].PublicKey.N) // ... mod N
assert.Equal("3726", ctPower.String(), "decryption is incorrect")
// Inspired by a squashed bug, we'll also check if theta was correctly computed from the
// terms of lambda computed earlier in the process.
states4Bytes := []byte(`[{"Parameters":{"NumberOfParticipants":3,"ParticipantIndex":0,"PaillierBitSize":512,"SecretSharingDegree":1,"SecretSharingStatisticalSecurity":20,"SecretSharingModulus":2623222045352356732683025662305979215904046499376168877804324390266714056732042608675318325182279767188274546065280153959994987994681446800680132187027204726065259578691071312441425971149525208889491572233926587610577246558357978325575132606555085287066466813663551180648008762652591343459455276595418684019307,"BiprimalityCheckTimes":0},"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"TermOfLambda":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697652541388313042170629790242611175803258418345217941728188565941105917548217448614606718569135605365358413578891583586155149218758657459584554111367553844664,"TestResponses":[]},{"Parameters":{"NumberOfParticipants":3,"ParticipantIndex":1,"PaillierBitSize":512,"SecretSharingDegree":1,"SecretSharingStatisticalSecurity":20,"SecretSharingModulus":2623222045352356732683025662305979215904046499376168877804324390266714056732042608675318325182279767188274546065280153959994987994681446800680132187027204726065259578691071312441425971149525208889491572233926587610577246558357978325575132606555085287066466813663551180648008762652591343459455276595418684019307,"BiprimalityCheckTimes":0},"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"TermOfLambda":25557955938562419505452003922262818619990788916190541227737041418537421777240436879859970327507060089129874099906967604894621066506567583448756035708761360,"TestResponses":null},{"Parameters":{"NumberOfParticipants":3,"ParticipantIndex":2,"PaillierBitSize":512,"SecretSharingDegree":1,"SecretSharingStatisticalSecurity":20,"SecretSharingModulus":2623222045352356732683025662305979215904046499376168877804324390266714056732042608675318325182279767188274546065280153959994987994681446800680132187027204726065259578691071312441425971149525208889491572233926587610577246558357978325575132606555085287066466813663551180648008762652591343459455276595418684019307,"BiprimalityCheckTimes":0},"N":923588794041132910322504663489230159340645205888572410576204511494217137733476860130052233334326157027202966831612503418564291553442062779657809148246697677036362133578704288207973851468378736925970832206358261932321121488532519774503731055510154940781895101061565530550463014148475619594070354394490660675257,"TermOfLambda":10787665490565197676068660975032690679537407281834856818578560682804749820662550345191506691878337503503743467014983993260833797125471924715616848074531764,"TestResponses":null}]`)
var states4 []*KeyGenerationStage4
json.Unmarshal(states4Bytes, &states4)
lambda := big.NewInt(0).Set(states4[0].TermOfLambda)
for i := 1; i != len(states4); i++ {
assert.Equal(secretKeys[0].PublicKey.N.String(), states4[i].N.String(), "N for %d", i)
// The bug was that we'd lambda.Add here instead (but it was hidden by the secret sharing)
lambda.Sub(lambda, states4[i].TermOfLambda)
}
// r^(lambda N) == 1 (mod N^2), if lambda was constructed correctly. Check it.
random := big.NewInt(2)
random.Exp(random, secretKeys[0].PublicKey.N, secretKeys[0].PublicKey.N2)
random.Exp(random, lambda, secretKeys[0].PublicKey.N2)
assert.Equal("1", random.String(), "exp(r, lambda*N, N^2) != 1")
// This should also hold for v*h(0), which contains lambda: vh == lambda.beta.n!.n!
random = big.NewInt(2)
random.Exp(random, secretKeys[0].PublicKey.N, secretKeys[0].PublicKey.N2)
random.Exp(random, vh, secretKeys[0].PublicKey.N2)
assert.Equal("1", random.String(), "exp(r, vh*N, N^2) != 1")
assert.Equal("0", big.NewInt(0).Mod(vh, lambda).String(), "vh mod lambda != 0; lambda should be a factor of vh == lambda.beta.n!.n!")
}