static int testPack(PQ_PARAM_SET_ID id) { int i; int T; int rc; PQ_PARAM_SET *P; if(!(P = pq_get_param_set_by_id(id))) { return -1; } unsigned char *scratch; uint16_t *iF; uint16_t *oF; uint16_t *ig; uint16_t *og; int64_t *iginv; int64_t *oginv; int64_t *ih; int64_t *oh; int64_t *isig; int64_t *osig; unsigned char *priv_blob; unsigned char *pub_blob; unsigned char *sig_blob; size_t prod = 2*(P->d1 + P->d2 + P->d3)*sizeof(uint16_t); size_t full = P->N*sizeof(int64_t); size_t priv_blob_len = PRIVKEY_PACKED_BYTES(P); size_t pub_blob_len = PUBKEY_PACKED_BYTES(P); size_t sig_len = SIGNATURE_BYTES(P); size_t offset; scratch = malloc(4*prod + 6*full + priv_blob_len + pub_blob_len + sig_len); offset = 0; iF = (uint16_t*)(scratch + offset); offset += prod; oF = (uint16_t*)(scratch + offset); offset += prod; ig = (uint16_t*)(scratch + offset); offset += prod; og = (uint16_t*)(scratch + offset); offset += prod; iginv = (int64_t*)(scratch + offset); offset += full; oginv = (int64_t*)(scratch + offset); offset += full; isig = (int64_t*)(scratch + offset); offset += full; osig = (int64_t*)(scratch + offset); offset += full; ih = (int64_t*)(scratch + offset); offset += full; oh = (int64_t*)(scratch + offset); offset += full; priv_blob = (unsigned char*)(scratch + offset); offset += priv_blob_len; pub_blob = (unsigned char*)(scratch + offset); offset += pub_blob_len; sig_blob = (unsigned char*)(scratch + offset); offset += sig_len; for(T=0; T<TIMES; T++) { fastrandombytes(scratch, 4*prod + 6*full + priv_blob_len + pub_blob_len + sig_len); for(i = 0; i < prod; i++) { iF[i] = iF[i] % P->N; ig[i] = ig[i] % P->N; } for(i=0; i < P->N; i++) { iginv[i] = cmod(iginv[i], P->p); ih[i] = cmod(ih[i], P->q); isig[i] = isig[i] % P->q; isig[i] = (isig[i] + P->q) % P->q; isig[i] -= isig[i] % P->p; isig[i] /= P->p; } rc = pack_private_key(P, iF, ig, iginv, priv_blob_len, priv_blob); if(PQNTRU_ERROR == rc) { printf("Private key pack error\n"); return -1; } rc = unpack_private_key(P, oF, og, oginv, priv_blob_len, priv_blob); if(PQNTRU_ERROR == rc) { printf("Private key unpack error\n"); return -1; } rc = pack_public_key(P, ih, pub_blob_len, pub_blob); if(PQNTRU_ERROR == rc) { printf("Public key pack error\n"); return -1; } rc = unpack_public_key(P, oh, pub_blob_len, pub_blob); if(PQNTRU_ERROR == rc) { printf("Public key unpack error\n"); return -1; } rc = pack_signature(P, isig, sig_len, sig_blob); if(PQNTRU_ERROR == rc) { printf("Signature pack error\n"); return -1; } rc = unpack_signature(P, osig, sig_len, sig_blob); if(PQNTRU_ERROR == rc) { printf("Signature unpack error\n"); return -1; } for(i=0; i<2*(P->d1 + P->d2 + P->d3); i++) { if(iF[i] != oF[i] || ig[i] != og[i]) { printf("product form keys not equal\n"); break; } } for(i=0; i<P->N; i++) { oh[i] = cmod(oh[i], P->q); oginv[i] = cmod(oginv[i], P->p); if(ih[i] != oh[i] || iginv[i] != oginv[i]) { printf("%d %ld %ld %ld %ld\n", i, ih[i], oh[i], iginv[i], oginv[i]); printf("public key or iginv not equal\n"); return -1; } if(isig[i] != osig[i]) { printf("%d %ld %ld\n", i, isig[i], osig[i]); printf("signatures not equal\n"); return -1; } } } }
int pq_verify( const size_t packed_sig_len, const unsigned char *packed_sig, const size_t public_key_blob_len, const unsigned char *public_key_blob, const size_t msg_len, const unsigned char *msg) { uint16_t i; PQ_PARAM_SET *P; size_t scratch_len; unsigned char *scratch; int8_t *sp; int8_t *tp; int64_t *h; int64_t *sig; int64_t *tmpx3; uint16_t N; uint16_t padN; int64_t q; int8_t p; uint16_t error = 0; int result; result = get_blob_params(&P, public_key_blob_len, public_key_blob); if(PQNTRU_ERROR == result) { return PQNTRU_ERROR; } N = P->N; padN = P->padded_N; p = P->p; q = P->q; scratch_len = 2 * N + 5 * POLYNOMIAL_BYTES(P); if(!(scratch = malloc(scratch_len))) { return PQNTRU_ERROR; } memset(scratch, 0, scratch_len); sig = (int64_t*)scratch; h = (int64_t*)(scratch + POLYNOMIAL_BYTES(P)); tmpx3 = (int64_t*)(scratch + 2*POLYNOMIAL_BYTES(P)); sp = (int8_t*)(scratch + 5*POLYNOMIAL_BYTES(P)); tp = (int8_t*)(scratch + 5*POLYNOMIAL_BYTES(P) + N); result = unpack_public_key(P, h, public_key_blob_len, public_key_blob); if(PQNTRU_ERROR == result) { free(scratch); return PQNTRU_ERROR; } challenge(sp, tp, public_key_blob_len, public_key_blob, msg_len, msg); result = unpack_signature(P, sig, sp, packed_sig_len, packed_sig); if(PQNTRU_ERROR == result) { free(scratch); return PQNTRU_ERROR; } for(i=0; i<N; i++) { error |= (cmod(sig[i] - sp[i], p)); error |= (sig[i] > P->norm_bound_s) || (-sig[i] > P->norm_bound_s); } pol_mul_coefficients(tmpx3, sig, h, N, padN, q, tmpx3); for(i=0; i<N; i++) { error |= (cmod(tmpx3[i], p) - tp[i]); error |= (tmpx3[i] > P->norm_bound_t) || (-tmpx3[i] > P->norm_bound_t) ; } free(scratch); if(0 == error) { return PQNTRU_OK; } return PQNTRU_ERROR; }