### ----------------------------------------------------
### -- Math::NumberTheory
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: t/03-chinese-remainder.rakutest
### ----------------------------------------------------
use v6.d;
use Test;

#use lib <. lib>;
use Math::NumberTheory;

plan *;

## 1
subtest {
    is chinese-remainder([3, 4], [4, 5]), 19;
    is chinese-remainder([2, 3, 5], [3, 5, 7]), 68;
}, "basic";

## 2
subtest {

    # Data
    my @data = 931074546, 117172357, 482333642, 199386034, 394354985;

    # Small value keys
    #my @keys = 8147, 6073, 7919, 5431, 7549;
    #my $encrypted = chinese-remainder(@data, @keys);
    #is $encrypted, 9881138511053968140;

    # Using these larger keys is also a performance check
    my @keys = 274199185649, 786765306443, 970592805341, 293623796783, 238475031661;

    # Encrypted data
    my $encrypted = chinese-remainder(@data, @keys);

    is $encrypted, 6681669841357504673192908619871066558177944924838942629020;

    my @decrypted = @keys.map($encrypted mod *);

    is-deeply @decrypted, @data;

}, "database encryption and decryption";

subtest {
    nok chinese-remainder([1, 2], [2 * 3, 2 * 5]);
}, "no solutions";

done-testing;


