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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'ext/ccurl/ccurl.c', line 30

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'ext/ccurl/ccurl.c', line 46

static VALUE ccurl_absorb(VALUE self, VALUE data) {
  trit_t *trits;
  int offset = 0;
  int i;
  int length = (int)RARRAY_LEN(data);

  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



116
117
118
119
120
121
# File 'ext/ccurl/ccurl.c', line 116

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/ccurl/ccurl.c', line 72

static VALUE ccurl_squeeze(VALUE self, VALUE data) {
  int offset = 0;
  int i;
  int length = (int)RARRAY_LEN(data);

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

  do {
    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);
    offset += HASH_LENGTH;
  } while ((length -= HASH_LENGTH) > 0);

  return Qnil;
}

#transformObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'ext/ccurl/ccurl.c', line 96

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



12
13
14
# File 'lib/iota/crypto/curl_c.rb', line 12

def version
  "C"
end