#!/bin/bash

# this generates known value tests for various csidh parameters

declare -a configuration=(\
    "--prime p512 --style df --exponent 10"  \
    "--prime p512 --style wd1 --exponent 10" \
    "--prime p512 --style wd2 --exponent 5" \
    "--prime p1024 --style df --exponent 3" \
    "--prime p1024 --style wd1 --exponent 3" \
    "--prime p1024 --style wd2 --exponent 2" \
    "--prime p1792 --style df --exponent 2" \
    "--prime p1792 --style wd1 --exponent 2" \
    "--prime p1792 --style wd2 --exponent 1"
    )

create_known_answer_script() {
    echo "echo 'starting test of $@'"

    sk_a="$(sibc "$@" csidh-genkey)"
    echo "sk_a='$sk_a'"
    pk_a="$(echo "$sk_a"|sibc "$@" csidh-pubkey -)"
    echo "pk_a='$pk_a'"
    echo '[ "$pk_a" != "$(echo "$sk_a"|sibc '"$@"' csidh-pubkey -)" ] && fail=1 && echo "fail: pk($sk_a) -> $pk_a" || echo "OK: pk_a '"$@"'"'

    sk_b="$(sibc "$@" csidh-genkey)"
    echo "sk_b='$sk_b'"
    pk_b="$(echo "$sk_b"|sibc "$@" csidh-pubkey -)"
    echo "pk_b='$pk_b'"
    echo '[ "$pk_b" != "$(echo "$sk_b"|sibc '"$@"' csidh-pubkey -)" ] && fail=1 && echo "fail: pk($sk_b) -> $pk_b" || echo "OK: pk_b '"$@"'"'

    ss_a="$(echo "$sk_a"|sibc "$@" csidh-dh - "$pk_b")"
    echo "ss_a='$ss_a'"
    echo '[ "$ss_a" != "$(echo "$sk_a"|sibc '"$@"' csidh-dh - "$pk_b")" ] && fail=1 && echo "fail: dh($sk_a,$pk_b) -> $ss_a" || echo "OK: ss_a '"$@"'"'

    ss_b="$(echo "$sk_b"|sibc "$@" csidh-dh - "$pk_a")"
    echo "ss_b='$ss_b'"
    echo '[ "$ss_b" != "$(echo "$sk_b"|sibc '"$@"' csidh-dh - "$pk_a")" ] && fail=1 && echo "fail: dh($sk_b,$pk_a) -> $ss_b" || echo "OK: ss_b '"$@"'"'

    [ "$ss_a" != "$ss_b" ] && echo "fail: $@" && exit 1

    echo '[ "$ss_a" != "$ss_b" ] && fail=1 && echo "fail: $ss_a != $ss_b" || echo "OK: same '"$@"'"'
    echo
}

echo '# script generated by create-tests.sh'
echo

for i in `eval echo {0..${#configuration[@]}}`; do
    echo
    echo "# $(date) # ${configuration[$i]}"
    for multievaluation in "--multievaluation" ""; do
        for tuned in "--tuned" ""; do
            for formula in tvelu svelu hvelu; do
                if [ "$formula" == "tvelu" ] && ([ "$multievaluation" != "" ] || [ "$tuned" != "" ])
                then
                    continue
                fi
                echo
                create_known_answer_script ${configuration[$i]} --formula $formula $multievaluation $tuned
            done
        done
    done
done
echo
echo "# create-tests.sh finished at $(date)"

