Coverage for src\pqlattice\integer\_modintring.py: 94%

1 from typing import overload

2

3 from ..typing import Array

4 from ._modring import cmodl, cmodr, mod, modinv, modpow

5

6

7 class ModIntRing:

8 def __init__(self, modulus: int):

9 """_summary_

10

11 Parameters

12 ----------

13 modulus : int

14 _description_

15

16 Raises

17 ------

18 ValueError

19 _description_

20 """

21 if abs(modulus) < 2:

22 raise ValueError(f"absolute value of modulus has to greater than one, given modulus: {modulus}")

23

24 self._modulus = abs(modulus)

25

26 @property

27 def q(self) -> int:

28 """_summary_

29

30 Returns

31 -------

32 _type_

33 _description_

34 """

35 return self._modulus

36

37 def is_zero(self, a: int) -> bool:

38 """_summary_

39

40 Parameters

41 ----------

42 a : int

43 _description_

44

45 Returns

46 -------

47 bool

48 _description_

49 """

50 return self.mod(a) == 0

51

52 @overload

53 def mod(self, a: int) -> int: ...

54

55 @overload

56 def mod(self, a: Array) -> Array: ...

57

58 def mod(self, a: int | Array) -> int | Array:

59 """_summary_

60

61 Parameters

62 ----------

63 a : int | Array

64 _description_

65

66 Returns

67 -------

68 int | Array

69 _description_

70 """

71 return mod(a, self.q)

72

73 @overload

74 def pow(self, a: int, r: int) -> int: ...

75

76 @overload

77 def pow(self, a: Array, r: int) -> Array: ...

78

79 def pow(self, a: int | Array, r: int) -> int | Array:

80 """_summary_

81

82 Parameters

83 ----------

84 a : int | Array

85 _description_

86 r : int

87 _description_

88

89 Returns

90 -------

91 int | Array

92 _description_

93 """

94 return self.mod(modpow(a, r, self.q))

95

96 @overload

97 def inv(self, a: int) -> int: ...

98

99 @overload

100 def inv(self, a: Array) -> Array: ...

101

102 def inv(self, a: int | Array) -> int | Array:

103 """_summary_

104

105 Parameters

106 ----------

107 a : int | Array

108 _description_

109

110 Returns

111 -------

112 int | Array

113 _description_

114 """

115 return self.mod(modinv(a, self.q))

116

117 @overload

118 def neg(self, a: int) -> int: ...

119

120 @overload

121 def neg(self, a: Array) -> Array: ...

122

123 def neg(self, a: int | Array) -> int | Array:

124 """_summary_

125

126 Parameters

127 ----------

128 a : int | Array

129 _description_

130

131 Returns

132 -------

133 int | Array

134 _description_

135 """

136 return self.mod(-a)

137

138 def add(self, a: int, b: int) -> int:

139 """_summary_

140

141 Parameters

142 ----------

143 a : int

144 _description_

145 b : int

146 _description_

147

148 Returns

149 -------

150 int

151 _description_

152 """

153 return self.mod(a + b)

154

155 def mul(self, a: int, b: int) -> int:

156 """_summary_

157

158 Parameters

159 ----------

160 a : int

161 _description_

162 b : int

163 _description_

164

165 Returns

166 -------

167 int

168 _description_

169 """

170 return self.mod(a * b)

171

172 def div(self, a: int, b: int) -> int:

173 """_summary_

174

175 Parameters

176 ----------

177 a : int

178 _description_

179 b : int

180 _description_

181

182 Returns

183 -------

184 int

185 _description_

186 """

187 return self.mul(a, self.inv(b))

188

189 def sub(self, a: int, b: int) -> int:

190 """_summary_

191

192 Parameters

193 ----------

194 a : int

195 _description_

196 b : int

197 _description_

198

199 Returns

200 -------

201 int

202 _description_

203 """

204 return self.mod(a - b)

205

206 @overload

207 def cmodl(self, a: int) -> int: ...

208

209 @overload

210 def cmodl(self, a: Array) -> Array: ...

211

212 def cmodl(self, a: int | Array) -> int | Array:

213 """_summary_

214

215 Parameters

216 ----------

217 a : int | Array

218 _description_

219

220 Returns

221 -------

222 int | Array

223 _description_

224 """

225 return cmodl(a, self.q)

226

227 @overload

228 def cmodr(self, a: int) -> int: ...

229

230 @overload

231 def cmodr(self, a: Array) -> Array: ...

232

233 def cmodr(self, a: int | Array) -> int | Array:

234 """_summary_

235

236 Parameters

237 ----------

238 a : int | Array

239 _description_

240

241 Returns

242 -------

243 int | Array

244 _description_

245 """

246 return cmodr(a, self.q)