Normalized() ensures that the coefficient b is always in the range -a < b <= a making subsequent computations, such as reductions or multiplications more straightforward by starting from a consistent state
()
| 226 | // making subsequent computations, such as reductions or multiplications more straightforward |
| 227 | // by starting from a consistent state |
| 228 | func (group *ClassGroup) Normalized() *ClassGroup { |
| 229 | a := bip.New().Set(group.a) |
| 230 | b := bip.New().Set(group.b) |
| 231 | c := bip.New().Set(group.c) |
| 232 | r1 := bip.New().Neg(a) |
| 233 | t := bip.New() |
| 234 | defer bip.Recycle(r1, t) |
| 235 | // if already normalized where -a < b <= a |
| 236 | if (b.Cmp(r1) == 1) && (b.Cmp(a) < 1) { |
| 237 | return group |
| 238 | } |
| 239 | |
| 240 | // r = floor((a - b) / (2 * a)) |
| 241 | r := floorDivision(r1.Sub(a, b), t.Mul(a, bigTwo)) |
| 242 | defer bip.Recycle(r) |
| 243 | // b = b + 2 * r * a |
| 244 | t.Mul(bigTwo, r) |
| 245 | t.Mul(t, a) |
| 246 | b.Add(b, t) |
| 247 | |
| 248 | // c = a * r^2 + b * r + c |
| 249 | t.Mul(a, r) |
| 250 | t.Mul(t, r) |
| 251 | r.Mul(group.b, r) |
| 252 | c.Add(c, t) |
| 253 | c.Add(c, r) |
| 254 | |
| 255 | return NewClassGroup(a, b, c) |
| 256 | } |
| 257 | |
| 258 | // Reduced() transforms a given class group element into a more compact and canonical representation of the group element |
| 259 | // reduction ensures that (a <= c) && (b >= 0 when a == c) |