Skip to content

Initial powdr acceleration #3

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

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 64 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 24 additions & 4 deletions k256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,33 @@ rust-version = "1.81"

[dependencies]
cfg-if = "1.0"
elliptic-curve = { version = "0.14.0-rc.0", default-features = false, features = ["sec1"] }
elliptic-curve = { version = "0.14.0-rc.0", default-features = false, features = [
"sec1",
] }

# optional dependencies
once_cell = { version = "1.20", optional = true, default-features = false }
ecdsa-core = { version = "=0.17.0-pre.9", package = "ecdsa", optional = true, default-features = false, features = ["der"] }
ecdsa-core = { version = "=0.17.0-pre.9", package = "ecdsa", optional = true, default-features = false, features = [
"der",
] }
hex-literal = { version = "0.4", optional = true }
serdect = { version = "0.3.0-rc.0", optional = true, default-features = false }
sha2 = { version = "=0.11.0-pre.4", optional = true, default-features = false }
signature = { version = "=2.3.0-pre.4", optional = true }

[target.'cfg(all(target_os = "zkvm", target_arch = "riscv32"))'.dependencies]
powdr-riscv-runtime = { git = "https://github.com/powdr-labs/powdr.git", tag = "v0.1.1", features = [
"std",
"getrandom",
"allow_fake_rand",
] }

[dev-dependencies]
blobby = "0.3"
criterion = "0.5"
ecdsa-core = { version = "=0.17.0-pre.9", package = "ecdsa", default-features = false, features = ["dev"] }
ecdsa-core = { version = "=0.17.0-pre.9", package = "ecdsa", default-features = false, features = [
"dev",
] }
hex = "0.4.3"
hex-literal = "0.4"
num-bigint = "0.4"
Expand All @@ -43,7 +56,14 @@ rand_core = { version = "0.6", features = ["getrandom"] }
sha3 = { version = "=0.11.0-pre.4", default-features = false }

[features]
default = ["arithmetic", "ecdsa", "pkcs8", "precomputed-tables", "schnorr", "std"]
default = [
"arithmetic",
"ecdsa",
"pkcs8",
"precomputed-tables",
"schnorr",
"std",
]
alloc = ["ecdsa-core?/alloc", "elliptic-curve/alloc"]
std = ["alloc", "ecdsa-core?/std", "elliptic-curve/std", "once_cell?/std"]

Expand Down
44 changes: 42 additions & 2 deletions k256/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use cfg_if::cfg_if;

cfg_if! {
if #[cfg(target_pointer_width = "32")] {

if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {
mod field_8x32;
} else if #[cfg(target_pointer_width = "32")] {
mod field_10x26;
} else if #[cfg(target_pointer_width = "64")] {
mod field_5x52;
Expand All @@ -20,7 +23,9 @@ cfg_if! {
use field_impl::FieldElementImpl;
} else {
cfg_if! {
if #[cfg(target_pointer_width = "32")] {
if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {
use field_8x32::FieldElement8x32 as FieldElementImpl;
} else if #[cfg(target_pointer_width = "32")] {
use field_10x26::FieldElement10x26 as FieldElementImpl;
} else if #[cfg(target_pointer_width = "64")] {
use field_5x52::FieldElement5x52 as FieldElementImpl;
Expand Down Expand Up @@ -99,11 +104,31 @@ impl FieldElement {
FieldElementImpl::from_bytes(bytes).map(Self)
}

/// Attempts to parse the given byte array as an SEC1-encoded field element (in little-endian!).
/// Does not check the result for being in the correct range.
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
pub(crate) fn from_bytes_unchecked_le(bytes: &[u8; 32]) -> Self {
Self(FieldElementImpl::from_bytes_unchecked_le(bytes))
}

/// Convert a `u64` to a field element.
pub const fn from_u64(w: u64) -> Self {
Self(FieldElementImpl::from_u64(w))
}

/// Returns the SEC1 encoding (in little-endian!) of this field element.
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
pub fn to_bytes_le(self) -> FieldBytes {
self.0.normalize().to_bytes_le()
}

/// Convert a `i64` to a field element.
/// Returned value may be only weakly normalized.
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
pub const fn from_i64(w: i64) -> Self {
Self(FieldElementImpl::from_i64(w))
}

/// Returns the SEC1 encoding of this field element.
pub fn to_bytes(self) -> FieldBytes {
self.0.normalize().to_bytes()
Expand Down Expand Up @@ -140,6 +165,14 @@ impl FieldElement {

/// Returns 2*self.
/// Doubles the magnitude.
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
pub fn double(&self) -> Self {
self.mul_single(2)
}

/// Returns 2*self.
/// Doubles the magnitude.
#[cfg(not(all(target_os = "zkvm", target_arch = "riscv32")))]
pub fn double(&self) -> Self {
Self(self.0.add(&(self.0)))
}
Expand Down Expand Up @@ -361,6 +394,13 @@ impl From<u64> for FieldElement {
}
}

#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
impl From<i64> for FieldElement {
fn from(k: i64) -> Self {
Self(FieldElementImpl::from_i64(k))
}
}

impl PartialEq for FieldElement {
fn eq(&self, other: &Self) -> bool {
self.0.ct_eq(&(other.0)).into()
Expand Down
Loading