From 6129b827f48b875942a248cf9c2cef17e50a41d9 Mon Sep 17 00:00:00 2001 From: Kolezhniuk Date: Wed, 15 Mar 2023 14:32:56 +0100 Subject: [PATCH 1/2] Add sponge hash to poseidon --- package-lock.json | 4 +-- package.json | 2 +- src/poseidon/poseidon-opt.ts | 39 ++++++++++++++++++++++++ tests/poseidon.test.ts | 59 ++++++++++++++++++++++++++++++++++-- tests/test-vectors.ts | 14 +++++++++ 5 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 tests/test-vectors.ts diff --git a/package-lock.json b/package-lock.json index 1513db7..38f4339 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@iden3/js-crypto", - "version": "1.0.0-beta.0", + "version": "1.0.0-beta.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@iden3/js-crypto", - "version": "1.0.0-beta.0", + "version": "1.0.0-beta.1", "license": "AGPL-3.0", "dependencies": { "blake-hash": "^2.0.0", diff --git a/package.json b/package.json index 5d17319..acb3a8f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@iden3/js-crypto", - "version": "1.0.0-beta.0", + "version": "1.0.0-beta.1", "description": "", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", diff --git a/src/poseidon/poseidon-opt.ts b/src/poseidon/poseidon-opt.ts index 09e407f..7bfa6b0 100644 --- a/src/poseidon/poseidon-opt.ts +++ b/src/poseidon/poseidon-opt.ts @@ -109,5 +109,44 @@ export class Poseidon { // @ts-ignore: if we reach here then hash should be assigned value return hash.valueOf(); } + + // SpongeHashX returns a sponge hash of inputs using Poseidon with configurable frame size + static spongeHashX(inputs: bigint[], frameSize: number): bigint { + if (frameSize < 2 || frameSize > 16) { + throw new Error('incorrect frame size'); + } + + // not used frame default to zero + let frame = new Array(frameSize).fill(BigInt(0)); + + let dirty = false; + let hash: bigint | undefined = undefined; + + let k = 0; + for (let i = 0; i < inputs.length; i++) { + dirty = true; + frame[k] = inputs[i]; + if (k === frameSize - 1) { + hash = this.hash(frame); + dirty = false; + frame = new Array(frameSize).fill(BigInt(0)); + frame[0] = hash; + k = 1; + } else { + k++; + } + } + + if (dirty) { + // we haven't hashed something in the main sponge loop and need to do hash here + hash = this.hash(frame); + } + + if (!hash) { + throw new Error('hash is undefined'); + } + + return hash; + } } export const poseidon = Poseidon; diff --git a/tests/poseidon.test.ts b/tests/poseidon.test.ts index 9d0d157..84134ed 100644 --- a/tests/poseidon.test.ts +++ b/tests/poseidon.test.ts @@ -1,8 +1,46 @@ -import { poseidon } from '../src'; +import { testVectors } from './test-vectors'; +import { Hex, poseidon } from '../src'; import ByteBuffer from 'bytebuffer'; -describe('Poseidon test', () => { +function fromLittleEndian(bytes: Uint8Array): bigint { + const n256 = BigInt(256); + let result = BigInt(0); + let base = BigInt(1); + bytes.forEach((byte) => { + result += base * BigInt(byte); + base = base * n256; + }); + return result; +} + +function fromBigEndian(bytes: Uint8Array): bigint { + return fromLittleEndian(bytes.reverse()); +} +function splitBytes(b: Uint8Array, chunkSize: number): Uint8Array[] { + if (!b.length) { + return []; + } + if (chunkSize >= b.length) { + return [b]; + } + const chunks: Uint8Array[] = []; + let currentLen = 0; + let currentStart = 0; + for (let i = 0; i < b.length; i++) { + if (currentLen == chunkSize) { + const chunk = b.slice(currentStart, i); + chunks.push(chunk); + currentLen = 0; + currentStart = i; + } + currentLen++; + } + chunks.push(b.slice(currentStart, b.length)); + return chunks; +} + +describe('Poseidon test', () => { it('test 2 inputs', () => { const inputs = [1, 2].map((v) => BigInt(v)); const res = poseidon.hash(inputs); @@ -75,4 +113,21 @@ describe('Poseidon test', () => { expect(poseidon.hashBytes(new Uint8Array(input)).toString(16)).toEqual(expectedHash); }); }); + + it('TestSpongeHashX', () => { + for (let i = 0; i < testVectors.length; i++) { + const vector = testVectors[i]; + const b = Hex.decodeString(vector.hexString); + const chunks = splitBytes(b, 31); + + const inputs: bigint[] = []; + + for (let i = 0; i < chunks.length; i++) { + inputs[i] = fromBigEndian(chunks[i]); + } + + const res = poseidon.spongeHashX(inputs, vector.frameSize); + expect(res.toString(16)).toEqual(vector.expectedHash); + } + }); }); diff --git a/tests/test-vectors.ts b/tests/test-vectors.ts new file mode 100644 index 0000000..11146c2 --- /dev/null +++ b/tests/test-vectors.ts @@ -0,0 +1,14 @@ +export const testVectors = [ + { + hexString: + '60806040523480156200001157600080fd5b5060405162002103380380620021038339810160408190526200003491620002e1565b6200003f3362000125565b60005b620000506001602062000329565b811015620000eb57602181602081106200006e576200006e62000343565b01546021826020811062000086576200008662000343565b0154604080516020810193909352820152606001604051602081830303815290604052805190602001206021826001620000c1919062000359565b60208110620000d457620000d462000343565b015580620000e28162000374565b91505062000042565b50604680546001600160a01b0319166001600160a01b0383161790556200011162000175565b6043556200011e62000250565b50620003b5565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b6041546000908190815b602081101562000248578160011660011415620001e05760018160208110620001ac57620001ac62000343565b0154604080516020810192909252810184905260600160405160208183030381529060405280519060200120925062000224565b8260218260208110620001f757620001f762000343565b01546040805160208101939093528201526060016040516020818303038152906040528051906020012092505b6200023160028362000392565b9150806200023f8162000374565b9150506200017f565b509092915050565b60458054906000620002628362000374565b909155505060435460425460408051602081019390935282015260600160408051808303601f19018152828252805160209182012060455460009081526044835283902055604354604254908452908301527f61014378f82a0d809aefaf87a8ac9505b89c321808287a6e7810f29304c1fce3910160405180910390a1565b600060208284031215620002f457600080fd5b81516001600160a01b03811681146200030c57600080fd5b9392505050565b634e487b7160e01b600052601160045260246000fd5b6000828210156200033e576200033e62000313565b500390565b634e487b7160e01b600052603260045260246000fd5b600082198211156200036f576200036f62000313565b500190565b60006000198214156200038b576200038b62000313565b5060010190565b600082620003b057634e487b7160e01b600052601260045260246000fd5b500490565b611d3e80620003c56000396000f3fe60806040526004361061010e5760003560e01c806355f6bc57116100a55780638da5cb5b11610074578063a71d644411610059578063a71d6444146102fb578063ed6be5c91461032b578063f2fde38b1461035557600080fd5b80638da5cb5b146102b0578063a5392cf6146102db57600080fd5b806355f6bc57146101f95780635ec6a8df14610219578063715018a61461026b5780637d8f04691461028057600080fd5b8063319cf735116100e1578063319cf7351461017d5780633381fe90146101935780633ae05047146101c05780633ed691ef146101d557600080fd5b806301fd904414610113578063029f27931461013c5780630e21fbd7146101525780632dfdf0b514610167575b600080fd5b34801561011f57600080fd5b5061012960425481565b6040519081526020015b60405180910390f35b34801561014857600080fd5b5061012960455481565b61016561016036600461170e565b610375565b005b34801561017357600080fd5b5061012960415481565b34801561018957600080fd5b5061012960435481565b34801561019f57600080fd5b506101296101ae36600461175f565b60446020526000908152604090205481565b3480156101cc57600080fd5b50610129610574565b3480156101e157600080fd5b50604554600090815260446020526040902054610129565b34801561020557600080fd5b5061016561021436600461175f565b610641565b34801561022557600080fd5b506046546102469073ffffffffffffffffffffffffffffffffffffffff1681565b60405173ffffffffffffffffffffffffffffffffffffffff9091168152602001610133565b34801561027757600080fd5b506101656106f8565b34801561028c57600080fd5b506102a061029b36600461186d565b610785565b6040519015158152602001610133565b3480156102bc57600080fd5b5060005473ffffffffffffffffffffffffffffffffffffffff16610246565b3480156102e757600080fd5b506101656102f6366004611915565b610931565b34801561030757600080fd5b506102a061031636600461175f565b60476020526000908152604090205460ff1681565b34801561033757600080fd5b50610340600081565b60405163ffffffff9091168152602001610133565b34801561036157600080fd5b506101656103703660046119d2565b610e4d565b73ffffffffffffffffffffffffffffffffffffffff841661042957823414610424576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603060248201527f4272696467653a3a6465706f7369743a20414d4f554e545f444f45535f4e4f5460448201527f5f4d415443485f4d53475f56414c55450000000000000000000000000000000060648201526084015b60405180910390fd5b61044b565b61044b73ffffffffffffffffffffffffffffffffffffffff8516333086610f7a565b63ffffffff82166104de576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602c60248201527f4272696467653a3a6465706f7369743a2044455354494e4154494f4e5f43414e60448201527f545f42455f4d41494e4e45540000000000000000000000000000000000000000606482015260840161041b565b6041546040805173ffffffffffffffffffffffffffffffffffffffff87811682526020820187905263ffffffff8681168385015290851660608301529092166080830152517f0a37f8bae6de7e960aeedce45875d5a75681918316c4bd81f4691152910f8e329181900360a00190a161055b848460008585611056565b610563610574565b60435561056e61125e565b50505050565b6041546000908190815b60208110156106395781600116600114156105d957600181602081106105a6576105a66119ef565b0154604080516020810192909252810184905260600160405160208183030381529060405280519060200120925061061a565b82602182602081106105ed576105ed6119ef565b01546040805160208101939093528201526060016040516020818303038152906040528051906020012092505b610625600283611a4d565b91508061063181611a88565b91505061057e565b509092915050565b60465473ffffffffffffffffffffffffffffffffffffffff1633146106e8576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602960248201527f4272696467653a3a757064617465526f6c6c757045786974526f6f743a204f4e60448201527f4c595f524f4c4c55500000000000000000000000000000000000000000000000606482015260840161041b565b60428190556106f561125e565b50565b60005473ffffffffffffffffffffffffffffffffffffffff163314610779576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604482015260640161041b565b610783600061130b565b565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e088811b821660208401527fffffffffffffffffffffffffffffffffffffffff00000000000000000000000060608c811b82166024860152603885018c90529189901b909216605884015286901b16605c8201526000908190607001604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08184030181529190528051602090910120905067ffffffffffffffff841660005b60208110156109205781600116600114156108b357868181518110610873576108736119ef565b602002602001015183604051602001610896929190918252602082015260400190565b604051602081830303815290604052805190602001209250610901565b828782815181106108c6576108c66119ef565b60200260200101516040516020016108e8929190918252602082015260400190565b6040516020818303038152906040528051906020012092505b61090c600283611a4d565b91508061091881611a88565b91505061084c565b505090911498975050505050505050565b67ffffffffffffffff841660009081526047602052604090205460ff16156109db576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602a60248201527f4272696467653a3a77697468647261773a20414c52454144595f434c41494d4560448201527f445f574954484452415700000000000000000000000000000000000000000000606482015260840161041b565b63ffffffff871615610a6f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603160248201527f4272696467653a3a77697468647261773a2044455354494e4154494f4e5f4e4560448201527f54574f524b5f4e4f545f4d41494e4e4554000000000000000000000000000000606482015260840161041b565b63ffffffff881615610b03576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602c60248201527f4272696467653a3a77697468647261773a204f524947494e5f4e4554574f524b60448201527f5f4e4f545f4d41494e4e45540000000000000000000000000000000000000000606482015260840161041b565b6000838152604460209081526040918290205482519182018590529181018390526060016040516020818303038152906040528051906020012014610bca576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603160248201527f4272696467653a3a77697468647261773a20474c4f42414c5f455849545f524f60448201527f4f545f444f45535f4e4f545f4d41544348000000000000000000000000000000606482015260840161041b565b610bda8a8a8a8a8a8a8a88610785565b610c40576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f4272696467653a3a77697468647261773a20534d545f494e56414c4944000000604482015260640161041b565b67ffffffffffffffff8416600090815260476020526040902080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0016600117905573ffffffffffffffffffffffffffffffffffffffff8a16610dad576040805160008082526020820190925273ffffffffffffffffffffffffffffffffffffffff8816908b90604051610cd49190611aed565b60006040518083038185875af1925050503d8060008114610d11576040519150601f19603f3d011682016040523d82523d6000602084013e610d16565b606091505b5050905080610da7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602560248201527f4272696467653a3a77697468647261773a204554485f5452414e534645525f4660448201527f41494c4544000000000000000000000000000000000000000000000000000000606482015260840161041b565b50610dce565b610dce73ffffffffffffffffffffffffffffffffffffffff8b16878b611380565b6040805167ffffffffffffffff8616815263ffffffff8a16602082015273ffffffffffffffffffffffffffffffffffffffff8c811682840152606082018c90528816608082015290517f8932892d010aea7e4fdefb3764910523c321e06bb52577dc2439501196bf72559181900360a00190a150505050505050505050565b60005473ffffffffffffffffffffffffffffffffffffffff163314610ece576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604482015260640161041b565b73ffffffffffffffffffffffffffffffffffffffff8116610f71576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201527f6464726573730000000000000000000000000000000000000000000000000000606482015260840161041b565b6106f58161130b565b60405173ffffffffffffffffffffffffffffffffffffffff8085166024830152831660448201526064810182905261056e9085907f23b872dd00000000000000000000000000000000000000000000000000000000906084015b604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08184030181529190526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fffffffff00000000000000000000000000000000000000000000000000000000909316929092179091526113db565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e085811b821660208401527fffffffffffffffffffffffffffffffffffffffff000000000000000000000000606089811b82166024860152603885018990529186901b909216605884015283901b16605c8201526000906070016040516020818303038152906040528051906020012090506001602060026110fd9190611c2b565b6111079190611c37565b60415410611197576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602a60248201527f4465706f736974436f6e74726163743a5f6465706f7369743a204d45524b4c4560448201527f5f545245455f46554c4c00000000000000000000000000000000000000000000606482015260840161041b565b6001604160008282546111aa9190611c4e565b909155505060415460005b602081101561124b5781600116600114156111e95782600182602081106111de576111de6119ef565b015550611257915050565b600181602081106111fc576111fc6119ef565b015460408051602081019290925281018490526060016040516020818303038152906040528051906020012092506002826112379190611a4d565b91508061124381611a88565b9150506111b5565b50611254611c66565b50505b5050505050565b6045805490600061126e83611a88565b9091555050604354604254604080516020810193909352820152606001604080518083037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0018152828252805160209182012060455460009081526044835283902055604354604254908452908301527f61014378f82a0d809aefaf87a8ac9505b89c321808287a6e7810f29304c1fce3910160405180910390a1565b6000805473ffffffffffffffffffffffffffffffffffffffff8381167fffffffffffffffffffffffff0000000000000000000000000000000000000000831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b60405173ffffffffffffffffffffffffffffffffffffffff83166024820152604481018290526113d69084907fa9059cbb0000000000000000000000000000000000000000000000000000000090606401610fd4565b505050565b600061143d826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c65648152508573ffffffffffffffffffffffffffffffffffffffff166114e79092919063ffffffff16565b8051909150156113d6578080602001905181019061145b9190611c95565b6113d6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e60448201527f6f74207375636365656400000000000000000000000000000000000000000000606482015260840161041b565b60606114f68484600085611500565b90505b9392505050565b606082471015611592576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f60448201527f722063616c6c0000000000000000000000000000000000000000000000000000606482015260840161041b565b843b6115fa576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e7472616374000000604482015260640161041b565b6000808673ffffffffffffffffffffffffffffffffffffffff1685876040516116239190611aed565b60006040518083038185875af1925050503d8060008114611660576040519150601f19603f3d011682016040523d82523d6000602084013e611665565b606091505b5091509150611675828286611680565b979650505050505050565b6060831561168f5750816114f9565b82511561169f5782518084602001fd5b816040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161041b9190611cb7565b73ffffffffffffffffffffffffffffffffffffffff811681146106f557600080fd5b803563ffffffff8116811461170957600080fd5b919050565b6000806000806080858703121561172457600080fd5b843561172f816116d3565b935060208501359250611744604086016116f5565b91506060850135611754816116d3565b939692955090935050565b60006020828403121561177157600080fd5b5035919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b600082601f8301126117b857600080fd5b8135602067ffffffffffffffff808311156117d5576117d5611778565b8260051b6040517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0603f8301168101818110848211171561181857611818611778565b60405293845285810183019383810192508785111561183657600080fd5b83870191505b848210156116755781358352918301919083019061183c565b803567ffffffffffffffff8116811461170957600080fd5b600080600080600080600080610100898b03121561188a57600080fd5b8835611895816116d3565b9750602089013596506118aa60408a016116f5565b95506118b860608a016116f5565b945060808901356118c8816116d3565b935060a089013567ffffffffffffffff8111156118e457600080fd5b6118f08b828c016117a7565b9350506118ff60c08a01611855565b915060e089013590509295985092959890939650565b6000806000806000806000806000806101408b8d03121561193557600080fd5b8a35611940816116d3565b995060208b0135985061195560408c016116f5565b975061196360608c016116f5565b965060808b0135611973816116d3565b955060a08b013567ffffffffffffffff81111561198f57600080fd5b61199b8d828e016117a7565b9550506119aa60c08c01611855565b935060e08b013592506101008b013591506101208b013590509295989b9194979a5092959850565b6000602082840312156119e457600080fd5b81356114f9816116d3565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600082611a83577f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b500490565b60007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff821415611aba57611aba611a1e565b5060010190565b60005b83811015611adc578181015183820152602001611ac4565b8381111561056e5750506000910152565b60008251611aff818460208701611ac1565b9190910192915050565b600181815b80851115611b6257817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff04821115611b4857611b48611a1e565b80851615611b5557918102915b93841c9390800290611b0e565b509250929050565b600082611b7957506001611c25565b81611b8657506000611c25565b8160018114611b9c5760028114611ba657611bc2565b6001915050611c25565b60ff841115611bb757611bb7611a1e565b50506001821b611c25565b5060208310610133831016604e8410600b8410161715611be5575081810a611c25565b611bef8383611b09565b807fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff04821115611c2157611c21611a1e565b0290505b92915050565b60006114f98383611b6a565b600082821015611c4957611c49611a1e565b500390565b60008219821115611c6157611c61611a1e565b500190565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052600160045260246000fd5b600060208284031215611ca757600080fd5b815180151581146114f957600080fd5b6020815260008251806020840152611cd6816040850160208701611ac1565b601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe016919091016040019291505056fea264697066735822122086e50005a89e4c2272c1cba11ff802c1213e2b08c8a20c742826166345c5ee3264736f6c63430008090033', + frameSize: 16, + expectedHash: '247d606141687ab92010f5c2f049412e8aceb58650c1d9a568d925ecd6e8322c' // expected to be different from HashBytes result because of different way to split bytes and hash + }, + { + hexString: + '60806040523480156200001157600080fd5b5060405162002103380380620021038339810160408190526200003491620002e1565b6200003f3362000125565b60005b620000506001602062000329565b811015620000eb57602181602081106200006e576200006e62000343565b01546021826020811062000086576200008662000343565b0154604080516020810193909352820152606001604051602081830303815290604052805190602001206021826001620000c1919062000359565b60208110620000d457620000d462000343565b015580620000e28162000374565b91505062000042565b50604680546001600160a01b0319166001600160a01b0383161790556200011162000175565b6043556200011e62000250565b50620003b5565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b6041546000908190815b602081101562000248578160011660011415620001e05760018160208110620001ac57620001ac62000343565b0154604080516020810192909252810184905260600160405160208183030381529060405280519060200120925062000224565b8260218260208110620001f757620001f762000343565b01546040805160208101939093528201526060016040516020818303038152906040528051906020012092505b6200023160028362000392565b9150806200023f8162000374565b9150506200017f565b509092915050565b60458054906000620002628362000374565b909155505060435460425460408051602081019390935282015260600160408051808303601f19018152828252805160209182012060455460009081526044835283902055604354604254908452908301527f61014378f82a0d809aefaf87a8ac9505b89c321808287a6e7810f29304c1fce3910160405180910390a1565b600060208284031215620002f457600080fd5b81516001600160a01b03811681146200030c57600080fd5b9392505050565b634e487b7160e01b600052601160045260246000fd5b6000828210156200033e576200033e62000313565b500390565b634e487b7160e01b600052603260045260246000fd5b600082198211156200036f576200036f62000313565b500190565b60006000198214156200038b576200038b62000313565b5060010190565b600082620003b057634e487b7160e01b600052601260045260246000fd5b500490565b611d3e80620003c56000396000f3fe60806040526004361061010e5760003560e01c806355f6bc57116100a55780638da5cb5b11610074578063a71d644411610059578063a71d6444146102fb578063ed6be5c91461032b578063f2fde38b1461035557600080fd5b80638da5cb5b146102b0578063a5392cf6146102db57600080fd5b806355f6bc57146101f95780635ec6a8df14610219578063715018a61461026b5780637d8f04691461028057600080fd5b8063319cf735116100e1578063319cf7351461017d5780633381fe90146101935780633ae05047146101c05780633ed691ef146101d557600080fd5b806301fd904414610113578063029f27931461013c5780630e21fbd7146101525780632dfdf0b514610167575b600080fd5b34801561011f57600080fd5b5061012960425481565b6040519081526020015b60405180910390f35b34801561014857600080fd5b5061012960455481565b61016561016036600461170e565b610375565b005b34801561017357600080fd5b5061012960415481565b34801561018957600080fd5b5061012960435481565b34801561019f57600080fd5b506101296101ae36600461175f565b60446020526000908152604090205481565b3480156101cc57600080fd5b50610129610574565b3480156101e157600080fd5b50604554600090815260446020526040902054610129565b34801561020557600080fd5b5061016561021436600461175f565b610641565b34801561022557600080fd5b506046546102469073ffffffffffffffffffffffffffffffffffffffff1681565b60405173ffffffffffffffffffffffffffffffffffffffff9091168152602001610133565b34801561027757600080fd5b506101656106f8565b34801561028c57600080fd5b506102a061029b36600461186d565b610785565b6040519015158152602001610133565b3480156102bc57600080fd5b5060005473ffffffffffffffffffffffffffffffffffffffff16610246565b3480156102e757600080fd5b506101656102f6366004611915565b610931565b34801561030757600080fd5b506102a061031636600461175f565b60476020526000908152604090205460ff1681565b34801561033757600080fd5b50610340600081565b60405163ffffffff9091168152602001610133565b34801561036157600080fd5b506101656103703660046119d2565b610e4d565b73ffffffffffffffffffffffffffffffffffffffff841661042957823414610424576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603060248201527f4272696467653a3a6465706f7369743a20414d4f554e545f444f45535f4e4f5460448201527f5f4d415443485f4d53475f56414c55450000000000000000000000000000000060648201526084015b60405180910390fd5b61044b565b61044b73ffffffffffffffffffffffffffffffffffffffff8516333086610f7a565b63ffffffff82166104de576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602c60248201527f4272696467653a3a6465706f7369743a2044455354494e4154494f4e5f43414e60448201527f545f42455f4d41494e4e45540000000000000000000000000000000000000000606482015260840161041b565b6041546040805173ffffffffffffffffffffffffffffffffffffffff87811682526020820187905263ffffffff8681168385015290851660608301529092166080830152517f0a37f8bae6de7e960aeedce45875d5a75681918316c4bd81f4691152910f8e329181900360a00190a161055b848460008585611056565b610563610574565b60435561056e61125e565b50505050565b6041546000908190815b60208110156106395781600116600114156105d957600181602081106105a6576105a66119ef565b0154604080516020810192909252810184905260600160405160208183030381529060405280519060200120925061061a565b82602182602081106105ed576105ed6119ef565b01546040805160208101939093528201526060016040516020818303038152906040528051906020012092505b610625600283611a4d565b91508061063181611a88565b91505061057e565b509092915050565b60465473ffffffffffffffffffffffffffffffffffffffff1633146106e8576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602960248201527f4272696467653a3a757064617465526f6c6c757045786974526f6f743a204f4e60448201527f4c595f524f4c4c55500000000000000000000000000000000000000000000000606482015260840161041b565b60428190556106f561125e565b50565b60005473ffffffffffffffffffffffffffffffffffffffff163314610779576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604482015260640161041b565b610783600061130b565b565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e088811b821660208401527fffffffffffffffffffffffffffffffffffffffff00000000000000000000000060608c811b82166024860152603885018c90529189901b909216605884015286901b16605c8201526000908190607001604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08184030181529190528051602090910120905067ffffffffffffffff841660005b60208110156109205781600116600114156108b357868181518110610873576108736119ef565b602002602001015183604051602001610896929190918252602082015260400190565b604051602081830303815290604052805190602001209250610901565b828782815181106108c6576108c66119ef565b60200260200101516040516020016108e8929190918252602082015260400190565b6040516020818303038152906040528051906020012092505b61090c600283611a4d565b91508061091881611a88565b91505061084c565b505090911498975050505050505050565b67ffffffffffffffff841660009081526047602052604090205460ff16156109db576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602a60248201527f4272696467653a3a77697468647261773a20414c52454144595f434c41494d4560448201527f445f574954484452415700000000000000000000000000000000000000000000606482015260840161041b565b63ffffffff871615610a6f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603160248201527f4272696467653a3a77697468647261773a2044455354494e4154494f4e5f4e4560448201527f54574f524b5f4e4f545f4d41494e4e4554000000000000000000000000000000606482015260840161041b565b63ffffffff881615610b03576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602c60248201527f4272696467653a3a77697468647261773a204f524947494e5f4e4554574f524b60448201527f5f4e4f545f4d41494e4e45540000000000000000000000000000000000000000606482015260840161041b565b6000838152604460209081526040918290205482519182018590529181018390526060016040516020818303038152906040528051906020012014610bca576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603160248201527f4272696467653a3a77697468647261773a20474c4f42414c5f455849545f524f60448201527f4f545f444f45535f4e4f545f4d41544348000000000000000000000000000000606482015260840161041b565b610bda8a8a8a8a8a8a8a88610785565b610c40576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f4272696467653a3a77697468647261773a20534d545f494e56414c4944000000604482015260640161041b565b67ffffffffffffffff8416600090815260476020526040902080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0016600117905573ffffffffffffffffffffffffffffffffffffffff8a16610dad576040805160008082526020820190925273ffffffffffffffffffffffffffffffffffffffff8816908b90604051610cd49190611aed565b60006040518083038185875af1925050503d8060008114610d11576040519150601f19603f3d011682016040523d82523d6000602084013e610d16565b606091505b5050905080610da7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602560248201527f4272696467653a3a77697468647261773a204554485f5452414e534645525f4660448201527f41494c4544000000000000000000000000000000000000000000000000000000606482015260840161041b565b50610dce565b610dce73ffffffffffffffffffffffffffffffffffffffff8b16878b611380565b6040805167ffffffffffffffff8616815263ffffffff8a16602082015273ffffffffffffffffffffffffffffffffffffffff8c811682840152606082018c90528816608082015290517f8932892d010aea7e4fdefb3764910523c321e06bb52577dc2439501196bf72559181900360a00190a150505050505050505050565b60005473ffffffffffffffffffffffffffffffffffffffff163314610ece576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604482015260640161041b565b73ffffffffffffffffffffffffffffffffffffffff8116610f71576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201527f6464726573730000000000000000000000000000000000000000000000000000606482015260840161041b565b6106f58161130b565b60405173ffffffffffffffffffffffffffffffffffffffff8085166024830152831660448201526064810182905261056e9085907f23b872dd00000000000000000000000000000000000000000000000000000000906084015b604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08184030181529190526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fffffffff00000000000000000000000000000000000000000000000000000000909316929092179091526113db565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e085811b821660208401527fffffffffffffffffffffffffffffffffffffffff000000000000000000000000606089811b82166024860152603885018990529186901b909216605884015283901b16605c8201526000906070016040516020818303038152906040528051906020012090506001602060026110fd9190611c2b565b6111079190611c37565b60415410611197576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602a60248201527f4465706f736974436f6e74726163743a5f6465706f7369743a204d45524b4c4560448201527f5f545245455f46554c4c00000000000000000000000000000000000000000000606482015260840161041b565b6001604160008282546111aa9190611c4e565b909155505060415460005b602081101561124b5781600116600114156111e95782600182602081106111de576111de6119ef565b015550611257915050565b600181602081106111fc576111fc6119ef565b015460408051602081019290925281018490526060016040516020818303038152906040528051906020012092506002826112379190611a4d565b91508061124381611a88565b9150506111b5565b50611254611c66565b50505b5050505050565b6045805490600061126e83611a88565b9091555050604354604254604080516020810193909352820152606001604080518083037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0018152828252805160209182012060455460009081526044835283902055604354604254908452908301527f61014378f82a0d809aefaf87a8ac9505b89c321808287a6e7810f29304c1fce3910160405180910390a1565b6000805473ffffffffffffffffffffffffffffffffffffffff8381167fffffffffffffffffffffffff0000000000000000000000000000000000000000831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b60405173ffffffffffffffffffffffffffffffffffffffff83166024820152604481018290526113d69084907fa9059cbb0000000000000000000000000000000000000000000000000000000090606401610fd4565b505050565b600061143d826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c65648152508573ffffffffffffffffffffffffffffffffffffffff166114e79092919063ffffffff16565b8051909150156113d6578080602001905181019061145b9190611c95565b6113d6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e60448201527f6f74207375636365656400000000000000000000000000000000000000000000606482015260840161041b565b60606114f68484600085611500565b90505b9392505050565b606082471015611592576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f60448201527f722063616c6c0000000000000000000000000000000000000000000000000000606482015260840161041b565b843b6115fa576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e7472616374000000604482015260640161041b565b6000808673ffffffffffffffffffffffffffffffffffffffff1685876040516116239190611aed565b60006040518083038185875af1925050503d8060008114611660576040519150601f19603f3d011682016040523d82523d6000602084013e611665565b606091505b5091509150611675828286611680565b979650505050505050565b6060831561168f5750816114f9565b82511561169f5782518084602001fd5b816040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161041b9190611cb7565b73ffffffffffffffffffffffffffffffffffffffff811681146106f557600080fd5b803563ffffffff8116811461170957600080fd5b919050565b6000806000806080858703121561172457600080fd5b843561172f816116d3565b935060208501359250611744604086016116f5565b91506060850135611754816116d3565b939692955090935050565b60006020828403121561177157600080fd5b5035919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b600082601f8301126117b857600080fd5b8135602067ffffffffffffffff808311156117d5576117d5611778565b8260051b6040517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0603f8301168101818110848211171561181857611818611778565b60405293845285810183019383810192508785111561183657600080fd5b83870191505b848210156116755781358352918301919083019061183c565b803567ffffffffffffffff8116811461170957600080fd5b600080600080600080600080610100898b03121561188a57600080fd5b8835611895816116d3565b9750602089013596506118aa60408a016116f5565b95506118b860608a016116f5565b945060808901356118c8816116d3565b935060a089013567ffffffffffffffff8111156118e457600080fd5b6118f08b828c016117a7565b9350506118ff60c08a01611855565b915060e089013590509295985092959890939650565b6000806000806000806000806000806101408b8d03121561193557600080fd5b8a35611940816116d3565b995060208b0135985061195560408c016116f5565b975061196360608c016116f5565b965060808b0135611973816116d3565b955060a08b013567ffffffffffffffff81111561198f57600080fd5b61199b8d828e016117a7565b9550506119aa60c08c01611855565b935060e08b013592506101008b013591506101208b013590509295989b9194979a5092959850565b6000602082840312156119e457600080fd5b81356114f9816116d3565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600082611a83577f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b500490565b60007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff821415611aba57611aba611a1e565b5060010190565b60005b83811015611adc578181015183820152602001611ac4565b8381111561056e5750506000910152565b60008251611aff818460208701611ac1565b9190910192915050565b600181815b80851115611b6257817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff04821115611b4857611b48611a1e565b80851615611b5557918102915b93841c9390800290611b0e565b509250929050565b600082611b7957506001611c25565b81611b8657506000611c25565b8160018114611b9c5760028114611ba657611bc2565b6001915050611c25565b60ff841115611bb757611bb7611a1e565b50506001821b611c25565b5060208310610133831016604e8410600b8410161715611be5575081810a611c25565b611bef8383611b09565b807fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff04821115611c2157611c21611a1e565b0290505b92915050565b60006114f98383611b6a565b600082821015611c4957611c49611a1e565b500390565b60008219821115611c6157611c61611a1e565b500190565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052600160045260246000fd5b600060208284031215611ca757600080fd5b815180151581146114f957600080fd5b6020815260008251806020840152611cd6816040850160208701611ac1565b601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe016919091016040019291505056fea264697066735822122086e50005a89e4c2272c1cba11ff802c1213e2b08c8a20c742826166345c5ee3264736f6c63430008090033000000000000000000000000', + frameSize: 16, + expectedHash: '13a3be23e0f86283cb0ba23739fb1020aa7c85ae498f8d543e37cadcd5ec408b' + } +]; From 708dc30c1fb8fdf5078e0630f25fa3f289f03915 Mon Sep 17 00:00:00 2001 From: Kolezhniuk Date: Thu, 16 Mar 2023 16:37:54 +0100 Subject: [PATCH 2/2] expose hashBytesX --- src/poseidon/poseidon-opt.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/poseidon/poseidon-opt.ts b/src/poseidon/poseidon-opt.ts index 7bfa6b0..684a517 100644 --- a/src/poseidon/poseidon-opt.ts +++ b/src/poseidon/poseidon-opt.ts @@ -68,21 +68,27 @@ export class Poseidon { return F.normalize(state[0]); } + // HashBytes returns a sponge hash of a msg byte slice split into blocks of 31 bytes static hashBytes(msg: Uint8Array): bigint { - const inputs = new Array(SPONGE_INPUTS).fill(BigInt(0)); + return Poseidon.hashBytesX(msg, SPONGE_INPUTS); + } + + // hashBytesX returns a sponge hash of a msg byte slice split into blocks of 31 bytes + static hashBytesX(msg: Uint8Array, frameSize: number): bigint { + const inputs = new Array(frameSize).fill(BigInt(0)); let dirty = false; - let hash: bigint; + let hash!: bigint; let k = 0; for (let i = 0; i < parseInt(`${msg.length / SPONGE_CHUNK_SIZE}`); i += 1) { dirty = true; inputs[k] = utils.beBuff2int(msg.slice(SPONGE_CHUNK_SIZE * i, SPONGE_CHUNK_SIZE * (i + 1))); - if (k === SPONGE_INPUTS - 1) { + if (k === frameSize - 1) { hash = Poseidon.hash(inputs); dirty = false; - inputs[0] = hash.valueOf(); + inputs[0] = hash; inputs.fill(BigInt(0), 1, SPONGE_CHUNK_SIZE); - for (let j = 1; j < SPONGE_INPUTS; j += 1) { + for (let j = 1; j < frameSize; j += 1) { inputs[j] = BigInt(0); } k = 1; @@ -106,8 +112,7 @@ export class Poseidon { hash = Poseidon.hash(inputs); } - // @ts-ignore: if we reach here then hash should be assigned value - return hash.valueOf(); + return hash; } // SpongeHashX returns a sponge hash of inputs using Poseidon with configurable frame size @@ -120,7 +125,7 @@ export class Poseidon { let frame = new Array(frameSize).fill(BigInt(0)); let dirty = false; - let hash: bigint | undefined = undefined; + let hash!: bigint; let k = 0; for (let i = 0; i < inputs.length; i++) {