Skip to content

Commit

Permalink
Add hashsum functor with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
anantoni committed Dec 28, 2024
1 parent a0df883 commit 75d5b70
Show file tree
Hide file tree
Showing 6 changed files with 505 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ WORD_SIZE=32
# rudimentary
all: libsoufflenum.so num_tests mappings_tests keccak256_tests

libsoufflenum.so: $(KECCAK_OBJ) num256.o mappings.o keccak256.o lists.o smt-api.o
g++ -std=c++17 -shared -o libsoufflenum.so $(KECCAK_OBJ) smt-api.o num256.o mappings.o keccak256.o lists.o -march=native -lz3 -fopenmp -DRAM_DOMAIN_SIZE=$(WORD_SIZE)
libsoufflenum.so: $(KECCAK_OBJ) num256.o mappings.o keccak256.o lists.o smt-api.o hashsum_functor.o lub_functor.o
g++ -std=c++17 -shared -o libsoufflenum.so $(KECCAK_OBJ) smt-api.o num256.o mappings.o keccak256.o lists.o hashsum_functor.o lub_functor.o -march=native -lz3 -fopenmp -DRAM_DOMAIN_SIZE=$(WORD_SIZE)
ln -sf libsoufflenum.so libfunctors.so

hashsum_functor.o: hashsum_functor.cpp
g++ -std=c++17 hashsum_functor.cpp -lz3 -fopenmp -DRAM_DOMAIN_SIZE=$(WORD_SIZE) -c -fPIC -o hashsum_functor.o

lub_functor.o: lub_functor.cpp
g++ -std=c++17 lub_functor.cpp -lz3 -fopenmp -DRAM_DOMAIN_SIZE=$(WORD_SIZE) -c -fPIC -o lub_functor.o

smt-api.o: smt-api.cpp
g++ -std=c++17 smt-api.cpp -lz3 -fopenmp -DRAM_DOMAIN_SIZE=$(WORD_SIZE) -c -fPIC -o smt-api.o
g++ -std=c++17 smt-api.cpp -lz3 -fopenmp -DRAM_DOMAIN_SIZE=$(WORD_SIZE) -c -fPIC -o smt-api.o

num256.o: num256.cpp
g++ -std=c++17 -O2 num256.cpp -c -fPIC -o num256.o -lz3 -fopenmp

num_tests: num256.cpp num256_test.cpp
num_tests: num256.cpp num256_test.cpp
g++ -std=c++17 -o num_tests num256_test.cpp
./num_tests

Expand Down
19 changes: 19 additions & 0 deletions hashsum-example.dl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.decl numbers(x:number)
.decl sum_result(x:number)
.decl hsum_result(x:number)
.output sum_result()
.output hsum_result()
.functor hashsum(i1:number, i2:number):number stateful

numbers(-1).
numbers(1).
numbers(2).
numbers(3).
numbers(4).
numbers(5).

sum_result(x) :-
x = sum a : { numbers(a), a > 0 }.

hsum_result(x) :-
x = @@hashsum a : 0, { numbers(a), a > 0 }.
36 changes: 36 additions & 0 deletions hashsum_functor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "souffle/SouffleFunctor.h"
#include "souffle/utility/MiscUtil.h"
#include <cassert>

#if RAM_DOMAIN_SIZE == 64
using FF_int = int64_t;
using FF_uint = uint64_t;
using FF_float = double;
#else
using FF_int = int32_t;
using FF_uint = uint32_t;
using FF_float = float;
#endif

extern "C" {
souffle::RamDomain hashsum(souffle::SymbolTable* symbolTable, souffle::RecordTable* recordTable,
souffle::RamDomain arg1, souffle::RamDomain arg2);
unsigned long djb2(const std::string &str);
}

souffle::RamDomain hashsum(souffle::SymbolTable* symbolTable, souffle::RecordTable* recordTable, souffle::RamDomain arg1, souffle::RamDomain arg2) {
assert(symbolTable && "NULL symbol table");
assert(recordTable && "NULL record table");

std::string result = std::to_string(arg1 + djb2(std::to_string(arg2)));
return arg1 + djb2(std::to_string(arg2));
}

unsigned long djb2(const std::string &str) {
unsigned long hash = 5381; // Initialize hash with a prime number
for (char c : str) {
// Multiply hash by 33 and add the current character
hash = ((hash << 5) + hash) + c; // hash * 33 + c
}
return hash;
}
39 changes: 39 additions & 0 deletions lub_functor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "souffle/SouffleFunctor.h"
#include "souffle/utility/MiscUtil.h"
#include <cassert>

#if RAM_DOMAIN_SIZE == 64
using FF_int = int64_t;
using FF_uint = uint64_t;
using FF_float = double;
#else
using FF_int = int32_t;
using FF_uint = uint32_t;
using FF_float = float;
#endif

extern "C" {
souffle::RamDomain lub(souffle::SymbolTable* symbolTable, souffle::RecordTable* recordTable,
souffle::RamDomain i1, souffle::RamDomain i2);
souffle::RamDomain lub_number(souffle::SymbolTable* symbolTable, souffle::RecordTable* recordTable,
souffle::RamDomain i, souffle::RamDomain x);
}

souffle::RamDomain lub(souffle::SymbolTable*, souffle::RecordTable* recordTable, souffle::RamDomain i1,
souffle::RamDomain i2) {
const souffle::RamDomain* interval1 = recordTable->unpack(i1, 2);
const souffle::RamDomain* interval2 = recordTable->unpack(i2, 2);
auto lb = std::min(interval1[0], interval2[0]);
auto ub = std::max(interval1[1], interval2[1]);
const souffle::RamDomain res[2] = {lb, ub};
return recordTable->pack(res, 2);
}

souffle::RamDomain lub_number(souffle::SymbolTable*, souffle::RecordTable* recordTable, souffle::RamDomain i,
souffle::RamDomain x) {
const souffle::RamDomain* interval = recordTable->unpack(i, 2);
auto lb = std::min(interval[0], x);
auto ub = std::max(interval[1], x);
const souffle::RamDomain res[2] = {lb, ub};
return recordTable->pack(res, 2);
}
Loading

0 comments on commit 75d5b70

Please sign in to comment.