Skip to content

Commit

Permalink
normalize error message
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Jul 9, 2024
1 parent 68e2350 commit 8b182bf
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions dppk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8b182bf

Please sign in to comment.