| ... | ... | @@ -472,6 +472,21 @@ fn Fe_(comptime bits: comptime_int) type { |
| 472 | 472 | return fe; |
| 473 | 473 | } |
| 474 | 474 | |
| 475 | /// Decodes and reduces a byte string whose value is below 2^m.bits(). |
| 476 | pub fn fromBytesReduced(m: Modulus(bits), bytes: []const u8, comptime endian: Endian) OverflowError!Self { |
| 477 | var fe = Self{ .v = FeUint.zero }; |
| 478 | const m_limbs_count = m.limbs_count(); |
| 479 | const fe_limbs = fe.v.limbs()[0..m_limbs_count]; |
| 480 | var acc = decodeBytes(fe_limbs, bytes, endian); |
| 481 | |
| 482 | acc |= math.shr(Limb, fe_limbs[m_limbs_count - 1], t_bits - m.leading); |
| 483 | if (!ct.eql(acc, 0)) return error.Overflow; |
| 484 | fe.v.limbs_len = m_limbs_count; |
| 485 | |
| 486 | _ = fe.v.conditionalSubWithOverflow(ct.limbsCmpGeq(fe.v, m.v), m.v); |
| 487 | return fe; |
| 488 | } |
| 489 | |
| 475 | 490 | /// Converts the field element to a byte string. |
| 476 | 491 | /// Returns an error if the element is in Montgomery form. |
| 477 | 492 | pub fn toBytes(self: Self, bytes: []u8, comptime endian: Endian) (OverflowError || RepresentationError)!void { |