Skip to content

Instantly share code, notes, and snippets.

@Lohann
Lohann / Serialization.sol
Created June 30, 2022 14:29
Solidity encode/decode
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
library Vote {
uint256 constant BITS_PER_CANDIDATE = 5;
uint256 constant BITMASK = (2**BITS_PER_CANDIDATE) - 1;
// Storage Layout:
// Voter (160bit) | size (5bit) | votes (5~90bit)
@Lohann
Lohann / Lottery.sol
Last active July 10, 2022 15:15
Example of a simple lottery smart-contract written in solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Lottery {
// PRICE PER TICKET
uint256 constant TICKET_PRICE = (10 ** 18) / 100; // 0.01eth
uint256 constant TICKET_OPTIONS = 30;
uint256 constant OPTIONS_PER_TICKET = 3;
@Lohann
Lohann / UnidirecionalPaymentChannel.sol
Last active July 24, 2022 14:57
Example of Unidirecional Payment Channel in solidty
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
library ECDSA {
/**
* @notice Recovers the address for an ECDSA signature and message hash,
* note that the hash must be prefixed with "\x19Ethereum Signed Message:\n{msg.length}"
* @return address The address that was used to sign the message
*/
const webcrypto = require('crypto').webcrypto.subtle;
const enc = new TextEncoder();
function bytesToBase64url(bytes) {
return Buffer.from(bytes).toString('base64url');
}
function jsonToBase64url(obj) {
const bytes = enc.encode(JSON.stringify(obj));
// "dependencies": {
// "axios": "^1.3.6"
// }
const axios = require('axios');
const CLIENT_ID = '<client_id_aqui>';
async function run(token) {
console.log(token);
}
@Lohann
Lohann / rust_rsa_accumulator.rs
Last active October 18, 2024 00:46
Basic implementation of RSA Accumulator in rust
// [dependencies]
// blake2b_simd = "1.0.1"
// num-bigint = "0.4.3"
// num-integer = "0.1.45"
// num-prime = { version = "0.4.3", features=["num-bigint"] }
// num-traits = "0.2.15"
use blake2b_simd;
use num_bigint::{BigInt, BigUint, Sign};
use num_integer::Integer;
@Lohann
Lohann / frost_dkg.py
Last active October 18, 2024 21:36
Threshold Signatures implementation based on FROST using schorr signatures over the RSA group
import hashlib
from random import SystemRandom
from collections import namedtuple
'''
FROST: Flexible Round-Optimized Schnorr Threshold Signatures
Implementation based in this paper:
https://eprint.iacr.org/2020/852.pdf
'''
"""
-- requirements.txt
bip_utils==2.9.1
mnemonic==0.21
"""
from mnemonic import Mnemonic
from bip_utils import Bip44, Bip44Changes, Bip44Coins
mnemonic = "bottom drive obey lake curtain smoke basket hold race lonely fit walk"
passphrase = "password here"
// Gas efficient SQRT method, computes floor(sqrt(x)).
// Constant gas cost of 355
//
// For testing use this tool: https://www.evm.codes/playground?fork=cancun
// Author: Lohann Ferreira
PUSH0
CALLDATALOAD
PUSH1 1
DUP2
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* Workaround example on how to inject and execute arbitrary bytecode in solidity contract
* Currently only YUL supports verbatim: https://github.com/ethereum/solidity/issues/12067
* But you cannot import Solidity code in YUL, or YUL code in solidity, so this workaround is necessary.
* It works as long the byte sequence `0x7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F00` appear in the runtime code.
*