forked from powdr-labs/powdr
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request powdr-labs#678 from powdr-labs/poseidon-gl-rust
poseidon gl rust
- Loading branch information
Showing
10 changed files
with
494 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,36 @@ | ||
// This is a dummy implementation of Poseidon hash, | ||
// which will be replaced with a call to the poseidon | ||
// coporocessor during compilation. | ||
// which will be replaced with a call to the Poseidon | ||
// coprocessor during compilation. | ||
// The function itself will be removed by the compiler | ||
// during the reachability analysis. | ||
extern "C" { | ||
fn poseidon_coprocessor(a: u32, b: u32) -> u32; | ||
fn poseidon_gl_coprocessor(data: *mut [u64; 12]); | ||
} | ||
|
||
pub fn poseidon_hash(a: u32, b: u32) -> u32 { | ||
unsafe { poseidon_coprocessor(a, b) } | ||
const GOLDILOCKS: u64 = 0xffffffff00000001; | ||
|
||
/// Calls the low level Poseidon coprocessor in PIL, where | ||
/// the last 4 elements are the "cap" | ||
/// and the return value is placed in data[0:4]. | ||
/// The safe version below also checks that each u64 element | ||
/// is less than the Goldilocks field. | ||
/// The unsafe version does not perform such checks. | ||
pub fn poseidon_gl(mut data: [u64; 12]) -> [u64; 4] { | ||
for &n in data.iter() { | ||
assert!(n < GOLDILOCKS); | ||
} | ||
|
||
unsafe { | ||
poseidon_gl_coprocessor(&mut data as *mut [u64; 12]); | ||
} | ||
|
||
[data[0], data[1], data[2], data[3]] | ||
} | ||
|
||
pub fn poseidon_gl_unsafe(mut data: [u64; 12]) -> [u64; 4] { | ||
unsafe { | ||
poseidon_gl_coprocessor(&mut data as *mut [u64; 12]); | ||
} | ||
|
||
[data[0], data[1], data[2], data[3]] | ||
} |
Oops, something went wrong.