def loopring_poseidon_hash(inputs): # prepare params, using loopring order params print(f"poseidon_hash {inputs}") params = poseidon_params(SNARK_SCALAR_FIELD, MAX_INPUT + 1, 6, 53, b'poseidon', 5, security_target=128) hash_value = poseidon(inputs, params) return hash_value
def __init__(self): """""" super().__init__() # exported account self.api_key = "" self.private_key = "" self.address = "" self.publicKeyX = "" self.publicKeyY = "" self.accountId = 0 # order related self.orderId = [None] * 256 self.time_offset = 0 self.order_sign_param = poseidon_params(SNARK_SCALAR_FIELD, 14, 6, 53, b'poseidon', 5, security_target=128) self.init(self.LOOPRING_REST_HOST) self.start()
from collections import namedtuple from sparse_merkle_tree import SparseMerkleTree from float import * from ethsnarks.eddsa import PureEdDSA from ethsnarks.jubjub import Point from ethsnarks.field import FQ from ethsnarks.merkletree import MerkleTree from ethsnarks.poseidon import poseidon, poseidon_params from ethsnarks.field import SNARK_SCALAR_FIELD poseidonParamsAccount = poseidon_params(SNARK_SCALAR_FIELD, 5, 6, 52, b'poseidon', 5, security_target=128) poseidonParamsBalance = poseidon_params(SNARK_SCALAR_FIELD, 5, 6, 52, b'poseidon', 5, security_target=128) poseidonParamsTradingHistory = poseidon_params(SNARK_SCALAR_FIELD, 5, 6, 52, b'poseidon',
for i in range(1): o = o + "i.t" + str(i) + " = o.t" + str(i) + ";\n" o = o + "}\n" o = o + "\n" o = o + "function hash_t6f6p52(" + struct + " memory i, uint q) internal pure returns (uint)\n{\n" o = o + "// validate inputs\n" for i in range(params.t): o = o + "require(i.t" + str(i) + " < q, \"INVALID_INPUT\");\n" o = o + "\n" for i in range(params.nRoundsF + params.nRoundsP): o = o + "// round " + str(i) + "\n" # ark o = o + "ark(i, q, " + str(params.constants_C[i]) + ");\n" # sbox if (i < params.nRoundsF/2) or (i >= params.nRoundsF/2 + params.nRoundsP): o = o + "sbox_full(i, q);\n" else: o = o + "sbox_partial(i, q);\n" # mix o = o + "mix(i, q);\n" o = o + "\nreturn i.t0;\n" o = o + "}\n" o = o + "\n" return o poseidonParamsEVM = poseidon_params(SNARK_SCALAR_FIELD, 6, 6, 52, b'poseidon', 5, security_target=128) data = poseidon_EVM_asm(poseidonParamsEVM) print(data)
import json import copy from collections import namedtuple from math import * from sparse_merkle_tree import SparseMerkleTree from float import * from ethsnarks.eddsa import PureEdDSA from ethsnarks.jubjub import Point from ethsnarks.field import FQ from ethsnarks.merkletree import MerkleTree from ethsnarks.poseidon import poseidon, poseidon_params from ethsnarks.field import SNARK_SCALAR_FIELD poseidonParamsAccount = poseidon_params(SNARK_SCALAR_FIELD, 7, 6, 52, b'poseidon', 5, security_target=128) poseidonParamsBalance = poseidon_params(SNARK_SCALAR_FIELD, 5, 6, 52, b'poseidon', 5, security_target=128) poseidonParamsStorage = poseidon_params(SNARK_SCALAR_FIELD, 5, 6, 52, b'poseidon', 5, security_target=128) BINARY_TREE_DEPTH_STORAGE = 14 BINARY_TREE_DEPTH_ACCOUNTS = 32 BINARY_TREE_DEPTH_TOKENS = 16 MAX_AMOUNT = 2 ** 96 - 1 class GeneralObject(object): pass def setValue(value, default): return default if value is None else value
# Taken and modified from # https://github.com/ethereum/research/blob/6ab4a5da40a325c55691dafb6928627fb598e3bd/trie_research/bintrie2/new_bintrie.py from ethsnarks.poseidon import poseidon, poseidon_params from ethsnarks.field import SNARK_SCALAR_FIELD poseidonMerkleTreeParams = poseidon_params(SNARK_SCALAR_FIELD, 5, 6, 52, b'poseidon', 5, security_target=128) class MerkleHasher_Poseidon(object): def __init__(self, num_children): assert num_children == 4 self._num_children = num_children def hash(self, depth, inputs): return poseidon(inputs, poseidonMerkleTreeParams) MerkleHasher = MerkleHasher_Poseidon class EphemDB(): def __init__(self, kv=None): self.kv = kv or {}
t = int(sys.argv[1]) + 1 best_constraints = 9999999 best_nRoundsF = 0 best_nRoundsP = 0 for nRoundsF in range(1, 16): for nRoundsP in range(1, 128): try: constraints = _poseidon_params(p, t, nRoundsF, nRoundsP, e, None, None, 128) #print("constraints: " + str(constraints)) if constraints < best_constraints: best_constraints = constraints best_nRoundsF = int(nRoundsF) best_nRoundsP = int(nRoundsP) except: pass print("t: " + str(t)) print("best_constraints: " + str(best_constraints)) print("best_nRoundsF: " + str(best_nRoundsF)) print("best_nRoundsP: " + str(best_nRoundsP)) print("cost: " + str(best_constraints / (t - 1))) constants = list(poseidon_constants(SNARK_SCALAR_FIELD, b'poseidon_matrix_0000', t * 2)) print("values = " + str(constants)) params = poseidon_params(SNARK_SCALAR_FIELD, t, best_nRoundsF, best_nRoundsP, b'poseidon', 5, security_target=128) print("expected_matrix = " + str(params.constants_M))