From db64504f1f398d0810a353135f6c5ca444baf82d Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewa Date: Wed, 12 Jul 2023 19:38:40 +0200 Subject: [PATCH 01/13] broken poseidon --- extractor/extractor_test.go | 40 ++-- extractor/poseidon_test.go | 378 ++++++++++++++++++++++++++++++++++++ go.mod | 3 + go.sum | 4 + 4 files changed, 404 insertions(+), 21 deletions(-) create mode 100644 extractor/poseidon_test.go diff --git a/extractor/extractor_test.go b/extractor/extractor_test.go index 006e474..8c2e960 100644 --- a/extractor/extractor_test.go +++ b/extractor/extractor_test.go @@ -2,6 +2,7 @@ package extractor import ( "fmt" + "github.com/consensys/gnark/frontend/cs/r1cs" "gnark-extractor/abstractor" "testing" @@ -10,17 +11,6 @@ import ( "github.com/stretchr/testify/assert" ) -// Example: Semaphore circuit -type DummyPoseidon2 struct { - In_1 frontend.Variable - In_2 frontend.Variable -} - -func (gadget DummyPoseidon2) DefineGadget(api abstractor.API) []frontend.Variable { - hash := api.Mul(gadget.In_1, gadget.In_2) - return []frontend.Variable{hash} -} - type DummyPoseidon1 struct { In frontend.Variable } @@ -37,7 +27,7 @@ type MerkleTreeInclusionProof struct { } func (gadget MerkleTreeInclusionProof) DefineGadget(api abstractor.API) []frontend.Variable { - dummy_poseidon := api.DefineGadget(&DummyPoseidon2{}) + dummy_poseidon := api.DefineGadget(&Poseidon{}) levels := len(gadget.PathIndices) hashes := make([]frontend.Variable, levels+1) @@ -46,8 +36,8 @@ func (gadget MerkleTreeInclusionProof) DefineGadget(api abstractor.API) []fronte for i := 0; i < levels; i++ { // Unrolled merkle_tree_inclusion_proof api.AssertIsBoolean(gadget.PathIndices[i]) - leftHash := dummy_poseidon.Call(DummyPoseidon2{hashes[i], gadget.Siblings[i]})[0] - rightHash := dummy_poseidon.Call(DummyPoseidon2{gadget.Siblings[i], hashes[i]})[0] + leftHash := dummy_poseidon.Call(Poseidon{hashes[i], gadget.Siblings[i]})[0] + rightHash := dummy_poseidon.Call(Poseidon{gadget.Siblings[i], hashes[i]})[0] hashes[i+1] = api.Select(gadget.PathIndices[i], rightHash, leftHash) } root := hashes[levels] @@ -73,16 +63,16 @@ type Semaphore struct { func (circuit *Semaphore) AbsDefine(api abstractor.API) error { // From https://github.com/semaphore-protocol/semaphore/blob/main/packages/circuits/semaphore.circom - dummy_poseidon_1 := api.DefineGadget(&DummyPoseidon1{}) - dummy_poseidon_2 := api.DefineGadget(&DummyPoseidon2{}) + dummy_poseidon_1 := api.DefineGadget(&Poseidon{}) + dummy_poseidon_2 := api.DefineGadget(&Poseidon{}) merkle_tree_inclusion_proof := api.DefineGadget(&MerkleTreeInclusionProof{ PathIndices: make([]frontend.Variable, circuit.Levels), Siblings: make([]frontend.Variable, circuit.Levels), }) - secret := dummy_poseidon_2.Call(DummyPoseidon2{circuit.IdentityNullifier, circuit.IdentityTrapdoor})[0] + secret := dummy_poseidon_2.Call(Poseidon{circuit.IdentityNullifier, circuit.IdentityTrapdoor})[0] identity_commitment := dummy_poseidon_1.Call(DummyPoseidon1{secret})[0] - nullifierHash := dummy_poseidon_2.Call(DummyPoseidon2{circuit.ExternalNullifier, circuit.IdentityNullifier})[0] + nullifierHash := dummy_poseidon_2.Call(Poseidon{circuit.ExternalNullifier, circuit.IdentityNullifier})[0] api.AssertIsEqual(nullifierHash, circuit.NullifierHash) // Verify root := merkle_tree_inclusion_proof.Call(MerkleTreeInclusionProof{ @@ -114,6 +104,14 @@ func TestSemaphore(t *testing.T) { fmt.Println("CircuitToLean error!") fmt.Println(err.Error()) } + ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &assignment) + if err != nil { + fmt.Println("Compile error!") + fmt.Println(err.Error()) + } + fmt.Println(ccs.GetNbCoefficients()) + fmt.Println(ccs.GetNbConstraints()) + } // Example: circuit with constant parameter @@ -199,12 +197,12 @@ type MerkleRecover struct { } func (circuit *MerkleRecover) AbsDefine(api abstractor.API) error { - hash := api.DefineGadget(&DummyHash{}) + hash := api.DefineGadget(&Poseidon{}) current := circuit.Element for i := 0; i < len(circuit.Path); i++ { - leftHash := hash.Call(DummyHash{current, circuit.Proof[i]})[0] - rightHash := hash.Call(DummyHash{circuit.Proof[i], current})[0] + leftHash := hash.Call(Poseidon{current, circuit.Proof[i]})[0] + rightHash := hash.Call(Poseidon{circuit.Proof[i], current})[0] current = api.Select(circuit.Path[i], rightHash, leftHash) } api.AssertIsEqual(current, circuit.Root) diff --git a/extractor/poseidon_test.go b/extractor/poseidon_test.go new file mode 100644 index 0000000..54e5a80 --- /dev/null +++ b/extractor/poseidon_test.go @@ -0,0 +1,378 @@ +package extractor + +import ( + "github.com/consensys/gnark/frontend" + "gnark-extractor/abstractor" + "math/big" +) + +func hex(s string) big.Int { + var bi big.Int + bi.SetString(s, 0) + return bi +} + +var MDS = [3][3]frontend.Variable{ + { + hex("0x109b7f411ba0e4c9b2b70caf5c36a7b194be7c11ad24378bfedb68592ba8118b"), + hex("0x16ed41e13bb9c0c66ae119424fddbcbc9314dc9fdbdeea55d6c64543dc4903e0"), + hex("0x2b90bba00fca0589f617e7dcbfe82e0df706ab640ceb247b791a93b74e36736d"), + }, + { + hex("0x2969f27eed31a480b9c36c764379dbca2cc8fdd1415c3dded62940bcde0bd771"), + hex("0x2e2419f9ec02ec394c9871c832963dc1b89d743c8c7b964029b2311687b1fe23"), + hex("0x101071f0032379b697315876690f053d148d4e109f5fb065c8aacc55a0f89bfa"), + }, + { + hex("0x143021ec686a3f330d5f9e654638065ce6cd79e28c5b3753326244ee65a1b1a7"), + hex("0x176cc029695ad02582a70eff08a6fd99d057e12e58e7d7b6b16cdfabc8ee2911"), + hex("0x19a3fc0a56702bf417ba7fee3802593fa644470307043f7773279cd71d25d5e0"), + }, +} + +var CONSTANTS = [][3]frontend.Variable{ + { + hex("0x0ee9a592ba9a9518d05986d656f40c2114c4993c11bb29938d21d47304cd8e6e"), + hex("0x00f1445235f2148c5986587169fc1bcd887b08d4d00868df5696fff40956e864"), + hex("0x08dff3487e8ac99e1f29a058d0fa80b930c728730b7ab36ce879f3890ecf73f5"), + }, { + hex("0x2f27be690fdaee46c3ce28f7532b13c856c35342c84bda6e20966310fadc01d0"), + hex("0x2b2ae1acf68b7b8d2416bebf3d4f6234b763fe04b8043ee48b8327bebca16cf2"), + hex("0x0319d062072bef7ecca5eac06f97d4d55952c175ab6b03eae64b44c7dbf11cfa"), + }, { + hex("0x28813dcaebaeaa828a376df87af4a63bc8b7bf27ad49c6298ef7b387bf28526d"), + hex("0x2727673b2ccbc903f181bf38e1c1d40d2033865200c352bc150928adddf9cb78"), + hex("0x234ec45ca27727c2e74abd2b2a1494cd6efbd43e340587d6b8fb9e31e65cc632"), + }, { + hex("0x15b52534031ae18f7f862cb2cf7cf760ab10a8150a337b1ccd99ff6e8797d428"), + hex("0x0dc8fad6d9e4b35f5ed9a3d186b79ce38e0e8a8d1b58b132d701d4eecf68d1f6"), + hex("0x1bcd95ffc211fbca600f705fad3fb567ea4eb378f62e1fec97805518a47e4d9c"), + }, { + hex("0x10520b0ab721cadfe9eff81b016fc34dc76da36c2578937817cb978d069de559"), + hex("0x1f6d48149b8e7f7d9b257d8ed5fbbaf42932498075fed0ace88a9eb81f5627f6"), + hex("0x1d9655f652309014d29e00ef35a2089bfff8dc1c816f0dc9ca34bdb5460c8705"), + }, { + hex("0x04df5a56ff95bcafb051f7b1cd43a99ba731ff67e47032058fe3d4185697cc7d"), + hex("0x0672d995f8fff640151b3d290cedaf148690a10a8c8424a7f6ec282b6e4be828"), + hex("0x099952b414884454b21200d7ffafdd5f0c9a9dcc06f2708e9fc1d8209b5c75b9"), + }, { + hex("0x052cba2255dfd00c7c483143ba8d469448e43586a9b4cd9183fd0e843a6b9fa6"), + hex("0x0b8badee690adb8eb0bd74712b7999af82de55707251ad7716077cb93c464ddc"), + hex("0x119b1590f13307af5a1ee651020c07c749c15d60683a8050b963d0a8e4b2bdd1"), + }, { + hex("0x03150b7cd6d5d17b2529d36be0f67b832c4acfc884ef4ee5ce15be0bfb4a8d09"), + hex("0x2cc6182c5e14546e3cf1951f173912355374efb83d80898abe69cb317c9ea565"), + hex("0x005032551e6378c450cfe129a404b3764218cadedac14e2b92d2cd73111bf0f9"), + }, { + hex("0x233237e3289baa34bb147e972ebcb9516469c399fcc069fb88f9da2cc28276b5"), + hex("0x05c8f4f4ebd4a6e3c980d31674bfbe6323037f21b34ae5a4e80c2d4c24d60280"), + hex("0x0a7b1db13042d396ba05d818a319f25252bcf35ef3aeed91ee1f09b2590fc65b"), + }, { + hex("0x2a73b71f9b210cf5b14296572c9d32dbf156e2b086ff47dc5df542365a404ec0"), + hex("0x1ac9b0417abcc9a1935107e9ffc91dc3ec18f2c4dbe7f22976a760bb5c50c460"), + hex("0x12c0339ae08374823fabb076707ef479269f3e4d6cb104349015ee046dc93fc0"), + }, { + hex("0x0b7475b102a165ad7f5b18db4e1e704f52900aa3253baac68246682e56e9a28e"), + hex("0x037c2849e191ca3edb1c5e49f6e8b8917c843e379366f2ea32ab3aa88d7f8448"), + hex("0x05a6811f8556f014e92674661e217e9bd5206c5c93a07dc145fdb176a716346f"), + }, { + hex("0x29a795e7d98028946e947b75d54e9f044076e87a7b2883b47b675ef5f38bd66e"), + hex("0x20439a0c84b322eb45a3857afc18f5826e8c7382c8a1585c507be199981fd22f"), + hex("0x2e0ba8d94d9ecf4a94ec2050c7371ff1bb50f27799a84b6d4a2a6f2a0982c887"), + }, { + hex("0x143fd115ce08fb27ca38eb7cce822b4517822cd2109048d2e6d0ddcca17d71c8"), + hex("0x0c64cbecb1c734b857968dbbdcf813cdf8611659323dbcbfc84323623be9caf1"), + hex("0x028a305847c683f646fca925c163ff5ae74f348d62c2b670f1426cef9403da53"), + }, { + hex("0x2e4ef510ff0b6fda5fa940ab4c4380f26a6bcb64d89427b824d6755b5db9e30c"), + hex("0x0081c95bc43384e663d79270c956ce3b8925b4f6d033b078b96384f50579400e"), + hex("0x2ed5f0c91cbd9749187e2fade687e05ee2491b349c039a0bba8a9f4023a0bb38"), + }, { + hex("0x30509991f88da3504bbf374ed5aae2f03448a22c76234c8c990f01f33a735206"), + hex("0x1c3f20fd55409a53221b7c4d49a356b9f0a1119fb2067b41a7529094424ec6ad"), + hex("0x10b4e7f3ab5df003049514459b6e18eec46bb2213e8e131e170887b47ddcb96c"), + }, { + hex("0x2a1982979c3ff7f43ddd543d891c2abddd80f804c077d775039aa3502e43adef"), + hex("0x1c74ee64f15e1db6feddbead56d6d55dba431ebc396c9af95cad0f1315bd5c91"), + hex("0x07533ec850ba7f98eab9303cace01b4b9e4f2e8b82708cfa9c2fe45a0ae146a0"), + }, { + hex("0x21576b438e500449a151e4eeaf17b154285c68f42d42c1808a11abf3764c0750"), + hex("0x2f17c0559b8fe79608ad5ca193d62f10bce8384c815f0906743d6930836d4a9e"), + hex("0x2d477e3862d07708a79e8aae946170bc9775a4201318474ae665b0b1b7e2730e"), + }, { + hex("0x162f5243967064c390e095577984f291afba2266c38f5abcd89be0f5b2747eab"), + hex("0x2b4cb233ede9ba48264ecd2c8ae50d1ad7a8596a87f29f8a7777a70092393311"), + hex("0x2c8fbcb2dd8573dc1dbaf8f4622854776db2eece6d85c4cf4254e7c35e03b07a"), + }, { + hex("0x1d6f347725e4816af2ff453f0cd56b199e1b61e9f601e9ade5e88db870949da9"), + hex("0x204b0c397f4ebe71ebc2d8b3df5b913df9e6ac02b68d31324cd49af5c4565529"), + hex("0x0c4cb9dc3c4fd8174f1149b3c63c3c2f9ecb827cd7dc25534ff8fb75bc79c502"), + }, { + hex("0x174ad61a1448c899a25416474f4930301e5c49475279e0639a616ddc45bc7b54"), + hex("0x1a96177bcf4d8d89f759df4ec2f3cde2eaaa28c177cc0fa13a9816d49a38d2ef"), + hex("0x066d04b24331d71cd0ef8054bc60c4ff05202c126a233c1a8242ace360b8a30a"), + }, { + hex("0x2a4c4fc6ec0b0cf52195782871c6dd3b381cc65f72e02ad527037a62aa1bd804"), + hex("0x13ab2d136ccf37d447e9f2e14a7cedc95e727f8446f6d9d7e55afc01219fd649"), + hex("0x1121552fca26061619d24d843dc82769c1b04fcec26f55194c2e3e869acc6a9a"), + }, { + hex("0x00ef653322b13d6c889bc81715c37d77a6cd267d595c4a8909a5546c7c97cff1"), + hex("0x0e25483e45a665208b261d8ba74051e6400c776d652595d9845aca35d8a397d3"), + hex("0x29f536dcb9dd7682245264659e15d88e395ac3d4dde92d8c46448db979eeba89"), + }, { + hex("0x2a56ef9f2c53febadfda33575dbdbd885a124e2780bbea170e456baace0fa5be"), + hex("0x1c8361c78eb5cf5decfb7a2d17b5c409f2ae2999a46762e8ee416240a8cb9af1"), + hex("0x151aff5f38b20a0fc0473089aaf0206b83e8e68a764507bfd3d0ab4be74319c5"), + }, { + hex("0x04c6187e41ed881dc1b239c88f7f9d43a9f52fc8c8b6cdd1e76e47615b51f100"), + hex("0x13b37bd80f4d27fb10d84331f6fb6d534b81c61ed15776449e801b7ddc9c2967"), + hex("0x01a5c536273c2d9df578bfbd32c17b7a2ce3664c2a52032c9321ceb1c4e8a8e4"), + }, { + hex("0x2ab3561834ca73835ad05f5d7acb950b4a9a2c666b9726da832239065b7c3b02"), + hex("0x1d4d8ec291e720db200fe6d686c0d613acaf6af4e95d3bf69f7ed516a597b646"), + hex("0x041294d2cc484d228f5784fe7919fd2bb925351240a04b711514c9c80b65af1d"), + }, { + hex("0x154ac98e01708c611c4fa715991f004898f57939d126e392042971dd90e81fc6"), + hex("0x0b339d8acca7d4f83eedd84093aef51050b3684c88f8b0b04524563bc6ea4da4"), + hex("0x0955e49e6610c94254a4f84cfbab344598f0e71eaff4a7dd81ed95b50839c82e"), + }, { + hex("0x06746a6156eba54426b9e22206f15abca9a6f41e6f535c6f3525401ea0654626"), + hex("0x0f18f5a0ecd1423c496f3820c549c27838e5790e2bd0a196ac917c7ff32077fb"), + hex("0x04f6eeca1751f7308ac59eff5beb261e4bb563583ede7bc92a738223d6f76e13"), + }, { + hex("0x2b56973364c4c4f5c1a3ec4da3cdce038811eb116fb3e45bc1768d26fc0b3758"), + hex("0x123769dd49d5b054dcd76b89804b1bcb8e1392b385716a5d83feb65d437f29ef"), + hex("0x2147b424fc48c80a88ee52b91169aacea989f6446471150994257b2fb01c63e9"), + }, { + hex("0x0fdc1f58548b85701a6c5505ea332a29647e6f34ad4243c2ea54ad897cebe54d"), + hex("0x12373a8251fea004df68abcf0f7786d4bceff28c5dbbe0c3944f685cc0a0b1f2"), + hex("0x21e4f4ea5f35f85bad7ea52ff742c9e8a642756b6af44203dd8a1f35c1a90035"), + }, { + hex("0x16243916d69d2ca3dfb4722224d4c462b57366492f45e90d8a81934f1bc3b147"), + hex("0x1efbe46dd7a578b4f66f9adbc88b4378abc21566e1a0453ca13a4159cac04ac2"), + hex("0x07ea5e8537cf5dd08886020e23a7f387d468d5525be66f853b672cc96a88969a"), + }, { + hex("0x05a8c4f9968b8aa3b7b478a30f9a5b63650f19a75e7ce11ca9fe16c0b76c00bc"), + hex("0x20f057712cc21654fbfe59bd345e8dac3f7818c701b9c7882d9d57b72a32e83f"), + hex("0x04a12ededa9dfd689672f8c67fee31636dcd8e88d01d49019bd90b33eb33db69"), + }, { + hex("0x27e88d8c15f37dcee44f1e5425a51decbd136ce5091a6767e49ec9544ccd101a"), + hex("0x2feed17b84285ed9b8a5c8c5e95a41f66e096619a7703223176c41ee433de4d1"), + hex("0x1ed7cc76edf45c7c404241420f729cf394e5942911312a0d6972b8bd53aff2b8"), + }, { + hex("0x15742e99b9bfa323157ff8c586f5660eac6783476144cdcadf2874be45466b1a"), + hex("0x1aac285387f65e82c895fc6887ddf40577107454c6ec0317284f033f27d0c785"), + hex("0x25851c3c845d4790f9ddadbdb6057357832e2e7a49775f71ec75a96554d67c77"), + }, { + hex("0x15a5821565cc2ec2ce78457db197edf353b7ebba2c5523370ddccc3d9f146a67"), + hex("0x2411d57a4813b9980efa7e31a1db5966dcf64f36044277502f15485f28c71727"), + hex("0x002e6f8d6520cd4713e335b8c0b6d2e647e9a98e12f4cd2558828b5ef6cb4c9b"), + }, { + hex("0x2ff7bc8f4380cde997da00b616b0fcd1af8f0e91e2fe1ed7398834609e0315d2"), + hex("0x00b9831b948525595ee02724471bcd182e9521f6b7bb68f1e93be4febb0d3cbe"), + hex("0x0a2f53768b8ebf6a86913b0e57c04e011ca408648a4743a87d77adbf0c9c3512"), + }, { + hex("0x00248156142fd0373a479f91ff239e960f599ff7e94be69b7f2a290305e1198d"), + hex("0x171d5620b87bfb1328cf8c02ab3f0c9a397196aa6a542c2350eb512a2b2bcda9"), + hex("0x170a4f55536f7dc970087c7c10d6fad760c952172dd54dd99d1045e4ec34a808"), + }, { + hex("0x29aba33f799fe66c2ef3134aea04336ecc37e38c1cd211ba482eca17e2dbfae1"), + hex("0x1e9bc179a4fdd758fdd1bb1945088d47e70d114a03f6a0e8b5ba650369e64973"), + hex("0x1dd269799b660fad58f7f4892dfb0b5afeaad869a9c4b44f9c9e1c43bdaf8f09"), + }, { + hex("0x22cdbc8b70117ad1401181d02e15459e7ccd426fe869c7c95d1dd2cb0f24af38"), + hex("0x0ef042e454771c533a9f57a55c503fcefd3150f52ed94a7cd5ba93b9c7dacefd"), + hex("0x11609e06ad6c8fe2f287f3036037e8851318e8b08a0359a03b304ffca62e8284"), + }, { + hex("0x1166d9e554616dba9e753eea427c17b7fecd58c076dfe42708b08f5b783aa9af"), + hex("0x2de52989431a859593413026354413db177fbf4cd2ac0b56f855a888357ee466"), + hex("0x3006eb4ffc7a85819a6da492f3a8ac1df51aee5b17b8e89d74bf01cf5f71e9ad"), + }, { + hex("0x2af41fbb61ba8a80fdcf6fff9e3f6f422993fe8f0a4639f962344c8225145086"), + hex("0x119e684de476155fe5a6b41a8ebc85db8718ab27889e85e781b214bace4827c3"), + hex("0x1835b786e2e8925e188bea59ae363537b51248c23828f047cff784b97b3fd800"), + }, { + hex("0x28201a34c594dfa34d794996c6433a20d152bac2a7905c926c40e285ab32eeb6"), + hex("0x083efd7a27d1751094e80fefaf78b000864c82eb571187724a761f88c22cc4e7"), + hex("0x0b6f88a3577199526158e61ceea27be811c16df7774dd8519e079564f61fd13b"), + }, { + hex("0x0ec868e6d15e51d9644f66e1d6471a94589511ca00d29e1014390e6ee4254f5b"), + hex("0x2af33e3f866771271ac0c9b3ed2e1142ecd3e74b939cd40d00d937ab84c98591"), + hex("0x0b520211f904b5e7d09b5d961c6ace7734568c547dd6858b364ce5e47951f178"), + }, { + hex("0x0b2d722d0919a1aad8db58f10062a92ea0c56ac4270e822cca228620188a1d40"), + hex("0x1f790d4d7f8cf094d980ceb37c2453e957b54a9991ca38bbe0061d1ed6e562d4"), + hex("0x0171eb95dfbf7d1eaea97cd385f780150885c16235a2a6a8da92ceb01e504233"), + }, { + hex("0x0c2d0e3b5fd57549329bf6885da66b9b790b40defd2c8650762305381b168873"), + hex("0x1162fb28689c27154e5a8228b4e72b377cbcafa589e283c35d3803054407a18d"), + hex("0x2f1459b65dee441b64ad386a91e8310f282c5a92a89e19921623ef8249711bc0"), + }, { + hex("0x1e6ff3216b688c3d996d74367d5cd4c1bc489d46754eb712c243f70d1b53cfbb"), + hex("0x01ca8be73832b8d0681487d27d157802d741a6f36cdc2a0576881f9326478875"), + hex("0x1f7735706ffe9fc586f976d5bdf223dc680286080b10cea00b9b5de315f9650e"), + }, { + hex("0x2522b60f4ea3307640a0c2dce041fba921ac10a3d5f096ef4745ca838285f019"), + hex("0x23f0bee001b1029d5255075ddc957f833418cad4f52b6c3f8ce16c235572575b"), + hex("0x2bc1ae8b8ddbb81fcaac2d44555ed5685d142633e9df905f66d9401093082d59"), + }, { + hex("0x0f9406b8296564a37304507b8dba3ed162371273a07b1fc98011fcd6ad72205f"), + hex("0x2360a8eb0cc7defa67b72998de90714e17e75b174a52ee4acb126c8cd995f0a8"), + hex("0x15871a5cddead976804c803cbaef255eb4815a5e96df8b006dcbbc2767f88948"), + }, { + hex("0x193a56766998ee9e0a8652dd2f3b1da0362f4f54f72379544f957ccdeefb420f"), + hex("0x2a394a43934f86982f9be56ff4fab1703b2e63c8ad334834e4309805e777ae0f"), + hex("0x1859954cfeb8695f3e8b635dcb345192892cd11223443ba7b4166e8876c0d142"), + }, { + hex("0x04e1181763050e58013444dbcb99f1902b11bc25d90bbdca408d3819f4fed32b"), + hex("0x0fdb253dee83869d40c335ea64de8c5bb10eb82db08b5e8b1f5e5552bfd05f23"), + hex("0x058cbe8a9a5027bdaa4efb623adead6275f08686f1c08984a9d7c5bae9b4f1c0"), + }, { + hex("0x1382edce9971e186497eadb1aeb1f52b23b4b83bef023ab0d15228b4cceca59a"), + hex("0x03464990f045c6ee0819ca51fd11b0be7f61b8eb99f14b77e1e6634601d9e8b5"), + hex("0x23f7bfc8720dc296fff33b41f98ff83c6fcab4605db2eb5aaa5bc137aeb70a58"), + }, { + hex("0x0a59a158e3eec2117e6e94e7f0e9decf18c3ffd5e1531a9219636158bbaf62f2"), + hex("0x06ec54c80381c052b58bf23b312ffd3ce2c4eba065420af8f4c23ed0075fd07b"), + hex("0x118872dc832e0eb5476b56648e867ec8b09340f7a7bcb1b4962f0ff9ed1f9d01"), + }, { + hex("0x13d69fa127d834165ad5c7cba7ad59ed52e0b0f0e42d7fea95e1906b520921b1"), + hex("0x169a177f63ea681270b1c6877a73d21bde143942fb71dc55fd8a49f19f10c77b"), + hex("0x04ef51591c6ead97ef42f287adce40d93abeb032b922f66ffb7e9a5a7450544d"), + }, { + hex("0x256e175a1dc079390ecd7ca703fb2e3b19ec61805d4f03ced5f45ee6dd0f69ec"), + hex("0x30102d28636abd5fe5f2af412ff6004f75cc360d3205dd2da002813d3e2ceeb2"), + hex("0x10998e42dfcd3bbf1c0714bc73eb1bf40443a3fa99bef4a31fd31be182fcc792"), + }, { + hex("0x193edd8e9fcf3d7625fa7d24b598a1d89f3362eaf4d582efecad76f879e36860"), + hex("0x18168afd34f2d915d0368ce80b7b3347d1c7a561ce611425f2664d7aa51f0b5d"), + hex("0x29383c01ebd3b6ab0c017656ebe658b6a328ec77bc33626e29e2e95b33ea6111"), + }, { + hex("0x10646d2f2603de39a1f4ae5e7771a64a702db6e86fb76ab600bf573f9010c711"), + hex("0x0beb5e07d1b27145f575f1395a55bf132f90c25b40da7b3864d0242dcb1117fb"), + hex("0x16d685252078c133dc0d3ecad62b5c8830f95bb2e54b59abdffbf018d96fa336"), + }, { + hex("0x0a6abd1d833938f33c74154e0404b4b40a555bbbec21ddfafd672dd62047f01a"), + hex("0x1a679f5d36eb7b5c8ea12a4c2dedc8feb12dffeec450317270a6f19b34cf1860"), + hex("0x0980fb233bd456c23974d50e0ebfde4726a423eada4e8f6ffbc7592e3f1b93d6"), + }, { + hex("0x161b42232e61b84cbf1810af93a38fc0cece3d5628c9282003ebacb5c312c72b"), + hex("0x0ada10a90c7f0520950f7d47a60d5e6a493f09787f1564e5d09203db47de1a0b"), + hex("0x1a730d372310ba82320345a29ac4238ed3f07a8a2b4e121bb50ddb9af407f451"), + }, { + hex("0x2c8120f268ef054f817064c369dda7ea908377feaba5c4dffbda10ef58e8c556"), + hex("0x1c7c8824f758753fa57c00789c684217b930e95313bcb73e6e7b8649a4968f70"), + hex("0x2cd9ed31f5f8691c8e39e4077a74faa0f400ad8b491eb3f7b47b27fa3fd1cf77"), + }, { + hex("0x23ff4f9d46813457cf60d92f57618399a5e022ac321ca550854ae23918a22eea"), + hex("0x09945a5d147a4f66ceece6405dddd9d0af5a2c5103529407dff1ea58f180426d"), + hex("0x188d9c528025d4c2b67660c6b771b90f7c7da6eaa29d3f268a6dd223ec6fc630"), + }, { + hex("0x3050e37996596b7f81f68311431d8734dba7d926d3633595e0c0d8ddf4f0f47f"), + hex("0x15af1169396830a91600ca8102c35c426ceae5461e3f95d89d829518d30afd78"), + hex("0x1da6d09885432ea9a06d9f37f873d985dae933e351466b2904284da3320d8acc"), + }, { + hex("0x2796ea90d269af29f5f8acf33921124e4e4fad3dbe658945e546ee411ddaa9cb"), + hex("0x202d7dd1da0f6b4b0325c8b3307742f01e15612ec8e9304a7cb0319e01d32d60"), + hex("0x096d6790d05bb759156a952ba263d672a2d7f9c788f4c831a29dace4c0f8be5f"), + }, { + hex("0x054efa1f65b0fce283808965275d877b438da23ce5b13e1963798cb1447d25a4"), + hex("0x1b162f83d917e93edb3308c29802deb9d8aa690113b2e14864ccf6e18e4165f1"), + hex("0x21e5241e12564dd6fd9f1cdd2a0de39eedfefc1466cc568ec5ceb745a0506edc"), + }, { + hex("0x1cfb5662e8cf5ac9226a80ee17b36abecb73ab5f87e161927b4349e10e4bdf08"), + hex("0x0f21177e302a771bbae6d8d1ecb373b62c99af346220ac0129c53f666eb24100"), + hex("0x1671522374606992affb0dd7f71b12bec4236aede6290546bcef7e1f515c2320"), + }, { + hex("0x0fa3ec5b9488259c2eb4cf24501bfad9be2ec9e42c5cc8ccd419d2a692cad870"), + hex("0x193c0e04e0bd298357cb266c1506080ed36edce85c648cc085e8c57b1ab54bba"), + hex("0x102adf8ef74735a27e9128306dcbc3c99f6f7291cd406578ce14ea2adaba68f8"), + }, { + hex("0x0fe0af7858e49859e2a54d6f1ad945b1316aa24bfbdd23ae40a6d0cb70c3eab1"), + hex("0x216f6717bbc7dedb08536a2220843f4e2da5f1daa9ebdefde8a5ea7344798d22"), + hex("0x1da55cc900f0d21f4a3e694391918a1b3c23b2ac773c6b3ef88e2e4228325161"), + }, +} + +type Poseidon struct { + In1 frontend.Variable + In2 frontend.Variable +} + +func (g Poseidon) DefineGadget(api abstractor.API) []frontend.Variable { + halfRoundG := api.DefineGadget(&halfRound{}) + fullRoundG := api.DefineGadget(&fullRound{}) + state := [3]frontend.Variable{0, g.In1, g.In2} + for i := 0; i < 4; i += 1 { + r := fullRoundG.Call(fullRound{state, CONSTANTS[i]}) + state = *(*[3]frontend.Variable)(r) + } + for i := 0; i < 57; i += 1 { + r := halfRoundG.Call(fullRound{state, CONSTANTS[4+i]}) + state = *(*[3]frontend.Variable)(r) + } + for i := 0; i < 4; i += 1 { + r := fullRoundG.Call(fullRound{state, CONSTANTS[61+i]}) + state = *(*[3]frontend.Variable)(r) + } + return []frontend.Variable{state[0]} +} + +type sbox struct { + inp frontend.Variable +} + +func (s sbox) DefineGadget(api abstractor.API) []frontend.Variable { + v2 := api.Mul(s.inp, s.inp) + v4 := api.Mul(v2, v2) + r := api.Mul(s.inp, v4) + return []frontend.Variable{r} +} + +type mds struct { + inp [3]frontend.Variable +} + +func (m mds) DefineGadget(api abstractor.API) []frontend.Variable { + var mds [3]frontend.Variable + for i := 0; i < 3; i += 1 { + var sum frontend.Variable = 0 + for j := 0; j < 3; j += 1 { + sum = api.Add(sum, api.Mul(m.inp[j], MDS[i][j])) + } + mds[i] = sum + } + return mds[:] +} + +type halfRound struct { + inp [3]frontend.Variable + consts [3]frontend.Variable +} + +func (h halfRound) DefineGadget(api abstractor.API) []frontend.Variable { + sboxG := api.DefineGadget(&sbox{}) + mdsG := api.DefineGadget(&mds{}) + for i := 0; i < 3; i += 1 { + h.inp[i] = api.Add(h.inp[i], h.consts[i]) + } + h.inp[0] = sboxG.Call(sbox{h.inp[0]})[0] + return mdsG.Call(mds{h.inp}) +} + +type fullRound struct { + inp [3]frontend.Variable + consts [3]frontend.Variable +} + +func (h fullRound) DefineGadget(api abstractor.API) []frontend.Variable { + sboxG := api.DefineGadget(&sbox{}) + mdsG := api.DefineGadget(mds{}) + for i := 0; i < 3; i += 1 { + h.inp[i] = api.Add(h.inp[i], h.consts[i]) + } + for i := 0; i < 3; i += 1 { + h.inp[i] = sboxG.Call(sbox{h.inp[i]})[0] + } + return mdsG.Call(mds{h.inp}) +} diff --git a/go.mod b/go.mod index ed0a7e1..b9d403d 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,8 @@ require ( github.com/blang/semver/v4 v4.0.0 // indirect github.com/consensys/bavard v0.1.13 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fxamacker/cbor/v2 v2.4.0 // indirect + github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect github.com/kr/text v0.2.0 // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect @@ -19,6 +21,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect github.com/rs/zerolog v1.29.0 // indirect + github.com/x448/float16 v0.8.4 // indirect golang.org/x/sys v0.5.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect rsc.io/tmplfunc v0.0.3 // indirect diff --git a/go.sum b/go.sum index 3a3dfa8..654745c 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,11 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88= +github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U= +github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -42,6 +45,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= From f3fa26a8bea02656c8b0e78d02d9ff9ee8dd95a4 Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Wed, 12 Jul 2023 19:41:25 +0100 Subject: [PATCH 02/13] Bug fixes for Array fields --- extractor/extractor.go | 34 +++++++++++++++++++--------------- extractor/extractor_test.go | 2 +- extractor/poseidon_test.go | 32 ++++++++++++++++---------------- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/extractor/extractor.go b/extractor/extractor.go index a974114..383d8cc 100644 --- a/extractor/extractor.go +++ b/extractor/extractor.go @@ -104,6 +104,16 @@ type ExGadget struct { func (g *ExGadget) isOp() {} +func ArrayToSlice(v reflect.Value) []frontend.Variable { + res := make([]frontend.Variable, v.Len()) + + for i := 0; i < v.Len(); i++ { + res[i] = v.Index(i).Elem().Interface().(frontend.Variable) + } + + return res +} + func (g *ExGadget) Call(gadget abstractor.GadgetDefinition) []frontend.Variable { args := []frontend.Variable{} @@ -112,8 +122,12 @@ func (g *ExGadget) Call(gadget abstractor.GadgetDefinition) []frontend.Variable for i := 0; i < rt.NumField(); i++ { fld := rt.Field(i) v := rv.FieldByName(fld.Name) - if v.Kind() == reflect.Slice || v.Kind() == reflect.Array { + if v.Kind() == reflect.Slice { args = append(args, v.Interface().([]frontend.Variable)) + } else if v.Kind() == reflect.Array { + // I can't convert from array to slice using Reflect because + // the field is unaddressable. + args = append(args, ArrayToSlice(v)) } else { args = append(args, v.Elem().Interface().(frontend.Variable)) } @@ -154,19 +168,6 @@ type CodeExtractor struct { Field ecc.ID } -func operandFromArray(args []frontend.Variable) []Operand { - ops := make([]Operand, len(args)) - for i, arg := range args { - switch arg.(type) { - case Input, Gate, Proj, Const: - ops[i] = arg.(Operand) - default: - ops[i] = arg.(Proj).Operand - } - } - return ops -} - func sanitizeVars(args ...frontend.Variable) []Operand { ops := []Operand{} for _, arg := range args { @@ -179,7 +180,7 @@ func sanitizeVars(args ...frontend.Variable) []Operand { casted := arg.(big.Int) ops = append(ops, Const{&casted}) case []frontend.Variable: - opsArray := operandFromArray(arg.([]frontend.Variable)) + opsArray := sanitizeVars(arg.([]frontend.Variable)...) ops = append(ops, ProjArray{opsArray}) default: fmt.Printf("invalid argument of type %T\n%#v\n", arg, arg) @@ -331,6 +332,9 @@ func getGadgetByName(gadgets []ExGadget, name string) abstractor.Gadget { } func (ce *CodeExtractor) DefineGadget(gadget abstractor.GadgetDefinition) abstractor.Gadget { + if reflect.ValueOf(gadget).Kind() != reflect.Ptr { + panic("DefineGadget only takes pointers to the gadget") + } schema, _ := GetSchema(gadget) CircuitInit(gadget, schema) // Can't use `schema.NbPublic + schema.NbSecret` diff --git a/extractor/extractor_test.go b/extractor/extractor_test.go index 8c2e960..9f76579 100644 --- a/extractor/extractor_test.go +++ b/extractor/extractor_test.go @@ -63,7 +63,7 @@ type Semaphore struct { func (circuit *Semaphore) AbsDefine(api abstractor.API) error { // From https://github.com/semaphore-protocol/semaphore/blob/main/packages/circuits/semaphore.circom - dummy_poseidon_1 := api.DefineGadget(&Poseidon{}) + dummy_poseidon_1 := api.DefineGadget(&DummyPoseidon1{}) dummy_poseidon_2 := api.DefineGadget(&Poseidon{}) merkle_tree_inclusion_proof := api.DefineGadget(&MerkleTreeInclusionProof{ PathIndices: make([]frontend.Variable, circuit.Levels), diff --git a/extractor/poseidon_test.go b/extractor/poseidon_test.go index 54e5a80..fdf8015 100644 --- a/extractor/poseidon_test.go +++ b/extractor/poseidon_test.go @@ -319,18 +319,18 @@ func (g Poseidon) DefineGadget(api abstractor.API) []frontend.Variable { } type sbox struct { - inp frontend.Variable + Inp frontend.Variable } func (s sbox) DefineGadget(api abstractor.API) []frontend.Variable { - v2 := api.Mul(s.inp, s.inp) + v2 := api.Mul(s.Inp, s.Inp) v4 := api.Mul(v2, v2) - r := api.Mul(s.inp, v4) + r := api.Mul(s.Inp, v4) return []frontend.Variable{r} } type mds struct { - inp [3]frontend.Variable + Inp [3]frontend.Variable } func (m mds) DefineGadget(api abstractor.API) []frontend.Variable { @@ -338,7 +338,7 @@ func (m mds) DefineGadget(api abstractor.API) []frontend.Variable { for i := 0; i < 3; i += 1 { var sum frontend.Variable = 0 for j := 0; j < 3; j += 1 { - sum = api.Add(sum, api.Mul(m.inp[j], MDS[i][j])) + sum = api.Add(sum, api.Mul(m.Inp[j], MDS[i][j])) } mds[i] = sum } @@ -346,33 +346,33 @@ func (m mds) DefineGadget(api abstractor.API) []frontend.Variable { } type halfRound struct { - inp [3]frontend.Variable - consts [3]frontend.Variable + Inp [3]frontend.Variable + Consts [3]frontend.Variable } func (h halfRound) DefineGadget(api abstractor.API) []frontend.Variable { sboxG := api.DefineGadget(&sbox{}) mdsG := api.DefineGadget(&mds{}) for i := 0; i < 3; i += 1 { - h.inp[i] = api.Add(h.inp[i], h.consts[i]) + h.Inp[i] = api.Add(h.Inp[i], h.Consts[i]) } - h.inp[0] = sboxG.Call(sbox{h.inp[0]})[0] - return mdsG.Call(mds{h.inp}) + h.Inp[0] = sboxG.Call(sbox{h.Inp[0]})[0] + return mdsG.Call(mds{h.Inp}) } type fullRound struct { - inp [3]frontend.Variable - consts [3]frontend.Variable + Inp [3]frontend.Variable + Consts [3]frontend.Variable } func (h fullRound) DefineGadget(api abstractor.API) []frontend.Variable { sboxG := api.DefineGadget(&sbox{}) - mdsG := api.DefineGadget(mds{}) + mdsG := api.DefineGadget(&mds{}) for i := 0; i < 3; i += 1 { - h.inp[i] = api.Add(h.inp[i], h.consts[i]) + h.Inp[i] = api.Add(h.Inp[i], h.Consts[i]) } for i := 0; i < 3; i += 1 { - h.inp[i] = sboxG.Call(sbox{h.inp[i]})[0] + h.Inp[i] = sboxG.Call(sbox{h.Inp[i]})[0] } - return mdsG.Call(mds{h.inp}) + return mdsG.Call(mds{h.Inp}) } From f48b29b6916ab8146297a3d86b1dca30e3e0fffb Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Wed, 12 Jul 2023 20:10:06 +0100 Subject: [PATCH 03/13] Replaced Vect with Vector --- extractor/lean_export.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extractor/lean_export.go b/extractor/lean_export.go index 65fc71b..20a90cf 100644 --- a/extractor/lean_export.go +++ b/extractor/lean_export.go @@ -14,7 +14,7 @@ import ( func ExportGadget(gadget ExGadget) string { kArgsType := "F" if len(gadget.Outputs) > 1 { - kArgsType = fmt.Sprintf("Vect F %d", len(gadget.Outputs)) + kArgsType = fmt.Sprintf("Vector F %d", len(gadget.Outputs)) } inAssignment := gadget.Args return fmt.Sprintf("def %s %s (k: %s -> Prop): Prop :=\n%s", gadget.Name, genArgs(inAssignment), kArgsType, genGadgetBody(inAssignment, gadget)) From 29c882dff7cb2b556c487c03baaa4a8f85d00cb4 Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Wed, 12 Jul 2023 21:52:34 +0100 Subject: [PATCH 04/13] Fixed assignGateVars for ProjArray --- extractor/lean_export.go | 41 ++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/extractor/lean_export.go b/extractor/lean_export.go index 20a90cf..867d81c 100644 --- a/extractor/lean_export.go +++ b/extractor/lean_export.go @@ -180,14 +180,18 @@ func genArgs(inAssignment []ExArg) string { return strings.Join(args, " ") } -func extractBaseArg(arg Operand) Operand { +func extractGateVars(arg Operand) []Operand { switch arg.(type) { case Proj: - return extractBaseArg(arg.(Proj).Operand) + return extractGateVars(arg.(Proj).Operand) case ProjArray: - return extractBaseArg(arg.(ProjArray).Proj[0]) + res := []Operand{} + for i := range arg.(ProjArray).Proj { + res = append(res, extractGateVars(arg.(ProjArray).Proj[i])...) + } + return res default: - return arg + return []Operand{arg} } } @@ -195,26 +199,31 @@ func assignGateVars(code []App, additional ...Operand) []string { gateVars := make([]string, len(code)) for _, app := range code { for _, arg := range app.Args { - base := extractBaseArg(arg) - switch base.(type) { - case Gate: - ix := base.(Gate).Index - if gateVars[ix] == "" { - gateVars[ix] = fmt.Sprintf("gate_%d", ix) + bases := extractGateVars(arg) + for _, base := range bases { + switch base.(type) { + case Gate: + ix := base.(Gate).Index + if gateVars[ix] == "" { + gateVars[ix] = fmt.Sprintf("gate_%d", ix) + } } } } } for _, out := range additional { - outBase := extractBaseArg(out) - switch outBase.(type) { - case Gate: - ix := outBase.(Gate).Index - if gateVars[ix] == "" { - gateVars[ix] = fmt.Sprintf("gate_%d", ix) + outBases := extractGateVars(out) + for _, outBase := range outBases { + switch outBase.(type) { + case Gate: + ix := outBase.(Gate).Index + if gateVars[ix] == "" { + gateVars[ix] = fmt.Sprintf("gate_%d", ix) + } } } } + return gateVars } From 3dc1812f960037617947bab8618be654d28095ed Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Thu, 20 Jul 2023 19:45:24 +0100 Subject: [PATCH 05/13] Added dynamic curve order in exported circuit --- extractor/extractor.go | 1 + extractor/lean_export.go | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/extractor/extractor.go b/extractor/extractor.go index 036fac3..d54e040 100644 --- a/extractor/extractor.go +++ b/extractor/extractor.go @@ -147,6 +147,7 @@ type ExCircuit struct { Inputs []ExArg Gadgets []ExGadget Code []App + Field ecc.ID } type CodeExtractor struct { diff --git a/extractor/lean_export.go b/extractor/lean_export.go index 97099a9..0890419 100644 --- a/extractor/lean_export.go +++ b/extractor/lean_export.go @@ -12,18 +12,17 @@ import ( "github.com/consensys/gnark/frontend/schema" ) -func ExportPrelude() string { - // Order could be extracted from circuit curve - s := `import ProvenZk.Gates +func ExportPrelude(field ecc.ID) string { + s := fmt.Sprintf(`import ProvenZk.Gates import ProvenZk.VectorExtensions namespace Circuit -def Order : ℕ := 21888242871839275222246405745257275088548364400416034343698204186575808495617 +def Order : ℕ := %s variable [Fact (Nat.Prime Order)] -abbrev F := ZMod Order` +abbrev F := ZMod Order`, field.ScalarField()) - return fmt.Sprintf("%s", s) + return s } func ExportFooter() string { @@ -46,7 +45,7 @@ func ExportCircuit(circuit ExCircuit) string { gadgets[i] = ExportGadget(gadget) } circ := fmt.Sprintf("def circuit %s: Prop :=\n%s", genArgs(circuit.Inputs), genCircuitBody(circuit)) - prelude := ExportPrelude() + prelude := ExportPrelude(circuit.Field) footer := ExportFooter() return fmt.Sprintf("%s\n\n%s\n\n%s\n\n%s", prelude, strings.Join(gadgets, "\n\n"), circ, footer) } @@ -176,6 +175,7 @@ func CircuitToLean(circuit abstractor.Circuit, field ecc.ID) (string, error) { Inputs: GetExArgs(circuit, schema.Fields), Gadgets: api.Gadgets, Code: api.Code, + Field: api.Field, } out := ExportCircuit(extractorCircuit) return out, nil From ff134eef91c517bd758efb422f759c881916c83c Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Thu, 20 Jul 2023 19:55:20 +0100 Subject: [PATCH 06/13] Added dynamic namespace name --- extractor/extractor.go | 1 + extractor/lean_export.go | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/extractor/extractor.go b/extractor/extractor.go index d54e040..2cbfb75 100644 --- a/extractor/extractor.go +++ b/extractor/extractor.go @@ -148,6 +148,7 @@ type ExCircuit struct { Gadgets []ExGadget Code []App Field ecc.ID + Name string } type CodeExtractor struct { diff --git a/extractor/lean_export.go b/extractor/lean_export.go index 0890419..f10ea06 100644 --- a/extractor/lean_export.go +++ b/extractor/lean_export.go @@ -12,22 +12,22 @@ import ( "github.com/consensys/gnark/frontend/schema" ) -func ExportPrelude(field ecc.ID) string { +func ExportPrelude(circuit ExCircuit) string { s := fmt.Sprintf(`import ProvenZk.Gates import ProvenZk.VectorExtensions -namespace Circuit +namespace %s def Order : ℕ := %s variable [Fact (Nat.Prime Order)] -abbrev F := ZMod Order`, field.ScalarField()) +abbrev F := ZMod Order`, circuit.Name, circuit.Field.ScalarField()) return s } -func ExportFooter() string { - s := `end Circuit` - return fmt.Sprintf("%s", s) +func ExportFooter(circuit ExCircuit) string { + s := fmt.Sprintf(`end %s`, circuit.Name) + return s } func ExportGadget(gadget ExGadget) string { @@ -45,8 +45,8 @@ func ExportCircuit(circuit ExCircuit) string { gadgets[i] = ExportGadget(gadget) } circ := fmt.Sprintf("def circuit %s: Prop :=\n%s", genArgs(circuit.Inputs), genCircuitBody(circuit)) - prelude := ExportPrelude(circuit.Field) - footer := ExportFooter() + prelude := ExportPrelude(circuit) + footer := ExportFooter(circuit) return fmt.Sprintf("%s\n\n%s\n\n%s\n\n%s", prelude, strings.Join(gadgets, "\n\n"), circ, footer) } @@ -171,11 +171,14 @@ func CircuitToLean(circuit abstractor.Circuit, field ecc.ID) (string, error) { return "", err } + name := reflect.TypeOf(circuit).Elem().Name() + extractorCircuit := ExCircuit{ Inputs: GetExArgs(circuit, schema.Fields), Gadgets: api.Gadgets, Code: api.Code, Field: api.Field, + Name: name, } out := ExportCircuit(extractorCircuit) return out, nil From 4df3794398b34395ce9bbcb1add1f76996f4999b Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Thu, 20 Jul 2023 20:46:05 +0100 Subject: [PATCH 07/13] Removing definegadget call --- abstractor/abstractor.go | 3 +++ abstractor/concretizer.go | 4 ++++ extractor/extractor.go | 5 +++++ extractor/extractor_test.go | 20 ++++++++++---------- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/abstractor/abstractor.go b/abstractor/abstractor.go index 94d6a7b..3275b45 100644 --- a/abstractor/abstractor.go +++ b/abstractor/abstractor.go @@ -13,6 +13,9 @@ type GadgetDefinition interface { type API interface { frontend.API DefineGadget(gadget GadgetDefinition) Gadget + + frontend.API + Call(gadget GadgetDefinition) []frontend.Variable } type Circuit interface { diff --git a/abstractor/concretizer.go b/abstractor/concretizer.go index 395e145..e511665 100644 --- a/abstractor/concretizer.go +++ b/abstractor/concretizer.go @@ -122,4 +122,8 @@ func (c *Concretizer) DefineGadget(gadget GadgetDefinition) Gadget { return &ConcreteGadget{c} } +func (c *Concretizer) Call(gadget GadgetDefinition) []frontend.Variable { + return c.Call(gadget) +} + var _ API = &(Concretizer{}) diff --git a/extractor/extractor.go b/extractor/extractor.go index 2cbfb75..ca2fa98 100644 --- a/extractor/extractor.go +++ b/extractor/extractor.go @@ -132,6 +132,11 @@ func (g *ExGadget) Call(gadget abstractor.GadgetDefinition) []frontend.Variable return outs } +func (ce *CodeExtractor) Call(gadget abstractor.GadgetDefinition) []frontend.Variable { + g := ce.DefineGadget(gadget) + return g.Call(gadget) +} + type ExArgType struct { Size int Type *ExArgType diff --git a/extractor/extractor_test.go b/extractor/extractor_test.go index 2deea23..8a6450e 100644 --- a/extractor/extractor_test.go +++ b/extractor/extractor_test.go @@ -35,15 +35,15 @@ type CircuitWithParameter struct { } func (circuit *CircuitWithParameter) AbsDefine(api abstractor.API) error { - slice_3 := api.DefineGadget(&SliceGadget{ - In_1: make([]frontend.Variable, 3), - In_2: make([]frontend.Variable, 3), - }) + // slice_3 := api.DefineGadget(&SliceGadget{ + // In_1: make([]frontend.Variable, 3), + // In_2: make([]frontend.Variable, 3), + // }) - slice_2 := api.DefineGadget(&SliceGadget{ - In_1: make([]frontend.Variable, 2), - In_2: make([]frontend.Variable, 2), - }) + // slice_2 := api.DefineGadget(&SliceGadget{ + // In_1: make([]frontend.Variable, 2), + // In_2: make([]frontend.Variable, 2), + // }) api.FromBinary(circuit.Path...) bin := api.ToBinary(circuit.In) @@ -51,10 +51,10 @@ func (circuit *CircuitWithParameter) AbsDefine(api abstractor.API) error { dec := api.FromBinary(bin...) api.AssertIsEqual(circuit.Param, dec) - slice_3.Call(SliceGadget{circuit.Path, circuit.Path}) + api.Call(&SliceGadget{circuit.Path, circuit.Path}) api.Mul(circuit.Path[0], circuit.Path[0]) - slice_2.Call(SliceGadget{circuit.Tree, circuit.Tree}) + api.Call(&SliceGadget{circuit.Tree, circuit.Tree}) api.AssertIsEqual(circuit.Param, circuit.In) return nil From 722dd3424e2a318816655f37148204bebd59a2b5 Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Thu, 20 Jul 2023 21:10:48 +0100 Subject: [PATCH 08/13] Merge main --- extractor/lean_export.go | 43 ++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/extractor/lean_export.go b/extractor/lean_export.go index 78de84d..6a70ec9 100644 --- a/extractor/lean_export.go +++ b/extractor/lean_export.go @@ -33,7 +33,7 @@ func ExportFooter(circuit ExCircuit) string { func ExportGadget(gadget ExGadget) string { kArgsType := "F" if len(gadget.Outputs) > 1 { - kArgsType = fmt.Sprintf("Vect F %d", len(gadget.Outputs)) + kArgsType = fmt.Sprintf("Vector F %d", len(gadget.Outputs)) } inAssignment := gadget.Args return fmt.Sprintf("def %s %s (k: %s -> Prop): Prop :=\n%s", gadget.Name, genArgs(inAssignment), kArgsType, genGadgetBody(inAssignment, gadget)) @@ -204,14 +204,18 @@ func genArgs(inAssignment []ExArg) string { return strings.Join(args, " ") } -func extractBaseArg(arg Operand) Operand { +func extractGateVars(arg Operand) []Operand { switch arg.(type) { case Proj: - return extractBaseArg(arg.(Proj).Operand) + return extractGateVars(arg.(Proj).Operand) case ProjArray: - return extractBaseArg(arg.(ProjArray).Proj[0]) + res := []Operand{} + for i := range arg.(ProjArray).Proj { + res = append(res, extractGateVars(arg.(ProjArray).Proj[i])...) + } + return res default: - return arg + return []Operand{arg} } } @@ -219,26 +223,31 @@ func assignGateVars(code []App, additional ...Operand) []string { gateVars := make([]string, len(code)) for _, app := range code { for _, arg := range app.Args { - base := extractBaseArg(arg) - switch base.(type) { - case Gate: - ix := base.(Gate).Index - if gateVars[ix] == "" { - gateVars[ix] = fmt.Sprintf("gate_%d", ix) + bases := extractGateVars(arg) + for _, base := range bases { + switch base.(type) { + case Gate: + ix := base.(Gate).Index + if gateVars[ix] == "" { + gateVars[ix] = fmt.Sprintf("gate_%d", ix) + } } } } } for _, out := range additional { - outBase := extractBaseArg(out) - switch outBase.(type) { - case Gate: - ix := outBase.(Gate).Index - if gateVars[ix] == "" { - gateVars[ix] = fmt.Sprintf("gate_%d", ix) + outBases := extractGateVars(out) + for _, outBase := range outBases { + switch outBase.(type) { + case Gate: + ix := outBase.(Gate).Index + if gateVars[ix] == "" { + gateVars[ix] = fmt.Sprintf("gate_%d", ix) + } } } } + return gateVars } From beea5c2d4ff0fe8818172370dc839caa53aef1c1 Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Fri, 21 Jul 2023 09:24:41 +0100 Subject: [PATCH 09/13] Streamlined gadget interface --- extractor/extractor.go | 6 +++++- extractor/extractor_test.go | 28 ++++++---------------------- extractor/lean_export.go | 21 ++++++++++++--------- extractor/poseidon_test.go | 2 +- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/extractor/extractor.go b/extractor/extractor.go index 67b7573..2a580af 100644 --- a/extractor/extractor.go +++ b/extractor/extractor.go @@ -147,7 +147,11 @@ func (g *ExGadget) Call(gadget abstractor.GadgetDefinition) []frontend.Variable } func (ce *CodeExtractor) Call(gadget abstractor.GadgetDefinition) []frontend.Variable { - g := ce.DefineGadget(gadget) + // Copying `gadget` because `DefineGadget` needs to manipulate the input + v := reflect.ValueOf(gadget) + tmp_gadget := reflect.New(v.Type()) + tmp_gadget.Elem().Set(v) + g := ce.DefineGadget(tmp_gadget.Interface().(abstractor.GadgetDefinition)) return g.Call(gadget) } diff --git a/extractor/extractor_test.go b/extractor/extractor_test.go index 8a6450e..b2fa028 100644 --- a/extractor/extractor_test.go +++ b/extractor/extractor_test.go @@ -35,26 +35,16 @@ type CircuitWithParameter struct { } func (circuit *CircuitWithParameter) AbsDefine(api abstractor.API) error { - // slice_3 := api.DefineGadget(&SliceGadget{ - // In_1: make([]frontend.Variable, 3), - // In_2: make([]frontend.Variable, 3), - // }) - - // slice_2 := api.DefineGadget(&SliceGadget{ - // In_1: make([]frontend.Variable, 2), - // In_2: make([]frontend.Variable, 2), - // }) - api.FromBinary(circuit.Path...) bin := api.ToBinary(circuit.In) bin = api.ToBinary(circuit.Param) dec := api.FromBinary(bin...) api.AssertIsEqual(circuit.Param, dec) - api.Call(&SliceGadget{circuit.Path, circuit.Path}) + api.Call(SliceGadget{circuit.Path, circuit.Path}) api.Mul(circuit.Path[0], circuit.Path[0]) - api.Call(&SliceGadget{circuit.Tree, circuit.Tree}) + api.Call(SliceGadget{circuit.Tree, circuit.Tree}) api.AssertIsEqual(circuit.Param, circuit.In) return nil @@ -95,12 +85,10 @@ type MerkleRecover struct { } func (circuit *MerkleRecover) AbsDefine(api abstractor.API) error { - hash := api.DefineGadget(&DummyHash{}) - current := circuit.Element for i := 0; i < len(circuit.Path); i++ { - leftHash := hash.Call(DummyHash{current, circuit.Proof[i]})[0] - rightHash := hash.Call(DummyHash{circuit.Proof[i], current})[0] + leftHash := api.Call(DummyHash{current, circuit.Proof[i]})[0] + rightHash := api.Call(DummyHash{circuit.Proof[i], current})[0] current = api.Select(circuit.Path[i], rightHash, leftHash) } api.AssertIsEqual(current, circuit.Root) @@ -140,10 +128,8 @@ type MySecondWidget struct { } func (gadget MySecondWidget) DefineGadget(api abstractor.API) []frontend.Variable { - my_widget := api.DefineGadget(&MyWidget{}) - mul := api.Mul(gadget.Test_1, gadget.Test_2) - snd := my_widget.Call(MyWidget{gadget.Test_1, gadget.Test_2})[0] + snd := api.Call(MyWidget{gadget.Test_1, gadget.Test_2})[0] r := api.Mul(mul, snd) return []frontend.Variable{r} } @@ -154,11 +140,9 @@ type TwoGadgets struct { } func (circuit *TwoGadgets) AbsDefine(api abstractor.API) error { - my_snd_widget := api.DefineGadget(&MySecondWidget{}) - sum := api.Add(circuit.In_1, circuit.In_2) prod := api.Mul(circuit.In_1, circuit.In_2) - my_snd_widget.Call(MySecondWidget{sum, prod}) + api.Call(MySecondWidget{sum, prod}) return nil } diff --git a/extractor/lean_export.go b/extractor/lean_export.go index 6a70ec9..905c8c9 100644 --- a/extractor/lean_export.go +++ b/extractor/lean_export.go @@ -84,6 +84,9 @@ func CircuitInit(class any, schema *schema.Schema) error { temp.Set(v) } + tmp_c := reflect.ValueOf(&class).Elem().Elem() + tmp := reflect.New(tmp_c.Type()).Elem() + tmp.Set(tmp_c) for j, f := range schema.Fields { field_name := f.Name field := v.FieldByName(field_name) @@ -91,21 +94,21 @@ func CircuitInit(class any, schema *schema.Schema) error { // Can't assign an array to another array, therefore // initialise each element in the array - if field_type.Kind() == reflect.Array || field_type.Kind() == reflect.Slice { - tmp_c := reflect.ValueOf(&class).Elem() - tmp := reflect.New(tmp_c.Elem().Type()).Elem() - tmp.Set(tmp_c.Elem()) + + if field_type.Kind() == reflect.Array { + ArrayInit(f, tmp.Elem().FieldByName(field_name), Input{j}) + } else if field_type.Kind() == reflect.Slice { + // Recreate a zeroed array to remove overlapping pointers if input + // arguments are duplicated (i.e. `api.Call(SliceGadget{circuit.Path, circuit.Path})`) + zero_array := make([]frontend.Variable, f.ArraySize, f.ArraySize) + tmp.Elem().FieldByName(field_name).Set(reflect.ValueOf(&zero_array).Elem()) + ArrayInit(f, tmp.Elem().FieldByName(field_name), Input{j}) - tmp_c.Set(tmp) } else if field_type.Kind() == reflect.Interface { init := Input{j} value := reflect.ValueOf(init) - tmp_c := reflect.ValueOf(&class).Elem() - tmp := reflect.New(tmp_c.Elem().Type()).Elem() - tmp.Set(tmp_c.Elem()) tmp.Elem().FieldByName(field_name).Set(value) - tmp_c.Set(tmp) } else { fmt.Printf("Skipped type %s\n", field_type.Kind()) } diff --git a/extractor/poseidon_test.go b/extractor/poseidon_test.go index fdf8015..92c5b45 100644 --- a/extractor/poseidon_test.go +++ b/extractor/poseidon_test.go @@ -2,7 +2,7 @@ package extractor import ( "github.com/consensys/gnark/frontend" - "gnark-extractor/abstractor" + "github.com/reilabs/gnark-extractor/abstractor" "math/big" ) From c33993275ef2748b548e32231df8884d12b8d750 Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Fri, 21 Jul 2023 09:31:01 +0100 Subject: [PATCH 10/13] Gofmt --- extractor/extractor.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/extractor/extractor.go b/extractor/extractor.go index 2a580af..a6a04cc 100644 --- a/extractor/extractor.go +++ b/extractor/extractor.go @@ -125,7 +125,7 @@ func (g *ExGadget) Call(gadget abstractor.GadgetDefinition) []frontend.Variable v := rv.FieldByName(fld.Name) if v.Kind() == reflect.Slice { args = append(args, v.Interface().([]frontend.Variable)) - } else if v.Kind() == reflect.Array { + } else if v.Kind() == reflect.Array { // I can't convert from array to slice using Reflect because // the field is unaddressable. args = append(args, ArrayToSlice(v)) @@ -344,9 +344,9 @@ func getGadgetByName(gadgets []ExGadget, name string) abstractor.Gadget { } func (ce *CodeExtractor) DefineGadget(gadget abstractor.GadgetDefinition) abstractor.Gadget { - if reflect.ValueOf(gadget).Kind() != reflect.Ptr { - panic("DefineGadget only takes pointers to the gadget") - } + if reflect.ValueOf(gadget).Kind() != reflect.Ptr { + panic("DefineGadget only takes pointers to the gadget") + } schema, _ := GetSchema(gadget) CircuitInit(gadget, schema) // Can't use `schema.NbPublic + schema.NbSecret` From 4783252c4a6fb4f226978d18590d27f28b577176 Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Fri, 21 Jul 2023 09:33:07 +0100 Subject: [PATCH 11/13] Gotidy --- go.mod | 3 --- go.sum | 4 ---- 2 files changed, 7 deletions(-) diff --git a/go.mod b/go.mod index 5bb3ed7..df44379 100644 --- a/go.mod +++ b/go.mod @@ -12,8 +12,6 @@ require ( github.com/blang/semver/v4 v4.0.0 // indirect github.com/consensys/bavard v0.1.13 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/fxamacker/cbor/v2 v2.4.0 // indirect - github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect github.com/kr/text v0.2.0 // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect @@ -21,7 +19,6 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect github.com/rs/zerolog v1.29.0 // indirect - github.com/x448/float16 v0.8.4 // indirect golang.org/x/sys v0.5.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect rsc.io/tmplfunc v0.0.3 // indirect diff --git a/go.sum b/go.sum index 654745c..3a3dfa8 100644 --- a/go.sum +++ b/go.sum @@ -12,11 +12,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88= -github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U= -github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -45,7 +42,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= -github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= From 117ec19c5e00ae6b461fffcc56c89d24f743fd61 Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Fri, 21 Jul 2023 10:29:18 +0100 Subject: [PATCH 12/13] Updated Poseidon test --- extractor/poseidon_test.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/extractor/poseidon_test.go b/extractor/poseidon_test.go index 92c5b45..ef6d55b 100644 --- a/extractor/poseidon_test.go +++ b/extractor/poseidon_test.go @@ -300,19 +300,17 @@ type Poseidon struct { } func (g Poseidon) DefineGadget(api abstractor.API) []frontend.Variable { - halfRoundG := api.DefineGadget(&halfRound{}) - fullRoundG := api.DefineGadget(&fullRound{}) state := [3]frontend.Variable{0, g.In1, g.In2} for i := 0; i < 4; i += 1 { - r := fullRoundG.Call(fullRound{state, CONSTANTS[i]}) + r := api.Call(fullRound{state, CONSTANTS[i]}) state = *(*[3]frontend.Variable)(r) } for i := 0; i < 57; i += 1 { - r := halfRoundG.Call(fullRound{state, CONSTANTS[4+i]}) + r := api.Call(halfRound{state, CONSTANTS[4+i]}) state = *(*[3]frontend.Variable)(r) } for i := 0; i < 4; i += 1 { - r := fullRoundG.Call(fullRound{state, CONSTANTS[61+i]}) + r := api.Call(fullRound{state, CONSTANTS[61+i]}) state = *(*[3]frontend.Variable)(r) } return []frontend.Variable{state[0]} @@ -351,13 +349,11 @@ type halfRound struct { } func (h halfRound) DefineGadget(api abstractor.API) []frontend.Variable { - sboxG := api.DefineGadget(&sbox{}) - mdsG := api.DefineGadget(&mds{}) for i := 0; i < 3; i += 1 { h.Inp[i] = api.Add(h.Inp[i], h.Consts[i]) } - h.Inp[0] = sboxG.Call(sbox{h.Inp[0]})[0] - return mdsG.Call(mds{h.Inp}) + h.Inp[0] = api.Call(sbox{h.Inp[0]})[0] + return api.Call(mds{h.Inp}) } type fullRound struct { @@ -366,13 +362,11 @@ type fullRound struct { } func (h fullRound) DefineGadget(api abstractor.API) []frontend.Variable { - sboxG := api.DefineGadget(&sbox{}) - mdsG := api.DefineGadget(&mds{}) for i := 0; i < 3; i += 1 { h.Inp[i] = api.Add(h.Inp[i], h.Consts[i]) } for i := 0; i < 3; i += 1 { - h.Inp[i] = sboxG.Call(sbox{h.Inp[i]})[0] + h.Inp[i] = api.Call(sbox{h.Inp[i]})[0] } - return mdsG.Call(mds{h.Inp}) + return api.Call(mds{h.Inp}) } From b3c1f36ea09a4cf9bf8f42ef7851820e7d350459 Mon Sep 17 00:00:00 2001 From: Giuseppe <8973725+Eagle941@users.noreply.github.com> Date: Fri, 21 Jul 2023 10:38:45 +0100 Subject: [PATCH 13/13] Moved poseidon to demo repo --- extractor/poseidon_test.go | 372 ------------------------------------- 1 file changed, 372 deletions(-) delete mode 100644 extractor/poseidon_test.go diff --git a/extractor/poseidon_test.go b/extractor/poseidon_test.go deleted file mode 100644 index ef6d55b..0000000 --- a/extractor/poseidon_test.go +++ /dev/null @@ -1,372 +0,0 @@ -package extractor - -import ( - "github.com/consensys/gnark/frontend" - "github.com/reilabs/gnark-extractor/abstractor" - "math/big" -) - -func hex(s string) big.Int { - var bi big.Int - bi.SetString(s, 0) - return bi -} - -var MDS = [3][3]frontend.Variable{ - { - hex("0x109b7f411ba0e4c9b2b70caf5c36a7b194be7c11ad24378bfedb68592ba8118b"), - hex("0x16ed41e13bb9c0c66ae119424fddbcbc9314dc9fdbdeea55d6c64543dc4903e0"), - hex("0x2b90bba00fca0589f617e7dcbfe82e0df706ab640ceb247b791a93b74e36736d"), - }, - { - hex("0x2969f27eed31a480b9c36c764379dbca2cc8fdd1415c3dded62940bcde0bd771"), - hex("0x2e2419f9ec02ec394c9871c832963dc1b89d743c8c7b964029b2311687b1fe23"), - hex("0x101071f0032379b697315876690f053d148d4e109f5fb065c8aacc55a0f89bfa"), - }, - { - hex("0x143021ec686a3f330d5f9e654638065ce6cd79e28c5b3753326244ee65a1b1a7"), - hex("0x176cc029695ad02582a70eff08a6fd99d057e12e58e7d7b6b16cdfabc8ee2911"), - hex("0x19a3fc0a56702bf417ba7fee3802593fa644470307043f7773279cd71d25d5e0"), - }, -} - -var CONSTANTS = [][3]frontend.Variable{ - { - hex("0x0ee9a592ba9a9518d05986d656f40c2114c4993c11bb29938d21d47304cd8e6e"), - hex("0x00f1445235f2148c5986587169fc1bcd887b08d4d00868df5696fff40956e864"), - hex("0x08dff3487e8ac99e1f29a058d0fa80b930c728730b7ab36ce879f3890ecf73f5"), - }, { - hex("0x2f27be690fdaee46c3ce28f7532b13c856c35342c84bda6e20966310fadc01d0"), - hex("0x2b2ae1acf68b7b8d2416bebf3d4f6234b763fe04b8043ee48b8327bebca16cf2"), - hex("0x0319d062072bef7ecca5eac06f97d4d55952c175ab6b03eae64b44c7dbf11cfa"), - }, { - hex("0x28813dcaebaeaa828a376df87af4a63bc8b7bf27ad49c6298ef7b387bf28526d"), - hex("0x2727673b2ccbc903f181bf38e1c1d40d2033865200c352bc150928adddf9cb78"), - hex("0x234ec45ca27727c2e74abd2b2a1494cd6efbd43e340587d6b8fb9e31e65cc632"), - }, { - hex("0x15b52534031ae18f7f862cb2cf7cf760ab10a8150a337b1ccd99ff6e8797d428"), - hex("0x0dc8fad6d9e4b35f5ed9a3d186b79ce38e0e8a8d1b58b132d701d4eecf68d1f6"), - hex("0x1bcd95ffc211fbca600f705fad3fb567ea4eb378f62e1fec97805518a47e4d9c"), - }, { - hex("0x10520b0ab721cadfe9eff81b016fc34dc76da36c2578937817cb978d069de559"), - hex("0x1f6d48149b8e7f7d9b257d8ed5fbbaf42932498075fed0ace88a9eb81f5627f6"), - hex("0x1d9655f652309014d29e00ef35a2089bfff8dc1c816f0dc9ca34bdb5460c8705"), - }, { - hex("0x04df5a56ff95bcafb051f7b1cd43a99ba731ff67e47032058fe3d4185697cc7d"), - hex("0x0672d995f8fff640151b3d290cedaf148690a10a8c8424a7f6ec282b6e4be828"), - hex("0x099952b414884454b21200d7ffafdd5f0c9a9dcc06f2708e9fc1d8209b5c75b9"), - }, { - hex("0x052cba2255dfd00c7c483143ba8d469448e43586a9b4cd9183fd0e843a6b9fa6"), - hex("0x0b8badee690adb8eb0bd74712b7999af82de55707251ad7716077cb93c464ddc"), - hex("0x119b1590f13307af5a1ee651020c07c749c15d60683a8050b963d0a8e4b2bdd1"), - }, { - hex("0x03150b7cd6d5d17b2529d36be0f67b832c4acfc884ef4ee5ce15be0bfb4a8d09"), - hex("0x2cc6182c5e14546e3cf1951f173912355374efb83d80898abe69cb317c9ea565"), - hex("0x005032551e6378c450cfe129a404b3764218cadedac14e2b92d2cd73111bf0f9"), - }, { - hex("0x233237e3289baa34bb147e972ebcb9516469c399fcc069fb88f9da2cc28276b5"), - hex("0x05c8f4f4ebd4a6e3c980d31674bfbe6323037f21b34ae5a4e80c2d4c24d60280"), - hex("0x0a7b1db13042d396ba05d818a319f25252bcf35ef3aeed91ee1f09b2590fc65b"), - }, { - hex("0x2a73b71f9b210cf5b14296572c9d32dbf156e2b086ff47dc5df542365a404ec0"), - hex("0x1ac9b0417abcc9a1935107e9ffc91dc3ec18f2c4dbe7f22976a760bb5c50c460"), - hex("0x12c0339ae08374823fabb076707ef479269f3e4d6cb104349015ee046dc93fc0"), - }, { - hex("0x0b7475b102a165ad7f5b18db4e1e704f52900aa3253baac68246682e56e9a28e"), - hex("0x037c2849e191ca3edb1c5e49f6e8b8917c843e379366f2ea32ab3aa88d7f8448"), - hex("0x05a6811f8556f014e92674661e217e9bd5206c5c93a07dc145fdb176a716346f"), - }, { - hex("0x29a795e7d98028946e947b75d54e9f044076e87a7b2883b47b675ef5f38bd66e"), - hex("0x20439a0c84b322eb45a3857afc18f5826e8c7382c8a1585c507be199981fd22f"), - hex("0x2e0ba8d94d9ecf4a94ec2050c7371ff1bb50f27799a84b6d4a2a6f2a0982c887"), - }, { - hex("0x143fd115ce08fb27ca38eb7cce822b4517822cd2109048d2e6d0ddcca17d71c8"), - hex("0x0c64cbecb1c734b857968dbbdcf813cdf8611659323dbcbfc84323623be9caf1"), - hex("0x028a305847c683f646fca925c163ff5ae74f348d62c2b670f1426cef9403da53"), - }, { - hex("0x2e4ef510ff0b6fda5fa940ab4c4380f26a6bcb64d89427b824d6755b5db9e30c"), - hex("0x0081c95bc43384e663d79270c956ce3b8925b4f6d033b078b96384f50579400e"), - hex("0x2ed5f0c91cbd9749187e2fade687e05ee2491b349c039a0bba8a9f4023a0bb38"), - }, { - hex("0x30509991f88da3504bbf374ed5aae2f03448a22c76234c8c990f01f33a735206"), - hex("0x1c3f20fd55409a53221b7c4d49a356b9f0a1119fb2067b41a7529094424ec6ad"), - hex("0x10b4e7f3ab5df003049514459b6e18eec46bb2213e8e131e170887b47ddcb96c"), - }, { - hex("0x2a1982979c3ff7f43ddd543d891c2abddd80f804c077d775039aa3502e43adef"), - hex("0x1c74ee64f15e1db6feddbead56d6d55dba431ebc396c9af95cad0f1315bd5c91"), - hex("0x07533ec850ba7f98eab9303cace01b4b9e4f2e8b82708cfa9c2fe45a0ae146a0"), - }, { - hex("0x21576b438e500449a151e4eeaf17b154285c68f42d42c1808a11abf3764c0750"), - hex("0x2f17c0559b8fe79608ad5ca193d62f10bce8384c815f0906743d6930836d4a9e"), - hex("0x2d477e3862d07708a79e8aae946170bc9775a4201318474ae665b0b1b7e2730e"), - }, { - hex("0x162f5243967064c390e095577984f291afba2266c38f5abcd89be0f5b2747eab"), - hex("0x2b4cb233ede9ba48264ecd2c8ae50d1ad7a8596a87f29f8a7777a70092393311"), - hex("0x2c8fbcb2dd8573dc1dbaf8f4622854776db2eece6d85c4cf4254e7c35e03b07a"), - }, { - hex("0x1d6f347725e4816af2ff453f0cd56b199e1b61e9f601e9ade5e88db870949da9"), - hex("0x204b0c397f4ebe71ebc2d8b3df5b913df9e6ac02b68d31324cd49af5c4565529"), - hex("0x0c4cb9dc3c4fd8174f1149b3c63c3c2f9ecb827cd7dc25534ff8fb75bc79c502"), - }, { - hex("0x174ad61a1448c899a25416474f4930301e5c49475279e0639a616ddc45bc7b54"), - hex("0x1a96177bcf4d8d89f759df4ec2f3cde2eaaa28c177cc0fa13a9816d49a38d2ef"), - hex("0x066d04b24331d71cd0ef8054bc60c4ff05202c126a233c1a8242ace360b8a30a"), - }, { - hex("0x2a4c4fc6ec0b0cf52195782871c6dd3b381cc65f72e02ad527037a62aa1bd804"), - hex("0x13ab2d136ccf37d447e9f2e14a7cedc95e727f8446f6d9d7e55afc01219fd649"), - hex("0x1121552fca26061619d24d843dc82769c1b04fcec26f55194c2e3e869acc6a9a"), - }, { - hex("0x00ef653322b13d6c889bc81715c37d77a6cd267d595c4a8909a5546c7c97cff1"), - hex("0x0e25483e45a665208b261d8ba74051e6400c776d652595d9845aca35d8a397d3"), - hex("0x29f536dcb9dd7682245264659e15d88e395ac3d4dde92d8c46448db979eeba89"), - }, { - hex("0x2a56ef9f2c53febadfda33575dbdbd885a124e2780bbea170e456baace0fa5be"), - hex("0x1c8361c78eb5cf5decfb7a2d17b5c409f2ae2999a46762e8ee416240a8cb9af1"), - hex("0x151aff5f38b20a0fc0473089aaf0206b83e8e68a764507bfd3d0ab4be74319c5"), - }, { - hex("0x04c6187e41ed881dc1b239c88f7f9d43a9f52fc8c8b6cdd1e76e47615b51f100"), - hex("0x13b37bd80f4d27fb10d84331f6fb6d534b81c61ed15776449e801b7ddc9c2967"), - hex("0x01a5c536273c2d9df578bfbd32c17b7a2ce3664c2a52032c9321ceb1c4e8a8e4"), - }, { - hex("0x2ab3561834ca73835ad05f5d7acb950b4a9a2c666b9726da832239065b7c3b02"), - hex("0x1d4d8ec291e720db200fe6d686c0d613acaf6af4e95d3bf69f7ed516a597b646"), - hex("0x041294d2cc484d228f5784fe7919fd2bb925351240a04b711514c9c80b65af1d"), - }, { - hex("0x154ac98e01708c611c4fa715991f004898f57939d126e392042971dd90e81fc6"), - hex("0x0b339d8acca7d4f83eedd84093aef51050b3684c88f8b0b04524563bc6ea4da4"), - hex("0x0955e49e6610c94254a4f84cfbab344598f0e71eaff4a7dd81ed95b50839c82e"), - }, { - hex("0x06746a6156eba54426b9e22206f15abca9a6f41e6f535c6f3525401ea0654626"), - hex("0x0f18f5a0ecd1423c496f3820c549c27838e5790e2bd0a196ac917c7ff32077fb"), - hex("0x04f6eeca1751f7308ac59eff5beb261e4bb563583ede7bc92a738223d6f76e13"), - }, { - hex("0x2b56973364c4c4f5c1a3ec4da3cdce038811eb116fb3e45bc1768d26fc0b3758"), - hex("0x123769dd49d5b054dcd76b89804b1bcb8e1392b385716a5d83feb65d437f29ef"), - hex("0x2147b424fc48c80a88ee52b91169aacea989f6446471150994257b2fb01c63e9"), - }, { - hex("0x0fdc1f58548b85701a6c5505ea332a29647e6f34ad4243c2ea54ad897cebe54d"), - hex("0x12373a8251fea004df68abcf0f7786d4bceff28c5dbbe0c3944f685cc0a0b1f2"), - hex("0x21e4f4ea5f35f85bad7ea52ff742c9e8a642756b6af44203dd8a1f35c1a90035"), - }, { - hex("0x16243916d69d2ca3dfb4722224d4c462b57366492f45e90d8a81934f1bc3b147"), - hex("0x1efbe46dd7a578b4f66f9adbc88b4378abc21566e1a0453ca13a4159cac04ac2"), - hex("0x07ea5e8537cf5dd08886020e23a7f387d468d5525be66f853b672cc96a88969a"), - }, { - hex("0x05a8c4f9968b8aa3b7b478a30f9a5b63650f19a75e7ce11ca9fe16c0b76c00bc"), - hex("0x20f057712cc21654fbfe59bd345e8dac3f7818c701b9c7882d9d57b72a32e83f"), - hex("0x04a12ededa9dfd689672f8c67fee31636dcd8e88d01d49019bd90b33eb33db69"), - }, { - hex("0x27e88d8c15f37dcee44f1e5425a51decbd136ce5091a6767e49ec9544ccd101a"), - hex("0x2feed17b84285ed9b8a5c8c5e95a41f66e096619a7703223176c41ee433de4d1"), - hex("0x1ed7cc76edf45c7c404241420f729cf394e5942911312a0d6972b8bd53aff2b8"), - }, { - hex("0x15742e99b9bfa323157ff8c586f5660eac6783476144cdcadf2874be45466b1a"), - hex("0x1aac285387f65e82c895fc6887ddf40577107454c6ec0317284f033f27d0c785"), - hex("0x25851c3c845d4790f9ddadbdb6057357832e2e7a49775f71ec75a96554d67c77"), - }, { - hex("0x15a5821565cc2ec2ce78457db197edf353b7ebba2c5523370ddccc3d9f146a67"), - hex("0x2411d57a4813b9980efa7e31a1db5966dcf64f36044277502f15485f28c71727"), - hex("0x002e6f8d6520cd4713e335b8c0b6d2e647e9a98e12f4cd2558828b5ef6cb4c9b"), - }, { - hex("0x2ff7bc8f4380cde997da00b616b0fcd1af8f0e91e2fe1ed7398834609e0315d2"), - hex("0x00b9831b948525595ee02724471bcd182e9521f6b7bb68f1e93be4febb0d3cbe"), - hex("0x0a2f53768b8ebf6a86913b0e57c04e011ca408648a4743a87d77adbf0c9c3512"), - }, { - hex("0x00248156142fd0373a479f91ff239e960f599ff7e94be69b7f2a290305e1198d"), - hex("0x171d5620b87bfb1328cf8c02ab3f0c9a397196aa6a542c2350eb512a2b2bcda9"), - hex("0x170a4f55536f7dc970087c7c10d6fad760c952172dd54dd99d1045e4ec34a808"), - }, { - hex("0x29aba33f799fe66c2ef3134aea04336ecc37e38c1cd211ba482eca17e2dbfae1"), - hex("0x1e9bc179a4fdd758fdd1bb1945088d47e70d114a03f6a0e8b5ba650369e64973"), - hex("0x1dd269799b660fad58f7f4892dfb0b5afeaad869a9c4b44f9c9e1c43bdaf8f09"), - }, { - hex("0x22cdbc8b70117ad1401181d02e15459e7ccd426fe869c7c95d1dd2cb0f24af38"), - hex("0x0ef042e454771c533a9f57a55c503fcefd3150f52ed94a7cd5ba93b9c7dacefd"), - hex("0x11609e06ad6c8fe2f287f3036037e8851318e8b08a0359a03b304ffca62e8284"), - }, { - hex("0x1166d9e554616dba9e753eea427c17b7fecd58c076dfe42708b08f5b783aa9af"), - hex("0x2de52989431a859593413026354413db177fbf4cd2ac0b56f855a888357ee466"), - hex("0x3006eb4ffc7a85819a6da492f3a8ac1df51aee5b17b8e89d74bf01cf5f71e9ad"), - }, { - hex("0x2af41fbb61ba8a80fdcf6fff9e3f6f422993fe8f0a4639f962344c8225145086"), - hex("0x119e684de476155fe5a6b41a8ebc85db8718ab27889e85e781b214bace4827c3"), - hex("0x1835b786e2e8925e188bea59ae363537b51248c23828f047cff784b97b3fd800"), - }, { - hex("0x28201a34c594dfa34d794996c6433a20d152bac2a7905c926c40e285ab32eeb6"), - hex("0x083efd7a27d1751094e80fefaf78b000864c82eb571187724a761f88c22cc4e7"), - hex("0x0b6f88a3577199526158e61ceea27be811c16df7774dd8519e079564f61fd13b"), - }, { - hex("0x0ec868e6d15e51d9644f66e1d6471a94589511ca00d29e1014390e6ee4254f5b"), - hex("0x2af33e3f866771271ac0c9b3ed2e1142ecd3e74b939cd40d00d937ab84c98591"), - hex("0x0b520211f904b5e7d09b5d961c6ace7734568c547dd6858b364ce5e47951f178"), - }, { - hex("0x0b2d722d0919a1aad8db58f10062a92ea0c56ac4270e822cca228620188a1d40"), - hex("0x1f790d4d7f8cf094d980ceb37c2453e957b54a9991ca38bbe0061d1ed6e562d4"), - hex("0x0171eb95dfbf7d1eaea97cd385f780150885c16235a2a6a8da92ceb01e504233"), - }, { - hex("0x0c2d0e3b5fd57549329bf6885da66b9b790b40defd2c8650762305381b168873"), - hex("0x1162fb28689c27154e5a8228b4e72b377cbcafa589e283c35d3803054407a18d"), - hex("0x2f1459b65dee441b64ad386a91e8310f282c5a92a89e19921623ef8249711bc0"), - }, { - hex("0x1e6ff3216b688c3d996d74367d5cd4c1bc489d46754eb712c243f70d1b53cfbb"), - hex("0x01ca8be73832b8d0681487d27d157802d741a6f36cdc2a0576881f9326478875"), - hex("0x1f7735706ffe9fc586f976d5bdf223dc680286080b10cea00b9b5de315f9650e"), - }, { - hex("0x2522b60f4ea3307640a0c2dce041fba921ac10a3d5f096ef4745ca838285f019"), - hex("0x23f0bee001b1029d5255075ddc957f833418cad4f52b6c3f8ce16c235572575b"), - hex("0x2bc1ae8b8ddbb81fcaac2d44555ed5685d142633e9df905f66d9401093082d59"), - }, { - hex("0x0f9406b8296564a37304507b8dba3ed162371273a07b1fc98011fcd6ad72205f"), - hex("0x2360a8eb0cc7defa67b72998de90714e17e75b174a52ee4acb126c8cd995f0a8"), - hex("0x15871a5cddead976804c803cbaef255eb4815a5e96df8b006dcbbc2767f88948"), - }, { - hex("0x193a56766998ee9e0a8652dd2f3b1da0362f4f54f72379544f957ccdeefb420f"), - hex("0x2a394a43934f86982f9be56ff4fab1703b2e63c8ad334834e4309805e777ae0f"), - hex("0x1859954cfeb8695f3e8b635dcb345192892cd11223443ba7b4166e8876c0d142"), - }, { - hex("0x04e1181763050e58013444dbcb99f1902b11bc25d90bbdca408d3819f4fed32b"), - hex("0x0fdb253dee83869d40c335ea64de8c5bb10eb82db08b5e8b1f5e5552bfd05f23"), - hex("0x058cbe8a9a5027bdaa4efb623adead6275f08686f1c08984a9d7c5bae9b4f1c0"), - }, { - hex("0x1382edce9971e186497eadb1aeb1f52b23b4b83bef023ab0d15228b4cceca59a"), - hex("0x03464990f045c6ee0819ca51fd11b0be7f61b8eb99f14b77e1e6634601d9e8b5"), - hex("0x23f7bfc8720dc296fff33b41f98ff83c6fcab4605db2eb5aaa5bc137aeb70a58"), - }, { - hex("0x0a59a158e3eec2117e6e94e7f0e9decf18c3ffd5e1531a9219636158bbaf62f2"), - hex("0x06ec54c80381c052b58bf23b312ffd3ce2c4eba065420af8f4c23ed0075fd07b"), - hex("0x118872dc832e0eb5476b56648e867ec8b09340f7a7bcb1b4962f0ff9ed1f9d01"), - }, { - hex("0x13d69fa127d834165ad5c7cba7ad59ed52e0b0f0e42d7fea95e1906b520921b1"), - hex("0x169a177f63ea681270b1c6877a73d21bde143942fb71dc55fd8a49f19f10c77b"), - hex("0x04ef51591c6ead97ef42f287adce40d93abeb032b922f66ffb7e9a5a7450544d"), - }, { - hex("0x256e175a1dc079390ecd7ca703fb2e3b19ec61805d4f03ced5f45ee6dd0f69ec"), - hex("0x30102d28636abd5fe5f2af412ff6004f75cc360d3205dd2da002813d3e2ceeb2"), - hex("0x10998e42dfcd3bbf1c0714bc73eb1bf40443a3fa99bef4a31fd31be182fcc792"), - }, { - hex("0x193edd8e9fcf3d7625fa7d24b598a1d89f3362eaf4d582efecad76f879e36860"), - hex("0x18168afd34f2d915d0368ce80b7b3347d1c7a561ce611425f2664d7aa51f0b5d"), - hex("0x29383c01ebd3b6ab0c017656ebe658b6a328ec77bc33626e29e2e95b33ea6111"), - }, { - hex("0x10646d2f2603de39a1f4ae5e7771a64a702db6e86fb76ab600bf573f9010c711"), - hex("0x0beb5e07d1b27145f575f1395a55bf132f90c25b40da7b3864d0242dcb1117fb"), - hex("0x16d685252078c133dc0d3ecad62b5c8830f95bb2e54b59abdffbf018d96fa336"), - }, { - hex("0x0a6abd1d833938f33c74154e0404b4b40a555bbbec21ddfafd672dd62047f01a"), - hex("0x1a679f5d36eb7b5c8ea12a4c2dedc8feb12dffeec450317270a6f19b34cf1860"), - hex("0x0980fb233bd456c23974d50e0ebfde4726a423eada4e8f6ffbc7592e3f1b93d6"), - }, { - hex("0x161b42232e61b84cbf1810af93a38fc0cece3d5628c9282003ebacb5c312c72b"), - hex("0x0ada10a90c7f0520950f7d47a60d5e6a493f09787f1564e5d09203db47de1a0b"), - hex("0x1a730d372310ba82320345a29ac4238ed3f07a8a2b4e121bb50ddb9af407f451"), - }, { - hex("0x2c8120f268ef054f817064c369dda7ea908377feaba5c4dffbda10ef58e8c556"), - hex("0x1c7c8824f758753fa57c00789c684217b930e95313bcb73e6e7b8649a4968f70"), - hex("0x2cd9ed31f5f8691c8e39e4077a74faa0f400ad8b491eb3f7b47b27fa3fd1cf77"), - }, { - hex("0x23ff4f9d46813457cf60d92f57618399a5e022ac321ca550854ae23918a22eea"), - hex("0x09945a5d147a4f66ceece6405dddd9d0af5a2c5103529407dff1ea58f180426d"), - hex("0x188d9c528025d4c2b67660c6b771b90f7c7da6eaa29d3f268a6dd223ec6fc630"), - }, { - hex("0x3050e37996596b7f81f68311431d8734dba7d926d3633595e0c0d8ddf4f0f47f"), - hex("0x15af1169396830a91600ca8102c35c426ceae5461e3f95d89d829518d30afd78"), - hex("0x1da6d09885432ea9a06d9f37f873d985dae933e351466b2904284da3320d8acc"), - }, { - hex("0x2796ea90d269af29f5f8acf33921124e4e4fad3dbe658945e546ee411ddaa9cb"), - hex("0x202d7dd1da0f6b4b0325c8b3307742f01e15612ec8e9304a7cb0319e01d32d60"), - hex("0x096d6790d05bb759156a952ba263d672a2d7f9c788f4c831a29dace4c0f8be5f"), - }, { - hex("0x054efa1f65b0fce283808965275d877b438da23ce5b13e1963798cb1447d25a4"), - hex("0x1b162f83d917e93edb3308c29802deb9d8aa690113b2e14864ccf6e18e4165f1"), - hex("0x21e5241e12564dd6fd9f1cdd2a0de39eedfefc1466cc568ec5ceb745a0506edc"), - }, { - hex("0x1cfb5662e8cf5ac9226a80ee17b36abecb73ab5f87e161927b4349e10e4bdf08"), - hex("0x0f21177e302a771bbae6d8d1ecb373b62c99af346220ac0129c53f666eb24100"), - hex("0x1671522374606992affb0dd7f71b12bec4236aede6290546bcef7e1f515c2320"), - }, { - hex("0x0fa3ec5b9488259c2eb4cf24501bfad9be2ec9e42c5cc8ccd419d2a692cad870"), - hex("0x193c0e04e0bd298357cb266c1506080ed36edce85c648cc085e8c57b1ab54bba"), - hex("0x102adf8ef74735a27e9128306dcbc3c99f6f7291cd406578ce14ea2adaba68f8"), - }, { - hex("0x0fe0af7858e49859e2a54d6f1ad945b1316aa24bfbdd23ae40a6d0cb70c3eab1"), - hex("0x216f6717bbc7dedb08536a2220843f4e2da5f1daa9ebdefde8a5ea7344798d22"), - hex("0x1da55cc900f0d21f4a3e694391918a1b3c23b2ac773c6b3ef88e2e4228325161"), - }, -} - -type Poseidon struct { - In1 frontend.Variable - In2 frontend.Variable -} - -func (g Poseidon) DefineGadget(api abstractor.API) []frontend.Variable { - state := [3]frontend.Variable{0, g.In1, g.In2} - for i := 0; i < 4; i += 1 { - r := api.Call(fullRound{state, CONSTANTS[i]}) - state = *(*[3]frontend.Variable)(r) - } - for i := 0; i < 57; i += 1 { - r := api.Call(halfRound{state, CONSTANTS[4+i]}) - state = *(*[3]frontend.Variable)(r) - } - for i := 0; i < 4; i += 1 { - r := api.Call(fullRound{state, CONSTANTS[61+i]}) - state = *(*[3]frontend.Variable)(r) - } - return []frontend.Variable{state[0]} -} - -type sbox struct { - Inp frontend.Variable -} - -func (s sbox) DefineGadget(api abstractor.API) []frontend.Variable { - v2 := api.Mul(s.Inp, s.Inp) - v4 := api.Mul(v2, v2) - r := api.Mul(s.Inp, v4) - return []frontend.Variable{r} -} - -type mds struct { - Inp [3]frontend.Variable -} - -func (m mds) DefineGadget(api abstractor.API) []frontend.Variable { - var mds [3]frontend.Variable - for i := 0; i < 3; i += 1 { - var sum frontend.Variable = 0 - for j := 0; j < 3; j += 1 { - sum = api.Add(sum, api.Mul(m.Inp[j], MDS[i][j])) - } - mds[i] = sum - } - return mds[:] -} - -type halfRound struct { - Inp [3]frontend.Variable - Consts [3]frontend.Variable -} - -func (h halfRound) DefineGadget(api abstractor.API) []frontend.Variable { - for i := 0; i < 3; i += 1 { - h.Inp[i] = api.Add(h.Inp[i], h.Consts[i]) - } - h.Inp[0] = api.Call(sbox{h.Inp[0]})[0] - return api.Call(mds{h.Inp}) -} - -type fullRound struct { - Inp [3]frontend.Variable - Consts [3]frontend.Variable -} - -func (h fullRound) DefineGadget(api abstractor.API) []frontend.Variable { - for i := 0; i < 3; i += 1 { - h.Inp[i] = api.Add(h.Inp[i], h.Consts[i]) - } - for i := 0; i < 3; i += 1 { - h.Inp[i] = api.Call(sbox{h.Inp[i]})[0] - } - return api.Call(mds{h.Inp}) -}