From 8b182bf88f1efab97c7c3b11995d350d120d04e2 Mon Sep 17 00:00:00 2001 From: xtaci Date: Tue, 9 Jul 2024 14:06:14 +0800 Subject: [PATCH] normalize error message --- dppk.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/dppk.go b/dppk.go index 12d4b2f..a423a8c 100644 --- a/dppk.go +++ b/dppk.go @@ -8,7 +8,14 @@ import ( ) // PRIME is the prime number used in the DPPK protocol. -const PRIME = "32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596231637" +// const PRIME = "32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596231637" +const PRIME = "977" + +const ( + ERRMSG_ORDER = "order must be at least 5" + ERRMSG_NULL_ENCRYPT = "encrypted values cannot be null" + ERRMSG_DATA_EXCEEDED_FIELD = "the secret to encrypt is not in the GF(p)" +) // PrivateKey represents a private key in the DPPK protocol. type PrivateKey struct { @@ -67,7 +74,7 @@ func UnmarshalPublicKey(vecU, vecV []*big.Int) *PublicKey { func GenerateKey(order int) (*PrivateKey, error) { // Ensure the order is at least 5 if order < 5 { - return nil, errors.New("order must be at least 5") + return nil, errors.New(ERRMSG_ORDER) } prime, _ := big.NewInt(0).SetString(PRIME, 10) @@ -164,7 +171,7 @@ func (dppk *PrivateKey) Encrypt(pk *PublicKey, msg []byte) (Ps *big.Int, Qs *big // Convert the message to a big integer secret := new(big.Int).SetBytes(msg) if secret.Cmp(dppk.PublicKey.prime) >= 0 { - return nil, nil, errors.New("data is too large") + return nil, nil, errors.New(ERRMSG_DATA_EXCEEDED_FIELD) } // Extend the vectors U and Q with a constant term of 1 @@ -203,6 +210,9 @@ func (dppk *PrivateKey) Encrypt(pk *PublicKey, msg []byte) (Ps *big.Int, Qs *big // Decrypt decrypts the encrypted values Ps and Qs using the private key. func (dppk *PrivateKey) Decrypt(Ps *big.Int, Qs *big.Int) (x1, x2 *big.Int, err error) { + if Ps == nil || Qs == nil { + return nil, nil, errors.New(ERRMSG_NULL_ENCRYPT) + } // Add constant term to get full Ps and Qs polynomial fullPS := new(big.Int).Set(Ps) fullQs := new(big.Int).Set(Qs)