| ... | ... | @@ -1663,3 +1663,30 @@ test "field element constants and subOne" { |
| 1663 | 1663 | try testing.expectEqual(4, try four.toPrimitive(u8)); |
| 1664 | 1664 | } |
| 1665 | 1665 | } |
| 1666 | |
| 1667 | test "even modulus arithmetic" { |
| 1668 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; |
| 1669 | |
| 1670 | const M = Modulus(256); |
| 1671 | const opts: M.Options = .{ .allow_even = true }; |
| 1672 | try testing.expectError(error.ModulusTooSmall, M.fromPrimitiveWithOptions(u8, 0, opts)); |
| 1673 | |
| 1674 | const m2 = try M.fromPrimitiveWithOptions(u8, 2, opts); |
| 1675 | var one = m2.one(); |
| 1676 | try testing.expect(m2.isOne(m2.mul(one, one))); |
| 1677 | try testing.expectError(error.UnexpectedRepresentation, m2.toMontgomery(&one)); |
| 1678 | one.montgomery = true; |
| 1679 | try testing.expectError(error.UnexpectedRepresentation, m2.fromMontgomery(&one)); |
| 1680 | |
| 1681 | const m = try M.fromPrimitiveWithOptions(u256, ((1 << 127) - 1) * 2, opts); |
| 1682 | const x = try M.Fe.fromPrimitive(u256, m, (1 << 126) + 12345); |
| 1683 | const e = [_]u8{ 0x0f, 0xff, 0xff }; |
| 1684 | const expected: u256 = 13725650424532690062485077506887663427; |
| 1685 | try testing.expectEqual(expected, try (try m.powWithEncodedPublicExponent(x, &e, .big)).toPrimitive(u256)); |
| 1686 | try testing.expectEqual(expected, try (try m.powWithEncodedExponent(x, &e, .big)).toPrimitive(u256)); |
| 1687 | |
| 1688 | var cap = maxUintBytes(Uint(256)); |
| 1689 | cap[cap.len - 1] = 0xfe; |
| 1690 | const mc = try M.fromBytesWithOptions(&cap, .big, opts); |
| 1691 | try testing.expect(mc.isOne(mc.sq(mc.subOne(mc.zero)))); |
| 1692 | } |