Skip to content

Commit

Permalink
Add fish_hash library and bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-if committed Jan 10, 2024
1 parent 9d202ab commit 163e411
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 2 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion ironfish-rust-nodejs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ crate-type = ["cdylib"]

[dependencies]
base64 = "0.13.0"
fish_hash = "0.1.0"
ironfish = { path = "../ironfish-rust" }
ironfish_mpc = { path = "../ironfish-mpc" }
napi = { version = "2.13.2", features = ["napi6"] }
napi-derive = "2.13.0"
ironfish_mpc = { path = "../ironfish-mpc" }

[build-dependencies]
napi-build = "2.0.1"
5 changes: 5 additions & 0 deletions ironfish-rust-nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export function wordsToSpendingKey(words: string, languageCode: LanguageCode): s
export function generateKeyFromPrivateKey(privateKey: string): Key
export function initializeSapling(): void
export function isValidPublicAddress(hexAddress: string): boolean
export class FishHashContext {
constructor(full: boolean)
prebuildDataset(threads: number): void
hash(header: Buffer): Buffer
}
export class BoxKeyPair {
constructor()
static fromHex(secretHex: string): BoxKeyPair
Expand Down
3 changes: 2 additions & 1 deletion ironfish-rust-nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { contribute, verifyTransform, KEY_LENGTH, NONCE_LENGTH, BoxKeyPair, randomBytes, boxMessage, unboxMessage, RollingFilter, initSignalHandler, triggerSegfault, ASSET_ID_LENGTH, ASSET_METADATA_LENGTH, ASSET_NAME_LENGTH, ASSET_LENGTH, Asset, NOTE_ENCRYPTION_KEY_LENGTH, MAC_LENGTH, ENCRYPTED_NOTE_PLAINTEXT_LENGTH, ENCRYPTED_NOTE_LENGTH, NoteEncrypted, PUBLIC_ADDRESS_LENGTH, RANDOMNESS_LENGTH, MEMO_LENGTH, AMOUNT_VALUE_LENGTH, DECRYPTED_NOTE_LENGTH, Note, PROOF_LENGTH, TRANSACTION_SIGNATURE_LENGTH, TRANSACTION_PUBLIC_KEY_RANDOMNESS_LENGTH, TRANSACTION_EXPIRATION_LENGTH, TRANSACTION_FEE_LENGTH, LATEST_TRANSACTION_VERSION, TransactionPosted, Transaction, verifyTransactions, LanguageCode, generateKey, spendingKeyToWords, wordsToSpendingKey, generateKeyFromPrivateKey, initializeSapling, FoundBlockResult, ThreadPoolHandler, isValidPublicAddress } = nativeBinding
const { FishHashContext, contribute, verifyTransform, KEY_LENGTH, NONCE_LENGTH, BoxKeyPair, randomBytes, boxMessage, unboxMessage, RollingFilter, initSignalHandler, triggerSegfault, ASSET_ID_LENGTH, ASSET_METADATA_LENGTH, ASSET_NAME_LENGTH, ASSET_LENGTH, Asset, NOTE_ENCRYPTION_KEY_LENGTH, MAC_LENGTH, ENCRYPTED_NOTE_PLAINTEXT_LENGTH, ENCRYPTED_NOTE_LENGTH, NoteEncrypted, PUBLIC_ADDRESS_LENGTH, RANDOMNESS_LENGTH, MEMO_LENGTH, AMOUNT_VALUE_LENGTH, DECRYPTED_NOTE_LENGTH, Note, PROOF_LENGTH, TRANSACTION_SIGNATURE_LENGTH, TRANSACTION_PUBLIC_KEY_RANDOMNESS_LENGTH, TRANSACTION_EXPIRATION_LENGTH, TRANSACTION_FEE_LENGTH, LATEST_TRANSACTION_VERSION, TransactionPosted, Transaction, verifyTransactions, LanguageCode, generateKey, spendingKeyToWords, wordsToSpendingKey, generateKeyFromPrivateKey, initializeSapling, FoundBlockResult, ThreadPoolHandler, isValidPublicAddress } = nativeBinding

module.exports.FishHashContext = FishHashContext
module.exports.contribute = contribute
module.exports.verifyTransform = verifyTransform
module.exports.KEY_LENGTH = KEY_LENGTH
Expand Down
37 changes: 37 additions & 0 deletions ironfish-rust-nodejs/src/fish_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use fish_hash::Context;
use napi::{bindgen_prelude::Buffer, JsBuffer};
use napi_derive::napi;

#[napi]
pub struct FishHashContext {
inner: Context,
}

#[napi]
impl FishHashContext {
#[napi(constructor)]
pub fn new(full: bool) -> Self {
Self {
inner: Context::new(full),
}
}

#[napi]
pub fn prebuild_dataset(&mut self, threads: u32) {
self.inner.prebuild_dataset(threads as usize)
}

#[napi]
pub fn hash(&mut self, header: JsBuffer) -> Buffer {
let bytes = header.into_value().unwrap();

let mut output = [0u8; 32];
fish_hash::hash(&mut output, &mut self.inner, bytes.as_ref());

Buffer::from(output.to_vec())
}
}
1 change: 1 addition & 0 deletions ironfish-rust-nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use napi_derive::napi;
use ironfish::mining;
use ironfish::sapling_bls12;

pub mod fish_hash;
pub mod mpc;
pub mod nacl;
pub mod rolling_filter;
Expand Down
20 changes: 20 additions & 0 deletions ironfish-rust-nodejs/tests/fish_hash.test.slow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { FishHashContext } from '..'

describe('FishHashContext', () => {
it('should be able to generate a new FishHash context', () => {
const context = new FishHashContext(false)
expect(context).toBeDefined()
})

it('should be able to hash a buffer', () => {
let data = Buffer.from('the quick brown fox jumps over the lazy dog')
const context = new FishHashContext(false)

const hash = context.hash(data)
expect(hash.toString('hex')).toEqual('6f4429716dc009d5d3b9775a4d6a5d58bccd9f73386bf88da7d5afdf5deb50f1')
})
})

0 comments on commit 163e411

Please sign in to comment.