Skip to content

Instantly share code, notes, and snippets.

View bquast's full-sized avatar
:octocat:

Bastiaan Quast bquast

:octocat:
View GitHub Profile
# 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()
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)
# adapted from: https://asecuritysite.com/encryption/lwe4
import sys
import numpy as np
import random
import math
nvals=20
B=[]
e=[]
# 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
# 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
@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()
@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()
## sigmoid function
sigmoid <- function(x, k=1, x0=0)
1 / (1+exp( -k*(x-x0) ))
## tanh^2 function
tanhsq <- function(x)
((exp(2*x)-1)^2)/((exp(2*x)+1)^2)
# define weights
Wa = matrix( c(0.45, 0.25), nrow=1 ); Ua = matrix(0.15); ba = matrix(0.20)
# sigmoid function
sigmoid <- function(x)
1 / (1 + exp(-x) )
# sigmoid derivative
sigmoid_output_to_derivative <- function(x)
x*(1-x)
# hidden layer size
hidden_dim = 4
# define some functions
## convert integer to binary
i2b <- function(integer, length=8)
as.numeric(intToBits(integer))[1:length]
## apply
int2bin <- function(integer, length=8)
t(sapply(integer, i2b, length=length))