From bfc107aab8ef5f428787919cf47af7cfd8ca8757 Mon Sep 17 00:00:00 2001 From: Jonathan LEI Date: Fri, 10 Mar 2023 04:28:21 +0000 Subject: [PATCH] Use `starknet-crypto` for poseidon --- Cargo.lock | 24 +- Cargo.toml | 2 +- src/vm/runners/builtin_runner/mod.rs | 1 - src/vm/runners/builtin_runner/poseidon.rs | 5 +- .../builtin_runner/poseidon_utils/mod.rs | 2 - .../poseidon_utils/poseidon_constants.rs | 983 ------------------ .../poseidon_utils/poseidon_hash.rs | 118 --- 7 files changed, 15 insertions(+), 1120 deletions(-) delete mode 100644 src/vm/runners/builtin_runner/poseidon_utils/mod.rs delete mode 100644 src/vm/runners/builtin_runner/poseidon_utils/poseidon_constants.rs delete mode 100644 src/vm/runners/builtin_runner/poseidon_utils/poseidon_hash.rs diff --git a/Cargo.lock b/Cargo.lock index a28f8f71cc..9dcbbf2714 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1195,11 +1195,11 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] name = "starknet-crypto" -version = "0.2.0" -source = "git+https://github.com/tdelabro/starknet-rs.git?branch=feature/manual-no-std-bigdecimal#13023c5bbd0426ce064bc31e21bc70b517818b85" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a79918cb8008cf57b5df7e2cc73e1fab90faf1bb95e5e19c65c92a1a34678171" dependencies = [ "crypto-bigint", - "hashbrown 0.13.2", "hex", "hmac", "num-bigint", @@ -1210,14 +1210,14 @@ dependencies = [ "starknet-crypto-codegen", "starknet-curve", "starknet-ff", - "thiserror-no-std", "zeroize", ] [[package]] name = "starknet-crypto-codegen" -version = "0.1.0" -source = "git+https://github.com/tdelabro/starknet-rs.git?branch=feature/manual-no-std-bigdecimal#13023c5bbd0426ce064bc31e21bc70b517818b85" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bff08f74f3ac785ac34ac05c68c5bd4df280107ab35df69dbcbde35183d89eba" dependencies = [ "starknet-curve", "starknet-ff", @@ -1226,23 +1226,23 @@ dependencies = [ [[package]] name = "starknet-curve" -version = "0.1.0" -source = "git+https://github.com/tdelabro/starknet-rs.git?branch=feature/manual-no-std-bigdecimal#13023c5bbd0426ce064bc31e21bc70b517818b85" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe0dbde7ef14d54c2117bc6d2efb68c2383005f1cd749b277c11df874d07b7af" dependencies = [ "starknet-ff", ] [[package]] name = "starknet-ff" -version = "0.2.0" -source = "git+https://github.com/tdelabro/starknet-rs.git?branch=feature/manual-no-std-bigdecimal#13023c5bbd0426ce064bc31e21bc70b517818b85" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78d484109da192f3a8cd58f674861c2d5e4b3e1765a466362c6f350ef213dfd1" dependencies = [ "ark-ff", "crypto-bigint", "getrandom", "hex", - "serde", - "thiserror-no-std", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 7794e20ad5..8c410669cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,7 +51,7 @@ hex = { version = "0.4.3", default-features = false } bincode = { tag = "v2.0.0-rc.2", git = "https://github.com/bincode-org/bincode.git", default-features = false, features = [ "serde", ] } -starknet-crypto = { git = "https://github.com/tdelabro/starknet-rs.git", branch = "feature/manual-no-std-bigdecimal", default-features = false } +starknet-crypto = { version = "0.4.1", default-features = false, features = ["signature-display"] } sha3 = { version = "0.10.1", default-features = false } rand_core = { version = "0.6.4", default-features = false } lazy_static = { version = "1.4.0", default-features = false, features = [ diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index d86f829d05..5e0f9354fc 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -13,7 +13,6 @@ mod hash; mod keccak; mod output; mod poseidon; -mod poseidon_utils; mod range_check; mod signature; diff --git a/src/vm/runners/builtin_runner/poseidon.rs b/src/vm/runners/builtin_runner/poseidon.rs index 22112b0b8a..34b7b67043 100644 --- a/src/vm/runners/builtin_runner/poseidon.rs +++ b/src/vm/runners/builtin_runner/poseidon.rs @@ -13,9 +13,8 @@ use crate::vm::vm_memory::memory::Memory; use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use felt::Felt; use num_integer::div_ceil; -use starknet_crypto::FieldElement; +use starknet_crypto::{poseidon_permute_comp, FieldElement}; -use super::poseidon_utils::poseidon_hash::permute_comp; use super::POSEIDON_BUILTIN_NAME; #[derive(Debug, Clone)] @@ -99,7 +98,7 @@ impl PoseidonBuiltinRunner { } // n_input_cells is fixed to 3, so this try_into will never fail let mut poseidon_state: [FieldElement; 3] = input_felts.try_into().unwrap(); - permute_comp(&mut poseidon_state); + poseidon_permute_comp(&mut poseidon_state); for (i, elem) in poseidon_state.iter().enumerate() { self.cache.borrow_mut().insert( (first_output_addr + i)?, diff --git a/src/vm/runners/builtin_runner/poseidon_utils/mod.rs b/src/vm/runners/builtin_runner/poseidon_utils/mod.rs deleted file mode 100644 index 1adc78c98f..0000000000 --- a/src/vm/runners/builtin_runner/poseidon_utils/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod poseidon_constants; -pub(super) mod poseidon_hash; diff --git a/src/vm/runners/builtin_runner/poseidon_utils/poseidon_constants.rs b/src/vm/runners/builtin_runner/poseidon_utils/poseidon_constants.rs deleted file mode 100644 index b28788a968..0000000000 --- a/src/vm/runners/builtin_runner/poseidon_utils/poseidon_constants.rs +++ /dev/null @@ -1,983 +0,0 @@ -use lazy_static::lazy_static; -use starknet_crypto::FieldElement; - -pub(crate) const FULL_ROUNDS: usize = 8; -pub(crate) const PARTIAL_ROUNDS: usize = 83; - -lazy_static! { - pub(crate) static ref POSEIDON_COMP_CONSTS: [FieldElement; 107] = [ - FieldElement::from_hex_be( - "0x06861759ea556a2339dd92f9562a30b9e58e2ad98109ae4780b7fd8eac77fe6f" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x03827681995d5af9ffc8397a3d00425a3da43f76abf28a64e4ab1a22f27508c4" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x03a3956d2fad44d0e7f760a2277dc7cb2cac75dc279b2d687a0dbe17704a8309" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0626c47a7d421fe1f13c4282214aa759291c78f926a2d1c6882031afe67ef4cd" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x078985f8e16505035bd6df5518cfd41f2d327fcc948d772cadfe17baca05d6a6" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05427f10867514a3204c659875341243c6e26a68b456dc1d142dcf34341696ff" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05af083f36e4c729454361733f0883c5847cd2c5d9d4cb8b0465e60edce699d7" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x07d71701bde3d06d54fa3f74f7b352a52d3975f92ff84b1ac77e709bfd388882" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0603da06882019009c26f8a6320a1c5eac1b64f699ffea44e39584467a6b1d3e" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04332a6f6bde2f288e79ce13f47ad1cdeebd8870fd13a36b613b9721f6453a5d" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x053d0ebf61664c685310a04c4dec2e7e4b9a813aaeff60d6c9e8caeb5cba78e7" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05346a68894845835ae5ebcb88028d2a6c82f99f928494ee1bfc2d15eaabfebc" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04b085eb1df4258c3453cc97445954bf3433b6ab9dd5a99592864c00f54a3f9a" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0731cfd19d508285965f12a079b2a169fdfe0a8e610e6f2d5ca5d7b0961f6d96" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0217d08b5339852bcc6f7a774936b3e72ecd9e1f9a73d743f8079c1e3587eeaa" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x000c935dd633b0fd63599b13c850dab3cb966ba510c81b20959e267008518c6e" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x052af8d378dd6772ee187ed23f79a7d98cf5a0a387103971467fe940e7b8b2be" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0294851c98b2682f1ec9918b9f12fcceaa6e28a7b79b2e506362cda595f8ab75" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x011b59990bacc280824d1021418d4f589da8c30063471494c204b169ab086064" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04b4df56e3d7753f91960d59ae099b9beb2ce690e6bbdcd0b599d49ceb2acd6a" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x005eecfa15a757dc3ecae9fbd8ff06e466243534f30629fc5f1cf09eb5161ac4" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0680bfdd8b9680e04659227634a1ec5282e5a7cef81b15677f8448bda4279059" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x01d0bf8fab0a1a7a14e2930794f7a3065c17e10b1cedd791b8877d97acd85053" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x02c2c8c79f808ace54ba207053c0d412c0fc11a610f14c48876701a37e32f464" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0354ec9ed01d20ec52aae19a9b858d3474d8234c11ad7bce630ad56c54afa562" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x030df20fcf6427bac38bb5d1a42287f4e4136ac5892340e994e6ea28deec1e55" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0528cf329c64e7ee3040bafbdeff61e241d99b424091e31472eda296fc9c6778" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x040416f24f623534634789660df5435ebf0c3e0c69e6c5b5ff6e757930bd1960" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x00380c8f936e2ed9fd488ae3bac7dce315ba21b11e88339cd5444435ccc9ea38" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x01cc4f5d5603d176f1a8e344392efd2d03ad0541832829d245e0e2291f255b75" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05728917af5da91f9539310d99f5d142e011d6c8e015ea5423c502aa99c09752" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x00efb450a9e86e1a46e295a348f0f23590925107d17c56d7c788fecc17219aa1" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x02020d74d36c421ae1a025616b342d0784b8fcd977de6c53a6c26693774dca99" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x07cfb309b75fd3bf2705558ae511dc82335050969f4bf84fa2b7b4f583989287" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04651e48b2e9349a5365e009ece626809d7b7d02a617eb98c785a784812d75e9" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x00d77627b270f65122d0269719da923ccae822d9aad0f0947a3b5c8f71c0dcc7" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0199ad3d641b54c4d571b3fe37773a8b82b003377f0dd8b7d3b7758c32908ea8" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x044f33640a8ecfd3973e2e9172a7333482b2d297be2da289319e72d137cdfe6e" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x07e4adf9894d964189d00a02dcf1e6be7f801234f5216eab6b6f366b6701abf7" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x03641fa5b3c90452f5ff808f8a9817eda7c6aecfb5471dfdca559fb4e711ee90" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x03de5729efd2fcbd897a49a78fa923fc306df32e6e2f0e02d0eee2c2cc3f3533" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x062691891a3fc1e27f622966ca0be20c06563500c8f06c9bdb77bd2882d6c994" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x06608d3bf11c18e4688739f72205763d1590cc4f9885ae1d86e96e0604baa0be" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x011c9c9b39cac71e3419726ce779116d07249f51cbdda4fd98c25cbbf593a316" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x061e23b58203269caef0850f74da27b9748e3312ea40c6844dd68c557c462ad7" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04182cd9ab1d9488f870a572010bc2a3d9878440b25951e4ce010855cf83bdc8" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0520fe6c4a096793f9055e6823116d15f1df2fe89d306f9965f6a59f4f3ecb71" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0346b2b2d6e5810129e093093dcd3dfa99ed6d71f47723ea3fbe4d4e2fd4afa1" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x01359ca923e7f1448ec1dd2a3684bee4e8b682c8e8e973acea72877ce9f7e6cf" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x047c655f55cf307800dfefdad24de86fde9deadab145a1b392420f37b95d9675" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04ab291f16555fa8a968cd7c9c285a9598efd925f2d58b7aa38ad87dca8441a8" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x039f409c7c782101223d1f6f7d86c21a22c44ef959510e392c9c7c5d17c629c5" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x044be36b782f882ad86eecb0cd6beb02e1a2f9fb5587a3babfacead0cafb6052" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x050a1dfde9b504ad2906db6eb5b507203cd1ceb394c52ce7107679a53a0d538b" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05c753c14da89e287b181c0dd11ac6c3680bdd7f1017dae083e7aebbeab183ab" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x02cf6306ed32232106c8015a3b180f386eee93e15f7b4f4fa57746525fc0520c" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x02c2014634d52e27420873cf347429091dfc6380689bd4f54d7d8e502c1c3a09" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x03cfb9c5bd93e02b2fdacde2058e33e5975c446345f010d850fc09cdf86ed8a1" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0363fa71a383cf3897933f1411fc5f806e311e84f72cb50a9ea4e1281f6b0299" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0728199657067ee16947b3fc76271676b4901b2a3686cffebcb960da91b05df8" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x03fdfbd47d27f3d34f0723b728e8921dc9bde34a9872df5a652a078d7e4ee021" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x07f241379440cacd7dc0efbe7858eb7de53cc02ca7d24197945c453398eff449" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05b2e8771ea9a0004e3bf056f3727797cbb457a27574d5f104354e52a5c25f0b" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x00a8ddbce708de44a7e0b3b0333146e1e910245be6bf822ea057a081bda2e23e" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x02d521e0daca24e431aa47cd90a0f551c12270e533835613edce2e19aa9b0f61" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x06cdbc0f2aa54d2cf7d5ac3b93f855af03eef7b07aaee00341a6266c30e08ae6" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x03dd96a17111ec8f4c5da3ad6794c0961ceee452cbe92c7a0941112b36ed9bf3" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05eafb1edeedc5c07ac07fdd06159344a2cfb92196a65d9ec0c5e732c36687dc" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04ab038d7b09eda9324577b260feaebdbcec5a7b7c7f449b312cfcd065c207e6" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04ca71981e4df6b505d2b0d94e235608463c58052570f68e495fc80c7fdef220" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x06dee9c6da4617e32aa419899c8ea8137e9b59d7e2759ffe573c15b77e413d2f" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x058f9e60b34ddab84dcbe2396065a4305b4a795a4770e4541e625d0460c6f186" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x047b7b4a802a10c1e6c9c735db6c34042d290906f274bea8fcecef17fc9af632" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x01849bcdb9ad7171096ecc936a186774084a074be0bfc0fbb9463a06a2bd430c" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x041870fbe04438348af5767bddaecd8aea3b49b4217547dec4d699b1466736cc" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0226c04e598076a9fa02aa64557daf28c0ec42e3d4da68d1965029d284738b07" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x01f0e971f0485a5b42eb92d6655c3ddb475cec4371f269a95335b2a7d6dac0fb" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x009f31cc2907dccbf994d35aa47ee3f4ebdf3703f795047a7b40dd3926431563" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04b40cce78f3b641e31ce4df58ce5a42c22cfbc198c84451ffe8cca4c64bd7d2" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x0191660489e4bd8a3e4563173de4a226f3ac736962fdfb70f72cb93ce50f8b9f" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x018c0919618db971f74eb01f293f2daea814b475103373dc7ed8dd4c7b467410" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x035b60253848530e845c8753121577d0ef37002e941c3dc1fb240bd57eadc803" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x01ae99db1575ae91c8b43a9f71a5f362581ad9b413d97fa6fd029134957451d5" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x03e6e1d0f3f8a0f728148ebcbd5d7d337d7cb8feb58a37d2d1dfb357e172647b" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x018bc36dffa8f96a659e1a171b55d2706ee3e9ad619e16f5c38dd1f4a209b8f3" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x02c7a3ef1afb6a302b54afc3a107ff9199a16efe9a1cc3ab83fa5b64893de4ed" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x053a7bd889bed07bf5e27dd8e92f6ae85e4fe4e84b0c6dde9856e94469de4bd7" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04d383ff7ffc6318fda704aca35995f86bec5a02ce9a0bf9d3cc0cc2f03ccea9" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04667b6762fb8ad53d07ef7e8a65b21ca96e0b3503037710d1292519c326f5cd" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x002cc8b43e75cf0b42a93c39ea98bcd46055dccc9589f02eb7fb536422e5921f" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x06b32ee98680871d38751447bfd76086ba4df0e7be59c55f4b2ce25582bf9c60" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x03e907927c7182faaa3b3c81358b82e734efac1f0609f0862d635cb1387102a3" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x03f3a5057b3a08975f0253728e512af78d2f437973f6a93793ea5e8424fbc6ea" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x014b491d73724779f8aa74b3fd8aa5821c21e1017224726a7a946bb6ca68d8f5" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05c8278c7bbfc30ae7f60e514fe3b9367aca84c54ad1373861695ea4abb814ef" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x064851937f9836ee5a08a7dde65e44b467018a82ba3bf99bba0b4502755c8074" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x06a9ac84251294769eca450ffb52b441882be77cb85f422ff9ea5e73f1d971dc" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x037ec35b710b0d04c9a2b71f2f7bd098c6a81d991d27f0fc1884f5ca545064de" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x005334f75b052c0235119816883040da72c6d0a61538bdfff46d6a242bfeb7a1" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05d0af4fcbd9e056c1020cca9d871ae68f80ee4af2ec6547cd49d6dca50aa431" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x030131bce2fba5694114a19c46d24e00b4699dc00f1d53ba5ab99537901b1e65" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05646a95a7c1ae86b34c0750ed2e641c538f93f13161be3c4957660f2e788965" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x04b9f291d7b430c79fac36230a11f43e78581f5259692b52c90df47b7d4ec01a" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x05006d393d3480f41a98f19127072dc83e00becf6ceb4d73d890e74abae01a13" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x062c9d42199f3b260e7cb8a115143106acf4f702e6b346fd202dc3b26a679d80" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x051274d092db5099f180b1a8a13b7f2c7606836eabd8af54bf1d9ac2dc5717a5" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x061fc552b8eb75e17ad0fb7aaa4ca528f415e14f0d9cdbed861a8db0bfff0c5b" - ) - .unwrap() - ]; -} - -#[cfg(test)] -mod test { - use super::*; - // When creating the constants for POSEIDON_COMP_CONSTANTS we skip compressing the raw keys and use the final pre-calculated values for performance - // Now we calculate poseidon compressed constants from raw keys for soundness check - - fn get_poseidon_comp_constants() -> Vec { - let round_keys = RAW_ROUND_KEYS - .iter() - .map(|key| key.map(|num| FieldElement::from_dec_str(num).expect("Invalid round key"))) - .collect::>(); - compress_roundkeys(&round_keys) - } - - // Code ported from the build.rs script here: - // https://github.com/eqlabs/pathfinder/blob/00a1a74a90a7b8a7f1d07ac3e616be1cb39cf8f1/crates/stark_poseidon/build.rs - - fn compress_roundkeys(rcs: &[[FieldElement; 3]]) -> Vec { - let mut result = Vec::new(); - - // Add first full rounds - result.extend(rcs[..FULL_ROUNDS / 2].iter().flatten()); - // Add compressed partial rounds and first of the last full rounds - result.extend(compress_roundkeys_partial(rcs)); - // Add last full rounds except the first of them - result.extend( - rcs[(FULL_ROUNDS / 2 + PARTIAL_ROUNDS + 1)..] - .iter() - .flatten(), - ); - result - } - - fn compress_roundkeys_partial(rcs: &[[FieldElement; 3]]) -> Vec { - let mut result = Vec::new(); - - let mut idx = FULL_ROUNDS / 2; - let mut state: [FieldElement; 3] = [FieldElement::ZERO; 3]; - - // Add keys for partial rounds - for _ in 0..PARTIAL_ROUNDS { - // AddRoundKey - state[0] = state[0] + rcs[idx][0]; - state[1] = state[1] + rcs[idx][1]; - state[2] = state[2] + rcs[idx][2]; - - // Add last state - result.push(state[2]); - - // Reset last state - state[2] = FieldElement::ZERO; - - // MixLayer - let t = state[0] + state[1] + state[2]; - state[0] = t + FieldElement::TWO * state[0]; - state[1] = t - FieldElement::TWO * state[1]; - state[2] = t - FieldElement::THREE * state[2]; - - idx += 1; - } - - // Add keys for first of the last full rounds - state[0] = state[0] + rcs[idx][0]; - state[1] = state[1] + rcs[idx][1]; - state[2] = state[2] + rcs[idx][2]; - result.push(state[0]); - result.push(state[1]); - result.push(state[2]); - - result - } - - // Extracted from: - // https://github.com/starkware-industries/poseidon/blob/5403dff9ff4eadb07deb5c0a43e88bedb011deb8/poseidon3.txt - - pub const RAW_ROUND_KEYS: [[&str; 3]; 91] = [ - [ - "2950795762459345168613727575620414179244544320470208355568817838579231751791", - "1587446564224215276866294500450702039420286416111469274423465069420553242820", - "1645965921169490687904413452218868659025437693527479459426157555728339600137", - ], - [ - "2782373324549879794752287702905278018819686065818504085638398966973694145741", - "3409172630025222641379726933524480516420204828329395644967085131392375707302", - "2379053116496905638239090788901387719228422033660130943198035907032739387135", - ], - [ - "2570819397480941104144008784293466051718826502582588529995520356691856497111", - "3546220846133880637977653625763703334841539452343273304410918449202580719746", - "2720682389492889709700489490056111332164748138023159726590726667539759963454", - ], - [ - "1899653471897224903834726250400246354200311275092866725547887381599836519005", - "2369443697923857319844855392163763375394720104106200469525915896159690979559", - "2354174693689535854311272135513626412848402744119855553970180659094265527996", - ], - [ - "2404084503073127963385083467393598147276436640877011103379112521338973185443", - "950320777137731763811524327595514151340412860090489448295239456547370725376", - "2121140748740143694053732746913428481442990369183417228688865837805149503386", - ], - [ - "2372065044800422557577242066480215868569521938346032514014152523102053709709", - "2618497439310693947058545060953893433487994458443568169824149550389484489896", - "3518297267402065742048564133910509847197496119850246255805075095266319996916", - ], - [ - "340529752683340505065238931581518232901634742162506851191464448040657139775", - "1954876811294863748406056845662382214841467408616109501720437541211031966538", - "813813157354633930267029888722341725864333883175521358739311868164460385261", - ], - [ - "71901595776070443337150458310956362034911936706490730914901986556638720031", - "2789761472166115462625363403490399263810962093264318361008954888847594113421", - "2628791615374802560074754031104384456692791616314774034906110098358135152410", - ], - [ - "3617032588734559635167557152518265808024917503198278888820567553943986939719", - "2624012360209966117322788103333497793082705816015202046036057821340914061980", - "149101987103211771991327927827692640556911620408176100290586418839323044234", - ], - [ - "1039927963829140138166373450440320262590862908847727961488297105916489431045", - "2213946951050724449162431068646025833746639391992751674082854766704900195669", - "2792724903541814965769131737117981991997031078369482697195201969174353468597", - ], - [ - "3212031629728871219804596347439383805499808476303618848198208101593976279441", - "3343514080098703935339621028041191631325798327656683100151836206557453199613", - "614054702436541219556958850933730254992710988573177298270089989048553060199", - ], - [ - "148148081026449726283933484730968827750202042869875329032965774667206931170", - "1158283532103191908366672518396366136968613180867652172211392033571980848414", - "1032400527342371389481069504520755916075559110755235773196747439146396688513", - ], - [ - "806900704622005851310078578853499250941978435851598088619290797134710613736", - "462498083559902778091095573017508352472262817904991134671058825705968404510", - "1003580119810278869589347418043095667699674425582646347949349245557449452503", - ], - [ - "619074932220101074089137133998298830285661916867732916607601635248249357793", - "2635090520059500019661864086615522409798872905401305311748231832709078452746", - "978252636251682252755279071140187792306115352460774007308726210405257135181", - ], - [ - "1766912167973123409669091967764158892111310474906691336473559256218048677083", - "1663265127259512472182980890707014969235283233442916350121860684522654120381", - "3532407621206959585000336211742670185380751515636605428496206887841428074250", - ], - [ - "2507023127157093845256722098502856938353143387711652912931112668310034975446", - "3321152907858462102434883844787153373036767230808678981306827073335525034593", - "3039253036806065280643845548147711477270022154459620569428286684179698125661", - ], - [ - "103480338868480851881924519768416587261556021758163719199282794248762465380", - "2394049781357087698434751577708655768465803975478348134669006211289636928495", - "2660531560345476340796109810821127229446538730404600368347902087220064379579", - ], - [ - "3603166934034556203649050570865466556260359798872408576857928196141785055563", - "1553799760191949768532188139643704561532896296986025007089826672890485412324", - "2744284717053657689091306578463476341218866418732695211367062598446038965164", - ], - [ - "320745764922149897598257794663594419839885234101078803811049904310835548856", - "979382242100682161589753881721708883681034024104145498709287731138044566302", - "1860426855810549882740147175136418997351054138609396651615467358416651354991", - ], - [ - "336173081054369235994909356892506146234495707857220254489443629387613956145", - "1632470326779699229772327605759783482411227247311431865655466227711078175883", - "921958250077481394074960433988881176409497663777043304881055317463712938502", - ], - [ - "3034358982193370602048539901033542101022185309652879937418114324899281842797", - "25626282149517463867572353922222474817434101087272320606729439087234878607", - "3002662261401575565838149305485737102400501329139562227180277188790091853682", - ], - [ - "2939684373453383817196521641512509179310654199629514917426341354023324109367", - "1076484609897998179434851570277297233169621096172424141759873688902355505136", - "2575095284833160494841112025725243274091830284746697961080467506739203605049", - ], - [ - "3565075264617591783581665711620369529657840830498005563542124551465195621851", - "2197016502533303822395077038351174326125210255869204501838837289716363437993", - "331415322883530754594261416546036195982886300052707474899691116664327869405", - ], - [ - "1935011233711290003793244296594669823169522055520303479680359990463281661839", - "3495901467168087413996941216661589517270845976538454329511167073314577412322", - "954195417117133246453562983448451025087661597543338750600301835944144520375", - ], - [ - "1271840477709992894995746871435810599280944810893784031132923384456797925777", - "2565310762274337662754531859505158700827688964841878141121196528015826671847", - "3365022288251637014588279139038152521653896670895105540140002607272936852513", - ], - [ - "1660592021628965529963974299647026602622092163312666588591285654477111176051", - "970104372286014048279296575474974982288801187216974504035759997141059513421", - "2617024574317953753849168721871770134225690844968986289121504184985993971227", - ], - [ - "999899815343607746071464113462778273556695659506865124478430189024755832262", - "2228536129413411161615629030408828764980855956560026807518714080003644769896", - "2701953891198001564547196795777701119629537795442025393867364730330476403227", - ], - [ - "837078355588159388741598313782044128527494922918203556465116291436461597853", - "2121749601840466143704862369657561429793951309962582099604848281796392359214", - "771812260179247428733132708063116523892339056677915387749121983038690154755", - ], - [ - "3317336423132806446086732225036532603224267214833263122557471741829060578219", - "481570067997721834712647566896657604857788523050900222145547508314620762046", - "242195042559343964206291740270858862066153636168162642380846129622127460192", - ], - [ - "2855462178889999218204481481614105202770810647859867354506557827319138379686", - "3525521107148375040131784770413887305850308357895464453970651672160034885202", - "1320839531502392535964065058804908871811967681250362364246430459003920305799", - ], - [ - "2514191518588387125173345107242226637171897291221681115249521904869763202419", - "2798335750958827619666318316247381695117827718387653874070218127140615157902", - "2808467767967035643407948058486565877867906577474361783201337540214875566395", - ], - [ - "3551834385992706206273955480294669176699286104229279436819137165202231595747", - "1219439673853113792340300173186247996249367102884530407862469123523013083971", - "761519904537984520554247997444508040636526566551719396202550009393012691157", - ], - [ - "3355402549169351700500518865338783382387571349497391475317206324155237401353", - "199541098009731541347317515995192175813554789571447733944970283654592727138", - "192100490643078165121235261796864975568292640203635147901612231594408079071", - ], - [ - "1187019357602953326192019968809486933768550466167033084944727938441427050581", - "189525349641911362389041124808934468936759383310282010671081989585219065700", - "2831653363992091308880573627558515686245403755586311978724025292003353336665", - ], - [ - "2052859812632218952608271535089179639890275494426396974475479657192657094698", - "1670756178709659908159049531058853320846231785448204274277900022176591811072", - "3538757242013734574731807289786598937548399719866320954894004830207085723125", - ], - [ - "710549042741321081781917034337800036872214466705318638023070812391485261299", - "2345013122330545298606028187653996682275206910242635100920038943391319595180", - "3528369671971445493932880023233332035122954362711876290904323783426765912206", - ], - [ - "1167120829038120978297497195837406760848728897181138760506162680655977700764", - "3073243357129146594530765548901087443775563058893907738967898816092270628884", - "378514724418106317738164464176041649567501099164061863402473942795977719726", - ], - [ - "333391138410406330127594722511180398159664250722328578952158227406762627796", - "1727570175639917398410201375510924114487348765559913502662122372848626931905", - "968312190621809249603425066974405725769739606059422769908547372904403793174", - ], - [ - "360659316299446405855194688051178331671817370423873014757323462844775818348", - "1386580151907705298970465943238806620109618995410132218037375811184684929291", - "3604888328937389309031638299660239238400230206645344173700074923133890528967", - ], - [ - "2496185632263372962152518155651824899299616724241852816983268163379540137546", - "486538168871046887467737983064272608432052269868418721234810979756540672990", - "1558415498960552213241704009433360128041672577274390114589014204605400783336", - ], - [ - "3512058327686147326577190314835092911156317204978509183234511559551181053926", - "2235429387083113882635494090887463486491842634403047716936833563914243946191", - "1290896777143878193192832813769470418518651727840187056683408155503813799882", - ], - [ - "1143310336918357319571079551779316654556781203013096026972411429993634080835", - "3235435208525081966062419599803346573407862428113723170955762956243193422118", - "1293239921425673430660897025143433077974838969258268884994339615096356996604", - ], - [ - "236252269127612784685426260840574970698541177557674806964960352572864382971", - "1733907592497266237374827232200506798207318263912423249709509725341212026275", - "302004309771755665128395814807589350526779835595021835389022325987048089868", - ], - [ - "3018926838139221755384801385583867283206879023218491758435446265703006270945", - "39701437664873825906031098349904330565195980985885489447836580931425171297", - "908381723021746969965674308809436059628307487140174335882627549095646509778", - ], - [ - "219062858908229855064136253265968615354041842047384625689776811853821594358", - "1283129863776453589317845316917890202859466483456216900835390291449830275503", - "418512623547417594896140369190919231877873410935689672661226540908900544012", - ], - [ - "1792181590047131972851015200157890246436013346535432437041535789841136268632", - "370546432987510607338044736824316856592558876687225326692366316978098770516", - "3323437805230586112013581113386626899534419826098235300155664022709435756946", - ], - [ - "910076621742039763058481476739499965761942516177975130656340375573185415877", - "1762188042455633427137702520675816545396284185254002959309669405982213803405", - "2186362253913140345102191078329764107619534641234549431429008219905315900520", - ], - [ - "2230647725927681765419218738218528849146504088716182944327179019215826045083", - "1069243907556644434301190076451112491469636357133398376850435321160857761825", - "2695241469149243992683268025359863087303400907336026926662328156934068747593", - ], - [ - "1361519681544413849831669554199151294308350560528931040264950307931824877035", - "1339116632207878730171031743761550901312154740800549632983325427035029084904", - "790593524918851401449292693473498591068920069246127392274811084156907468875", - ], - [ - "2723400368331924254840192318398326090089058735091724263333980290765736363637", - "3457180265095920471443772463283225391927927225993685928066766687141729456030", - "1483675376954327086153452545475557749815683871577400883707749788555424847954", - ], - [ - "2926303836265506736227240325795090239680154099205721426928300056982414025239", - "543969119775473768170832347411484329362572550684421616624136244239799475526", - "237401230683847084256617415614300816373730178313253487575312839074042461932", - ], - [ - "844568412840391587862072008674263874021460074878949862892685736454654414423", - "151922054871708336050647150237534498235916969120198637893731715254687336644", - "1299332034710622815055321547569101119597030148120309411086203580212105652312", - ], - [ - "487046922649899823989594814663418784068895385009696501386459462815688122993", - "1104883249092599185744249485896585912845784382683240114120846423960548576851", - "1458388705536282069567179348797334876446380557083422364875248475157495514484", - ], - [ - "850248109622750774031817200193861444623975329881731864752464222442574976566", - "2885843173858536690032695698009109793537724845140477446409245651176355435722", - "3027068551635372249579348422266406787688980506275086097330568993357835463816", - ], - [ - "3231892723647447539926175383213338123506134054432701323145045438168976970994", - "1719080830641935421242626784132692936776388194122314954558418655725251172826", - "1172253756541066126131022537343350498482225068791630219494878195815226839450", - ], - [ - "1619232269633026603732619978083169293258272967781186544174521481891163985093", - "3495680684841853175973173610562400042003100419811771341346135531754869014567", - "1576161515913099892951745452471618612307857113799539794680346855318958552758", - ], - [ - "2618326122974253423403350731396350223238201817594761152626832144510903048529", - "2696245132758436974032479782852265185094623165224532063951287925001108567649", - "930116505665110070247395429730201844026054810856263733273443066419816003444", - ], - [ - "2786389174502246248523918824488629229455088716707062764363111940462137404076", - "1555260846425735320214671887347115247546042526197895180675436886484523605116", - "2306241912153325247392671742757902161446877415586158295423293240351799505917", - ], - [ - "411529621724849932999694270803131456243889635467661223241617477462914950626", - "1542495485262286701469125140275904136434075186064076910329015697714211835205", - "1853045663799041100600825096887578544265580718909350942241802897995488264551", - ], - [ - "2963055259497271220202739837493041799968576111953080503132045092194513937286", - "2303806870349915764285872605046527036748108533406243381676768310692344456050", - "2622104986201990620910286730213140904984256464479840856728424375142929278875", - ], - [ - "2369987021925266811581727383184031736927816625797282287927222602539037105864", - "285070227712021899602056480426671736057274017903028992288878116056674401781", - "3034087076179360957800568733595959058628497428787907887933697691951454610691", - ], - [ - "469095854351700119980323115747590868855368701825706298740201488006320881056", - "360001976264385426746283365024817520563236378289230404095383746911725100012", - "3438709327109021347267562000879503009590697221730578667498351600602230296178", - ], - [ - "63573904800572228121671659287593650438456772568903228287754075619928214969", - "3470881855042989871434874691030920672110111605547839662680968354703074556970", - "724559311507950497340993415408274803001166693839947519425501269424891465492", - ], - [ - "880409284677518997550768549487344416321062350742831373397603704465823658986", - "6876255662475867703077362872097208259197756317287339941435193538565586230", - "2701916445133770775447884812906226786217969545216086200932273680400909154638", - ], - [ - "425152119158711585559310064242720816611629181537672850898056934507216982586", - "1475552998258917706756737045704649573088377604240716286977690565239187213744", - "2413772448122400684309006716414417978370152271397082147158000439863002593561", - ], - [ - "392160855822256520519339260245328807036619920858503984710539815951012864164", - "1075036996503791536261050742318169965707018400307026402939804424927087093987", - "2176439430328703902070742432016450246365760303014562857296722712989275658921", - ], - [ - "1413865976587623331051814207977382826721471106513581745229680113383908569693", - "4879283427490523253696177116563427032332223531862961281430108575019551814", - "3392583297537374046875199552977614390492290683707960975137418536812266544902", - ], - [ - "3600854486849487646325182927019642276644093512133907046667282144129939150983", - "2779924664161372134024229593301361846129279572186444474616319283535189797834", - "2722699960903170449291146429799738181514821447014433304730310678334403972040", - ], - [ - "819109815049226540285781191874507704729062681836086010078910930707209464699", - "3046121243742768013822760785918001632929744274211027071381357122228091333823", - "1339019590803056172509793134119156250729668216522001157582155155947567682278", - ], - [ - "1933279639657506214789316403763326578443023901555983256955812717638093967201", - "2138221547112520744699126051903811860205771600821672121643894708182292213541", - "2694713515543641924097704224170357995809887124438248292930846280951601597065", - ], - [ - "2471734202930133750093618989223585244499567111661178960753938272334153710615", - "504903761112092757611047718215309856203214372330635774577409639907729993533", - "1943979703748281357156510253941035712048221353507135074336243405478613241290", - ], - [ - "684525210957572142559049112233609445802004614280157992196913315652663518936", - "1705585400798782397786453706717059483604368413512485532079242223503960814508", - "192429517716023021556170942988476050278432319516032402725586427701913624665", - ], - [ - "1586493702243128040549584165333371192888583026298039652930372758731750166765", - "686072673323546915014972146032384917012218151266600268450347114036285993377", - "3464340397998075738891129996710075228740496767934137465519455338004332839215", - ], - [ - "2805249176617071054530589390406083958753103601524808155663551392362371834663", - "667746464250968521164727418691487653339733392025160477655836902744186489526", - "1131527712905109997177270289411406385352032457456054589588342450404257139778", - ], - [ - "1908969485750011212309284349900149072003218505891252313183123635318886241171", - "1025257076985551890132050019084873267454083056307650830147063480409707787695", - "2153175291918371429502545470578981828372846236838301412119329786849737957977", - ], - [ - "3410257749736714576487217882785226905621212230027780855361670645857085424384", - "3442969106887588154491488961893254739289120695377621434680934888062399029952", - "3029953900235731770255937704976720759948880815387104275525268727341390470237", - ], - [ - "85453456084781138713939104192561924536933417707871501802199311333127894466", - "2730629666577257820220329078741301754580009106438115341296453318350676425129", - "178242450661072967256438102630920745430303027840919213764087927763335940415", - ], - [ - "2844589222514708695700541363167856718216388819406388706818431442998498677557", - "3547876269219141094308889387292091231377253967587961309624916269569559952944", - "2525005406762984211707203144785482908331876505006839217175334833739957826850", - ], - [ - "3096397013555211396701910432830904669391580557191845136003938801598654871345", - "574424067119200181933992948252007230348512600107123873197603373898923821490", - "1714030696055067278349157346067719307863507310709155690164546226450579547098", - ], - [ - "2339895272202694698739231405357972261413383527237194045718815176814132612501", - "3562501318971895161271663840954705079797767042115717360959659475564651685069", - "69069358687197963617161747606993436483967992689488259107924379545671193749", - ], - [ - "2614502738369008850475068874731531583863538486212691941619835266611116051561", - "655247349763023251625727726218660142895322325659927266813592114640858573566", - "2305235672527595714255517865498269719545193172975330668070873705108690670678", - ], - [ - "926416070297755413261159098243058134401665060349723804040714357642180531931", - "866523735635840246543516964237513287099659681479228450791071595433217821460", - "2284334068466681424919271582037156124891004191915573957556691163266198707693", - ], - [ - "1812588309302477291425732810913354633465435706480768615104211305579383928792", - "2836899808619013605432050476764608707770404125005720004551836441247917488507", - "2989087789022865112405242078196235025698647423649950459911546051695688370523", - ], - [ - "68056284404189102136488263779598243992465747932368669388126367131855404486", - "505425339250887519581119854377342241317528319745596963584548343662758204398", - "2118963546856545068961709089296976921067035227488975882615462246481055679215", - ], - [ - "2253872596319969096156004495313034590996995209785432485705134570745135149681", - "1625090409149943603241183848936692198923183279116014478406452426158572703264", - "179139838844452470348634657368199622305888473747024389514258107503778442495", - ], - [ - "1567067018147735642071130442904093290030432522257811793540290101391210410341", - "2737301854006865242314806979738760349397411136469975337509958305470398783585", - "3002738216460904473515791428798860225499078134627026021350799206894618186256", - ], - [ - "374029488099466837453096950537275565120689146401077127482884887409712315162", - "973403256517481077805460710540468856199855789930951602150773500862180885363", - "2691967457038172130555117632010860984519926022632800605713473799739632878867", - ], - [ - "3515906794910381201365530594248181418811879320679684239326734893975752012109", - "148057579455448384062325089530558091463206199724854022070244924642222283388", - "1541588700238272710315890873051237741033408846596322948443180470429851502842", - ], - [ - "147013865879011936545137344076637170977925826031496203944786839068852795297", - "2630278389304735265620281704608245039972003761509102213752997636382302839857", - "1359048670759642844930007747955701205155822111403150159614453244477853867621", - ], - [ - "2438984569205812336319229336885480537793786558293523767186829418969842616677", - "2137792255841525507649318539501906353254503076308308692873313199435029594138", - "2262318076430740712267739371170174514379142884859595360065535117601097652755", - ], - [ - "2792703718581084537295613508201818489836796608902614779596544185252826291584", - "2294173715793292812015960640392421991604150133581218254866878921346561546149", - "2770011224727997178743274791849308200493823127651418989170761007078565678171", - ], - ]; - - #[cfg(test)] - mod tests { - use super::*; - - #[test] - fn check_pre_calculated_poseidon_comp_constants_match_compressed_from_raw_keys() { - let poseidon_comp_constants = get_poseidon_comp_constants(); - assert_eq!(&*POSEIDON_COMP_CONSTS.to_vec(), poseidon_comp_constants); - } - } -} diff --git a/src/vm/runners/builtin_runner/poseidon_utils/poseidon_hash.rs b/src/vm/runners/builtin_runner/poseidon_utils/poseidon_hash.rs deleted file mode 100644 index 2ff75ace9b..0000000000 --- a/src/vm/runners/builtin_runner/poseidon_utils/poseidon_hash.rs +++ /dev/null @@ -1,118 +0,0 @@ -// Code ported from the implementation from pathfinder here: -// https://github.com/eqlabs/pathfinder/blob/00a1a74a90a7b8a7f1d07ac3e616be1cb39cf8f1/crates/stark_poseidon/src/lib.rs - -use super::poseidon_constants::{FULL_ROUNDS, PARTIAL_ROUNDS, POSEIDON_COMP_CONSTS}; -use starknet_crypto::FieldElement; - -/// Linear layer for MDS matrix M = ((3,1,1), (1,-1,1), (1,1,2)) -/// Given state vector x, it returns Mx, optimized by precomputing t. -#[inline(always)] -fn mix(state: &mut [FieldElement; 3]) { - let t = state[0] + state[1] + state[2]; - state[0] = t + FieldElement::TWO * state[0]; - state[1] = t - FieldElement::TWO * state[1]; - state[2] = t - FieldElement::THREE * state[2]; -} - -#[inline] -fn round_comp(state: &mut [FieldElement; 3], idx: usize, full: bool) { - if full { - state[0] = state[0] + POSEIDON_COMP_CONSTS[idx]; - state[1] = state[1] + POSEIDON_COMP_CONSTS[idx + 1]; - state[2] = state[2] + POSEIDON_COMP_CONSTS[idx + 2]; - state[0] = state[0] * state[0] * state[0]; - state[1] = state[1] * state[1] * state[1]; - state[2] = state[2] * state[2] * state[2]; - } else { - state[2] = state[2] + POSEIDON_COMP_CONSTS[idx]; - state[2] = state[2] * state[2] * state[2]; - } - mix(state); -} - -/// Poseidon permutation function -pub fn permute_comp(state: &mut [FieldElement; 3]) { - let mut idx = 0; - - // Full rounds - for _ in 0..(FULL_ROUNDS / 2) { - round_comp(state, idx, true); - idx += 3; - } - - // Partial rounds - for _ in 0..PARTIAL_ROUNDS { - round_comp(state, idx, false); - idx += 1; - } - - // Full rounds - for _ in 0..(FULL_ROUNDS / 2) { - round_comp(state, idx, true); - idx += 3; - } -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn test_permute_a() { - let mut poseidon_state = [FieldElement::THREE, FieldElement::ZERO, FieldElement::TWO]; - permute_comp(&mut poseidon_state); - assert_eq!( - poseidon_state, - [ - FieldElement::from_hex_be( - "0x268c44203f1c763bca21beb5aec78b9063cdcdd0fdf6b598bb8e1e8f2b6253f" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x2b85c9f686f5d3036db55b2ca58a763a3065bc1bc8efbe0e70f3a7171f6cad3" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x61df3789eef0e1ee0dbe010582a00dd099191e6395dfb976e7be3be2fa9d54b" - ) - .unwrap() - ] - ) - } - - #[test] - fn test_permute_b() { - let mut poseidon_state = [ - FieldElement::from_hex_be( - "0x268c44203f1c763bca21beb5aec78b9063cdcdd0fdf6b598bb8e1e8f2b6253f", - ) - .unwrap(), - FieldElement::from_hex_be( - "0x2b85c9f686f5d3036db55b2ca58a763a3065bc1bc8efbe0e70f3a7171f6cad3", - ) - .unwrap(), - FieldElement::from_hex_be( - "0x61df3789eef0e1ee0dbe010582a00dd099191e6395dfb976e7be3be2fa9d54b", - ) - .unwrap(), - ]; - permute_comp(&mut poseidon_state); - assert_eq!( - poseidon_state, - [ - FieldElement::from_hex_be( - "0x4ec565b1b01606b5222602b20f8ddc4a8a7c75b559b852ab183a0daf5930b5c" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x4d3c32c3c7cd39b6444db42e2437eeda12e459d28ce49a0f761a23d64c29e4c" - ) - .unwrap(), - FieldElement::from_hex_be( - "0x749d4d0ddf41548e039f183b745a08b80fad54e9ac389021148350bdda70a92" - ) - .unwrap() - ] - ) - } -}