From 5faaf12534b1b0838d1c91a3b1551f54cf60a953 Mon Sep 17 00:00:00 2001 From: Conghao Shen Date: Wed, 22 Jun 2022 12:46:01 -0700 Subject: [PATCH 1/4] feat: mds security hardcoded test --- manta-pay/src/crypto/hash/poseidon/mds.rs | 59 ++++--------------- .../hash/poseidon/mds_hardcoded_tests/width3 | 1 + .../hash/poseidon/mds_hardcoded_tests/width4 | 1 + .../hash/poseidon/mds_hardcoded_tests/width5 | 1 + .../hash/poseidon/mds_hardcoded_tests/width6 | 1 + .../hash/poseidon/mds_hardcoded_tests/width7 | 1 + .../hash/poseidon/mds_hardcoded_tests/width8 | 1 + 7 files changed, 19 insertions(+), 46 deletions(-) create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width3 create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width4 create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width5 create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width6 create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width7 create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width8 diff --git a/manta-pay/src/crypto/hash/poseidon/mds.rs b/manta-pay/src/crypto/hash/poseidon/mds.rs index 1e3cc7155..ab3d9bc5c 100644 --- a/manta-pay/src/crypto/hash/poseidon/mds.rs +++ b/manta-pay/src/crypto/hash/poseidon/mds.rs @@ -358,52 +358,19 @@ mod test { /// Checks if `mds` matches hardcoded sage outputs. #[test] fn mds_matches_hardcoded_sage_output() { - let width = 3; - let expected_mds = Matrix::>::new_unchecked(vec![ - vec![ - Fp(field_new!( - Fr, - "34957250116750793652965160338790643891793701667018425215069105799959054123009" - )), - Fp(field_new!( - Fr, - "39326906381344642859585805381139474378267914375395728366952744024953935888385" - )), - Fp(field_new!( - Fr, - "31461525105075714287668644304911579502614331500316582693562195219963148710708" - )), - ], - vec![ - Fp(field_new!( - Fr, - "39326906381344642859585805381139474378267914375395728366952744024953935888385" - )), - Fp(field_new!( - Fr, - "31461525105075714287668644304911579502614331500316582693562195219963148710708" - )), - Fp(field_new!( - Fr, - "43696562645938492066206450423488304864742127083773031518836382249948817653761" - )), - ], - vec![ - Fp(field_new!( - Fr, - "31461525105075714287668644304911579502614331500316582693562195219963148710708" - )), - Fp(field_new!( - Fr, - "43696562645938492066206450423488304864742127083773031518836382249948817653761" - )), - Fp(field_new!( - Fr, - "14981678621464625851270783002338847382197300714436467949315331057125308909861" - )), - ], - ]); - assert_eq!(MdsMatrices::>::generate_mds(width), expected_mds); + let test_cases = [ + (3, include!("mds_hardcoded_tests/width3")), + (4, include!("mds_hardcoded_tests/width4")), + (5, include!("mds_hardcoded_tests/width5")), + (6, include!("mds_hardcoded_tests/width6")), + (7, include!("mds_hardcoded_tests/width7")), + (8, include!("mds_hardcoded_tests/width8")), + ]; + + for (width, matrix) in test_cases { + let expected_mds = Matrix::>::new_unchecked(matrix); + assert_eq!(MdsMatrices::>::generate_mds(width), expected_mds); + } } /// Checks if mds is invertible. diff --git a/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width3 b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width3 new file mode 100644 index 000000000..fa76e68cb --- /dev/null +++ b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width3 @@ -0,0 +1 @@ +vec![vec![Fp(field_new!(Fr, "34957250116750793652965160338790643891793701667018425215069105799959054123009")),Fp(field_new!(Fr, "39326906381344642859585805381139474378267914375395728366952744024953935888385")),Fp(field_new!(Fr, "31461525105075714287668644304911579502614331500316582693562195219963148710708"))],vec![Fp(field_new!(Fr, "39326906381344642859585805381139474378267914375395728366952744024953935888385")),Fp(field_new!(Fr, "31461525105075714287668644304911579502614331500316582693562195219963148710708")),Fp(field_new!(Fr, "43696562645938492066206450423488304864742127083773031518836382249948817653761"))],vec![Fp(field_new!(Fr, "31461525105075714287668644304911579502614331500316582693562195219963148710708")),Fp(field_new!(Fr, "43696562645938492066206450423488304864742127083773031518836382249948817653761")),Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861"))]] \ No newline at end of file diff --git a/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width4 b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width4 new file mode 100644 index 000000000..13b09723b --- /dev/null +++ b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width4 @@ -0,0 +1 @@ +vec![vec![Fp(field_new!(Fr, "39326906381344642859585805381139474378267914375395728366952744024953935888385")),Fp(field_new!(Fr, "31461525105075714287668644304911579502614331500316582693562195219963148710708")),Fp(field_new!(Fr, "43696562645938492066206450423488304864742127083773031518836382249948817653761")),Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861"))],vec![Fp(field_new!(Fr, "31461525105075714287668644304911579502614331500316582693562195219963148710708")),Fp(field_new!(Fr, "43696562645938492066206450423488304864742127083773031518836382249948817653761")),Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861")),Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449"))],vec![Fp(field_new!(Fr, "43696562645938492066206450423488304864742127083773031518836382249948817653761")),Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861")),Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003"))],vec![Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861")),Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354"))]] \ No newline at end of file diff --git a/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width5 b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width5 new file mode 100644 index 000000000..10b315760 --- /dev/null +++ b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width5 @@ -0,0 +1 @@ +vec![vec![Fp(field_new!(Fr, "31461525105075714287668644304911579502614331500316582693562195219963148710708")),Fp(field_new!(Fr, "43696562645938492066206450423488304864742127083773031518836382249948817653761")),Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861")),Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003"))],vec![Fp(field_new!(Fr, "43696562645938492066206450423488304864742127083773031518836382249948817653761")),Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861")),Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354"))],vec![Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861")),Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921"))],vec![Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137"))],vec![Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588"))]] \ No newline at end of file diff --git a/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width6 b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width6 new file mode 100644 index 000000000..a552735dc --- /dev/null +++ b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width6 @@ -0,0 +1 @@ +vec![vec![Fp(field_new!(Fr, "43696562645938492066206450423488304864742127083773031518836382249948817653761")),Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861")),Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921"))],vec![Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861")),Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137"))],vec![Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588"))],vec![Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187"))],vec![Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407"))],vec![Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481"))]] \ No newline at end of file diff --git a/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width7 b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width7 new file mode 100644 index 000000000..666448660 --- /dev/null +++ b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width7 @@ -0,0 +1 @@ +vec![vec![Fp(field_new!(Fr, "14981678621464625851270783002338847382197300714436467949315331057125308909861")),Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588"))],vec![Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187"))],vec![Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407"))],vec![Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481"))],vec![Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481")),Fp(field_new!(Fr, "12337852982382633053987703648984933138280130000124150075906743223514960278709"))],vec![Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481")),Fp(field_new!(Fr, "12337852982382633053987703648984933138280130000124150075906743223514960278709")),Fp(field_new!(Fr, "32044145940354894181884730310558090234144226528100223113813346983295799612758"))],vec![Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481")),Fp(field_new!(Fr, "12337852982382633053987703648984933138280130000124150075906743223514960278709")),Fp(field_new!(Fr, "32044145940354894181884730310558090234144226528100223113813346983295799612758")),Fp(field_new!(Fr, "49676092271172180454213648902491967635706839211026183200361360873626024280065"))]] \ No newline at end of file diff --git a/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width8 b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width8 new file mode 100644 index 000000000..37e29962c --- /dev/null +++ b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width8 @@ -0,0 +1 @@ +vec![vec![Fp(field_new!(Fr, "45881390778235416669516772944662720107979233437961683094778201362446258536449")),Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407"))],vec![Fp(field_new!(Fr, "11652416705583597884321720112930214630597900555672808405023035266653018041003")),Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481"))],vec![Fp(field_new!(Fr, "15730762552537857143834322152455789751307165750158291346781097609981574355354")),Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481")),Fp(field_new!(Fr, "12337852982382633053987703648984933138280130000124150075906743223514960278709"))],vec![Fp(field_new!(Fr, "47668977431932900435861582280169059852445956818661488929639689727216891985921")),Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481")),Fp(field_new!(Fr, "12337852982382633053987703648984933138280130000124150075906743223514960278709")),Fp(field_new!(Fr, "32044145940354894181884730310558090234144226528100223113813346983295799612758"))],vec![Fp(field_new!(Fr, "48066218910532341272827095465837135351216339792150334670720020474943699419137")),Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481")),Fp(field_new!(Fr, "12337852982382633053987703648984933138280130000124150075906743223514960278709")),Fp(field_new!(Fr, "32044145940354894181884730310558090234144226528100223113813346983295799612758")),Fp(field_new!(Fr, "49676092271172180454213648902491967635706839211026183200361360873626024280065"))],vec![Fp(field_new!(Fr, "44368817455876007328763472737695817247276621346600308926818480438409568694588")),Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481")),Fp(field_new!(Fr, "12337852982382633053987703648984933138280130000124150075906743223514960278709")),Fp(field_new!(Fr, "32044145940354894181884730310558090234144226528100223113813346983295799612758")),Fp(field_new!(Fr, "49676092271172180454213648902491967635706839211026183200361360873626024280065")),Fp(field_new!(Fr, "7865381276268928571917161076227894875653582875079145673390548804990787177677"))],vec![Fp(field_new!(Fr, "33708776898295408165359261755262406609943926607482052885959494878531945047187")),Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481")),Fp(field_new!(Fr, "12337852982382633053987703648984933138280130000124150075906743223514960278709")),Fp(field_new!(Fr, "32044145940354894181884730310558090234144226528100223113813346983295799612758")),Fp(field_new!(Fr, "49676092271172180454213648902491967635706839211026183200361360873626024280065")),Fp(field_new!(Fr, "7865381276268928571917161076227894875653582875079145673390548804990787177677")),Fp(field_new!(Fr, "4993892873821541950423594334112949127399100238145489316438443685708436303287"))],vec![Fp(field_new!(Fr, "27965800093400634922372128271032515113434961333614740172055284639967243298407")),Fp(field_new!(Fr, "49158632976680803574482256726424342972834892969244660458690930031192419860481")),Fp(field_new!(Fr, "12337852982382633053987703648984933138280130000124150075906743223514960278709")),Fp(field_new!(Fr, "32044145940354894181884730310558090234144226528100223113813346983295799612758")),Fp(field_new!(Fr, "49676092271172180454213648902491967635706839211026183200361360873626024280065")),Fp(field_new!(Fr, "7865381276268928571917161076227894875653582875079145673390548804990787177677")),Fp(field_new!(Fr, "4993892873821541950423594334112949127399100238145489316438443685708436303287")),Fp(field_new!(Fr, "50052426303529545457654661394177512845068254659594563376121674213577736585217"))]] \ No newline at end of file From e5bc30d1e4927515bd11519c7516b2cb3be3f929 Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Thu, 23 Jun 2022 00:47:30 -0400 Subject: [PATCH 2/4] feat: add sagescript & hardcoded tests --- CHANGELOG.md | 1 + manta-pay/src/crypto/hash/poseidon/README.md | 27 +++ .../src/crypto/hash/poseidon/constants.rs | 2 +- manta-pay/src/crypto/hash/poseidon/mds.rs | 12 +- .../poseidon/mds_hardcoded_tests/.gitignore | 1 + .../correct_mds_generation.sage | 193 ++++++++++++++++++ .../hash/poseidon/mds_hardcoded_tests/width10 | 1 + .../hash/poseidon/mds_hardcoded_tests/width11 | 1 + .../hash/poseidon/mds_hardcoded_tests/width12 | 1 + .../hash/poseidon/mds_hardcoded_tests/width2 | 1 + .../hash/poseidon/mds_hardcoded_tests/width9 | 1 + 11 files changed, 237 insertions(+), 4 deletions(-) create mode 100644 manta-pay/src/crypto/hash/poseidon/README.md create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/.gitignore create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/correct_mds_generation.sage create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width10 create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width11 create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width12 create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width2 create mode 100644 manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/width9 diff --git a/CHANGELOG.md b/CHANGELOG.md index 1db6fdc92..4e61ca05f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] ### Added +- [\#130](https://github.com/Manta-Network/manta-rs/pull/130) Add the sage script and the hardcoded tests for the security of mds matrix ### Changed diff --git a/manta-pay/src/crypto/hash/poseidon/README.md b/manta-pay/src/crypto/hash/poseidon/README.md new file mode 100644 index 000000000..0f8a592ae --- /dev/null +++ b/manta-pay/src/crypto/hash/poseidon/README.md @@ -0,0 +1,27 @@ +# Poseidon + +## Code Structure + +* `compat.rs`: Contains legacy implementation for Poseidon hash that is kept for compatibility. +* `constants.rs`: Generates poseidon permutation round numbers. +* `lfsr.rs`: Implements linear feedback shift register as a random number generator. +* `matrix.rs`: Implements basic linear algebra. +* `mds.rs`: Generate MDS matrix. +* `mod.rs`: Implements Poseidon hash. +* `preprocessing.rs`: Preprocess round constants and MDS matrix for optimized poseidon hash. +* `round_constants.rs`: Generates round constants. +* `mds_hardcoded_tests/correct_mds_generation.sage`: Generates hardcoded tests based on sage script adapted from [here](https://extgit.iaik.tugraz.at/krypto/hadeshash/-/blob/master/code/generate_parameters_grain.sage). +* `mds_hardcoded_tests/width*n*`: Contains a hardcoded $n\times n$ MDS matrix generated from the sage script. + +## Generate MDS Hardcoded Tests from SAGE + +The following script generates secure MDS matrices: + +```sh +cd mds_hardcoded_tests +for width in {2..12} +do + sage correct_mds_generation.sage 1 0 $width 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001 +done +cd .. +``` diff --git a/manta-pay/src/crypto/hash/poseidon/constants.rs b/manta-pay/src/crypto/hash/poseidon/constants.rs index c42095c4c..8522b6846 100644 --- a/manta-pay/src/crypto/hash/poseidon/constants.rs +++ b/manta-pay/src/crypto/hash/poseidon/constants.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with manta-rs. If not, see . -//! Poseidon Permutation Parameters +//! Poseidon Permutation Round Numbers use crate::crypto::hash::poseidon::Specification; diff --git a/manta-pay/src/crypto/hash/poseidon/mds.rs b/manta-pay/src/crypto/hash/poseidon/mds.rs index ab3d9bc5c..986768a04 100644 --- a/manta-pay/src/crypto/hash/poseidon/mds.rs +++ b/manta-pay/src/crypto/hash/poseidon/mds.rs @@ -359,17 +359,23 @@ mod test { #[test] fn mds_matches_hardcoded_sage_output() { let test_cases = [ + (2, include!("mds_hardcoded_tests/width2")), (3, include!("mds_hardcoded_tests/width3")), (4, include!("mds_hardcoded_tests/width4")), (5, include!("mds_hardcoded_tests/width5")), (6, include!("mds_hardcoded_tests/width6")), (7, include!("mds_hardcoded_tests/width7")), (8, include!("mds_hardcoded_tests/width8")), + (9, include!("mds_hardcoded_tests/width9")), + (10, include!("mds_hardcoded_tests/width10")), + (11, include!("mds_hardcoded_tests/width11")), + (12, include!("mds_hardcoded_tests/width12")), ]; - for (width, matrix) in test_cases { - let expected_mds = Matrix::>::new_unchecked(matrix); - assert_eq!(MdsMatrices::>::generate_mds(width), expected_mds); + assert_eq!( + MdsMatrices::generate_mds(width), + Matrix::new_unchecked(matrix) + ); } } diff --git a/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/.gitignore b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/.gitignore new file mode 100644 index 000000000..1cd45b30c --- /dev/null +++ b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/.gitignore @@ -0,0 +1 @@ +correct_mds_generation.sage.py diff --git a/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/correct_mds_generation.sage b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/correct_mds_generation.sage new file mode 100644 index 000000000..5f2400609 --- /dev/null +++ b/manta-pay/src/crypto/hash/poseidon/mds_hardcoded_tests/correct_mds_generation.sage @@ -0,0 +1,193 @@ +# adapted from: https://extgit.iaik.tugraz.at/krypto/hadeshash/-/blob/master/code/generate_parameters_grain.sage + +# Remark: This script contains functionality for GF(2^n), but currently works only over GF(p)! A few small adaptations are needed for GF(2^n). +from sage.rings.polynomial.polynomial_gf2x import GF2X_BuildIrred_list + +if len(sys.argv) < 4: + print("Usage: