2021-10-06 11:28:14 +02:00
|
|
|
// Copyright (c) 2018 Jordi Baylina
|
|
|
|
// License: LGPL-3.0+
|
|
|
|
//
|
|
|
|
|
2024-12-05 17:53:43 +08:00
|
|
|
import { keccak_256 } from '@noble/hashes/sha3'
|
2021-10-30 13:57:29 +02:00
|
|
|
import Contract from "./evmasm.js";
|
2024-12-05 17:53:43 +08:00
|
|
|
import { toUtf8Bytes } from './utils.js';
|
2021-10-06 11:28:14 +02:00
|
|
|
|
2021-10-30 13:57:29 +02:00
|
|
|
export function createCode(seed, n) {
|
2021-10-06 11:28:14 +02:00
|
|
|
|
2024-12-05 17:53:43 +08:00
|
|
|
let ci = keccak_256(toUtf8Bytes(seed));
|
2021-10-06 11:28:14 +02:00
|
|
|
|
|
|
|
const C = new Contract();
|
|
|
|
|
|
|
|
C.push(0x64);
|
|
|
|
C.push("0x00");
|
|
|
|
C.push("0x00");
|
|
|
|
C.calldatacopy();
|
|
|
|
C.push("0x0100000000000000000000000000000000000000000000000000000000");
|
|
|
|
C.push("0x00");
|
|
|
|
C.mload();
|
|
|
|
C.div();
|
|
|
|
C.push("0x3f1a1187"); // MiMCSponge(uint256,uint256,uint256)
|
|
|
|
C.eq();
|
|
|
|
C.jmpi("start");
|
|
|
|
C.invalid();
|
|
|
|
|
|
|
|
C.label("start");
|
|
|
|
C.push("0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"); // q
|
|
|
|
C.push("0x44");
|
|
|
|
C.mload(); // k q
|
|
|
|
C.push("0x04");
|
|
|
|
C.mload(); // xL k q
|
|
|
|
C.dup(2); // q xL k q
|
|
|
|
C.push("0x24");
|
|
|
|
C.mload(); // xR q xL k q
|
|
|
|
C.dup(1); // q xR q xL k q
|
|
|
|
C.dup(0); // q q xR q xL k q
|
|
|
|
C.dup(4); // xL q q xR q xL k q
|
|
|
|
C.dup(6); // k xL q q xR q xL k q
|
|
|
|
C.addmod(); // t=k+xL q xR q xL k q
|
|
|
|
C.dup(1); // q t q xR q xL k q
|
|
|
|
C.dup(0); // q q t q xR q xL k q
|
|
|
|
C.dup(2); // t q q t q xR q xL k q
|
|
|
|
C.dup(0); // t t q q t q xR q xL k q
|
|
|
|
C.mulmod(); // b=t^2 q t q xR q xL k q
|
|
|
|
C.dup(0); // b b q t q xR q xL k q
|
|
|
|
C.mulmod(); // c=t^4 t q xR q xL k q
|
|
|
|
C.mulmod(); // d=t^5 xR q xL k q
|
|
|
|
C.addmod(); // e=t^5+xR xL k q (for next round: xL xR k q)
|
|
|
|
|
|
|
|
for (let i=0; i<n-1; i++) {
|
|
|
|
if (i < n-2) {
|
2024-12-05 17:53:43 +08:00
|
|
|
ci = keccak_256(ci);
|
2021-10-06 11:28:14 +02:00
|
|
|
} else {
|
|
|
|
ci = "0x00";
|
|
|
|
}
|
|
|
|
C.swap(1); // xR xL k q
|
|
|
|
C.dup(3); // q xR xL k q
|
|
|
|
C.dup(3); // k q xR xL k q
|
|
|
|
C.dup(1); // q k q xR xL k q
|
|
|
|
C.dup(4); // xL q k q xR xL k q
|
2024-12-05 17:53:43 +08:00
|
|
|
C.push(toHexString(ci)); // ci xL q k q xR xL k q
|
2021-10-06 11:28:14 +02:00
|
|
|
C.addmod(); // a=ci+xL k q xR xL k q
|
|
|
|
C.addmod(); // t=a+k xR xL k q
|
|
|
|
C.dup(4); // q t xR xL k q
|
|
|
|
C.swap(1); // t q xR xL k q
|
|
|
|
C.dup(1); // q t q xR xL k q
|
|
|
|
C.dup(0); // q q t q xR xL k q
|
|
|
|
C.dup(2); // t q q t q xR xL k q
|
|
|
|
C.dup(0); // t t q q t q xR xL k q
|
|
|
|
C.mulmod(); // b=t^2 q t q xR xL k q
|
|
|
|
C.dup(0); // b b q t q xR xL k q
|
|
|
|
C.mulmod(); // c=t^4 t q xR xL k q
|
|
|
|
C.mulmod(); // d=t^5 xR xL k q
|
|
|
|
C.dup(4); // q d xR xL k q
|
|
|
|
C.swap(2); // xR d q xL k q
|
|
|
|
C.addmod(); // e=t^5+xR xL k q (for next round: xL xR k q)
|
|
|
|
}
|
|
|
|
|
|
|
|
C.push("0x20");
|
|
|
|
C.mstore(); // Save it to pos 0;
|
|
|
|
C.push("0x00");
|
|
|
|
C.mstore(); // Save it to pos 1;
|
|
|
|
C.push("0x40");
|
|
|
|
C.push("0x00");
|
|
|
|
C.return();
|
|
|
|
|
|
|
|
return C.createTxData();
|
|
|
|
}
|
|
|
|
|
2021-10-30 13:57:29 +02:00
|
|
|
export const abi = [
|
2021-10-06 11:28:14 +02:00
|
|
|
{
|
|
|
|
"constant": true,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "xL_in",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "xR_in",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "k",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "MiMCSponge",
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "xL",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "xR",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"payable": false,
|
|
|
|
"stateMutability": "pure",
|
|
|
|
"type": "function"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|