wip
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

implement SipHash

autumn 82cc8256 37b36c2b

+181 -47
+1
crone/core/core.c
··· 7 7 8 8 #include "ptr_list.h" 9 9 #include "list.h" 10 + #include "hash.h" 10 11
+114
crone/core/hash.h
··· 1 + 2 + /* SipHash magic numbers */ 3 + const uint64_t magic_number_0 = 0x736f6d6570736575; 4 + const uint64_t magic_number_1 = 0x646f72616e646f6d; 5 + const uint64_t magic_number_2 = 0x6c7967656e657261; 6 + const uint64_t magic_number_3 = 0x7465646279746573; 7 + 8 + /* insecurely stored keys bc nobody's trying to hashdos my interpreter */ 9 + const uint64_t key_0 = 0x0706050403020100;//0xdeadbeefdeadbeef; 10 + const uint64_t key_1 = 0x0f0e0d0c0b0a0908;//acdc1234f0069420; 11 + 12 + const uint64_t init_v0 = key_0 ^ magic_number_0; 13 + const uint64_t init_v1 = key_1 ^ magic_number_1; 14 + const uint64_t init_v2 = key_0 ^ magic_number_2; 15 + const uint64_t init_v3 = key_1 ^ magic_number_3; 16 + 17 + const uint32_t compress_rounds = 2; 18 + const uint32_t finalize_rounds = 4; 19 + 20 + #define ROTL(x, r) ((x << r) | (x >> (64 - r))) 21 + #define ROTR(x, r) ((x >> r) | (x << (64 - r))) 22 + 23 + hash calculate_hash(string s) { 24 + uint8_t b = s.length % 256; 25 + uint64_t v0 = init_v0; 26 + uint64_t v1 = init_v1; 27 + uint64_t v2 = init_v2; 28 + uint64_t v3 = init_v3; 29 + 30 + fprintf(stderr, " %lx %lx %lx %lx \n", v0, v1, v2, v3); 31 + 32 + size_t position = 0; 33 + while (s.length - position > 7) { 34 + uint64_t m = 35 + ((uint64_t)s.data[position]) | 36 + ((uint64_t)s.data[position+1] << 8) | 37 + ((uint64_t)s.data[position+2] << 16) | 38 + ((uint64_t)s.data[position+3] << 24) | 39 + ((uint64_t)s.data[position+4] << 32) | 40 + ((uint64_t)s.data[position+5] << 40) | 41 + ((uint64_t)s.data[position+6] << 48) | 42 + ((uint64_t)s.data[position+7] << 56); 43 + 44 + v3 ^= m; 45 + fprintf(stderr, " %lx %lx %lx %lx \n", v0, v1, v2, v3); 46 + 47 + for (int round = 0; round < compress_rounds; ++round) { 48 + // SipRound, matching formatting of specification 49 + v0 += v1; v2 += v3; 50 + v1 = ROTL(v1, 13); v3 = ROTL(v3, 16); 51 + v1 ^= v0; v3 ^= v2; 52 + v0 = ROTL(v0, 32); 53 + v2 += v1; v0 += v3; 54 + v1 = ROTL(v1, 17); v3 = ROTL(v3, 21); 55 + v1 ^= v2; v3 ^= v0; 56 + v2 = ROTL(v2, 32); 57 + } 58 + fprintf(stderr, " %lx %lx %lx %lx \n", v0, v1, v2, v3); 59 + 60 + v0 ^= m; 61 + 62 + fprintf(stderr, " %lx %lx %lx %lx \n", v0, v1, v2, v3); 63 + 64 + position += 8; 65 + } 66 + 67 + uint64_t m = ((uint64_t)b << 56); 68 + size_t byte = 0; 69 + while (s.length - position > byte) { 70 + m |= ((uint64_t)s.data[position+byte] << (8*byte)); 71 + ++byte; 72 + } 73 + 74 + v3 ^= m; 75 + 76 + for (int round = 0; round < compress_rounds; ++round) { 77 + // SipRound, matching formatting of specification 78 + v0 += v1; v2 += v3; 79 + v1 = ROTL(v1, 13); v3 = ROTL(v3, 16); 80 + v1 ^= v0; v3 ^= v2; 81 + v0 = ROTL(v0, 32); 82 + v2 += v1; v0 += v3; 83 + v1 = ROTL(v1, 17); v3 = ROTL(v3, 21); 84 + v1 ^= v2; v3 ^= v0; 85 + v2 = ROTL(v2, 32); 86 + } 87 + 88 + v0 ^= m; 89 + 90 + v2 ^= (uint64_t)0xff; 91 + 92 + fprintf(stderr, " %lx %lx %lx %lx \n", v0, v1, v2, v3); 93 + 94 + for (int round = 0; round < finalize_rounds; ++round) { 95 + // SipRound, matching formatting of specification 96 + v0 += v1; v2 += v3; 97 + v1 = ROTL(v1, 13); v3 = ROTL(v3, 16); 98 + v1 ^= v0; v3 ^= v2; 99 + v0 = ROTL(v0, 32); 100 + v2 += v1; v0 += v3; 101 + v1 = ROTL(v1, 17); v3 = ROTL(v3, 21); 102 + v1 ^= v2; v3 ^= v0; 103 + v2 = ROTL(v2, 32); 104 + } 105 + 106 + fprintf(stderr, " %lx %lx %lx %lx \n", v0, v1, v2, v3); 107 + 108 + fprintf(stderr, " %lx \n", v0 ^ v1 ^ v2 ^ v3); 109 + 110 + return v0 ^ v1 ^ v2 ^ v3; 111 + } 112 + 113 + // TODO maybe-more-performant version ingesting one u8 at a time 114 +
+4 -5
crone/interface/core.h
··· 1 1 2 + #include <stdint.h> 3 + 4 + #include "core/sane_string.h" 2 5 #include "core/ptr_list.h" 3 6 #include "core/list.h" 4 7 #include "core/dev.h" 5 - 6 - typedef struct string { 7 - char *data; 8 - size_t length; 9 - } string; 8 + #include "core/hash.h" 10 9
+5
crone/interface/core/hash.h
··· 1 + 2 + typedef uint64_t hash; 3 + 4 + hash calculate_hash(string s); 5 +
+6
crone/interface/core/sane_string.h
··· 1 + 2 + typedef struct string { 3 + uint8_t *data; 4 + size_t length; 5 + } string; 6 +
+17 -38
crone/lang/interpreter.c
··· 7 7 #include <core.h> 8 8 9 9 #define STATES(OP) \ 10 - OP(INVALID) \ 11 10 OP(OUTER_SPACE) \ 12 11 OP(TERM) \ 12 + OP(PUNCTUATION) \ 13 13 OP(INNER_SPACE) \ 14 - OP(PUNCTUATION) \ 15 14 OP(BLOCK) \ 15 + OP(EXIT_BLOCK) \ 16 + OP(CUSTOM) \ 16 17 17 18 #define COMMA_SEPARATED(VALUE) VALUE, 18 19 #define COMMA_SEPARATED_STRINGS(VALUE) #VALUE, ··· 25 26 STATES(COMMA_SEPARATED_STRINGS) 26 27 }; 27 28 28 - void print_substring(char *start, size_t length) { 29 + void print_substring(uint8_t *start, size_t length) { 29 30 for (size_t i = 0; i < length; ++i) { 30 31 if (start[i] == '\n') { 31 32 fputc('\\', stderr); ··· 37 38 } 38 39 39 40 typedef struct parse { 40 - char *start; 41 + uint8_t *start; 41 42 size_t length; 42 - parser_state parsed_as; 43 + void *custom_data; 43 44 list subparses; 45 + parser_state parsed_as; 44 46 } parse; 45 47 46 48 const char *spaces = " "; ··· 60 62 fprintf(stderr, "]\n"); 61 63 } 62 64 63 - void transition(list *l, parse *p, char *end_position, parser_state next_state) { 65 + void transition(list *l, parse *p, uint8_t *end_position, parser_state next_state) { 64 66 p->length = (size_t)end_position - (size_t)(p->start); 65 67 list_append(l,p); 66 68 p->start = end_position; ··· 87 89 switch (current_parse.parsed_as) { 88 90 case OUTER_SPACE: 89 91 if (_char == '}') { 90 - NEXT(INVALID); 92 + NEXT(EXIT_BLOCK); 91 93 } else if (_char == '{') { 92 94 NEXT(BLOCK); 93 95 } else if (_char == ';') { ··· 102 104 } else if (_char == ';') { 103 105 NEXT(PUNCTUATION); 104 106 } else if (_char == '}') { 105 - NEXT(INVALID); 107 + NEXT(EXIT_BLOCK); 106 108 } else if (!isspace(_char)) { 107 109 NEXT(TERM); 108 110 } ··· 111 113 if (isspace(_char)) { 112 114 NEXT(INNER_SPACE); 113 115 } else if (_char == '}') { 114 - NEXT(INVALID); 116 + NEXT(EXIT_BLOCK); 115 117 } else if (_char == '{') { 116 118 NEXT(BLOCK); 117 119 } else if (_char == ';') { ··· 127 129 if (isspace(_char)) { 128 130 NEXT(OUTER_SPACE); 129 131 } else if (_char == '}') { 130 - NEXT(INVALID); 132 + NEXT(EXIT_BLOCK); 131 133 } else if (_char == '{') { 132 134 NEXT(BLOCK); 133 135 } else if (_char != ';') { 134 136 NEXT(TERM); 135 137 } 136 138 break; 137 - case INVALID: 139 + case EXIT_BLOCK: 138 140 return parses; 141 + break; 142 + case CUSTOM: 143 + CRASH("CUSTOM parser state encountered in non-custom context"); 139 144 break; 140 145 } 141 146 } ··· 180 185 } 181 186 182 187 cleanup_parses(parses); 183 - } 184 188 185 - /* 186 - 187 - # general syntax: 188 - 189 - term prelude { 190 - block 189 + fprintf(stderr, "parse size = %lu\n", sizeof(parse)); 191 190 } 192 191 193 - or 194 - 195 - term prelude; 196 - 197 - # example: 198 - 199 - ## code 200 - exposed void ptrs_cleanup(ptr_list list) { 201 - free(list.data); 202 - } 203 - 204 - ## parse 205 - term="exposed" 206 - prelude="void ptrs_cleanup(ptr_list list)" 207 - block="free(list.data);" 208 - 209 - 210 - */ 211 - 212 -
+18 -2
crone/lang/parseme.cr
··· 1 1 2 - term0 term1 term2 term3 { 2 + term0 term1 term2 term3 { 3 3 block block 4 4 5 5 block { ··· 24 24 25 25 hex FF0000; 26 26 27 - no reason you {can't} keep writing after {a block}; 27 + should i [add] alternate (forms) of "differentiated" {blocks} <and> punctuation? 28 + 29 + { 30 + 31 + no reason you {can't} keep writing after {a block}; 32 + 33 + #=================================================# 34 + 35 + but ideally the { 36 + linter 37 + } 38 + 39 + would format it { 40 + like this 41 + } 42 + 43 + } 28 44 29 45 extraneous punctuation;;; 30 46
+16 -2
program.c
··· 10 10 11 11 #include <crone.h> 12 12 13 + const uint8_t test_str[15] = { 14 + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 15 + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E 16 + }; 17 + 13 18 int main() { 19 + 20 + string s = { 21 + .data = (uint8_t*)test_str, 22 + .length = 15 23 + }; 24 + 25 + hash h = calculate_hash(s); 26 + 27 + /* parser 14 28 //int fileDesc = open("./crone/core/core.cr", O_RDONLY, 0); 15 29 int fileDesc = open("./crone/lang/parseme.cr", O_RDONLY, 0); 16 30 ··· 43 57 if (result == -1) { 44 58 CRASH("bad munmap"); 45 59 } 60 + */ 46 61 47 62 48 - 49 - /* 63 + /* graphics 50 64 window_handle window = window_getWindow(); 51 65 52 66 vulkan_handle vk = vulkan_init();