Created
April 23, 2023 21:44
-
-
Save EgosOwn/73d478d6192629d2124f1e78aac80c58 to your computer and use it in GitHub Desktop.
mimc implementation in js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| forward_mimc: (input_data, steps) => { | |
| const encoder = new TextEncoder(); | |
| const decoder = new TextDecoder(); | |
| const inputBytes = encoder.encode(input_data); | |
| let inp = BigInt('0x' + Array.from(inputBytes, byte => byte.toString(16).padStart(2, '0')).join('')); | |
| for (let i = 1n; i < BigInt(steps); i++) { | |
| inp = (inp ** 3n + _round_constants[Number(i % BigInt(_round_constants.length))]) % _modulus; | |
| } | |
| const byteLength = Math.ceil(inp.toString(2).length / 8); | |
| const resultArray = new Uint8Array(byteLength); | |
| for (let i = 0; i < byteLength; i++) { | |
| resultArray[i] = Number((inp >> (8n * BigInt(i))) & 0xffn); | |
| } | |
| return decoder.decode(resultArray); | |
| }, | |
| reverse_mimc: (input_data, steps) => { | |
| const inputBytes = input_data | |
| let rtrace = BigInt('0x' + Array.from(inputBytes, byte => byte.toString(16).padStart(2, '0')).join('')); | |
| for (let i = BigInt(steps) - 1n; i > 0n; i--) { | |
| rtrace = onionrVDF.powerMod(rtrace - _round_constants[Number(i % BigInt(_round_constants.length))], _little_fermat_expt, _modulus); | |
| } | |
| const byteLength = Math.ceil(rtrace.toString(2).length / 8); | |
| const resultArray = new Uint8Array(byteLength); | |
| for (let i = 0; i < byteLength; i++) { | |
| resultArray[i] = Number((rtrace >> (8n * BigInt(i))) & 0xffn); | |
| } | |
| return resultArray.reverse() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment