summaryrefslogtreecommitdiff
path: root/attic/historic/anode/libspark
diff options
context:
space:
mode:
Diffstat (limited to 'attic/historic/anode/libspark')
-rw-r--r--attic/historic/anode/libspark/Makefile16
-rw-r--r--attic/historic/anode/libspark/experiments/FindGoodSegmentDelimiters.cpp161
-rw-r--r--attic/historic/anode/libspark/experiments/Makefile5
-rw-r--r--attic/historic/anode/libspark/streamencoder.h108
-rw-r--r--attic/historic/anode/libspark/wrapper.h66
5 files changed, 356 insertions, 0 deletions
diff --git a/attic/historic/anode/libspark/Makefile b/attic/historic/anode/libspark/Makefile
new file mode 100644
index 00000000..0d3fedd8
--- /dev/null
+++ b/attic/historic/anode/libspark/Makefile
@@ -0,0 +1,16 @@
+SYSNAME:=${shell uname}
+SYSNAME!=uname
+include ../config.mk.${SYSNAME}
+
+LIBSPARK_OBJS=
+
+all: libspark
+
+libspark: $(LIBSPARK_OBJS)
+ ar rcs libspark.a $(LIBSPARK_OBJS)
+ ranlib libspark.a
+
+clean: force
+ rm -f *.a *.so *.dylib *.dll *.lib *.exe *.o
+
+force: ;
diff --git a/attic/historic/anode/libspark/experiments/FindGoodSegmentDelimiters.cpp b/attic/historic/anode/libspark/experiments/FindGoodSegmentDelimiters.cpp
new file mode 100644
index 00000000..9b1ecaa1
--- /dev/null
+++ b/attic/historic/anode/libspark/experiments/FindGoodSegmentDelimiters.cpp
@@ -0,0 +1,161 @@
+// Searches for good delimiters to cut streams into relatively well sized
+// segments.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sys/time.h>
+#include <boost/cstdint.hpp>
+#include <boost/array.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/thread.hpp>
+#include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
+#include <iostream>
+#include <vector>
+#include <map>
+
+// Desired size range
+#define MIN_DESIRED_SIZE 4096
+#define MAX_DESIRED_SIZE 131072
+
+#define DELIMITER_SET_SIZE 1
+typedef boost::array<boost::uint16_t,DELIMITER_SET_SIZE> DelimArray;
+
+struct BestEntry
+{
+ DelimArray best;
+ double bestScore;
+ std::vector<unsigned char> data;
+};
+
+boost::mutex bestLock;
+boost::mutex outLock;
+std::map<std::string,BestEntry> best;
+
+static void runThread(const std::string &fileName)
+{
+ char tmp[4096];
+
+ boost::mt19937 prng;
+ {
+ boost::uint32_t seed;
+ FILE *ur = fopen("/dev/urandom","r");
+ fread((void *)&seed,1,sizeof(seed),ur);
+ fclose(ur);
+ prng.seed(seed);
+ }
+
+ BestEntry *myEntry;
+ {
+ boost::mutex::scoped_lock l(bestLock);
+ myEntry = &(best[fileName]);
+ myEntry->bestScore = 99999999.0;
+ }
+
+ {
+ boost::mutex::scoped_lock l(outLock);
+
+ std::cout << "*** Reading test data from: " << fileName << std::endl;
+ FILE *f = fopen(fileName.c_str(),"r");
+ if (f) {
+ int n;
+ while ((n = fread((void *)tmp,1,sizeof(tmp),f)) > 0) {
+ for(int i=0;i<n;++i)
+ myEntry->data.push_back((unsigned char)tmp[i]);
+ }
+ fclose(f);
+ }
+
+ if (myEntry->data.size() <= 0) {
+ std::cout << "Error: no data read." << std::endl;
+ exit(1);
+ } else std::cout << "*** Read " << myEntry->data.size() << " bytes of test data." << std::endl;
+
+ std::cout.flush();
+ }
+
+ DelimArray current;
+ for(unsigned int i=0;i<DELIMITER_SET_SIZE;++i)
+ current[i] = (boost::uint16_t)prng();
+
+ for(;;) {
+ unsigned long numTooShort = 0;
+ unsigned long numTooLong = 0;
+ unsigned long numGood = 0;
+
+ boost::uint32_t shiftRegister = 0;
+ unsigned long segSize = 0;
+ for(std::vector<unsigned char>::iterator i=myEntry->data.begin();i!=myEntry->data.end();++i) {
+ shiftRegister <<= 1;
+ shiftRegister |= (((boost::uint32_t)*i) & 1);
+
+ ++segSize;
+
+ boost::uint16_t transformedShiftRegister = (boost::uint16_t)(shiftRegister);
+
+ for(DelimArray::iterator d=current.begin();d!=current.end();++d) {
+ if (transformedShiftRegister == *d) {
+ if (segSize < MIN_DESIRED_SIZE)
+ ++numTooShort;
+ else if (segSize > MAX_DESIRED_SIZE)
+ ++numTooLong;
+ else ++numGood;
+ segSize = 0;
+ break;
+ }
+ }
+ }
+ if (segSize) {
+ if (segSize < MIN_DESIRED_SIZE)
+ ++numTooShort;
+ else if (segSize > MAX_DESIRED_SIZE)
+ ++numTooLong;
+ else ++numGood;
+ }
+
+ if (numGood) {
+ double score = ((double)(numTooShort + numTooLong)) / ((double)numGood);
+
+ if (score < myEntry->bestScore) {
+ myEntry->best = current;
+ myEntry->bestScore = score;
+
+ boost::mutex::scoped_lock l(outLock);
+
+ std::cout << fileName << ": ";
+
+ for(DelimArray::iterator d=current.begin();d!=current.end();++d) {
+ sprintf(tmp,"0x%.4x",(unsigned int)*d);
+ if (d != current.begin())
+ std::cout << ',';
+ std::cout << tmp;
+ }
+
+ std::cout << ": " << numTooShort << " / " << numGood << " / " << numTooLong << " (" << score << ")" << std::endl;
+ std::cout.flush();
+
+ if ((numTooShort == 0)&&(numTooLong == 0))
+ break;
+ }
+ }
+
+ for(DelimArray::iterator i=current.begin();i!=current.end();++i)
+ *i = (boost::uint16_t)prng();
+ }
+}
+
+int main(int argc,char **argv)
+{
+ std::vector< boost::shared_ptr<boost::thread> > threads;
+
+ for(int i=1;i<argc;++i) {
+ boost::shared_ptr<boost::thread> t(new boost::thread(boost::bind(&runThread,std::string(argv[i]))));
+ threads.push_back(t);
+ }
+
+ for(std::vector< boost::shared_ptr<boost::thread> >::iterator i=threads.begin();i!=threads.end();++i)
+ (*i)->join();
+
+ return 0;
+}
diff --git a/attic/historic/anode/libspark/experiments/Makefile b/attic/historic/anode/libspark/experiments/Makefile
new file mode 100644
index 00000000..83aa4f67
--- /dev/null
+++ b/attic/historic/anode/libspark/experiments/Makefile
@@ -0,0 +1,5 @@
+all:
+ g++ -O6 -ftree-vectorize -o FindGoodSegmentDelimiters FindGoodSegmentDelimiters.cpp -lboost_thread -lpthread
+
+clean:
+ rm FindGoodSegmentDelimiters
diff --git a/attic/historic/anode/libspark/streamencoder.h b/attic/historic/anode/libspark/streamencoder.h
new file mode 100644
index 00000000..b487ca40
--- /dev/null
+++ b/attic/historic/anode/libspark/streamencoder.h
@@ -0,0 +1,108 @@
+/* libanode: the Anode C reference implementation
+ * Copyright (C) 2009 Adam Ierymenko <adam.ierymenko@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef _SPARK_STREAMENCODER_H
+#define _SPARK_STREAMENCODER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct
+{
+ unsigned char *input_buf;
+ unsigned long input_buf_capacity;
+ unsigned long input_length;
+
+ unsigned char *stream_out_buf;
+ unsigned long stream_out_buf_capacity;
+ unsigned long stream_out_length;
+
+ void (*data_segment_add_func)(const void *data,unsigned long len,const void *global_hash,unsigned long global_hash_len);
+} SparkStreamEncoder;
+
+/**
+ * Initialize a spark stream encoder
+ *
+ * @param enc Encoder structure to initialize
+ * @param data_segment_add_func Function to call to store or cache data
+ */
+void SparkStreamEncoder_init(
+ SparkStreamEncoder *enc,
+ void (*data_segment_add_func)(
+ const void *data,
+ unsigned long len,
+ const void *global_hash,
+ unsigned long global_hash_len));
+
+/**
+ * Clean up a spark stream encoder structure
+ *
+ * @param enc Structure to clear
+ */
+void SparkStreamEncoder_destroy(SparkStreamEncoder *enc);
+
+/**
+ * Add data to encode
+ *
+ * @param enc Encoder structure
+ * @param data Data to encode
+ * @param len Length of data in bytes
+ * @return Number of bytes of result stream now available
+ */
+unsigned long SparkStreamEncoder_put(
+ SparkStreamEncoder *enc,
+ const void *data,
+ unsigned long len);
+
+/**
+ * Flush all data currently in input buffer
+ *
+ * @param enc Encoder structure to flush
+ */
+void SparkStreamEncoder_flush(SparkStreamEncoder *enc);
+
+/**
+ * @return Number of bytes of output stream available
+ */
+static inline unsigned long SparkStreamEncoder_available(SparkStreamEncoder *enc)
+{
+ return enc->stream_out_length;
+}
+
+/**
+ * @return Pointer to result stream bytes (may return null if none available)
+ */
+static inline const void *SparkStreamEncoder_get(SparkStreamEncoder *enc)
+{
+ return (const void *)(enc->stream_out_buf);
+}
+
+/**
+ * @return "Consume" result stream bytes after they're read or sent
+ */
+static inline void SparkStreamEncoder_consume(SparkStreamEncoder *enc,unsigned long len)
+{
+ unsigned long i;
+ for(i=len;i<enc->stream_out_length;++i)
+ enc->stream_out_buf[i - len] = enc->stream_out_buf[i];
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/attic/historic/anode/libspark/wrapper.h b/attic/historic/anode/libspark/wrapper.h
new file mode 100644
index 00000000..eb8c593d
--- /dev/null
+++ b/attic/historic/anode/libspark/wrapper.h
@@ -0,0 +1,66 @@
+/* libanode: the Anode C reference implementation
+ * Copyright (C) 2009 Adam Ierymenko <adam.ierymenko@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef _SPARK_WRAPPER_H
+#define _SPARK_WRAPPER_H
+
+#include <openssl/sha.h>
+#include "../libanode/aes128.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Spark uses SHA-256 with hash length 32 */
+#define SPARK_HASH_LENGTH 32
+
+// Wrap a segment for forward propagation
+static inline void Spark_wrap(void *data,unsigned long len,void *plaintext_hash_buf,void *global_hash_buf)
+{
+ unsigned char expkey[ANODE_AES128_EXP_KEY_SIZE];
+
+ SHA256((const unsigned char *)data,len,(unsigned char *)plaintext_hash_buf);
+
+ Anode_aes128_expand_key(expkey,(const unsigned char *)plaintext_hash_buf);
+ Anode_aes128_cfb_encrypt(expkey,((const unsigned char *)plaintext_hash_buf) + 16,(unsigned char *)data,len);
+
+ SHA256((const unsigned char *)data,len,(unsigned char *)global_hash_buf);
+}
+
+// Unwrap a segment and check its integrity
+static inline int Spark_unwrap(void *data,unsigned long len,const void *plaintext_hash)
+{
+ unsigned char expkey[ANODE_AES128_EXP_KEY_SIZE];
+ unsigned char check_hash[32];
+ unsigned long i;
+
+ Anode_aes128_expand_key(expkey,(const unsigned char *)plaintext_hash);
+ Anode_aes128_cfb_decrypt(expkey,((const unsigned char *)plaintext_hash) + 16,(unsigned char *)data,len);
+
+ SHA256((const unsigned char *)data,len,check_hash);
+
+ for(i=0;i<32;++i) {
+ if (check_hash[i] != ((const unsigned char *)plaintext_hash)[i])
+ return 0;
+ }
+ return 1;
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif