authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-09-07 21:07:26+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-10-25 08:38:01+01:00
logbb3fd332249e312b68f8839e98e386aa70980742
tree2094b0b0fb2b8a6472becc3eb7267f773f6eeadf
parent00795888e851bd3c082a16cae8507d7cad7cc4ba

crypto.ff: add some tests for even moduli


1 files changed, 27 insertions(+), 0 deletions(-)

lib/std/crypto/ff.zig+27
...@@ -1663,3 +1663,30 @@ test "field element constants and subOne" {...@@ -1663,3 +1663,30 @@ test "field element constants and subOne" {
1663 try testing.expectEqual(4, try four.toPrimitive(u8));1663 try testing.expectEqual(4, try four.toPrimitive(u8));
1664 }1664 }
1665}1665}
1666
1667test "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}