def raw_serialize(self): # initialize what we'll send back result = b'' # go through each command for command in self.commands: # if the command is an integer, it's an op code if type(command) == int: # turn the command into a single byte integer using int_to_byte result += int_to_byte(command) else: # otherwise, this is an element # get the length in bytes length = len(command) # for large lengths, we have to use a pushdata op code if length < 75: # turn the length into a single byte integer result += int_to_byte(length) elif length > 75 and length < 0x100: # 76 is pushdata1 result += int_to_byte(76) result += int_to_byte(length) elif length >= 0x100 and length <= 520: # 77 is pushdata2 result += int_to_byte(77) result += int_to_little_endian(length, 2) else: raise ValueError('too long a command') result += command return result
def raw_serialize(self) -> bytes: # initialize what we'll send back result = b'' # go through each command for current in self: if current == OP_0: result += int_to_byte(0) elif OP_CODE_NAMES.get(current) is None: # this is an element # get the length in bytes length = len(current) # for large lengths, we have to use a pushdata op code if length < 75: # turn the length into a single byte integer result += int_to_byte(length) elif length > 75 and length < 0x100: # 76 is pushdata1 result += OP_PUSHDATA1 result += int_to_byte(length) elif length >= 0x100 and length <= 520: # 77 is pushdata2 result += OP_PUSHDATA2 result += int_to_little_endian(length, 2) else: raise ValueError('too long a command') result += current return result
def get_sig_legacy(self, input_index, private_key, redeem_script=None): # get the sig hash (z) z = self.sig_hash(input_index, redeem_script=redeem_script) # get der signature of z from private key der = private_key.sign(z).der() # append the SIGHASH_ALL with int_to_byte(SIGHASH_ALL) return der + int_to_byte(SIGHASH_ALL)
def _serialize(self, version): raw = version raw += int_to_byte(self.depth) raw += self.parent_fingerprint raw += int_to_big_endian(self.child_number, 4) raw += self.chain_code raw += self.point.sec() return raw
def _prv(self, version): raw = version raw += int_to_byte(self.depth) raw += self.parent_fingerprint raw += int_to_big_endian(self.child_number, 4) raw += self.chain_code raw += int_to_big_endian(self.private_key.secret, 33) return encode_base58_checksum(raw)
def _serialize(self, version): # start with the version raw = version # add the depth using int_to_byte raw += int_to_byte(self.depth) # add the parent_fingerprint raw += self.parent_fingerprint # add the child number in 4 bytes using int_to_big_endian raw += int_to_big_endian(self.child_number, 4) # add the chain code raw += self.chain_code # add the SEC pubkey raw += self.point.sec() return raw
def filterload(self, flag=1): '''Return a network message whose command is filterload''' # encode_varint self.size payload = encode_varint(self.size) # next is the self.filter_bytes() payload += self.filter_bytes() # function count is 4 bytes little endian payload += int_to_little_endian(self.function_count, 4) # tweak is 4 bytes little endian payload += int_to_little_endian(self.tweak, 4) # flag is 1 byte little endian payload += int_to_byte(flag) # return a GenericMessage with b'filterload' as the command return GenericMessage(b'filterload', payload)
def raw_serialize(self, version): # version + depth + parent_fingerprint + child number + chain code + private key # start with version, which should be a constant depending on testnet raw = version # add depth, which is 1 byte using int_to_byte raw += int_to_byte(self.depth) # add the parent_fingerprint raw += self.parent_fingerprint # add the child number 4 bytes using int_to_big_endian raw += int_to_big_endian(self.child_number, 4) # add the chain code raw += self.chain_code # add the 0 byte and the private key's secret in big endian, 33 bytes raw += int_to_big_endian(self.private_key.secret, 33) return raw
def _prv(self, version): '''Returns the base58-encoded x/y/z prv. Expects a 4-byte version.''' # version + depth + parent_fingerprint + child number + chain code + private key # start with version, which should be a constant depending on testnet raw = version # add depth, which is 1 byte using int_to_byte raw += int_to_byte(self.depth) # add the parent_fingerprint raw += self.parent_fingerprint # add the child number 4 bytes using int_to_big_endian raw += int_to_big_endian(self.child_number, 4) # add the chain code raw += self.chain_code # add the 0 byte and the private key's secret in big endian, 33 bytes raw += int_to_big_endian(self.private_key.secret, 33) # return the whole thing base58-encoded return encode_base58_checksum(raw)
# rest is pure calculation H_0 = sha256_helper.calcH(helper.byte_to_int_array(T1[0]), E_0, F_0, G_0, 0x428a2f98L, helper.byte_to_int_array(message_0[0])) if (silent != 1): print "H_0=", hex(H_0) H_1 = fixedint.UInt32(G_0) F_1 = fixedint.UInt32(E_0) G_1 = fixedint.UInt32(F_0) T1_1 = np.zeros([len(T1), 4], dtype=np.uint8) for i in range(0, len(T1)): E_tmp = fixedint.UInt32(helper.byte_to_int_array(E1[i])) S0E1 = fixedint.UInt32(helper.byte_to_int_array(Sigma1_E1[i])) const = fixedint.UInt32(0x71374491L) mes = fixedint.UInt32(helper.byte_to_int_array(message_1[i])) value = H_1 + S0E1 + sha256_helper.__Ch(E_tmp, F_1, G_1) + const + mes T1_1[i] = helper.int_to_byte(value) ''' * DPA 9 * C_0 equals D_1 * E_2 = D_1 +T1_2 ''' if (silent != 1): print 'DPA9: Recovering C_0=D_1 from E_2 = D_1 +T1_2...' C_0_0, j = dpa.dpa_addition(T[:, current_sample:], 0, T1_1, 3, -1, threshold) current_sample += j C_0_1, j = dpa.dpa_addition(T[:, current_sample:], 0, T1_1, 2, C_0_0, threshold) current_sample += j C_0_2, j = dpa.dpa_addition(T[:, current_sample:], 0, T1_1, 1, C_0_1, threshold) current_sample += j C_0_3, j = dpa.dpa_addition(T[:, current_sample:], 0, T1_1, 0, C_0_2,
def Sigma1(w): value = fixedint.UInt32(helper.byte_to_int_array(w)) result = __Sigma1(value) return helper.int_to_byte(result)
def Ch(x, y, z): x = fixedint.UInt32(x) y = fixedint.UInt32(y) z = fixedint.UInt32(z) result = __Ch(x, y, z) return helper.int_to_byte(result)