Fixed error with base point

This commit is contained in:
2026-03-03 20:13:24 -05:00
parent a199c8ffaa
commit 4938953741

View File

@@ -1,7 +1,7 @@
const std = @import("std"); const std = @import("std");
const p: u256 = (1 << 255) - 19; const p: u256 = (1 << 255) - 19;
const Bx: u256 = 15112221349535807912866137220509078750507884956996801852099526895779190960831; const Bx: u256 = 15112221349535400772501151409588531511454012693041857206046113283949847762202;
const By: u256 = 46316835694926478169428394003475163141307993866256225615783033011972563869189; const By: u256 = 46316835694926478169428394003475163141307993866256225615783033603165251855960;
const d: u256 = 37095705934669439343138083508754565189542113879843219016388785533085940283555; const d: u256 = 37095705934669439343138083508754565189542113879843219016388785533085940283555;
const Point = struct { const Point = struct {
x: u256, x: u256,
@@ -11,24 +11,28 @@ const Point = struct {
}; };
const ident = Point{ .x = 0, .y = 1, .z = 1, .t = 0 }; const ident = Point{ .x = 0, .y = 1, .z = 1, .t = 0 };
pub fn main() !void { pub fn main() !void {
var seed: [32]u8 = undefined;
std.crypto.random.bytes(&seed);
var hash = std.crypto.hash.sha2.Sha512.init(.{});
hash.update(&seed);
const digest = hash.finalResult();
var scalar = digest[0..32].*;
scalar[0] &= 0b11111100;
scalar[31] &= 0b01111111;
scalar[31] |= 0b01000000;
const int_num: u256 = std.mem.readInt(u256, &scalar, .little);
const B = Point{ .x = Bx, .y = By, .z = 1, .t = mul(Bx, By) };
const product = scalar_mult(B, int_num);
const final = compress(product);
var gpa: std.heap.DebugAllocator(.{}) = .init; var gpa: std.heap.DebugAllocator(.{}) = .init;
const alloc = gpa.allocator(); const alloc = gpa.allocator();
defer { defer {
_ = gpa.deinit(); _ = gpa.deinit();
} }
var seed: [32]u8 = undefined;
std.crypto.random.bytes(&seed);
var digest: [64]u8 = undefined;
std.crypto.hash.sha2.Sha512.hash(&seed, &digest, .{});
var scalar = digest[0..32].*;
scalar[0] &= 0b11111000;
scalar[31] &= 0b01111111;
scalar[31] |= 0b01000000;
const int_num: u256 = std.mem.readInt(u256, &scalar, .little);
const B = Point{ .x = Bx, .y = By, .z = 1, .t = mul(Bx, By) };
const product = scalar_mult(B, int_num);
const final = compress(product);
var args_it = try std.process.argsWithAllocator(alloc); var args_it = try std.process.argsWithAllocator(alloc);
defer args_it.deinit(); defer args_it.deinit();
_ = args_it.next(); _ = args_it.next();
@@ -37,9 +41,14 @@ pub fn main() !void {
const priv_pem = try encodePrivateKey(alloc, seed, final, user); const priv_pem = try encodePrivateKey(alloc, seed, final, user);
defer alloc.free(pub_pem); defer alloc.free(pub_pem);
defer alloc.free(priv_pem); defer alloc.free(priv_pem);
std.debug.print("{s}", .{priv_pem}); std.debug.print("{s}", .{priv_pem});
std.debug.print("{s}", .{pub_pem}); std.debug.print("{s}", .{pub_pem});
std.debug.print("{s}\n", .{std.fmt.bytesToHex(seed, .lower)});
std.debug.print("{s}\n", .{std.fmt.bytesToHex(final, .lower)});
} }
fn scalar_mult(pon: Point, scalar: u256) Point { fn scalar_mult(pon: Point, scalar: u256) Point {
var result = ident; var result = ident;
var current = pon; var current = pon;
@@ -100,6 +109,7 @@ fn compress(point: Point) [32]u8 {
var bytes: [32]u8 = undefined; var bytes: [32]u8 = undefined;
std.mem.writeInt(u256, &bytes, y, .little); std.mem.writeInt(u256, &bytes, y, .little);
bytes[31] &= 0x7f;
bytes[31] |= @as(u8, @intCast(x & 1)) << 7; bytes[31] |= @as(u8, @intCast(x & 1)) << 7;
return bytes; return bytes;