0%
HomeSmart contracts
MemoryPageFactRegistry

MemoryPageFactRegistry

Deploy on Alchemy
    Verified
  • Verified
  • FactRegistry
  • IQueryableFactRegistry

The following smart contract implements a Fact Registry design pattern for verifying cryptographic claims. The MemoryPageFactRegistry contract extends the FactRegistry contract and provides methods for registering facts based on memory (address, value) pairs. It supports two types of memory pages: regular pages and continuous pages. The contract computes a hash of the memory pairs and a cumulative product of a linear combination of the pairs. The fact consists of (pageType, prime, n, z, alpha, prod, memoryHash, address). The contract emits events for registering facts and implements an interface for querying the fact registry.

0x28067505e54b7ac2a5f860b343340be8e73edecd
Copied
Copied
MemoryPageFactRegistry Source Code
{"FactRegistry.sol":{"content":"/*\n Copyright 2019-2022 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\nimport \"IQueryableFactRegistry.sol\";\n\ncontract FactRegistry is IQueryableFactRegistry {\n // Mapping: fact hash -\u003e true.\n mapping(bytes32 =\u003e bool) private verifiedFact;\n\n // Indicates whether the Fact Registry has at least one fact registered.\n bool anyFactRegistered = false;\n\n /*\n Checks if a fact has been verified.\n */\n function isValid(bytes32 fact) external view override returns (bool) {\n return _factCheck(fact);\n }\n\n /*\n This is an internal method to check if the fact is already registered.\n In current implementation of FactRegistry it\u0027s identical to isValid().\n But the check is against the local fact registry,\n So for a derived referral fact registry, it\u0027s not the same.\n */\n function _factCheck(bytes32 fact) internal view returns (bool) {\n return verifiedFact[fact];\n }\n\n function registerFact(bytes32 factHash) internal {\n // This function stores the fact hash in the mapping.\n verifiedFact[factHash] = true;\n\n // Mark first time off.\n if (!anyFactRegistered) {\n anyFactRegistered = true;\n }\n }\n\n /*\n Indicates whether at least one fact was registered.\n */\n function hasRegisteredFact() external view override returns (bool) {\n return anyFactRegistered;\n }\n}\n"},"IFactRegistry.sol":{"content":"/*\n Copyright 2019-2022 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\n/*\n The Fact Registry design pattern is a way to separate cryptographic verification from the\n business logic of the contract flow.\n\n A fact registry holds a hash table of verified \"facts\" which are represented by a hash of claims\n that the registry hash check and found valid. This table may be queried by accessing the\n isValid() function of the registry with a given hash.\n\n In addition, each fact registry exposes a registry specific function for submitting new claims\n together with their proofs. The information submitted varies from one registry to the other\n depending of the type of fact requiring verification.\n\n For further reading on the Fact Registry design pattern see this\n `StarkWare blog post \u003chttps://medium.com/starkware/the-fact-registry-a64aafb598b6\u003e`_.\n*/\ninterface IFactRegistry {\n /*\n Returns true if the given fact was previously registered in the contract.\n */\n function isValid(bytes32 fact) external view returns (bool);\n}\n"},"IQueryableFactRegistry.sol":{"content":"/*\n Copyright 2019-2022 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\nimport \"IFactRegistry.sol\";\n\n/*\n Extends the IFactRegistry interface with a query method that indicates\n whether the fact registry has successfully registered any fact or is still empty of such facts.\n*/\ninterface IQueryableFactRegistry is IFactRegistry {\n /*\n Returns true if at least one fact has been registered.\n */\n function hasRegisteredFact() external view returns (bool);\n}\n"},"MemoryPageFactRegistry.sol":{"content":"/*\n Copyright 2019-2022 StarkWare Industries Ltd.\n\n Licensed under the Apache License, Version 2.0 (the \"License\").\n You may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.starkware.co/open-source-license/\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions\n and limitations under the License.\n*/\n// SPDX-License-Identifier: Apache-2.0.\npragma solidity ^0.6.12;\n\nimport \"FactRegistry.sol\";\n\ncontract MemoryPageFactRegistryConstants {\n // A page based on a list of pairs (address, value).\n // In this case, memoryHash = hash(address, value, address, value, address, value, ...).\n uint256 internal constant REGULAR_PAGE = 0;\n // A page based on adjacent memory cells, starting from a given address.\n // In this case, memoryHash = hash(value, value, value, ...).\n uint256 internal constant CONTINUOUS_PAGE = 1;\n}\n\n/*\n A fact registry for the claim:\n I know n pairs (addr, value) for which the hash of the pairs is memoryHash, and the cumulative\n product: \\prod_i( z - (addr_i + alpha * value_i) ) is prod.\n The exact format of the hash depends on the type of the page\n (see MemoryPageFactRegistryConstants).\n The fact consists of (pageType, prime, n, z, alpha, prod, memoryHash, address).\n Note that address is only available for CONTINUOUS_PAGE, and otherwise it is 0.\n*/\ncontract MemoryPageFactRegistry is FactRegistry, MemoryPageFactRegistryConstants {\n event LogMemoryPageFactRegular(bytes32 factHash, uint256 memoryHash, uint256 prod);\n event LogMemoryPageFactContinuous(bytes32 factHash, uint256 memoryHash, uint256 prod);\n\n /*\n Registers a fact based of the given memory (address, value) pairs (REGULAR_PAGE).\n */\n function registerRegularMemoryPage(\n uint256[] calldata memoryPairs,\n uint256 z,\n uint256 alpha,\n uint256 prime\n )\n external\n returns (\n bytes32 factHash,\n uint256 memoryHash,\n uint256 prod\n )\n {\n require(memoryPairs.length \u003c 2**20, \"Too many memory values.\");\n require(memoryPairs.length % 2 == 0, \"Size of memoryPairs must be even.\");\n require(z \u003c prime, \"Invalid value of z.\");\n require(alpha \u003c prime, \"Invalid value of alpha.\");\n (factHash, memoryHash, prod) = computeFactHash(memoryPairs, z, alpha, prime);\n emit LogMemoryPageFactRegular(factHash, memoryHash, prod);\n\n registerFact(factHash);\n }\n\n function computeFactHash(\n uint256[] memory memoryPairs,\n uint256 z,\n uint256 alpha,\n uint256 prime\n )\n private\n pure\n returns (\n bytes32 factHash,\n uint256 memoryHash,\n uint256 prod\n )\n {\n uint256 memorySize = memoryPairs.length / 2; // NOLINT: divide-before-multiply.\n\n prod = 1;\n\n assembly {\n let memoryPtr := add(memoryPairs, 0x20)\n\n // Each value of memoryPairs is a pair: (address, value).\n let lastPtr := add(memoryPtr, mul(memorySize, 0x40))\n for {\n let ptr := memoryPtr\n } lt(ptr, lastPtr) {\n ptr := add(ptr, 0x40)\n } {\n // Compute address + alpha * value.\n let address_value_lin_comb := addmod(\n // address=\n mload(ptr),\n mulmod(\n // value=\n mload(add(ptr, 0x20)),\n alpha,\n prime\n ),\n prime\n )\n prod := mulmod(prod, add(z, sub(prime, address_value_lin_comb)), prime)\n }\n\n memoryHash := keccak256(\n memoryPtr,\n mul(\n // 0x20 * 2.\n 0x40,\n memorySize\n )\n )\n }\n\n factHash = keccak256(\n abi.encodePacked(\n REGULAR_PAGE,\n prime,\n memorySize,\n z,\n alpha,\n prod,\n memoryHash,\n uint256(0)\n )\n );\n }\n\n /*\n Registers a fact based on the given values, assuming continuous addresses.\n values should be [value at startAddr, value at (startAddr + 1), ...].\n */\n function registerContinuousMemoryPage(\n // NOLINT: external-function.\n uint256 startAddr,\n uint256[] memory values,\n uint256 z,\n uint256 alpha,\n uint256 prime\n )\n public\n returns (\n bytes32 factHash,\n uint256 memoryHash,\n uint256 prod\n )\n {\n require(values.length \u003c 2**20, \"Too many memory values.\");\n require(prime \u003c 2**254, \"prime is too big for the optimizations in this function.\");\n require(z \u003c prime, \"Invalid value of z.\");\n require(alpha \u003c prime, \"Invalid value of alpha.\");\n require(startAddr \u003c 2**64 \u0026\u0026 startAddr \u003c prime, \"Invalid value of startAddr.\");\n\n uint256 nValues = values.length;\n\n assembly {\n // Initialize prod to 1.\n prod := 1\n // Initialize valuesPtr to point to the first value in the array.\n let valuesPtr := add(values, 0x20)\n\n let minus_z := mod(sub(prime, z), prime)\n\n // Start by processing full batches of 8 cells, addr represents the last address in each\n // batch.\n let addr := add(startAddr, 7)\n let lastAddr := add(startAddr, nValues)\n for {\n\n } lt(addr, lastAddr) {\n addr := add(addr, 8)\n } {\n // Compute the product of (lin_comb - z) instead of (z - lin_comb), since we\u0027re\n // doing an even number of iterations, the result is the same.\n prod := mulmod(\n prod,\n mulmod(\n add(add(sub(addr, 7), mulmod(mload(valuesPtr), alpha, prime)), minus_z),\n add(\n add(sub(addr, 6), mulmod(mload(add(valuesPtr, 0x20)), alpha, prime)),\n minus_z\n ),\n prime\n ),\n prime\n )\n\n prod := mulmod(\n prod,\n mulmod(\n add(\n add(sub(addr, 5), mulmod(mload(add(valuesPtr, 0x40)), alpha, prime)),\n minus_z\n ),\n add(\n add(sub(addr, 4), mulmod(mload(add(valuesPtr, 0x60)), alpha, prime)),\n minus_z\n ),\n prime\n ),\n prime\n )\n\n prod := mulmod(\n prod,\n mulmod(\n add(\n add(sub(addr, 3), mulmod(mload(add(valuesPtr, 0x80)), alpha, prime)),\n minus_z\n ),\n add(\n add(sub(addr, 2), mulmod(mload(add(valuesPtr, 0xa0)), alpha, prime)),\n minus_z\n ),\n prime\n ),\n prime\n )\n\n prod := mulmod(\n prod,\n mulmod(\n add(\n add(sub(addr, 1), mulmod(mload(add(valuesPtr, 0xc0)), alpha, prime)),\n minus_z\n ),\n add(add(addr, mulmod(mload(add(valuesPtr, 0xe0)), alpha, prime)), minus_z),\n prime\n ),\n prime\n )\n\n valuesPtr := add(valuesPtr, 0x100)\n }\n\n // Handle leftover.\n // Translate addr to the beginning of the last incomplete batch.\n addr := sub(addr, 7)\n for {\n\n } lt(addr, lastAddr) {\n addr := add(addr, 1)\n } {\n let address_value_lin_comb := addmod(\n addr,\n mulmod(mload(valuesPtr), alpha, prime),\n prime\n )\n prod := mulmod(prod, add(z, sub(prime, address_value_lin_comb)), prime)\n valuesPtr := add(valuesPtr, 0x20)\n }\n\n memoryHash := keccak256(add(values, 0x20), mul(0x20, nValues))\n }\n\n factHash = keccak256(\n abi.encodePacked(CONTINUOUS_PAGE, prime, nValues, z, alpha, prod, memoryHash, startAddr)\n );\n\n emit LogMemoryPageFactContinuous(factHash, memoryHash, prod);\n\n registerFact(factHash);\n }\n}\n"}}
MemoryPageFactRegistry ABI
Copied
[{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"factHash","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"memoryHash","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"prod","type":"uint256"}],"name":"LogMemoryPageFactContinuous","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"factHash","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"memoryHash","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"prod","type":"uint256"}],"name":"LogMemoryPageFactRegular","type":"event"},{"inputs":[],"name":"hasRegisteredFact","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"fact","type":"bytes32"}],"name":"isValid","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"startAddr","type":"uint256"},{"internalType":"uint256[]","name":"values","type":"uint256[]"},{"internalType":"uint256","name":"z","type":"uint256"},{"internalType":"uint256","name":"alpha","type":"uint256"},{"internalType":"uint256","name":"prime","type":"uint256"}],"name":"registerContinuousMemoryPage","outputs":[{"internalType":"bytes32","name":"factHash","type":"bytes32"},{"internalType":"uint256","name":"memoryHash","type":"uint256"},{"internalType":"uint256","name":"prod","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"memoryPairs","type":"uint256[]"},{"internalType":"uint256","name":"z","type":"uint256"},{"internalType":"uint256","name":"alpha","type":"uint256"},{"internalType":"uint256","name":"prime","type":"uint256"}],"name":"registerRegularMemoryPage","outputs":[{"internalType":"bytes32","name":"factHash","type":"bytes32"},{"internalType":"uint256","name":"memoryHash","type":"uint256"},{"internalType":"uint256","name":"prod","type":"uint256"}],"stateMutability":"nonpayable","type":"function"}]
MemoryPageFactRegistry Bytecode
Copied
60806040526001805460ff1916905534801561001a57600080fd5b50610a138061002a6000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c8063405a6362146100515780635578ceae146100eb5780636a938567146101a0578063d6354e15146101d1575b600080fd5b6100cd6004803603608081101561006757600080fd5b81019060208101813564010000000081111561008257600080fd5b82018360208201111561009457600080fd5b803590602001918460208302840111640100000000831117156100b657600080fd5b9193509150803590602081013590604001356101d9565b60408051938452602084019290925282820152519081900360600190f35b6100cd600480360360a081101561010157600080fd5b8135919081019060408101602082013564010000000081111561012357600080fd5b82018360208201111561013557600080fd5b8035906020019184602083028401116401000000008311171561015757600080fd5b9190808060200260200160405190810160405280939291908181526020018383602002808284376000920191909152509295505082359350505060208101359060400135610422565b6101bd600480360360208110156101b657600080fd5b5035610821565b604080519115158252519081900360200190f35b6101bd610832565b6000808062100000871061024e57604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601760248201527f546f6f206d616e79206d656d6f72792076616c7565732e000000000000000000604482015290519081900360640190fd5b60028706156102a8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260218152602001806109856021913960400191505060405180910390fd5b83861061031657604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601360248201527f496e76616c69642076616c7565206f66207a2e00000000000000000000000000604482015290519081900360640190fd5b83851061038457604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601760248201527f496e76616c69642076616c7565206f6620616c7068612e000000000000000000604482015290519081900360640190fd5b6103c58888808060200260200160405190810160405280939291908181526020018383602002808284376000920191909152508a925089915088905061083b565b6040805184815260208101849052808201839052905193965091945092507f98fd0d40bd3e226c28fb29ff2d386bd8f9e19f2f8436441e6b854651d3b687b3919081900360600190a1610417836108ff565b955095509592505050565b60008060006210000087511061049957604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601760248201527f546f6f206d616e79206d656d6f72792076616c7565732e000000000000000000604482015290519081900360640190fd5b7f40000000000000000000000000000000000000000000000000000000000000008410610511576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260388152602001806109a66038913960400191505060405180910390fd5b83861061057f57604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601360248201527f496e76616c69642076616c7565206f66207a2e00000000000000000000000000604482015290519081900360640190fd5b8385106105ed57604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601760248201527f496e76616c69642076616c7565206f6620616c7068612e000000000000000000604482015290519081900360640190fd5b680100000000000000008810801561060457508388105b61066f57604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601b60248201527f496e76616c69642076616c7565206f66207374617274416464722e0000000000604482015290519081900360640190fd5b5085516001906020880187860386900660078b018b84015b8082101561072f578889848b8d602089015109600686030101858c8e89510960078703010109870995508889848b8d606089015109600486030101858c8e60408a01510960058703010109870995508889848b8d60a089015109600286030101858c8e60808a01510960038703010109870995508889848b8d60e089015109850101858c8e60c08a015109600187030101098709955061010084019350600882019150610687565b6007820391505b808210156107635788898b8651098308925088838a038c0187099550602084019350600182019150610736565b50505060208083028a820120604080516001818501528082018a905260608101869052608081018c905260a081018b905260c0810187905260e081018390526101008082018f905282518083039091018152610120820180845281519190950120938490526101408101839052610160810187905290519297509095507fb8b9c39aeba1cfd98c38dfeebe11c2f7e02b334cbe9f05f22b442a5d9c1ea0c592508190036101800190a1610815846108ff565b50955095509592505050565b600061082c8261096f565b92915050565b60015460ff1690565b600080600080600288518161084c57fe5b0490506001915060208801604082028101815b818110156108865787888a60208401510982510888818a038c01870995505060400161085f565b5050816040028120935050600085828989868860006040516020018089815260200188815260200187815260200186815260200185815260200184815260200183815260200182815260200198505050505050505050604051602081830303815290604052805190602001209350509450945094915050565b600081815260208190526040902080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001660019081179091555460ff1661096c57600180547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0016811790555b50565b60009081526020819052604090205460ff169056fe53697a65206f66206d656d6f72795061697273206d757374206265206576656e2e7072696d6520697320746f6f2062696720666f7220746865206f7074696d697a6174696f6e7320696e20746869732066756e6374696f6e2ea26469706673582212202a7125ee3ff6d1ef4755eb259508129817ac6dd317892ee6f809c448fdafa48664736f6c634300060c0033
Smart Contracts contract page background

Checkout more smart contracts

    Ethereum  logo

    SHILAINU

    Verified

    The following smart contract is the SHILAINU token contract, which is an ERC20 token with a total supply of 1 trillion. It includes features such as transaction limits, fees, and automatic liquidity provision. The contract also has a blacklist mode and the ability to set fee and transaction exemptions for specific addresses. The purpose of the contract is to provide a decentralized currency for the Shiba Inu community.

    0x20c3fa331a385b63ee39137e99d0cf2db142fce1
    Copied
    • Verified
    • Fungible Token
    • ERC20
    Ethereum  logo

    Token

    Verified

    The following smart contract is a token contract that implements the ERC20 standard. It includes features such as a fee system, wallet and transaction limits, and liquidity provision. The contract also allows for the destruction of tokens through a fee system. The contract is designed to be used with the Uniswap decentralized exchange.

    0xd015422879a1308ba557510345e944b912b9ab73
    Copied
    • Verified
    Ethereum  logo

    OptimizedTransparentUpgradeableProxy

    Verified

    This contract is a proxy and the implementation details are not yet known.

    0x644192291cc835a93d6330b24ea5f5fedd0eef9e
    Copied
    • Verified
    • Proxy
Section background image

Build blockchain magic

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get your API key