Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove DefaultCsprng #6731

Closed
wants to merge 1 commit into from
Closed

Remove DefaultCsprng #6731

wants to merge 1 commit into from

Conversation

jedisct1
Copy link
Contributor

Reserve the name for the forthcoming global CSPRNG instead.

Not a lot of changes required since we don't use this type.

Add some comments by the way, as well as a Rand interface to getRandom().

@kubkon kubkon added the standard library This issue involves writing Zig code for the standard library. label Oct 18, 2020
lib/std/rand.zig Outdated
pub const DefaultPrng = Xoroshiro128;

/// Cryptographically secure random numbers.
pub const DefaultCsprng = Gimli;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep this here and switch it over when the new one arrives.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface may be different, though.

}

fn fill(r: *Random, buf: []u8) void {
std.crypto.randomBytes(buf) catch @panic("System random number generator not available");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this makes me think std.crypto.randomBytes is badly location; perhaps should have its implementation moved here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having it in std.crypto also makes sense. Maybe we can reexport it.

@andrewrk
Copy link
Member

I'm on board for some sweeping breaking changes to reorganize this, but let's leave this until we have the new thing, so that users have a clear upgrade path.

@jedisct1
Copy link
Contributor Author

Sounds reasonable!

The removal of DefaultCsprng has been reverted, so the PR becomes adding a Rand interface to getRandom() and comments :)

Comment on lines +784 to +788
pub fn init() OsRandom {
return OsRandom{
.random = Random{ .fillFn = fill },
};
}
Copy link
Member

@andrewrk andrewrk Oct 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proposal: delete this function, and follow this pattern:

zig/lib/std/heap.zig

Lines 88 to 98 in 1e13e8e

pub const page_allocator = if (std.Target.current.isWasm())
&wasm_page_allocator_state
else if (std.Target.current.os.tag == .freestanding)
root.os.heap.page_allocator
else
&page_allocator_state;
var page_allocator_state = Allocator{
.allocFn = PageAllocator.alloc,
.resizeFn = PageAllocator.resize,
};

Combined with moving it to std.crypto, usage would look like this:

std.crypto.os_random.bytes(buf);

Which I think would be enough to justify deleting std.crypto.randomBytes. This would be a good time to plan what the namespace of the #6704 API would be. Perhaps std.crypto.random meaning that we could tell users to upgrade from std.crypto.randomBytes to std.crypto.random.bytes which is a pretty satisfying upgrade, especially considering that bytes could then be replaced with any of the other Random interface functions, and it gains the benefits of the thread-local CSPRNG infrastructure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants