From efd8deceffb6d5c500f0060df54a371641007b8c Mon Sep 17 00:00:00 2001 From: Keelan10 Date: Thu, 30 Mar 2023 22:24:14 +0400 Subject: [PATCH] Restructure code --- kem/ntruprime/sntrup1013/ntruprime.go | 20 ++- kem/ntruprime/sntrup1277/ntruprime.go | 20 ++- kem/ntruprime/sntrup653/ntruprime.go | 20 ++- kem/ntruprime/sntrup761/ntruprime.go | 20 ++- kem/ntruprime/sntrup857/ntruprime.go | 20 ++- kem/ntruprime/sntrup953/ntruprime.go | 20 ++- kem/ntruprime/templates/sntrup.templ.go | 20 ++- kem/schemes/schemes.go | 17 +- kem/schemes/schemes_test.go | 10 +- pke/ntruprime/kem/kem.go | 96 +---------- pke/ntruprime/kem/schemes/sntrup/schemes.go | 14 +- .../kem/schemes/sntrup/schemes_test.go | 152 ------------------ 12 files changed, 162 insertions(+), 267 deletions(-) delete mode 100644 pke/ntruprime/kem/schemes/sntrup/schemes_test.go diff --git a/kem/ntruprime/sntrup1013/ntruprime.go b/kem/ntruprime/sntrup1013/ntruprime.go index 680c754db..c09bb6a0a 100644 --- a/kem/ntruprime/sntrup1013/ntruprime.go +++ b/kem/ntruprime/sntrup1013/ntruprime.go @@ -13,8 +13,9 @@ import ( "crypto/sha512" "github.com/cloudflare/circl/internal/nist" + "github.com/cloudflare/circl/kem" "github.com/cloudflare/circl/kem/ntruprime/internal" - "github.com/cloudflare/circl/pke/ntruprime/kem" + sntrupKem "github.com/cloudflare/circl/pke/ntruprime/kem" ntrup "github.com/cloudflare/circl/pke/ntruprime/sntrup1013" ) @@ -45,6 +46,12 @@ const ( ) const ( + // Size of seed for NewKeyFromSeed + // Note that during keyGen, a random small is generated until a valid one (whose reciprocal succeeds) is found + // The size of keySeed depends on the number of times the reciprocal fails + // This is why DeriveKeyPairFromGen is used to deterministically derive key pair instead of using seed + KeySeedSize = 4*p + p*4 + inputsBytes + // Size of seed for EncapsulateTo. EncapsulationSeedSize = 4 * p @@ -816,14 +823,18 @@ type PublicKey struct { type scheme struct{} -var sch kem.Scheme = &scheme{} +var sch sntrupKem.Scheme = &scheme{} // Scheme returns a KEM interface. func Scheme() kem.Scheme { return sch } +// SntrupScheme returns a sntrup.KEM interface +func SntrupScheme() sntrupKem.Scheme { return sch } + func (*scheme) Name() string { return "sntrup1013" } func (*scheme) PublicKeySize() int { return PublicKeySize } func (*scheme) PrivateKeySize() int { return PrivateKeySize } +func (*scheme) SeedSize() int { return KeySeedSize } func (*scheme) SharedKeySize() int { return SharedKeySize } func (*scheme) CiphertextSize() int { return CiphertextSize } func (*scheme) EncapsulationSeedSize() int { return EncapsulationSeedSize } @@ -876,6 +887,11 @@ func (*scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) { } +// Not used +func (*scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) { + return nil, nil +} + func (*scheme) DeriveKeyPairFromGen(gen *nist.DRBG) (kem.PublicKey, kem.PrivateKey) { if gen == nil { diff --git a/kem/ntruprime/sntrup1277/ntruprime.go b/kem/ntruprime/sntrup1277/ntruprime.go index ab6e4b74f..a1318569d 100644 --- a/kem/ntruprime/sntrup1277/ntruprime.go +++ b/kem/ntruprime/sntrup1277/ntruprime.go @@ -13,8 +13,9 @@ import ( "crypto/sha512" "github.com/cloudflare/circl/internal/nist" + "github.com/cloudflare/circl/kem" "github.com/cloudflare/circl/kem/ntruprime/internal" - "github.com/cloudflare/circl/pke/ntruprime/kem" + sntrupKem "github.com/cloudflare/circl/pke/ntruprime/kem" ntrup "github.com/cloudflare/circl/pke/ntruprime/sntrup1277" ) @@ -45,6 +46,12 @@ const ( ) const ( + // Size of seed for NewKeyFromSeed + // Note that during keyGen, a random small is generated until a valid one (whose reciprocal succeeds) is found + // The size of keySeed depends on the number of times the reciprocal fails + // This is why DeriveKeyPairFromGen is used to deterministically derive key pair instead of using seed + KeySeedSize = 4*p + p*4 + inputsBytes + // Size of seed for EncapsulateTo. EncapsulationSeedSize = 4 * p @@ -816,14 +823,18 @@ type PublicKey struct { type scheme struct{} -var sch kem.Scheme = &scheme{} +var sch sntrupKem.Scheme = &scheme{} // Scheme returns a KEM interface. func Scheme() kem.Scheme { return sch } +// SntrupScheme returns a sntrup.KEM interface +func SntrupScheme() sntrupKem.Scheme { return sch } + func (*scheme) Name() string { return "sntrup1277" } func (*scheme) PublicKeySize() int { return PublicKeySize } func (*scheme) PrivateKeySize() int { return PrivateKeySize } +func (*scheme) SeedSize() int { return KeySeedSize } func (*scheme) SharedKeySize() int { return SharedKeySize } func (*scheme) CiphertextSize() int { return CiphertextSize } func (*scheme) EncapsulationSeedSize() int { return EncapsulationSeedSize } @@ -876,6 +887,11 @@ func (*scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) { } +// Not used +func (*scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) { + return nil, nil +} + func (*scheme) DeriveKeyPairFromGen(gen *nist.DRBG) (kem.PublicKey, kem.PrivateKey) { if gen == nil { diff --git a/kem/ntruprime/sntrup653/ntruprime.go b/kem/ntruprime/sntrup653/ntruprime.go index 126367bd4..e5d4aae79 100644 --- a/kem/ntruprime/sntrup653/ntruprime.go +++ b/kem/ntruprime/sntrup653/ntruprime.go @@ -13,8 +13,9 @@ import ( "crypto/sha512" "github.com/cloudflare/circl/internal/nist" + "github.com/cloudflare/circl/kem" "github.com/cloudflare/circl/kem/ntruprime/internal" - "github.com/cloudflare/circl/pke/ntruprime/kem" + sntrupKem "github.com/cloudflare/circl/pke/ntruprime/kem" ntrup "github.com/cloudflare/circl/pke/ntruprime/sntrup653" ) @@ -45,6 +46,12 @@ const ( ) const ( + // Size of seed for NewKeyFromSeed + // Note that during keyGen, a random small is generated until a valid one (whose reciprocal succeeds) is found + // The size of keySeed depends on the number of times the reciprocal fails + // This is why DeriveKeyPairFromGen is used to deterministically derive key pair instead of using seed + KeySeedSize = 4*p + p*4 + inputsBytes + // Size of seed for EncapsulateTo. EncapsulationSeedSize = 4 * p @@ -816,14 +823,18 @@ type PublicKey struct { type scheme struct{} -var sch kem.Scheme = &scheme{} +var sch sntrupKem.Scheme = &scheme{} // Scheme returns a KEM interface. func Scheme() kem.Scheme { return sch } +// SntrupScheme returns a sntrup.KEM interface +func SntrupScheme() sntrupKem.Scheme { return sch } + func (*scheme) Name() string { return "sntrup653" } func (*scheme) PublicKeySize() int { return PublicKeySize } func (*scheme) PrivateKeySize() int { return PrivateKeySize } +func (*scheme) SeedSize() int { return KeySeedSize } func (*scheme) SharedKeySize() int { return SharedKeySize } func (*scheme) CiphertextSize() int { return CiphertextSize } func (*scheme) EncapsulationSeedSize() int { return EncapsulationSeedSize } @@ -876,6 +887,11 @@ func (*scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) { } +// Not used +func (*scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) { + return nil, nil +} + func (*scheme) DeriveKeyPairFromGen(gen *nist.DRBG) (kem.PublicKey, kem.PrivateKey) { if gen == nil { diff --git a/kem/ntruprime/sntrup761/ntruprime.go b/kem/ntruprime/sntrup761/ntruprime.go index bb7ed200a..1ea6b232d 100644 --- a/kem/ntruprime/sntrup761/ntruprime.go +++ b/kem/ntruprime/sntrup761/ntruprime.go @@ -13,8 +13,9 @@ import ( "crypto/sha512" "github.com/cloudflare/circl/internal/nist" + "github.com/cloudflare/circl/kem" "github.com/cloudflare/circl/kem/ntruprime/internal" - "github.com/cloudflare/circl/pke/ntruprime/kem" + sntrupKem "github.com/cloudflare/circl/pke/ntruprime/kem" ntrup "github.com/cloudflare/circl/pke/ntruprime/sntrup761" ) @@ -45,6 +46,12 @@ const ( ) const ( + // Size of seed for NewKeyFromSeed + // Note that during keyGen, a random small is generated until a valid one (whose reciprocal succeeds) is found + // The size of keySeed depends on the number of times the reciprocal fails + // This is why DeriveKeyPairFromGen is used to deterministically derive key pair instead of using seed + KeySeedSize = 4*p + p*4 + inputsBytes + // Size of seed for EncapsulateTo. EncapsulationSeedSize = 4 * p @@ -816,14 +823,18 @@ type PublicKey struct { type scheme struct{} -var sch kem.Scheme = &scheme{} +var sch sntrupKem.Scheme = &scheme{} // Scheme returns a KEM interface. func Scheme() kem.Scheme { return sch } +// SntrupScheme returns a sntrup.KEM interface +func SntrupScheme() sntrupKem.Scheme { return sch } + func (*scheme) Name() string { return "sntrup761" } func (*scheme) PublicKeySize() int { return PublicKeySize } func (*scheme) PrivateKeySize() int { return PrivateKeySize } +func (*scheme) SeedSize() int { return KeySeedSize } func (*scheme) SharedKeySize() int { return SharedKeySize } func (*scheme) CiphertextSize() int { return CiphertextSize } func (*scheme) EncapsulationSeedSize() int { return EncapsulationSeedSize } @@ -876,6 +887,11 @@ func (*scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) { } +// Not used +func (*scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) { + return nil, nil +} + func (*scheme) DeriveKeyPairFromGen(gen *nist.DRBG) (kem.PublicKey, kem.PrivateKey) { if gen == nil { diff --git a/kem/ntruprime/sntrup857/ntruprime.go b/kem/ntruprime/sntrup857/ntruprime.go index 8680ddedb..35b2feb19 100644 --- a/kem/ntruprime/sntrup857/ntruprime.go +++ b/kem/ntruprime/sntrup857/ntruprime.go @@ -13,8 +13,9 @@ import ( "crypto/sha512" "github.com/cloudflare/circl/internal/nist" + "github.com/cloudflare/circl/kem" "github.com/cloudflare/circl/kem/ntruprime/internal" - "github.com/cloudflare/circl/pke/ntruprime/kem" + sntrupKem "github.com/cloudflare/circl/pke/ntruprime/kem" ntrup "github.com/cloudflare/circl/pke/ntruprime/sntrup857" ) @@ -45,6 +46,12 @@ const ( ) const ( + // Size of seed for NewKeyFromSeed + // Note that during keyGen, a random small is generated until a valid one (whose reciprocal succeeds) is found + // The size of keySeed depends on the number of times the reciprocal fails + // This is why DeriveKeyPairFromGen is used to deterministically derive key pair instead of using seed + KeySeedSize = 4*p + p*4 + inputsBytes + // Size of seed for EncapsulateTo. EncapsulationSeedSize = 4 * p @@ -816,14 +823,18 @@ type PublicKey struct { type scheme struct{} -var sch kem.Scheme = &scheme{} +var sch sntrupKem.Scheme = &scheme{} // Scheme returns a KEM interface. func Scheme() kem.Scheme { return sch } +// SntrupScheme returns a sntrup.KEM interface +func SntrupScheme() sntrupKem.Scheme { return sch } + func (*scheme) Name() string { return "sntrup857" } func (*scheme) PublicKeySize() int { return PublicKeySize } func (*scheme) PrivateKeySize() int { return PrivateKeySize } +func (*scheme) SeedSize() int { return KeySeedSize } func (*scheme) SharedKeySize() int { return SharedKeySize } func (*scheme) CiphertextSize() int { return CiphertextSize } func (*scheme) EncapsulationSeedSize() int { return EncapsulationSeedSize } @@ -876,6 +887,11 @@ func (*scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) { } +// Not used +func (*scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) { + return nil, nil +} + func (*scheme) DeriveKeyPairFromGen(gen *nist.DRBG) (kem.PublicKey, kem.PrivateKey) { if gen == nil { diff --git a/kem/ntruprime/sntrup953/ntruprime.go b/kem/ntruprime/sntrup953/ntruprime.go index a7e36df7b..73bfc5ec7 100644 --- a/kem/ntruprime/sntrup953/ntruprime.go +++ b/kem/ntruprime/sntrup953/ntruprime.go @@ -13,8 +13,9 @@ import ( "crypto/sha512" "github.com/cloudflare/circl/internal/nist" + "github.com/cloudflare/circl/kem" "github.com/cloudflare/circl/kem/ntruprime/internal" - "github.com/cloudflare/circl/pke/ntruprime/kem" + sntrupKem "github.com/cloudflare/circl/pke/ntruprime/kem" ntrup "github.com/cloudflare/circl/pke/ntruprime/sntrup953" ) @@ -45,6 +46,12 @@ const ( ) const ( + // Size of seed for NewKeyFromSeed + // Note that during keyGen, a random small is generated until a valid one (whose reciprocal succeeds) is found + // The size of keySeed depends on the number of times the reciprocal fails + // This is why DeriveKeyPairFromGen is used to deterministically derive key pair instead of using seed + KeySeedSize = 4*p + p*4 + inputsBytes + // Size of seed for EncapsulateTo. EncapsulationSeedSize = 4 * p @@ -816,14 +823,18 @@ type PublicKey struct { type scheme struct{} -var sch kem.Scheme = &scheme{} +var sch sntrupKem.Scheme = &scheme{} // Scheme returns a KEM interface. func Scheme() kem.Scheme { return sch } +// SntrupScheme returns a sntrup.KEM interface +func SntrupScheme() sntrupKem.Scheme { return sch } + func (*scheme) Name() string { return "sntrup953" } func (*scheme) PublicKeySize() int { return PublicKeySize } func (*scheme) PrivateKeySize() int { return PrivateKeySize } +func (*scheme) SeedSize() int { return KeySeedSize } func (*scheme) SharedKeySize() int { return SharedKeySize } func (*scheme) CiphertextSize() int { return CiphertextSize } func (*scheme) EncapsulationSeedSize() int { return EncapsulationSeedSize } @@ -876,6 +887,11 @@ func (*scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) { } +// Not used +func (*scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) { + return nil, nil +} + func (*scheme) DeriveKeyPairFromGen(gen *nist.DRBG) (kem.PublicKey, kem.PrivateKey) { if gen == nil { diff --git a/kem/ntruprime/templates/sntrup.templ.go b/kem/ntruprime/templates/sntrup.templ.go index 79bd48a1f..71cc85a10 100644 --- a/kem/ntruprime/templates/sntrup.templ.go +++ b/kem/ntruprime/templates/sntrup.templ.go @@ -17,8 +17,9 @@ import ( "crypto/sha512" "github.com/cloudflare/circl/internal/nist" - "github.com/cloudflare/circl/pke/ntruprime/kem" + "github.com/cloudflare/circl/kem" "github.com/cloudflare/circl/kem/ntruprime/internal" + sntrupKem "github.com/cloudflare/circl/pke/ntruprime/kem" ntrup "github.com/cloudflare/circl/pke/ntruprime/{{.Pkg}}" ) @@ -50,6 +51,12 @@ const ( ) const ( + // Size of seed for NewKeyFromSeed + // Note that during keyGen, a random small is generated until a valid one (whose reciprocal succeeds) is found + // The size of keySeed depends on the number of times the reciprocal fails + // This is why DeriveKeyPairFromGen is used to deterministically derive key pair instead of using seed + KeySeedSize = 4*p + p*4 + inputsBytes + // Size of seed for EncapsulateTo. EncapsulationSeedSize = 4*p @@ -824,14 +831,18 @@ type PublicKey struct { type scheme struct{} -var sch kem.Scheme = &scheme{} +var sch sntrupKem.Scheme = &scheme{} // Scheme returns a KEM interface. func Scheme() kem.Scheme { return sch } +// SntrupScheme returns a sntrup.KEM interface +func SntrupScheme() sntrupKem.Scheme { return sch } + func (*scheme) Name() string { return "{{.Pkg}}" } func (*scheme) PublicKeySize() int { return PublicKeySize } func (*scheme) PrivateKeySize() int { return PrivateKeySize } +func (*scheme) SeedSize() int { return KeySeedSize } func (*scheme) SharedKeySize() int { return SharedKeySize } func (*scheme) CiphertextSize() int { return CiphertextSize } func (*scheme) EncapsulationSeedSize() int { return EncapsulationSeedSize } @@ -886,6 +897,11 @@ func (*scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) { } +// Not used +func (*scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) { + return nil, nil +} + func (*scheme) DeriveKeyPairFromGen(gen *nist.DRBG) (kem.PublicKey, kem.PrivateKey) { if gen == nil { diff --git a/kem/schemes/schemes.go b/kem/schemes/schemes.go index a6f9f6afe..410378350 100644 --- a/kem/schemes/schemes.go +++ b/kem/schemes/schemes.go @@ -15,6 +15,7 @@ // FrodoKEM-640-SHAKE // Kyber512, Kyber768, Kyber1024 // Kyber512, Kyber768, Kyber1024 +// NTRULPR653, NTRULPR761, NTRULPR857, NTRULPR953, NTRULPR1013, NTRULPR1277 // SNTRUP653, SNTRUP761, SNTRUP857, SNTRUP953, SNTRUP1013, SNTRUP1277 package schemes @@ -35,6 +36,12 @@ import ( "github.com/cloudflare/circl/kem/ntruprime/ntrulpr761" "github.com/cloudflare/circl/kem/ntruprime/ntrulpr857" "github.com/cloudflare/circl/kem/ntruprime/ntrulpr953" + "github.com/cloudflare/circl/kem/ntruprime/sntrup1013" + "github.com/cloudflare/circl/kem/ntruprime/sntrup1277" + "github.com/cloudflare/circl/kem/ntruprime/sntrup653" + "github.com/cloudflare/circl/kem/ntruprime/sntrup761" + "github.com/cloudflare/circl/kem/ntruprime/sntrup857" + "github.com/cloudflare/circl/kem/ntruprime/sntrup953" ) var allSchemes = [...]kem.Scheme{ @@ -51,12 +58,18 @@ var allSchemes = [...]kem.Scheme{ hybrid.Kyber768X25519(), hybrid.Kyber768X448(), hybrid.Kyber1024X448(), - ntrulpr1013.Scheme(), - ntrulpr1277.Scheme(), ntrulpr653.Scheme(), ntrulpr761.Scheme(), ntrulpr857.Scheme(), ntrulpr953.Scheme(), + ntrulpr1013.Scheme(), + ntrulpr1277.Scheme(), + sntrup653.Scheme(), + sntrup761.Scheme(), + sntrup857.Scheme(), + sntrup953.Scheme(), + sntrup1013.Scheme(), + sntrup1277.Scheme(), } var allSchemeNames map[string]kem.Scheme diff --git a/kem/schemes/schemes_test.go b/kem/schemes/schemes_test.go index 6c7276a78..843147820 100644 --- a/kem/schemes/schemes_test.go +++ b/kem/schemes/schemes_test.go @@ -159,10 +159,16 @@ func Example_schemes() { // Kyber768-X25519 // Kyber768-X448 // Kyber1024-X448 - // ntrulpr1013 - // ntrulpr1277 // ntrulpr653 // ntrulpr761 // ntrulpr857 // ntrulpr953 + // ntrulpr1013 + // ntrulpr1277 + // sntrup653 + // sntrup761 + // sntrup857 + // sntrup953 + // sntrup1013 + // sntrup1277 } diff --git a/pke/ntruprime/kem/kem.go b/pke/ntruprime/kem/kem.go index ec66b01fa..59bfd92c4 100644 --- a/pke/ntruprime/kem/kem.go +++ b/pke/ntruprime/kem/kem.go @@ -1,104 +1,20 @@ // Package kem provides a unified interface for Streamlined NTRU Prime KEM schemes. // -// # A register of schemes is available in the package +// # A register of Streamlined NTRU Prime schemes is available in the package // // github.com/cloudflare/circl/pke/ntruprime/kem/schemes/sntrup package kem import ( - "encoding" - "errors" - "github.com/cloudflare/circl/internal/nist" + "github.com/cloudflare/circl/kem" ) -// A KEM public key -type PublicKey interface { - // Returns the scheme for this public key - Scheme() Scheme - - encoding.BinaryMarshaler - Equal(PublicKey) bool -} - -// A KEM private key -type PrivateKey interface { - // Returns the scheme for this private key - Scheme() Scheme - - encoding.BinaryMarshaler - Equal(PrivateKey) bool - Public() PublicKey -} - // A Scheme represents a specific instance of a NTRU PRIME KEM. type Scheme interface { - // Name of the scheme - Name() string - - // GenerateKeyPair creates a new key pair. - GenerateKeyPair() (PublicKey, PrivateKey, error) - - // Encapsulate generates a shared key ss for the public key and - // encapsulates it into a ciphertext ct. - Encapsulate(pk PublicKey) (ct, ss []byte, err error) - - // Returns the shared key encapsulated in ciphertext ct for the - // private key sk. - Decapsulate(sk PrivateKey, ct []byte) ([]byte, error) - - // Unmarshals a PublicKey from the provided buffer. - UnmarshalBinaryPublicKey([]byte) (PublicKey, error) - - // Unmarshals a PrivateKey from the provided buffer. - UnmarshalBinaryPrivateKey([]byte) (PrivateKey, error) - - // Size of encapsulated keys. - CiphertextSize() int + kem.Scheme - // Size of established shared keys. - SharedKeySize() int - - // Size of packed private keys. - PrivateKeySize() int - - // Size of packed public keys. - PublicKeySize() int - - // DeriveKeyPair deterministicallly derives a pair of keys from a nist DRBG. - // Panics if the DRBG is nil - DeriveKeyPairFromGen(gen *nist.DRBG) (PublicKey, PrivateKey) - - // EncapsulateDeterministically generates a shared key ss for the public - // key deterministically from the given seed and encapsulates it into - // a ciphertext ct. If unsure, you're better off using Encapsulate(). - EncapsulateDeterministically(pk PublicKey, seed []byte) ( - ct, ss []byte, err error) - - // Size of seed used in EncapsulateDeterministically(). - EncapsulationSeedSize() int + // DeriveKeyPairFromGen deterministicallly derives a pair of keys from a nist DRBG. + // Only used for deterministic testing + DeriveKeyPairFromGen(gen *nist.DRBG) (kem.PublicKey, kem.PrivateKey) } - -var ( - // ErrTypeMismatch is the error used if types of, for instance, private - // and public keys don't match - ErrTypeMismatch = errors.New("types mismatch") - - // ErrPubKeySize is the error used if the provided public key is of - // the wrong size. - ErrPubKeySize = errors.New("wrong size for public key") - - // ErrCiphertextSize is the error used if the provided ciphertext - // is of the wrong size. - ErrCiphertextSize = errors.New("wrong size for ciphertext") - - // ErrPrivKeySize is the error used if the provided private key is of - // the wrong size. - ErrPrivKeySize = errors.New("wrong size for private key") - - // ErrPubKey is the error used if the provided public key is invalid. - ErrPubKey = errors.New("invalid public key") - - // ErrCipherText is the error used if the provided ciphertext is invalid. - ErrCipherText = errors.New("invalid ciphertext") -) diff --git a/pke/ntruprime/kem/schemes/sntrup/schemes.go b/pke/ntruprime/kem/schemes/sntrup/schemes.go index 3bb2edadf..1fc9b6f1e 100644 --- a/pke/ntruprime/kem/schemes/sntrup/schemes.go +++ b/pke/ntruprime/kem/schemes/sntrup/schemes.go @@ -5,7 +5,7 @@ // Post-quantum kems: // // SNTRUP653, SNTRUP761, SNTRUP857, SNTRUP953, SNTRUP1013, SNTRUP1277 -package schemes +package sntrupSchemes import ( "strings" @@ -20,12 +20,12 @@ import ( ) var allSchemes = [...]kem.Scheme{ - sntrup653.Scheme(), - sntrup761.Scheme(), - sntrup857.Scheme(), - sntrup953.Scheme(), - sntrup1013.Scheme(), - sntrup1277.Scheme(), + sntrup653.SntrupScheme(), + sntrup761.SntrupScheme(), + sntrup857.SntrupScheme(), + sntrup953.SntrupScheme(), + sntrup1013.SntrupScheme(), + sntrup1277.SntrupScheme(), } var allSchemeNames map[string]kem.Scheme diff --git a/pke/ntruprime/kem/schemes/sntrup/schemes_test.go b/pke/ntruprime/kem/schemes/sntrup/schemes_test.go deleted file mode 100644 index 4e5f0ccdb..000000000 --- a/pke/ntruprime/kem/schemes/sntrup/schemes_test.go +++ /dev/null @@ -1,152 +0,0 @@ -package schemes_test - -import ( - "bytes" - "fmt" - "testing" - - schemes "github.com/cloudflare/circl/pke/ntruprime/kem/schemes/sntrup" -) - -func TestCaseSensitivity(t *testing.T) { - if schemes.ByName("sntrup1277") != schemes.ByName("SNTRUP1277") { - t.Fatal() - } -} - -func BenchmarkGenerateKeyPair(b *testing.B) { - allSchemes := schemes.All() - for _, scheme := range allSchemes { - scheme := scheme - b.Run(scheme.Name(), func(b *testing.B) { - for i := 0; i < b.N; i++ { - _, _, _ = scheme.GenerateKeyPair() - } - }) - } -} - -func BenchmarkEncapsulate(b *testing.B) { - allSchemes := schemes.All() - for _, scheme := range allSchemes { - scheme := scheme - pk, _, _ := scheme.GenerateKeyPair() - b.Run(scheme.Name(), func(b *testing.B) { - for i := 0; i < b.N; i++ { - _, _, _ = scheme.Encapsulate(pk) - } - }) - } -} - -func BenchmarkDecapsulate(b *testing.B) { - allSchemes := schemes.All() - for _, scheme := range allSchemes { - scheme := scheme - pk, sk, _ := scheme.GenerateKeyPair() - ct, _, _ := scheme.Encapsulate(pk) - b.Run(scheme.Name(), func(b *testing.B) { - for i := 0; i < b.N; i++ { - _, _ = scheme.Decapsulate(sk, ct) - } - }) - } -} - -func TestApi(t *testing.T) { - allSchemes := schemes.All() - for _, scheme := range allSchemes { - scheme := scheme - t.Run(scheme.Name(), func(t *testing.T) { - if scheme == nil { - t.Fatal() - } - - pk, sk, err := scheme.GenerateKeyPair() - if err != nil { - t.Fatal() - } - - packedPk, err := pk.MarshalBinary() - if err != nil { - t.Fatal() - } - - if len(packedPk) != scheme.PublicKeySize() { - t.Fatal() - } - - packedSk, err := sk.MarshalBinary() - if err != nil { - t.Fatal() - } - - if len(packedSk) != scheme.PrivateKeySize() { - t.Fatal() - } - - pk2, err := scheme.UnmarshalBinaryPublicKey(packedPk) - if err != nil { - t.Fatal() - } - - sk2, err := scheme.UnmarshalBinaryPrivateKey(packedSk) - if err != nil { - t.Fatal() - } - - if !sk.Equal(sk2) { - t.Fatal() - } - - if !pk.Equal(pk2) { - t.Fatal() - } - - ct, ss, err := scheme.Encapsulate(pk2) - if err != nil { - t.Fatal(err) - } - if len(ct) != scheme.CiphertextSize() { - t.Fatal() - } - if len(ss) != scheme.SharedKeySize() { - t.Fatal() - } - - ct3, ss3, err := scheme.Encapsulate(pk2) - if err != nil { - t.Fatal(err) - } - if bytes.Equal(ss3, ss) { - t.Fatal() - } - if bytes.Equal(ct3, ct) { - t.Fatal() - } - - ss2, err := scheme.Decapsulate(sk2, ct) - if err != nil { - t.Fatal(err) - } - if !bytes.Equal(ss, ss2) { - t.Fatal() - } - }) - } -} - -func Example_schemes() { - // import "github.com/cloudflare/circl/pke/ntruprime/kem/schemes/sntrup" - - for _, sch := range schemes.All() { - fmt.Println(sch.Name()) - } - // Output: - // sntrup653 - // sntrup761 - // sntrup857 - // sntrup953 - // sntrup1013 - // sntrup1277 -}