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

Risc0 Acceleration for Curve25519 #1

Merged
merged 27 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7e1f7fb
add risc0 acceleration
jjtny1 Jul 25, 2023
9c9a43f
modify ed25519 crate to work with zkvm
jjtny1 Jul 25, 2023
aa8f15b
rm authors
jjtny1 Jul 26, 2023
fff10e8
use risc0 modmul for negate
jjtny1 Jul 26, 2023
c007979
use single risc0 mul for negation of scalar
jjtny1 Jul 26, 2023
ab98850
remove usages of mul_internal/montgomery_reduce back to back code
jjtny1 Jul 26, 2023
abd9627
add improvements and lookup tables
jjtny1 Aug 4, 2023
c036905
use denormalized form
jjtny1 Aug 10, 2023
8083f6d
pr feedback
jjtny1 Aug 21, 2023
dfc2614
revert serde version change
jjtny1 Aug 21, 2023
442efd7
update comments
jjtny1 Aug 21, 2023
89e183b
rm elliptic-curve and fix some uses of denormalize for serialization
jjtny1 Aug 29, 2023
ba0f316
rm vector files and use include_bytes! macro instead
jjtny1 Sep 5, 2023
69d3430
fix overflow issue and potentially constant-timedness issue
nategraf Sep 8, 2023
326397c
fix a couple more overflow and normalization issues
nategraf Sep 8, 2023
9884d64
Merge pull request #1 from risc0/victor/joby/risc0-accel
jjtny1 Sep 12, 2023
ee8d7cd
pr feedback. fix usage of normalized/denormalized
jjtny1 Sep 12, 2023
59f1792
Merge branch 'joby/risc0-accel' of github.com:jjtny1/curve25519-dalek…
jjtny1 Sep 12, 2023
908b7d6
pr feedback
jjtny1 Sep 12, 2023
41981f1
supress warning for risc zero builds
jjtny1 Sep 12, 2023
c1471ef
Merge commit 'e94a5fe5ab1c124363504d7ebcf78f0db166362a' into joby/ris…
nategraf Sep 14, 2023
333cb51
address compiler warning
nategraf Sep 14, 2023
e722a2e
fix test build
nategraf Sep 14, 2023
2f1feee
fix normalization errors
nategraf Sep 14, 2023
6263e76
very minor edits and comments
nategraf Sep 14, 2023
078f430
remove rust-toolchain.toml
nategraf Sep 14, 2023
9fbd7c6
Merge pull request #2 from risc0/victor/merge-e94a5fe5ab1c124363504d7…
jjtny1 Sep 14, 2023
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
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ resolver = "2"
[profile.dev]
opt-level = 2

[patch.crates-io.crypto-bigint]
git = "https://github.com/risc0/RustCrypto-crypto-bigint"
tag = "v0.5.2-risc0"

[patch.crates-io.sha2]
git = "https://github.com/risc0/RustCrypto-hashes"
tag = "sha2-v0.10.6-risc0"

7 changes: 5 additions & 2 deletions curve25519-dalek/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ features = ["serde", "rand_core", "digest", "legacy_compatibility"]
[dev-dependencies]
sha2 = { version = "0.10", default-features = false }
bincode = "1"
criterion = { version = "0.4.0", features = ["html_reports"] }
hex = "0.4.2"
rand = "0.8"
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }

Expand All @@ -53,13 +51,18 @@ digest = { version = "0.10", default-features = false, optional = true }
subtle = { version = "2.3.0", default-features = false }
serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] }
zeroize = { version = "1", default-features = false, optional = true }
crypto-bigint = { version = "0.5", default-features = false, features = ["zeroize"] }
hex = "0.4.3"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you gate these behind a cfg(not(target_os = "zkvm"))? Goal being that building this fork for the host should be essentially the same as upstream

Copy link
Author

Choose a reason for hiding this comment

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

Do you mean cfg(target_os = "zkvm")

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, yes that's what I meant


[target.'cfg(target_arch = "x86_64")'.dependencies]
cpufeatures = "0.2.6"

[target.'cfg(curve25519_dalek_backend = "fiat")'.dependencies]
fiat-crypto = "0.1.19"

[target.'cfg(not(target_os = "zkvm"))'.dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
Copy link
Author

Choose a reason for hiding this comment

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

This library does not build in the zkvm. It's only used for benchmark tests


[features]
default = ["alloc", "precomputed-tables", "zeroize"]
alloc = ["zeroize?/alloc"]
Expand Down
54 changes: 47 additions & 7 deletions curve25519-dalek/src/backend/serial/curve_models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,25 @@ impl ProjectivePoint {
let XX = self.X.square();
let YY = self.Y.square();
let ZZ2 = self.Z.square2();
let X_plus_Y = &self.X + &self.Y;
let X_plus_Y_sq = X_plus_Y.square();
let YY_plus_XX = &YY + &XX;
let YY_minus_XX = &YY - &XX;

cfg_if::cfg_if! {
if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {
// According to https://en.wikipedia.org/wiki/Edwards_curve#Doubling,
// (x + y)^2 - x^2 - y^2 is used as an optimization for computing 2xy.
// However, multiplication is faster inside the zkvm so we compute
// 2xy directly instead.
let new_x = &(&FieldElement::TWO * &self.X) * &self.Y;
} else {
let X_plus_Y = &self.X + &self.Y;
let X_plus_Y_sq = X_plus_Y.square();
let new_x = &X_plus_Y_sq - &YY_plus_XX;
}
}

CompletedPoint {
X: &X_plus_Y_sq - &YY_plus_XX,
X: new_x,
Y: YY_plus_XX,
Z: YY_minus_XX,
T: &ZZ2 - &YY_minus_XX,
Expand Down Expand Up @@ -418,7 +430,14 @@ impl<'a, 'b> Add<&'b ProjectiveNielsPoint> for &'a EdwardsPoint {
let MM = &Y_minus_X * &other.Y_minus_X;
let TT2d = &self.T * &other.T2d;
let ZZ = &self.Z * &other.Z;
let ZZ2 = &ZZ + &ZZ;

cfg_if::cfg_if! {
if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {
let ZZ2 = &FieldElement::TWO * &ZZ;
} else {
let ZZ2 = &ZZ + &ZZ;
}
}

CompletedPoint {
X: &PP - &MM,
Expand All @@ -440,7 +459,14 @@ impl<'a, 'b> Sub<&'b ProjectiveNielsPoint> for &'a EdwardsPoint {
let MP = &Y_minus_X * &other.Y_plus_X;
let TT2d = &self.T * &other.T2d;
let ZZ = &self.Z * &other.Z;
let ZZ2 = &ZZ + &ZZ;

cfg_if::cfg_if! {
if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {
let ZZ2 = &FieldElement::TWO * &ZZ;
} else {
let ZZ2 = &ZZ + &ZZ;
}
}

CompletedPoint {
X: &PM - &MP,
Expand All @@ -461,7 +487,14 @@ impl<'a, 'b> Add<&'b AffineNielsPoint> for &'a EdwardsPoint {
let PP = &Y_plus_X * &other.y_plus_x;
let MM = &Y_minus_X * &other.y_minus_x;
let Txy2d = &self.T * &other.xy2d;
let Z2 = &self.Z + &self.Z;

cfg_if::cfg_if! {
if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {
let Z2 = &FieldElement::TWO * &self.Z;
} else {
let Z2 = &self.Z + &self.Z;
}
}

CompletedPoint {
X: &PP - &MM,
Expand All @@ -482,7 +515,14 @@ impl<'a, 'b> Sub<&'b AffineNielsPoint> for &'a EdwardsPoint {
let PM = &Y_plus_X * &other.y_minus_x;
let MP = &Y_minus_X * &other.y_plus_x;
let Txy2d = &self.T * &other.xy2d;
let Z2 = &self.Z + &self.Z;

cfg_if::cfg_if! {
if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {
let Z2 = &FieldElement::TWO * &self.Z;
} else {
let Z2 = &self.Z + &self.Z;
}
}

CompletedPoint {
X: &PM - &MP,
Expand Down
4 changes: 4 additions & 0 deletions curve25519-dalek/src/backend/serial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ cfg_if! {
#[cfg(curve25519_dalek_bits = "64")]
pub mod fiat_u64;

} else if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {

pub mod risc0;

} else {

#[cfg(curve25519_dalek_bits = "32")]
Expand Down
Loading