Skip to content

Instantly share code, notes, and snippets.

View Sajjon's full-sized avatar
💭
FOSS|Rust&Swift|Principal Cryptography & Middleware Engineer@RDX Works/Radix DLT

Alexander Cyon Sajjon

💭
FOSS|Rust&Swift|Principal Cryptography & Middleware Engineer@RDX Works/Radix DLT
View GitHub Profile
/// A dictionary wrapper that uses a `CaseIterable` type as its key.
/// This differs from a dictionary in two ways:
///
/// - Key-value pairs are accessed in a fixed order, which is the same
/// as the `Key`'s `allCases` property.
/// - Every possible key must have a value given, so using the key-based
/// subscript returns a non-optional value.
struct CaseMap<Key: CaseIterable & Hashable, Value> : Collection {
typealias Index = Key.AllCases.Index
/// Returns a binary predicate using the given key path to create an ascending order
/// for elements of type `Root`.
func ascending<Root, Value: Comparable>(_ path: KeyPath<Root, Value>) -> (Root, Root) -> Bool {
return { $0[keyPath: path] < $1[keyPath: path] }
}
/// Returns a binary predicate using the given key path to create a descending order
/// for elements of type `Root`.
func descending<Root, Value: Comparable>(_ path: KeyPath<Root, Value>) -> (Root, Root) -> Bool {
return { $0[keyPath: path] > $1[keyPath: path] }
@DougGregor
DougGregor / dynamic_member_lookup_environment.swift
Created May 2, 2018 16:59
Using Swift 4.2's @dynamicMemberLookup to expose environment variables
import Darwin
@dynamicMemberLookup
struct Environment {
subscript(dynamicMember name: String) -> String? {
get {
guard let value = getenv(name) else { return nil }
return String(validatingUTF8: value)
}
@kallewoof
kallewoof / test_vectors.md
Last active April 18, 2022 14:51
Test vectors, Schnorr signatures

Schnorr signature test vectors

Test vector overview

  1. Hash function H(m) = SHA256(m)

Basics

  • For a private key x, the public key is xG.
  • A signature on the message m with private key x is (R, s) where R=kG, s=k+H(R,X,m)x.
  • Verifying a signature is testing whether sG = R+H(R,X,m)X.
@hfossli
hfossli / SHA256-Bridging-Header.h
Last active July 1, 2024 02:01
AES 256 in swift 4 with CommonCrypto
#import <CommonCrypto/CommonCrypto.h>

Merry's very quick guide to elliptic curve crypto

An elliptic curve is made up of the following:

  1. A field, F_p.
  • What this means is all arithmetic on scalars is modulo p.
  • Modern ECC have p as some large prime.
  • For curve25519, p = 2^255 - 19, a prime.
  1. An equation and it's parameters:
@shihrer
shihrer / Packer.py
Last active October 8, 2021 17:57
Simple packing solution for Python. This is a python version of the algorithm located at http://codeincomplete.com/posts/2011/5/7/bin_packing/
"""
MIT License
Copyright (c) 2016 Michael Shihrer (michael@shihrer.me)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@stribika
stribika / montgomery_ladder.py
Created February 14, 2016 23:13
Basic Montgomery ladder implementation. In the test it works with just numbers, but you can plug in any operation.
#!/usr/bin/python3 -O
from math import floor, log
def montgomery_ladder(x, n, op, select):
k = floor(log(n, 2)) + 1
x1 = x
x2 = op(x, x)
for i in range(k - 2, -1, -1):
bit = 1 if n & (1 << i) else 0
@natmchugh
natmchugh / Montgomery.py
Created January 12, 2016 21:08
Montgomery Ladder
import random
class Montgomery:
# B*v^2 = u^3 + A*u^2 + u
def __init__(self, A, B, p):
self.A = A
self.B = B
self.p = p
@natecook1000
natecook1000 / operatorCharacters.swift
Last active January 3, 2024 21:33
Allowed characters for Swift operators
import Foundation
extension UnicodeScalar : ForwardIndexType {
public func successor() -> UnicodeScalar {
return UnicodeScalar(value + 1)
}
}
var operatorHeads: [UnicodeScalar] = Array("=-+!*%<>&|^~?".unicodeScalars)
operatorHeads += Array("\u{00A1}" ... "\u{00A7}")