Skip to content

Instantly share code, notes, and snippets.

View HarryR's full-sized avatar
🏴‍☠️
My time travel machine is stuck at 60 seconds per minute

HaRoLd HarryR

🏴‍☠️
My time travel machine is stuck at 60 seconds per minute
View GitHub Profile
import z3
s = z3.Solver()
bvp = 256
bvs = 2**bvp
balanceOf_signer = z3.BitVec('balanceOf_signer', bvp)
wad = z3.BitVec('wad', bvp)
reward = z3.BitVec('reward', bvp)
contract_balance = z3.Int('contract_balance') # z3.IntVal((10**18) * 4.48)
# Calculate balance of user after performing withdrawal
pragma solidity ^0.8.9;
contract E2Example
{
event EncryptedResponse(bytes32 nonce, bytes data);
event DecryptedInput(uint256 a, uint256 b, uint256 c);
event PublicKey(bytes32 x);
@HarryR
HarryR / download-solc.sh
Created November 23, 2020 17:56
Download solidity compiler
#!/usr/bin/env bash
if [[ -z $1 ]]; then
>&2 echo "Usage: `basename $0` [N.M|latest]"
>&2 echo ""
>&2 echo "Example to download v0.7.2:"
>&2 echo ""
>&2 echo ' $' "`basename $0` 7.2"
>&2 echo ""
>&2 echo "Example to download latest version:"
@HarryR
HarryR / signal.hpp
Created December 9, 2019 17:16
Tiny declarative signals / event hooks for C++11
#pragma once
#include <functional>
template<typename... ArgsT>
struct EventHook;
template<typename... ArgsT>
/// Conditionally enable a function if they share the same CurveT type
/// e.g. template<IsSameCurve<CurveT,OtherType> = 0>
template<typename MyCurve, typename OtherType>
using IsSameCurve = std::enable_if_t<std::is_same<MyCurve,typename OtherType::CurveT>::value,int>;
/// Conditionally enable a function if the are of the same types and curves
/// Uses CanonicalSelfT to determine if they're the same general type
/// e.g. template<IsSameCurve<CurveT,OtherType> = 0>
@HarryR
HarryR / text2sms.py
Created November 8, 2019 14:58
Text to old-school phone keyboard
def text2sms(text):
pairs = [' ', '!', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
mapping = {c:(str(i)*(j+1)) for i,p in enumerate(pairs) for j, c in enumerate(p)}
return '-'.join(mapping[c] for c in text if c in mapping)
@HarryR
HarryR / tal.py
Created October 23, 2019 13:03
Parse simple(ish) algorithms from whitepapers, do basic optimisations to transform into consistent state
import re
from pypeg2 import word, attr, maybe_some, blank, endl, parse, optional
from collections import defaultdict
class ExprBase(str):
pass
class Expression(ExprBase):
def __str__(self):
return str(self.inside) + ''.join([str(_) for _ in self.rhs])
"""
https://cryptosith.org/michael/data/talks/2013-08-01-SIAMAG13.pdf
https://www.issac-conference.org/2015/Slides/Schost.pdf
http://www.craigcostello.com.au/pairings/PairingsForBeginners.pdf
"""
fresh_compute = False # Perform expensive-(ish) computations for curve orders
field_modulus = 22369874298875696930346742206501054934775599465297184582183496627646774052458024540232479018147881220178054575403841904557897715222633333372134756426301062487682326574958588001132586331462553235407484089304633076250782629492557320825577
desired_curve_order = 258664426012969094010652733694893533536393512754914660539884262666720468348340822774968888139573360124440321458177
# Let (L, R) = x, 0
# for i in range(128): (L, R) = (L, R) ** 3 + (k_i1, k_i2) (interpreting the two values as an element of some quadratic field over F_p,
# so the # actual equations are newL = L**3 + 3*q*L*R**2 + k_i1, newR = 3*L**2*R + q*R**3 + k_i2,
from random import randint
q = 21888242871839275222246405745257275088696311157297823662689037894645226208583
q = 199
@HarryR
HarryR / bls12_381.sage
Created September 26, 2019 20:13
Sage script to derive all necessary parameters for BLS12-381 curve (including frobenius coefficients and montgomery reduction constants etc.)
field_modulus = 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787
desired_curve_order = 52435875175126190479447740508185965837690552500527637822603658699938581184513
Fp = GF(field_modulus)
PARAM_A4 = 0
PARAM_A6 = 4
E = EllipticCurve(Fp, [PARAM_A4, PARAM_A6])
E_order = E.order()