Skip to content

Commit

Permalink
Merge pull request #151 from binance-chain/paillier-check
Browse files Browse the repository at this point in the history
Check paillier ciphertext
  • Loading branch information
yycen authored Nov 12, 2021
2 parents c26beac + a1dabf2 commit 681ebad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crypto/paillier/paillier.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ type (
)

var (
ErrMessageTooLong = fmt.Errorf("the message is too large or < 0")
ErrMessageTooLong = fmt.Errorf("the message is too large or < 0")
ErrMessageMalFormed = fmt.Errorf("the message is mal-formed")

zero = big.NewInt(0)
one = big.NewInt(1)
Expand Down Expand Up @@ -173,6 +174,10 @@ func (privateKey *PrivateKey) Decrypt(c *big.Int) (m *big.Int, err error) {
if c.Cmp(zero) == -1 || c.Cmp(N2) != -1 { // c < 0 || c >= N2 ?
return nil, ErrMessageTooLong
}
cg := new(big.Int).GCD(nil, nil, c, N2)
if cg.Cmp(one) == 1 {
return nil, ErrMessageMalFormed
}
// 1. L(u) = (c^LambdaN-1 mod N2) / N
Lc := L(new(big.Int).Exp(c, privateKey.LambdaN, N2), privateKey.N)
// 2. L(u) = (Gamma^LambdaN-1 mod N2) / N
Expand Down
4 changes: 4 additions & 0 deletions crypto/paillier/paillier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func TestEncryptDecrypt(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, exp.Cmp(ret),
"wrong decryption ", ret, " is not ", exp)

cypher = new(big.Int).Set(privateKey.N)
_, err = privateKey.Decrypt(cypher)
assert.Error(t, err)
}

func TestHomoMul(t *testing.T) {
Expand Down

0 comments on commit 681ebad

Please sign in to comment.