How to print coefficients of CKKS private key

I know that in CKKS (well, the same holds for BGV, FV, etc) the secret key is just a polynomial in \mathbb{Z}[X] / \langle X^N + 1 \rangle, that is, integer coefficients and degree smaller than N. Moreover, the coefficients are generally small (belonging to the set {-1, 0, 1} or following a discrete Gaussian with small variance).

Thus, I would like to print these coefficients.

If I try

CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
auto keys = cc->KeyGen();
std::cout << "Secret key: " << keys.secretKey << std::endl;

it just prints some memory address.

I also tried to use the methods defined in privatekey.h, but this gives me compilation errors.

For example, trying keys.secretKey.GetPrivateElement() produces the error message

error: ‘lbcrypto::PrivateKey<lbcrypto::DCRTPolyImpl<bigintdyn::mubintvec<bigintdyn::ubint > > > {aka class std::shared_ptr<lbcrypto::PrivateKeyImpl<lbcrypto::DCRTPolyImpl<bigintdyn::mubintvec<bigintdyn::ubint > > > >}’ has no member named ‘GetPrivateElement’

@hilder

I’ve modified your question to include LaTeX typesetting. Do LMK if that changes the nature of the question

1 Like

keys.secretKey is an std::shared_ptr. Use the line below instead.

std::cout << "Secret key: " << keys.secretKey->GetPrivateElement() << std::endl;
1 Like

Thank you! So, this gives me access to the secret key in double-CRT representation and I could print the coefficients using the method CRTInterpolate().

const DCRTPoly& ckks_sk = keys.secretKey->GetPrivateElement();
std::cout << ckks_sk.CRTInterpolate() << std::endl;