Skip to content

Commit 39aab01

Browse files
leonardoaltpacheco
andauthored
Initial powdr acceleration (#3)
Co-authored-by: Leandro Pacheco <[email protected]>
1 parent 1bfea04 commit 39aab01

File tree

11 files changed

+910
-120
lines changed

11 files changed

+910
-120
lines changed

Diff for: Cargo.lock

+64-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: k256/Cargo.toml

+24-4
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,33 @@ rust-version = "1.81"
2020

2121
[dependencies]
2222
cfg-if = "1.0"
23-
elliptic-curve = { version = "0.14.0-rc.0", default-features = false, features = ["sec1"] }
23+
elliptic-curve = { version = "0.14.0-rc.0", default-features = false, features = [
24+
"sec1",
25+
] }
2426

2527
# optional dependencies
2628
once_cell = { version = "1.20", optional = true, default-features = false }
27-
ecdsa-core = { version = "=0.17.0-pre.9", package = "ecdsa", optional = true, default-features = false, features = ["der"] }
29+
ecdsa-core = { version = "=0.17.0-pre.9", package = "ecdsa", optional = true, default-features = false, features = [
30+
"der",
31+
] }
2832
hex-literal = { version = "0.4", optional = true }
2933
serdect = { version = "0.3.0-rc.0", optional = true, default-features = false }
3034
sha2 = { version = "=0.11.0-pre.4", optional = true, default-features = false }
3135
signature = { version = "=2.3.0-pre.4", optional = true }
3236

37+
[target.'cfg(all(target_os = "zkvm", target_arch = "riscv32"))'.dependencies]
38+
powdr-riscv-runtime = { git = "https://github.com/powdr-labs/powdr.git", tag = "v0.1.1", features = [
39+
"std",
40+
"getrandom",
41+
"allow_fake_rand",
42+
] }
43+
3344
[dev-dependencies]
3445
blobby = "0.3"
3546
criterion = "0.5"
36-
ecdsa-core = { version = "=0.17.0-pre.9", package = "ecdsa", default-features = false, features = ["dev"] }
47+
ecdsa-core = { version = "=0.17.0-pre.9", package = "ecdsa", default-features = false, features = [
48+
"dev",
49+
] }
3750
hex = "0.4.3"
3851
hex-literal = "0.4"
3952
num-bigint = "0.4"
@@ -43,7 +56,14 @@ rand_core = { version = "0.6", features = ["getrandom"] }
4356
sha3 = { version = "=0.11.0-pre.4", default-features = false }
4457

4558
[features]
46-
default = ["arithmetic", "ecdsa", "pkcs8", "precomputed-tables", "schnorr", "std"]
59+
default = [
60+
"arithmetic",
61+
"ecdsa",
62+
"pkcs8",
63+
"precomputed-tables",
64+
"schnorr",
65+
"std",
66+
]
4767
alloc = ["ecdsa-core?/alloc", "elliptic-curve/alloc"]
4868
std = ["alloc", "ecdsa-core?/std", "elliptic-curve/std", "once_cell?/std"]
4969

Diff for: k256/src/arithmetic/field.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use cfg_if::cfg_if;
66

77
cfg_if! {
8-
if #[cfg(target_pointer_width = "32")] {
8+
9+
if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {
10+
mod field_8x32;
11+
} else if #[cfg(target_pointer_width = "32")] {
912
mod field_10x26;
1013
} else if #[cfg(target_pointer_width = "64")] {
1114
mod field_5x52;
@@ -20,7 +23,9 @@ cfg_if! {
2023
use field_impl::FieldElementImpl;
2124
} else {
2225
cfg_if! {
23-
if #[cfg(target_pointer_width = "32")] {
26+
if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {
27+
use field_8x32::FieldElement8x32 as FieldElementImpl;
28+
} else if #[cfg(target_pointer_width = "32")] {
2429
use field_10x26::FieldElement10x26 as FieldElementImpl;
2530
} else if #[cfg(target_pointer_width = "64")] {
2631
use field_5x52::FieldElement5x52 as FieldElementImpl;
@@ -99,11 +104,31 @@ impl FieldElement {
99104
FieldElementImpl::from_bytes(bytes).map(Self)
100105
}
101106

107+
/// Attempts to parse the given byte array as an SEC1-encoded field element (in little-endian!).
108+
/// Does not check the result for being in the correct range.
109+
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
110+
pub(crate) fn from_bytes_unchecked_le(bytes: &[u8; 32]) -> Self {
111+
Self(FieldElementImpl::from_bytes_unchecked_le(bytes))
112+
}
113+
102114
/// Convert a `u64` to a field element.
103115
pub const fn from_u64(w: u64) -> Self {
104116
Self(FieldElementImpl::from_u64(w))
105117
}
106118

119+
/// Returns the SEC1 encoding (in little-endian!) of this field element.
120+
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
121+
pub fn to_bytes_le(self) -> FieldBytes {
122+
self.0.normalize().to_bytes_le()
123+
}
124+
125+
/// Convert a `i64` to a field element.
126+
/// Returned value may be only weakly normalized.
127+
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
128+
pub const fn from_i64(w: i64) -> Self {
129+
Self(FieldElementImpl::from_i64(w))
130+
}
131+
107132
/// Returns the SEC1 encoding of this field element.
108133
pub fn to_bytes(self) -> FieldBytes {
109134
self.0.normalize().to_bytes()
@@ -140,6 +165,14 @@ impl FieldElement {
140165

141166
/// Returns 2*self.
142167
/// Doubles the magnitude.
168+
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
169+
pub fn double(&self) -> Self {
170+
self.mul_single(2)
171+
}
172+
173+
/// Returns 2*self.
174+
/// Doubles the magnitude.
175+
#[cfg(not(all(target_os = "zkvm", target_arch = "riscv32")))]
143176
pub fn double(&self) -> Self {
144177
Self(self.0.add(&(self.0)))
145178
}
@@ -361,6 +394,13 @@ impl From<u64> for FieldElement {
361394
}
362395
}
363396

397+
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
398+
impl From<i64> for FieldElement {
399+
fn from(k: i64) -> Self {
400+
Self(FieldElementImpl::from_i64(k))
401+
}
402+
}
403+
364404
impl PartialEq for FieldElement {
365405
fn eq(&self, other: &Self) -> bool {
366406
self.0.ct_eq(&(other.0)).into()

0 commit comments

Comments
 (0)