cryptokemkeypair function
Generates a Kyber key pair consisting of a public key pk
and a private key sk
.
The function first generates an IND-CPA key pair and stores the public key pk
and
the private key sk
. It then hashes the public key and appends this hash to the private key.
Next, it generates a random value z
and appends it to the private key. The function returns
0 upon successful execution.
Parameters:
pk
: The public key, which must have space forKYBER_PUBLICKEYBYTES
bytes.sk
: The private key, which must have space forKYBER_SECRETKEYBYTES
bytes.
Returns:
int
: Always returns 0, indicating success.
Implementation
int cryptokemkeypair(Uint8List pk, Uint8List sk) {
// generate IND-CPA keypair
indcpakeypair(pk, sk);
// store pk at the end of sk
for (int i = 0; i < KYBER_PUBLICKEYBYTES; i++) {
sk[KYBER_SECRETKEYBYTES - KYBER_PUBLICKEYBYTES - KYBER_SYMBYTES + i] =
pk[i];
}
// hash(pk)
Uint8List hashPk = shake128(pk, KYBER_SYMBYTES);
// store hash of pk at the end of sk (after pk)
for (int i = 0; i < KYBER_SYMBYTES; i++) {
sk[KYBER_SECRETKEYBYTES - KYBER_SYMBYTES + i] = hashPk[i];
}
// generate random z
Uint8List z = randombytes(KYBER_SYMBYTES);
// store z at the end of sk
for (int i = 0; i < KYBER_SYMBYTES; i++) {
sk[KYBER_SECRETKEYBYTES - KYBER_SYMBYTES * 2 + i] = z[i];
}
return 0;
}