Skip to content

Commit

Permalink
go-iden3-crypto compatibility tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign committed Aug 26, 2023
1 parent 54d704a commit c76e62d
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/bn254/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,43 @@ const Fr = @import("fr.zig");
const parameters = @import("../parameters.zig");
const poseidon = @import("../poseidon.zig");

test "babyjubjub" {
test "go-iden3-crypto compatibility" {
var allocator = std.testing.allocator;
var babyjubjub_parameters = try parameters.get_babyjubjub_parameters(allocator);
defer babyjubjub_parameters.deinit();

var instance = poseidon.Poseidon(Fr, 2).init(babyjubjub_parameters);
const test_case = struct { v: []const u256, exp_hash: u256 };
const test_cases = [_]test_case{
.{ .v = &[_]u256{1}, .exp_hash = 18586133768512220936620570745912940619677854269274689475585506675881198879027 },
.{ .v = &[_]u256{ 1, 2 }, .exp_hash = 7853200120776062878684798364095072458815029376092732009249414926327459813530 },
.{ .v = &[_]u256{ 1, 2, 0, 0, 0 }, .exp_hash = 1018317224307729531995786483840663576608797660851238720571059489595066344487 },
.{ .v = &[_]u256{ 1, 2, 0, 0, 0, 0 }, .exp_hash = 15336558801450556532856248569924170992202208561737609669134139141992924267169 },
.{ .v = &[_]u256{ 3, 4, 0, 0, 0 }, .exp_hash = 5811595552068139067952687508729883632420015185677766880877743348592482390548 },
.{ .v = &[_]u256{ 3, 4, 0, 0, 0, 0 }, .exp_hash = 12263118664590987767234828103155242843640892839966517009184493198782366909018 },
.{ .v = &[_]u256{ 1, 2, 3, 4, 5, 6 }, .exp_hash = 20400040500897583745843009878988256314335038853985262692600694741116813247201 },
.{ .v = &[_]u256{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, .exp_hash = 8354478399926161176778659061636406690034081872658507739535256090879947077494 },
.{ .v = &[_]u256{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0 }, .exp_hash = 5540388656744764564518487011617040650780060800286365721923524861648744699539 },
.{ .v = &[_]u256{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 }, .exp_hash = 11882816200654282475720830292386643970958445617880627439994635298904836126497 },
.{ .v = &[_]u256{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, .exp_hash = 9989051620750914585850546081941653841776809718687451684622678807385399211877 },
};

var one: u256 = 1;
var buf: [32]u8 = undefined;
std.mem.writeIntLittle(u256, &buf, one);
var nonMontB1: Fr.NonMontgomeryDomainFieldElement = undefined;
Fr.fromBytes(&nonMontB1, buf);
var b1: Fr.MontgomeryDomainFieldElement = undefined;
Fr.toMontgomery(&b1, nonMontB1);
inline for (test_cases) |tc| {
var instance = poseidon.Poseidon(Fr, tc.v.len + 1).init(babyjubjub_parameters);
var buf: [32]u8 = undefined;

var res = instance.hash(.{b1});
var frs: [tc.v.len]Fr.NonMontgomeryDomainFieldElement = undefined;
for (tc.v, 0..) |v, i| {
std.mem.writeIntLittle(u256, &buf, v);
var nonMont: Fr.NonMontgomeryDomainFieldElement = undefined;
Fr.fromBytes(&nonMont, buf);
Fr.toMontgomery(&frs[i], nonMont);
}

Fr.toBytes(&buf, res);
const A = std.mem.readInt(u256, &buf, std.builtin.Endian.Little);
var hash = instance.hash(frs);

try std.testing.expect(18586133768512220936620570745912940619677854269274689475585506675881198879027 == A);
Fr.toBytes(&buf, hash);
const res = std.mem.readInt(u256, &buf, std.builtin.Endian.Little);

try std.testing.expect(tc.exp_hash == res);
}
}

0 comments on commit c76e62d

Please sign in to comment.