def bytes_to_ele(q, S): a = '' if (config.is_q_prime() and q % 2 == 1): # q为奇素数 a = 0 t = math.ceil(math.log(q, 2)) l = math.ceil(t / 8) a = bytes_to_int(S) if not (a >= 0 and a <= q - 1): print( "*** ERROR: 域元素须在区间[0, q-1]上 *** function:bytes_to_ele(q, S) ***" ) return -1 elif config.is_q_power_of_two(): # q为2的幂 m = math.ceil(math.log(q, 2)) a = padding_0_to_length(a, m) '''a = bytes_to_bits(S) temp = a a = '' for i in range(0, 2): a = a + temp[i] for i in range(0, m-len(temp)+2): a = a + '0' for i in range(0, len(temp)-2): a = a + temp[i+2]''' if not len(a) - 2 == m: print("*** ERROR: 域元素必须为长度为m的比特串 *** function:bytes_to_ele(q, S)") return -1 else: print("*** ERROR: q不满足奇素数或2的幂 *** function:bytes_to_ele(q, S) ***") return -1 return a
def ele_to_int(a): #print("--- 域元素到字节串的转换 ---") x = 0 q = config.get_q() if (config.is_q_prime() and q % 2 == 1): # q为奇素数 x = a elif config.is_q_power_of_two(): # q为2的幂 if type(a) == str and a[0:2] == '0b': m = math.log(q, 2) if len(a) - 2 == m: #a = a.replace('0b', '') a = remove_0b_at_beginning(a) for i in a: x = x * 2 + int(i) else: print( "*** ERROR: 域元素必须为长度为m的比特串 *** function:ele_to_int(a, q)") return -1 else: print("*** ERROR: 输入必须为比特串 *** function:ele_to_int(a, q) ***") return -1 else: print("*** ERROR: q不满足奇素数或2的幂 *** function:ele_to_int(a, q) ***") return -1 return x
def bytes_to_point(a, b, S): q = config.get_q() l = math.ceil(math.log(q, 2) / 8) PC = '' X = [] Y = [] # a. if len(S) == 2 * l + 1: #为压缩表示形式或者混合表示形式 PC = S[0] for i in range(1, l + 1): X.append(S[i]) for i in range(l + 1, 2 * l + 1): Y.append(S[i]) elif len(S) == l + 1: #压缩表示形式 PC = S[0] for i in range(1, l): X.append(S[i]) else: print('*** ERROR: wrong size function: bytes_to_point ***') # b. 将X转换成与元素x x = bytes_to_ele(q, X) ##### c. 压缩表示形式 ##### y1 = '' # c.1 and c.2 if PC == 2: y1 = '0' elif PC == 3: y1 = '1' ##### d. 未压缩表示形式 ##### elif PC == 4: y = bytes_to_ele(q, Y) ##### e. 混合表示形式 ##### # e.1 and e.2 elif PC == 6 or 7: y = bytes_to_ele(q, Y) else: print('ERROR in bytes_to_point') # f. result = 0 if (type(x) != type(1)): x = int(x, 2) if (type(y) != type(1)): y = int(y, 2) if (config.is_q_prime() and q % 2 == 1): # q为奇素数 if (y**2) % q != (x**3 + a * x + b) % q: return -1 elif config.is_q_power_of_two(): if (y**2 + x * y) != (x**3 + a * x + b): return -1 # g. point = Point(x, y) return point
def ele_to_bytes(a): #print("--- 域元素到字节串的转换 ---") S = [] q = config.get_q() if (config.is_q_prime() and q % 2 == 1): # q为奇素数 if (a >= 0 and a <= q - 1): t = math.ceil(math.log(q, 2)) l = math.ceil(t / 8) S = int_to_bytes(a, l) else: print( "*** ERROR: 域元素须在区间[0, q-1]上 *** function:ele_to_bytes(a) ***") return -1 elif config.is_q_power_of_two(): # q为2的幂 if type(a) == str and a[0:2] == '0b': m = math.ceil(math.log(q, 2)) a = padding_0_to_length(a, m) '''temp = a a = '' for i in range(0, 2): a = a + temp[i] for i in range(0, m-len(temp)+2): a = a + '0' for i in range(0, len(temp)-2): a = a + temp[i+2]''' if len(a) - 2 == m: S = bits_to_bytes(a) else: print("*** ERROR: 域元素必须为长度为m的比特串 *** function:ele_to_bytes(a)") return -1 else: print("*** ERROR: 输入必须为比特串 *** function:ele_to_bytes(a) ***") return -1 else: print("*** ERROR: q不满足奇素数或2的幂 *** function:ele_to_bytes(a) ***") return -1 return S