Skip to content

Instantly share code, notes, and snippets.

View bquast's full-sized avatar
🎯

Bastiaan Quast bquast

🎯
View GitHub Profile
# HE illustrated primer
# define some parameters (small as an example)
# N.B. these parameters are not secure, they are completely insecure
d = 4
n = 2^d
t = (n/2)-1
q = 874
# load library to create polynomials
library(matrixStats)
# Softmax function
softmax <- function(x) {
exp_x <- exp(x - max(x))
exp_x / sum(exp_x)
}
# Scaled dot product attention
scaled_dot_product_attention <- function(Q, K, V, mask = NULL) {
# rnn.R
# Bastiaan Quast
# [email protected]
# Set the seed to obtain identical random values
set.seed(0)
# compute sigmoid nonlinearity
sigmoid = function(x)
1 / (1+exp(-x))
# cnn.R
# Bastiaan Quast
# [email protected]
# Convolution function
conv2d <- function(input, filter, stride = 1) {
input_height <- dim(input)[1]
input_width <- dim(input)[2]
filter_height <- dim(filter)[1]
filter_width <- dim(filter)[2]
# NN-1-layer.R
# Bastiaan Quast
# [email protected]
# create data
inputs = matrix(c(0,1,1,
1,0,1,
1,0,1,
1,1,0 ), nrow=4, byrow=TRUE)
# DNN2.R
# Bastiaan Quast
# [email protected]
# derivative
der <- function(x){
x * (1-x)
}
# input data
# transformer simple.R
# Bastiaan Quast
softmax <- function(x) {
e_x <- exp(x - max(x)) # subtract max to avoid numerical instability
return(e_x / sum(e_x))
}
# Initialize input queries, keys, and values
query <- rnorm(5)
@bquast
bquast / CKKSencoder.py
Last active December 19, 2023 12:37
OpenMind CKKSencoder CKKS encoder Daniel Huynh
import numpy as np
from numpy.polynomial import Polynomial
# Set the parameters
M = 8
N = M // 2
scale = 64
xi = np.exp(2 * np.pi * 1j / M)
def vandermonde(xi: np.complex128, M: int) -> np.array:
@bquast
bquast / CKKS-encoder.R
Last active December 23, 2023 23:18
Homomorphic Encryption CKKS encoder R
library(polynom)
M <- 8
N <- M %/% 2
scale <- 64
xi <- complex(real = cos(2 * pi / M), imaginary = sin(2 * pi / M))
vandermonde <- function(xi, M) {
N <- M %/% 2
# Initialize an empty matrix with complex data type
@bquast
bquast / BFV-2.py
Last active January 3, 2024 13:56
BFV in python without using functions
import numpy as np
from numpy.polynomial import Polynomial
def polynomial_modulo(polynomial, mod):
"""
Perform polynomial modulo operation using divmod.
"""
q, r = divmod(polynomial, mod)
return r