Class: IOTA::Crypto::CCurl

Inherits:
Object
  • Object
show all
Defined in:
lib/iota/crypto/curl_c.rb,
ext/ccurl/ccurl.c

Constant Summary collapse

NUMBER_OF_ROUNDS =
81
HASH_LENGTH =
243
STATE_LENGTH =
3 * HASH_LENGTH

Instance Method Summary collapse

Constructor Details

#initialize(rounds) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'ext/ccurl/ccurl.c', line 26

static VALUE ccurl_init(VALUE self, VALUE rounds) {
  Curl *ctx;
  int requested = NUMBER_OF_ROUNDS;

  if (TYPE(rounds) != T_NIL) {
    requested =  NUM2INT(rounds);
  }

  Data_Get_Struct(self, Curl, ctx);

  ctx->rounds = requested;
  memset(ctx->state, (trit_t)0, STATE_LENGTH * sizeof(trit_t));

  return self;
}

Instance Method Details

#absorb(data) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/ccurl/ccurl.c', line 42

static VALUE ccurl_absorb(VALUE self, VALUE data) {
  trit_t *trits;
  int offset = 0;
  int i;
  int length = NUM2INT(rb_funcall(data, rb_intern("length"), 0, 0));

  Curl *ctx;
  Data_Get_Struct(self, Curl, ctx);

  trits = (trit_t*)malloc(length * sizeof(trit_t));

  for (i = 0; i < length; ++i) {
    trits[i] = (trit_t)(NUM2LONG(rb_ary_entry(data, i)));
  }

  do {
    memcpy(ctx->state, trits + offset, (length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(trit_t));
    ccurl_transform(self);
    offset += HASH_LENGTH;
  } while ((length -= HASH_LENGTH) > 0);

  free(trits);

  return Qnil;
}

#resetObject


108
109
110
111
112
113
# File 'ext/ccurl/ccurl.c', line 108

static VALUE ccurl_reset(VALUE self) {
  Curl *ctx;
  Data_Get_Struct(self, Curl, ctx);
  memset(ctx->state, 0, STATE_LENGTH * sizeof(char));
  return Qnil;
}

#squeeze(data) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'ext/ccurl/ccurl.c', line 68

static VALUE ccurl_squeeze(VALUE self, VALUE data) {
  int length = NUM2INT(rb_funcall(data, rb_intern("length"), 0, 0));
  int i;

  Curl *ctx;
  Data_Get_Struct(self, Curl, ctx);

  for(; length < HASH_LENGTH; length++) {
    rb_ary_push(data, LONG2NUM(0));
  }

  for (i = 0; i < HASH_LENGTH; i++) {
    rb_ary_store(data, i, LONG2NUM(ctx->state[i]));
  }

  ccurl_transform(self);

  return Qnil;
}

#transformObject


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/ccurl/ccurl.c', line 88

static VALUE ccurl_transform(VALUE self) {
  trit_t scratchpad[STATE_LENGTH];
  int round, scratchpadIndex=0, scratchpadIndexSave, stateIndex;

  Curl *ctx;
  Data_Get_Struct(self, Curl, ctx);

  for (round = 0; round < NUMBER_OF_ROUNDS; round++) {
    memcpy(scratchpad, ctx->state, STATE_LENGTH * sizeof(trit_t));

    for (stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
      scratchpadIndexSave = scratchpadIndex;
      scratchpadIndex += (scratchpadIndex < 365 ? 364 : -365);
      ctx->state[stateIndex] = TRUTH_TABLE[scratchpad[scratchpadIndexSave ] + scratchpad[scratchpadIndex ] * 3 + 4];
    }
  }

  return Qnil;
}

#versionObject


8
9
10
# File 'lib/iota/crypto/curl_c.rb', line 8

def version
  "C"
end