Browse Source

moves KeyVariant to api.go and removes PrimeFieldId

KeyVariant type needs to be available in API. Instead of using
PrimeFieldId we can simply use uint8
master
Henry Case 6 years ago
parent
commit
bfceb6190a
5 changed files with 30 additions and 32 deletions
  1. +1
    -5
      internal/isogeny/types.go
  2. +6
    -3
      sidh/api.go
  3. +2
    -2
      sidh/params.go
  4. +9
    -9
      sidh/sidh_test.go
  5. +12
    -13
      sike/sike_test.go

+ 1
- 5
internal/isogeny/types.go View File

@@ -4,10 +4,6 @@ const (
FP_MAX_WORDS = 12 // Currently p751.NumWords
)

// I keep it bool in order to be able to apply logical NOT
type KeyVariant uint
type PrimeFieldId uint

// Representation of an element of the base field F_p.
//
// No particular meaning is assigned to the representation -- it could represent
@@ -36,7 +32,7 @@ type DomainParams struct {
}

type SidhParams struct {
Id PrimeFieldId
Id uint8
// Bytelen of P
Bytelen int
// The public key size, in bytes.


+ 6
- 3
sidh/api.go View File

@@ -6,10 +6,13 @@ import (
"io"
)

// I keep it bool in order to be able to apply logical NOT
type KeyVariant uint

// Id's correspond to bitlength of the prime field characteristic
// Currently FP_751 is the only one supported by this implementation
const (
FP_503 PrimeFieldId = iota
FP_503 uint8 = iota
FP_751
FP_964
maxPrimeFieldId
@@ -65,7 +68,7 @@ func (key *key) Variant() KeyVariant {

// NewPrivateKey initializes private key.
// Usage of this function guarantees that the object is correctly initialized.
func NewPrivateKey(id PrimeFieldId, v KeyVariant) *PrivateKey {
func NewPrivateKey(id uint8, v KeyVariant) *PrivateKey {
prv := &PrivateKey{key: key{params: Params(id), keyVariant: v}}
if (v & KeyVariant_SIDH_A) == KeyVariant_SIDH_A {
prv.Scalar = make([]byte, prv.params.A.SecretByteLen)
@@ -80,7 +83,7 @@ func NewPrivateKey(id PrimeFieldId, v KeyVariant) *PrivateKey {

// NewPublicKey initializes public key.
// Usage of this function guarantees that the object is correctly initialized.
func NewPublicKey(id PrimeFieldId, v KeyVariant) *PublicKey {
func NewPublicKey(id uint8, v KeyVariant) *PublicKey {
return &PublicKey{key: key{params: Params(id), keyVariant: v}}
}



+ 2
- 2
sidh/params.go View File

@@ -7,11 +7,11 @@ import (
)

// Keeps mapping: SIDH prime field ID to domain parameters
var sidhParams = make(map[PrimeFieldId]SidhParams)
var sidhParams = make(map[uint8]SidhParams)

// Params returns domain parameters corresponding to finite field and identified by
// `id` provieded by the caller. Function panics in case `id` wasn't registered earlier.
func Params(id PrimeFieldId) *SidhParams {
func Params(id uint8) *SidhParams {
if val, ok := sidhParams[id]; ok {
return &val
}


+ 9
- 9
sidh/sidh_test.go View File

@@ -14,7 +14,7 @@ import (
/* -------------------------------------------------------------------------
Test data
-------------------------------------------------------------------------*/
var tdata = map[PrimeFieldId]struct {
var tdata = map[uint8]struct {
name string
PkA string
PrA string
@@ -100,7 +100,7 @@ func checkErr(t testing.TB, err error, msg string) {
}

// Utility used for running same test with all registered prime fields
type MultiIdTestingFunc func(testing.TB, PrimeFieldId)
type MultiIdTestingFunc func(testing.TB, uint8)

func Do(f MultiIdTestingFunc, t testing.TB) {
for id, val := range tdata {
@@ -110,7 +110,7 @@ func Do(f MultiIdTestingFunc, t testing.TB) {
}

// Converts string to private key
func convToPrv(s string, v KeyVariant, id PrimeFieldId) *PrivateKey {
func convToPrv(s string, v KeyVariant, id uint8) *PrivateKey {
key := NewPrivateKey(id, v)
hex, e := hex.DecodeString(s)
if e != nil {
@@ -124,7 +124,7 @@ func convToPrv(s string, v KeyVariant, id PrimeFieldId) *PrivateKey {
}

// Converts string to public key
func convToPub(s string, v KeyVariant, id PrimeFieldId) *PublicKey {
func convToPub(s string, v KeyVariant, id uint8) *PublicKey {
key := NewPublicKey(id, v)
hex, e := hex.DecodeString(s)
if e != nil {
@@ -140,7 +140,7 @@ func convToPub(s string, v KeyVariant, id PrimeFieldId) *PublicKey {
/* -------------------------------------------------------------------------
Unit tests
-------------------------------------------------------------------------*/
func testKeygen(t testing.TB, id PrimeFieldId) {
func testKeygen(t testing.TB, id uint8) {
alicePrivate := convToPrv(tdata[id].PrA, KeyVariant_SIDH_A, id)
bobPrivate := convToPrv(tdata[id].PrB, KeyVariant_SIDH_B, id)
expPubA := convToPub(tdata[id].PkA, KeyVariant_SIDH_A, id)
@@ -157,7 +157,7 @@ func testKeygen(t testing.TB, id PrimeFieldId) {
}
}

func testRoundtrip(t testing.TB, id PrimeFieldId) {
func testRoundtrip(t testing.TB, id uint8) {
var err error

prvA := NewPrivateKey(id, KeyVariant_SIDH_A)
@@ -185,7 +185,7 @@ func testRoundtrip(t testing.TB, id PrimeFieldId) {
}
}

func testKeyAgreement(t testing.TB, id PrimeFieldId, pkA, prA, pkB, prB string) {
func testKeyAgreement(t testing.TB, id uint8, pkA, prA, pkB, prB string) {
var e error

// KeyPairs
@@ -225,7 +225,7 @@ func testKeyAgreement(t testing.TB, id PrimeFieldId, pkA, prA, pkB, prB string)
}
}

func testImportExport(t testing.TB, id PrimeFieldId) {
func testImportExport(t testing.TB, id uint8) {
var err error
a := NewPublicKey(id, KeyVariant_SIDH_A)
b := NewPublicKey(id, KeyVariant_SIDH_B)
@@ -253,7 +253,7 @@ func testImportExport(t testing.TB, id PrimeFieldId) {
}
}

func testPrivateKeyBelowMax(t testing.TB, id PrimeFieldId) {
func testPrivateKeyBelowMax(t testing.TB, id uint8) {
params := Params(id)
for variant, keySz := range map[KeyVariant]*DomainParams{
KeyVariant_SIDH_A: &params.A,


+ 12
- 13
sike/sike_test.go View File

@@ -13,11 +13,10 @@ import (
"fmt"

rand "crypto/rand"
. "github.com/cloudflare/p751sidh/internal/isogeny"
. "github.com/cloudflare/p751sidh/sidh"
)

type MultiIdTestingFunc func(*testing.T, PrimeFieldId)
type MultiIdTestingFunc func(*testing.T, uint8)

func Do(f MultiIdTestingFunc, t *testing.T) {
for id, val := range tdata {
@@ -26,7 +25,7 @@ func Do(f MultiIdTestingFunc, t *testing.T) {
}
}

var tdata = map[PrimeFieldId]struct {
var tdata = map[uint8]struct {
name string
KatFile string
PkB string
@@ -52,7 +51,7 @@ func checkErr(t testing.TB, err error, msg string) {
}

// Encrypt, Decrypt, check if input/output plaintext is the same
func testPKERoundTrip(t *testing.T, id PrimeFieldId) {
func testPKERoundTrip(t *testing.T, id uint8) {
// Message to be encrypted
var params = Params(id)
var msg = make([]byte, params.MsgLen)
@@ -89,7 +88,7 @@ func testPKERoundTrip(t *testing.T, id PrimeFieldId) {
}

// Generate key and check if can encrypt
func testPKEKeyGeneration(t *testing.T, id PrimeFieldId) {
func testPKEKeyGeneration(t *testing.T, id uint8) {
// Message to be encrypted
var params = Params(id)
var msg = make([]byte, params.MsgLen)
@@ -114,7 +113,7 @@ func testPKEKeyGeneration(t *testing.T, id PrimeFieldId) {
}
}

func testNegativePKE(t *testing.T, id PrimeFieldId) {
func testNegativePKE(t *testing.T, id uint8) {
var msg [40]byte
var err error
var params = Params(id)
@@ -145,7 +144,7 @@ func testNegativePKE(t *testing.T, id PrimeFieldId) {
}
}

func testKEMRoundTrip(t *testing.T, pkB, skB []byte, id PrimeFieldId) {
func testKEMRoundTrip(t *testing.T, pkB, skB []byte, id uint8) {
// Import keys
pk := NewPublicKey(id, KeyVariant_SIKE)
sk := NewPrivateKey(id, KeyVariant_SIKE)
@@ -178,7 +177,7 @@ func TestKEMRoundTrip(t *testing.T) {
}
}

func testKEMKeyGeneration(t *testing.T, id PrimeFieldId) {
func testKEMKeyGeneration(t *testing.T, id uint8) {
// Generate key
sk := NewPrivateKey(id, KeyVariant_SIKE)
checkErr(t, sk.Generate(rand.Reader), "error: key generation")
@@ -195,7 +194,7 @@ func testKEMKeyGeneration(t *testing.T, id PrimeFieldId) {
}
}

func testNegativeKEM(t *testing.T, id PrimeFieldId) {
func testNegativeKEM(t *testing.T, id uint8) {
sk := NewPrivateKey(id, KeyVariant_SIKE)
checkErr(t, sk.Generate(rand.Reader), "error: key generation")
pk := sk.GeneratePublicKey()
@@ -228,7 +227,7 @@ func testNegativeKEM(t *testing.T, id PrimeFieldId) {

// In case invalid ciphertext is provided, SIKE's decapsulation must
// return same (but unpredictable) result for a given key.
func testNegativeKEMSameWrongResult(t *testing.T, id PrimeFieldId) {
func testNegativeKEMSameWrongResult(t *testing.T, id uint8) {
sk := NewPrivateKey(id, KeyVariant_SIKE)
checkErr(t, sk.Generate(rand.Reader), "error: key generation")
pk := sk.GeneratePublicKey()
@@ -283,7 +282,7 @@ func readAndCheckLine(r *bufio.Reader) []byte {
return ret
}

func testKeygen(pk, sk []byte, id PrimeFieldId) bool {
func testKeygen(pk, sk []byte, id uint8) bool {
// Import provided private key
var prvKey = NewPrivateKey(id, KeyVariant_SIKE)
if prvKey.Import(sk) != nil {
@@ -295,7 +294,7 @@ func testKeygen(pk, sk []byte, id PrimeFieldId) bool {
return bytes.Equal(pubKey.Export(), pk)
}

func testDecapsulation(pk, sk, ct, ssExpected []byte, id PrimeFieldId) bool {
func testDecapsulation(pk, sk, ct, ssExpected []byte, id uint8) bool {
var pubKey = NewPublicKey(id, KeyVariant_SIKE)
var prvKey = NewPrivateKey(id, KeyVariant_SIKE)
if pubKey.Import(pk) != nil || prvKey.Import(sk) != nil {
@@ -313,7 +312,7 @@ func testDecapsulation(pk, sk, ct, ssExpected []byte, id PrimeFieldId) bool {
return bytes.Equal(ssGot, ssExpected)
}

func testSIKE_KAT(t *testing.T, id PrimeFieldId) {
func testSIKE_KAT(t *testing.T, id uint8) {
params := Params(id)
f, err := os.Open(tdata[id].KatFile)
if err != nil {


Loading…
Cancel
Save