Skip to content

Instantly share code, notes, and snippets.

View bquast's full-sized avatar
:octocat:

Bastiaan Quast bquast

:octocat:
View GitHub Profile
@bquast
bquast / RSA.R
Created July 18, 2019 10:59
Basic example of the RSA algorithm in R
# RSA example
#
# Bastiaan Quast
# International Telecommunication Union
# bastiaan.quast@itu.int
# Two people - Alice and Bob - want to communicate privately
# A third person Eve wants to read the private communication
Alice = list()
Bob = list()
@bquast
bquast / Paillier.R
Last active August 16, 2019 11:55
Paillier Cryptosystem for E-voting (using RSA) in R
# Paillier cryptosystem in R
# Bastiaan Quast
# install and load the GNU Multiple Percision Arithmetic R package
install.packages('gmp')
library(gmp)
# define parameters
# p and q are private to the Inspector
Inspector = list()
# Author: Bill Buchanan
# source: https://asecuritysite.com/encryption/pal_ex
from random import randint
import sys
def gcd(a,b):
"""Compute the greatest common divisor of a and b"""
while b > 0:
a, b = b, a % b
# define some functions
# modular exponentiation
# source: https://gist.github.com/ttezel/4635562
function result = modexp (x, y, n)
%anything raised to 0th power = 1 so return 1
if (y == 0)
result = 1;
return;
end
# adapted from: https://asecuritysite.com/encryption/lwe4
import sys
import numpy as np
import random
import math
nvals=20
B=[]
e=[]
import numpy as np
import sys
def gen_poly(n,q):
global xN_1
l = 0 #Gamma Distribution Location (Mean "center" of dist.)
poly = np.floor(np.random.normal(l,size=(n)))
while (len(poly) != n):
poly = np.floor(np.random.normal(l,size=(n)))
poly = np.floor(p.polydiv(poly,xN_1)[1]%q)
# contructor function
tensor <- function(x) {
# check that it's numeric
if (!is.numeric(x)) stop("X must be numeric")
# create the array and change the class
y <- structure(array(x), class = "tensor")
# add attributes
attributes(y)$creators <- list()
# logsumexp
logsumexp <- function (x) {
y = max(x)
y + log(sum(exp(x - y)))
}
# softmax
softmax <- function (x) {
exp(x - logsumexp(x))
}
@bquast
bquast / attention.R
Last active May 18, 2023 13:50
R implementation of attention, see blog post: https://qua.st/attention-in-R
# attention.R
# Bastiaan Quast
# bquast@gmail.com
# based on:
# https://machinelearningmastery.com/the-attention-mechanism-from-scratch/
# encoder representations of four different words
word_1 = matrix(c(1,0,0), nrow=1)
word_2 = matrix(c(0,1,0), nrow=1)
# HE illustrated primer
# define some parameters (small as an example)
# N.B. these parameters are not secure, they are completely insecure
n = 4
d = 2^n
t = 7
q = 874
# load library to create polynomials