Skip to content

Commit

Permalink
kyber
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmucb committed Apr 9, 2024
1 parent df658ff commit 2fd3912
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 1 deletion.
2 changes: 1 addition & 1 deletion v2/dilithium/test_dilithium.mak
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License
# File: test_hash.mak
# File: test_dilithium.mak


ifndef SRC_DIR
Expand Down
121 changes: 121 additions & 0 deletions v2/include/kyber.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//
// Copyright 2024 John Manferdelli, All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// or in the the file LICENSE-2.0.txt in the top level sourcedirectory
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
// File: dilithium.h.h

#ifndef _DILITHIUM_H__
#define _DILITHIUM_H__

#include "crypto_support.h"
#include "symmetric_cipher.h"
using namespace std;

class dilithium_parameters {
public:
dilithium_parameters();
~dilithium_parameters();

int n_;
int k_;
int l_;
int d_;

int q_;
int wt_c_;
int gamma_1_;
int gamma_2_;
int eta_;
int beta_;
};

int center_normalize(int x, int a);
int inf_norm(vector<int> v);
#if 1
int high_bits(int x, int a, int q);
int low_bits(int x, int a, int q);
#else
int high_bits(int x, int a);
int low_bits(int x, int a);
#endif

class coefficient_vector {
public:
int q_;
int len_;

coefficient_vector(int q, int len);
~coefficient_vector();

vector<int> c_;
};

bool coefficients_high_bits(int a, coefficient_vector& in, coefficient_vector* out);
bool coefficients_low_bits(int a, coefficient_vector& in, coefficient_vector* out);

class module_array {
public:
int q_;
int n_;
int nr_;
int nc_;

module_array(int q, int n, int nr, int nc);
~module_array();

coefficient_vector** c_;
int index(int r, int c);
};

class module_vector {
public:
int q_;
int dim_;
int n_;

module_vector(int q, int n, int dim);
~module_vector();

coefficient_vector** c_;
};

bool coefficient_add(coefficient_vector& in1, coefficient_vector& in2, coefficient_vector* out);
bool coefficient_mult(coefficient_vector& in1, coefficient_vector& in2, coefficient_vector* out);
void print_coefficient_vector(coefficient_vector& v);
bool coefficient_set_vector(coefficient_vector& in, coefficient_vector* out);
bool coefficient_vector_zero(coefficient_vector* out);
bool coefficient_vector_add_to(coefficient_vector& in, coefficient_vector* out);
bool coefficient_equal(coefficient_vector& in1, coefficient_vector& in2);

void print_module_array(module_array& ma);
bool module_vector_mult_by_scalar(coefficient_vector& in1, module_vector& in2, module_vector* out);
bool module_vector_add(module_vector& in1, module_vector& in2, module_vector* out);
bool module_vector_subtract(module_vector& in1, module_vector& in2, module_vector* out);
bool module_apply_array(module_array& A, module_vector& v, module_vector* out);
bool module_vector_is_zero(module_vector& in);
bool make_module_vector_zero(module_vector* out);
bool module_vector_equal(module_vector& in1, module_vector& in2);
void print_module_vector(module_vector& mv);

void print_dilithium_parameters(dilithium_parameters& p);
bool init_dilithium_parameters(dilithium_parameters* p);

bool dilithium_keygen(dilithium_parameters& params, module_array* A,
module_vector* t, module_vector* s1, module_vector* s2);
bool dilithium_sign(dilithium_parameters& params, module_array& A,
module_vector& t, module_vector& s1, module_vector& s2,
int m_len, byte* M, module_vector* z,
int len_cc, int* cc);
bool dilithium_verify(dilithium_parameters& params, module_array& A,
module_vector& t, int m_len, byte* M,
module_vector& z, int len_cc, int* cc);
#endif
22 changes: 22 additions & 0 deletions v2/kyber/kyber.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2014-2024, John Manferdelli, All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// or in the the file LICENSE-2.0.txt in the top level sourcedirectory
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
// File: kyber.cc

#include "crypto_support.h"
#include "kyber.h"
#include "sha3.h"

using namespace std;

// This is the "vanilla" kyber, which is slow and has
// large keys.

50 changes: 50 additions & 0 deletions v2/kyber/test_kyber.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2014-2024 John Manferdelli, All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// or in the the file LICENSE-2.0.txt in the top level sourcedirectory
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
// File: test_kyber.cc

#include <gtest/gtest.h>
#include <gflags/gflags.h>
#include <stdio.h>
#include "crypto_support.h"
#include "support.pb.h"
#include "kyber.h"

DEFINE_bool(print_all, false, "Print intermediate test computations");


bool test_kyber1() {
return true;
}


TEST (kyber, test_kyber1) {
EXPECT_TRUE(test_kyber1());
}


int main(int an, char** av) {
gflags::ParseCommandLineFlags(&an, &av, true);
an = 1;
::testing::InitGoogleTest(&an, av);

if (!init_crypto()) {
printf("init_crypto failed\n");
return 1;
}

int result = RUN_ALL_TESTS();

close_crypto();
printf("\n");
return 0;
}
92 changes: 92 additions & 0 deletions v2/kyber/test_kyber.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright 2014-2024 John Manferdelli, All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# or in the the file LICENSE-2.0.txt in the top level sourcedirectory
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License
# File: test_kyber.mak


ifndef SRC_DIR
SRC_DIR=$(HOME)/src/github.com/jlmucb/crypto/v2
endif
ifndef OBJ_DIR
OBJ_DIR=$(HOME)/cryptoobj/v2
endif
ifndef EXE_DIR
EXE_DIR=$(HOME)/cryptobin
endif
#ifndef GOOGLE_INCLUDE
#GOOGLE_INCLUDE=/usr/local/include/g
#endif
ifndef LOCAL_LIB
LOCAL_LIB=/usr/local/lib
endif
ifndef TARGET_MACHINE_TYPE
TARGET_MACHINE_TYPE= x64
endif

S= $(SRC_DIR)/kyber
O= $(OBJ_DIR)/kyber
S_HASH=$(SRC_DIR)/hash
S_SUPPORT=$(SRC_DIR)/crypto_support
INCLUDE= -I$(SRC_DIR)/include -I$(S) -I$(S_SUPPORT) -I/usr/local/include

CFLAGS=$(INCLUDE) -O3 -g -Wall -std=c++11 -Wno-unused-variable
CFLAGS1=$(INCLUDE) -O1 -g -Wall -std=c++11 -Wno-unused-variable
CC=g++
LINK=g++
PROTO=protoc
AR=ar
LDFLAGS= -lprotobuf -lgtest -lgflags -lpthread

dobj= $(O)/test_kyber.o $(O)/support.pb.o $(O)/crypto_support.o $(O)/crypto_names.o \
$(O)/hash.o $(O)/sha3.o $(O)/kyber.o

all: test_kyber.exe
clean:
@echo "removing object files"
rm $(O)/*.o
@echo "removing executable file"
rm $(EXE_DIR)/test_kyber.exe

test_kyber.exe: $(dobj)
@echo "linking executable files"
$(LINK) -o $(EXE_DIR)/test_kyber.exe $(dobj) $(LDFLAGS)

$(S_SUPPORT)/support.pb.cc $(S_SUPPORT)/support.pb.h: $(S_SUPPORT)/support.proto
$(PROTO) -I=$(S) --cpp_out=$(S_SUPPORT) $(S_SUPPORT)/support.proto

$(O)/test_kyber.o: $(S)/test_kyber.cc
@echo "compiling test_kyber.cc"
$(CC) $(CFLAGS) -c $(I) -o $(O)/test_kyber.o $(S)/test_kyber.cc

$(O)/support.pb.o: $(S_SUPPORT)/support.pb.cc $(S_SUPPORT)/support.pb.h
@echo "compiling support.pb.cc"
$(CC) $(CFLAGS) -c $(I) -o $(O)/support.pb.o $(S_SUPPORT)/support.pb.cc

$(O)/crypto_support.o: $(S_SUPPORT)/crypto_support.cc $(S_SUPPORT)/support.pb.h
@echo "compiling crypto_support.cc"
$(CC) $(CFLAGS) -c $(I) -o $(O)/crypto_support.o $(S_SUPPORT)/crypto_support.cc

$(O)/crypto_names.o: $(S_SUPPORT)/crypto_names.cc
@echo "compiling crypto_names.cc"
$(CC) $(CFLAGS) -c $(I) -o $(O)/crypto_names.o $(S_SUPPORT)/crypto_names.cc

$(O)/hash.o: $(S_HASH)/hash.cc
@echo "compiling hash.cc"
$(CC) $(CFLAGS) -c $(I) -o $(O)/hash.o $(S_HASH)/hash.cc

$(O)/sha3.o: $(S_HASH)/sha3.cc
@echo "compiling sha3.cc"
$(CC) $(CFLAGS) -c $(I) -o $(O)/sha3.o $(S_HASH)/sha3.cc

$(O)/kyber.o: $(S)/kyber.cc
@echo "compiling kyber.cc"
$(CC) $(CFLAGS) -c $(I) -o $(O)/kyber.o $(S)/kyber.cc

0 comments on commit 2fd3912

Please sign in to comment.