Skip to content

Instantly share code, notes, and snippets.

@itzmeanjan
Last active December 12, 2022 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itzmeanjan/d14afc3866b82119221682f0f3c9822d to your computer and use it in GitHub Desktop.
Save itzmeanjan/d14afc3866b82119221682f0f3c9822d to your computer and use it in GitHub Desktop.
Git Patch to Generate Known Answer Tests ( KATs ) from Dilithium Reference Implementation
diff --git a/ref/hex_print.h b/ref/hex_print.h
new file mode 100644
index 0000000..7afd872
--- /dev/null
+++ b/ref/hex_print.h
@@ -0,0 +1,9 @@
+#include <stdio.h>
+#include <stdint.h>
+
+inline void to_hex(const uint8_t *const bytes, const size_t blen) {
+ for(size_t i = 0; i < blen; i++) {
+ printf("%.2x", bytes[i]);
+ }
+ printf("\n");
+}
diff --git a/ref/sign.c b/ref/sign.c
index 5d0455c..e92aa9d 100644
--- a/ref/sign.c
+++ b/ref/sign.c
@@ -7,6 +7,7 @@
#include "randombytes.h"
#include "symmetric.h"
#include "fips202.h"
+#include "hex_print.h"
/*************************************************
* Name: crypto_sign_keypair
@@ -30,6 +31,9 @@ int crypto_sign_keypair(uint8_t *pk, uint8_t *sk) {
/* Get randomness for rho, rhoprime and key */
randombytes(seedbuf, SEEDBYTES);
+ printf("seed = ");
+ to_hex(seedbuf, SEEDBYTES);
+
shake256(seedbuf, 2*SEEDBYTES + CRHBYTES, seedbuf, SEEDBYTES);
rho = seedbuf;
rhoprime = rho + SEEDBYTES;
@@ -61,6 +65,11 @@ int crypto_sign_keypair(uint8_t *pk, uint8_t *sk) {
shake256(tr, SEEDBYTES, pk, CRYPTO_PUBLICKEYBYTES);
pack_sk(sk, rho, tr, key, &t0, &s1, &s2);
+ printf("pkey = ");
+ to_hex(pk, CRYPTO_PUBLICKEYBYTES);
+ printf("skey = ");
+ to_hex(sk, CRYPTO_SECRETKEYBYTES);
+
return 0;
}
@@ -83,6 +92,10 @@ int crypto_sign_signature(uint8_t *sig,
size_t mlen,
const uint8_t *sk)
{
+ printf("mlen = %zu\n", mlen);
+ printf("msg = ");
+ to_hex(m, mlen);
+
unsigned int n;
uint8_t seedbuf[3*SEEDBYTES + 2*CRHBYTES];
uint8_t *rho, *tr, *key, *mu, *rhoprime;
@@ -174,6 +187,11 @@ rej:
/* Write signature */
pack_sig(sig, sig, &z, &h);
*siglen = CRYPTO_BYTES;
+
+ printf("sig = ");
+ to_hex(sig, *siglen);
+ printf("\n");
+
return 0;
}
@itzmeanjan
Copy link
Author

itzmeanjan commented Nov 5, 2022

Steps to follow for generating Dilithium{2, 3, 5} Known Answer Tests

Note

These KATs are used to test correctness & compatibility of Dilithium implementation https://github.com/itzmeanjan/dilithium

  • Create working directory
cd
mkdir tmp
cd tmp
  • Clone Dilithium reference implementation
git clone https://github.com/pq-crystals/dilithium.git
git checkout 3e9b9f1412f6c7435dbeb4e10692ea58f181ee51

Warning

Don't forget to setup environment following Dilithium documents ( see the README.md in Dilithium repository ).

  • Clone this gist ( the git patch file )
git clone https://gist.github.com/d14afc3866b82119221682f0f3c9822d.git
  • Copy patch file to Dilithium repository
cp c8f5bc9640d0f0bdd2437dfe364d7710/dilithium_kat_generation.patch dilithium/
  • Apply git patch
cd dilithium
git apply dilithium_kat_generation.patch
  • Check status to find out which files were touched
git status
  • Generate KATs for Dilithium{2, 3, 5} ( see content of file dilithium{2,3,5}.kat )
pushd ref

make PQCgenKAT_sign2 && ./PQCgenKAT_sign2 > ../dilithium2.kat
make PQCgenKAT_sign3 && ./PQCgenKAT_sign3 > ../dilithium3.kat
make PQCgenKAT_sign5 && ./PQCgenKAT_sign5 > ../dilithium5.kat

popd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment