mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-02-25 17:11:22 -05:00
Use hash_point_to_point from external repo
This commit is contained in:
parent
cfed03083c
commit
ccb808bdf7
36
Cargo.lock
generated
36
Cargo.lock
generated
@ -1371,6 +1371,16 @@ version = "1.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3"
|
||||
|
||||
[[package]]
|
||||
name = "hash_edwards_to_edwards"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/comit-network/hash_edwards_to_edwards#b994b2bee919bf8abeb6116c9ec1288251b66abc"
|
||||
dependencies = [
|
||||
"bindgen",
|
||||
"cc",
|
||||
"curve25519-dalek",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.9.1"
|
||||
@ -2202,16 +2212,11 @@ dependencies = [
|
||||
name = "monero-adaptor"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"bindgen",
|
||||
"cc",
|
||||
"curve25519-dalek",
|
||||
"hex 0.4.3",
|
||||
"monero",
|
||||
"nazgul",
|
||||
"rand 0.7.3",
|
||||
"tiny-keccak",
|
||||
"tokio 1.4.0",
|
||||
"anyhow",
|
||||
"curve25519-dalek",
|
||||
"hash_edwards_to_edwards",
|
||||
"rand 0.7.3",
|
||||
"tiny-keccak",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2302,17 +2307,6 @@ dependencies = [
|
||||
"unsigned-varint 0.7.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nazgul"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9dff2f512a99611eb14f9bbdcc69e900c93952c4cc94656d2278ea5aa294cec2"
|
||||
dependencies = [
|
||||
"curve25519-dalek",
|
||||
"digest 0.9.0",
|
||||
"rand_core 0.5.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nohash-hasher"
|
||||
version = "0.2.0"
|
||||
|
@ -5,15 +5,8 @@ authors = ["CoBloX Team <team@coblox.tech>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
monero = "0.11"
|
||||
anyhow = "1"
|
||||
hex = "0.4"
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "time", "macros", "sync", "process", "fs"] }
|
||||
curve25519-dalek = "3"
|
||||
rand = "0.7"
|
||||
nazgul = "0.1"
|
||||
tiny-keccak = "2"
|
||||
|
||||
[build-dependencies]
|
||||
bindgen = "0.58"
|
||||
cc = "1"
|
||||
tiny-keccak = { version = "2", features = ["keccak"] }
|
||||
hash_edwards_to_edwards = { git = "https://github.com/comit-network/hash_edwards_to_edwards" }
|
@ -1,15 +0,0 @@
|
||||
extern crate bindgen;
|
||||
|
||||
fn main() {
|
||||
// Tell cargo to invalidate the built crate whenever the wrapper changes
|
||||
println!("cargo:rerun-if-changed=depend/hash");
|
||||
|
||||
let mut base_config = cc::Build::new();
|
||||
base_config.include("depend/hash/include");
|
||||
base_config.file("depend/hash/hash.c");
|
||||
base_config.file("depend/hash/crypto-ops.c");
|
||||
base_config.file("depend/hash/keccak.c");
|
||||
base_config.compile("hash");
|
||||
|
||||
println!("cargo:rustc-link-lib=static=hash");
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,21 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include "include/keccak.h"
|
||||
#include "include/crypto-ops.h"
|
||||
|
||||
void hash_to_scalar(const uint8_t *in, uint8_t *md) {
|
||||
keccak(in, 32, md, 32);
|
||||
sc_reduce32(md);
|
||||
}
|
||||
|
||||
// Hash a key to p3 representation
|
||||
void hash_to_p3(const uint8_t *in, ge_p3 *hash8_p3) {
|
||||
uint8_t md[32];
|
||||
ge_p2 hash_p2;
|
||||
ge_p1p1 hash8_p1p1;
|
||||
|
||||
keccak(in, 32, md, 32);
|
||||
ge_fromfe_frombytes_vartime(&hash_p2, md);
|
||||
ge_mul8(&hash8_p1p1, &hash_p2);
|
||||
ge_p1p1_to_p3(hash8_p3, &hash8_p1p1);
|
||||
}
|
||||
|
@ -1,165 +0,0 @@
|
||||
// Copyright (c) 2014-2020, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
||||
|
||||
#pragma once
|
||||
|
||||
/* From fe.h */
|
||||
|
||||
typedef int32_t fe[10];
|
||||
|
||||
/* From ge.h */
|
||||
|
||||
typedef struct {
|
||||
fe X;
|
||||
fe Y;
|
||||
fe Z;
|
||||
} ge_p2;
|
||||
|
||||
typedef struct {
|
||||
fe X;
|
||||
fe Y;
|
||||
fe Z;
|
||||
fe T;
|
||||
} ge_p3;
|
||||
|
||||
typedef struct {
|
||||
fe X;
|
||||
fe Y;
|
||||
fe Z;
|
||||
fe T;
|
||||
} ge_p1p1;
|
||||
|
||||
typedef struct {
|
||||
fe yplusx;
|
||||
fe yminusx;
|
||||
fe xy2d;
|
||||
} ge_precomp;
|
||||
|
||||
typedef struct {
|
||||
fe YplusX;
|
||||
fe YminusX;
|
||||
fe Z;
|
||||
fe T2d;
|
||||
} ge_cached;
|
||||
|
||||
/* From ge_add.c */
|
||||
|
||||
void ge_add(ge_p1p1 *, const ge_p3 *, const ge_cached *);
|
||||
|
||||
/* From ge_double_scalarmult.c, modified */
|
||||
|
||||
typedef ge_cached ge_dsmp[8];
|
||||
extern const ge_precomp ge_Bi[8];
|
||||
void ge_dsm_precomp(ge_dsmp r, const ge_p3 *s);
|
||||
void ge_double_scalarmult_base_vartime(ge_p2 *, const unsigned char *, const ge_p3 *, const unsigned char *);
|
||||
void ge_triple_scalarmult_base_vartime(ge_p2 *, const unsigned char *, const unsigned char *, const ge_dsmp, const unsigned char *, const ge_dsmp);
|
||||
void ge_double_scalarmult_base_vartime_p3(ge_p3 *, const unsigned char *, const ge_p3 *, const unsigned char *);
|
||||
|
||||
/* From ge_frombytes.c, modified */
|
||||
|
||||
extern const fe fe_sqrtm1;
|
||||
extern const fe fe_d;
|
||||
int ge_frombytes_vartime(ge_p3 *, const unsigned char *);
|
||||
|
||||
/* From ge_p1p1_to_p2.c */
|
||||
|
||||
void ge_p1p1_to_p2(ge_p2 *, const ge_p1p1 *);
|
||||
|
||||
/* From ge_p1p1_to_p3.c */
|
||||
|
||||
void ge_p1p1_to_p3(ge_p3 *, const ge_p1p1 *);
|
||||
|
||||
/* From ge_p2_dbl.c */
|
||||
|
||||
void ge_p2_dbl(ge_p1p1 *, const ge_p2 *);
|
||||
|
||||
/* From ge_p3_to_cached.c */
|
||||
|
||||
extern const fe fe_d2;
|
||||
void ge_p3_to_cached(ge_cached *, const ge_p3 *);
|
||||
|
||||
/* From ge_p3_to_p2.c */
|
||||
|
||||
void ge_p3_to_p2(ge_p2 *, const ge_p3 *);
|
||||
|
||||
/* From ge_p3_tobytes.c */
|
||||
|
||||
void ge_p3_tobytes(unsigned char *, const ge_p3 *);
|
||||
|
||||
/* From ge_scalarmult_base.c */
|
||||
|
||||
extern const ge_precomp ge_base[32][8];
|
||||
void ge_scalarmult_base(ge_p3 *, const unsigned char *);
|
||||
|
||||
/* From ge_tobytes.c */
|
||||
|
||||
void ge_tobytes(unsigned char *, const ge_p2 *);
|
||||
|
||||
/* From sc_reduce.c */
|
||||
|
||||
void sc_reduce(unsigned char *);
|
||||
|
||||
/* New code */
|
||||
|
||||
void ge_scalarmult(ge_p2 *, const unsigned char *, const ge_p3 *);
|
||||
void ge_scalarmult_p3(ge_p3 *, const unsigned char *, const ge_p3 *);
|
||||
void ge_double_scalarmult_precomp_vartime(ge_p2 *, const unsigned char *, const ge_p3 *, const unsigned char *, const ge_dsmp);
|
||||
void ge_triple_scalarmult_precomp_vartime(ge_p2 *, const unsigned char *, const ge_dsmp, const unsigned char *, const ge_dsmp, const unsigned char *, const ge_dsmp);
|
||||
void ge_double_scalarmult_precomp_vartime2(ge_p2 *, const unsigned char *, const ge_dsmp, const unsigned char *, const ge_dsmp);
|
||||
void ge_double_scalarmult_precomp_vartime2_p3(ge_p3 *, const unsigned char *, const ge_dsmp, const unsigned char *, const ge_dsmp);
|
||||
void ge_mul8(ge_p1p1 *, const ge_p2 *);
|
||||
extern const fe fe_ma2;
|
||||
extern const fe fe_ma;
|
||||
extern const fe fe_fffb1;
|
||||
extern const fe fe_fffb2;
|
||||
extern const fe fe_fffb3;
|
||||
extern const fe fe_fffb4;
|
||||
extern const ge_p3 ge_p3_identity;
|
||||
extern const ge_p3 ge_p3_H;
|
||||
void ge_fromfe_frombytes_vartime(ge_p2 *, const unsigned char *);
|
||||
void sc_0(unsigned char *);
|
||||
void sc_reduce32(unsigned char *);
|
||||
void sc_add(unsigned char *, const unsigned char *, const unsigned char *);
|
||||
void sc_sub(unsigned char *, const unsigned char *, const unsigned char *);
|
||||
void sc_mulsub(unsigned char *, const unsigned char *, const unsigned char *, const unsigned char *);
|
||||
void sc_mul(unsigned char *, const unsigned char *, const unsigned char *);
|
||||
void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, const unsigned char *c);
|
||||
int sc_check(const unsigned char *);
|
||||
int sc_isnonzero(const unsigned char *); /* Doesn't normalize */
|
||||
|
||||
// internal
|
||||
uint64_t load_3(const unsigned char *in);
|
||||
uint64_t load_4(const unsigned char *in);
|
||||
void ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
|
||||
void fe_add(fe h, const fe f, const fe g);
|
||||
void fe_tobytes(unsigned char *, const fe);
|
||||
void fe_invert(fe out, const fe z);
|
||||
|
||||
int ge_p3_is_point_at_infinity(const ge_p3 *p);
|
@ -1,97 +0,0 @@
|
||||
// Copyright (c) 2014-2020, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(__cplusplus)
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "int-util.h"
|
||||
#include "warnings.h"
|
||||
|
||||
static inline void *padd(void *p, size_t i) {
|
||||
return (char *) p + i;
|
||||
}
|
||||
|
||||
static inline const void *cpadd(const void *p, size_t i) {
|
||||
return (const char *) p + i;
|
||||
}
|
||||
|
||||
PUSH_WARNINGS
|
||||
DISABLE_VS_WARNINGS(4267)
|
||||
static_assert(sizeof(size_t) == 4 || sizeof(size_t) == 8, "size_t must be 4 or 8 bytes long");
|
||||
static inline void place_length(uint8_t *buffer, size_t bufsize, size_t length) {
|
||||
if (sizeof(size_t) == 4) {
|
||||
*(uint32_t *) padd(buffer, bufsize - 4) = swap32be(length);
|
||||
} else {
|
||||
*(uint64_t *) padd(buffer, bufsize - 8) = swap64be(length);
|
||||
}
|
||||
}
|
||||
POP_WARNINGS
|
||||
|
||||
#pragma pack(push, 1)
|
||||
union hash_state {
|
||||
uint8_t b[200];
|
||||
uint64_t w[25];
|
||||
};
|
||||
#pragma pack(pop)
|
||||
static_assert(sizeof(union hash_state) == 200, "Invalid structure size");
|
||||
|
||||
void hash_permutation(union hash_state *state);
|
||||
void hash_process(union hash_state *state, const uint8_t *buf, size_t count);
|
||||
|
||||
#endif
|
||||
|
||||
enum {
|
||||
HASH_SIZE = 32,
|
||||
HASH_DATA_AREA = 136
|
||||
};
|
||||
|
||||
void cn_fast_hash(const void *data, size_t length, char *hash);
|
||||
void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int prehashed, uint64_t height);
|
||||
|
||||
void hash_extra_blake(const void *data, size_t length, char *hash);
|
||||
void hash_extra_groestl(const void *data, size_t length, char *hash);
|
||||
void hash_extra_jh(const void *data, size_t length, char *hash);
|
||||
void hash_extra_skein(const void *data, size_t length, char *hash);
|
||||
|
||||
void tree_hash(const char (*hashes)[HASH_SIZE], size_t count, char *root_hash);
|
||||
|
||||
#define RX_BLOCK_VERSION 12
|
||||
void rx_slow_hash_allocate_state(void);
|
||||
void rx_slow_hash_free_state(void);
|
||||
uint64_t rx_seedheight(const uint64_t height);
|
||||
void rx_seedheights(const uint64_t height, uint64_t *seed_height, uint64_t *next_height);
|
||||
void rx_slow_hash(const uint64_t mainheight, const uint64_t seedheight, const char *seedhash, const void *data, size_t length, char *hash, int miners, int is_alt);
|
||||
void rx_reorg(const uint64_t split_height);
|
@ -1,320 +0,0 @@
|
||||
// Copyright (c) 2014-2020, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#include <sys/param.h>
|
||||
#endif
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
#include <byteswap.h>
|
||||
#endif
|
||||
|
||||
#if defined(__sun) && defined(__SVR4)
|
||||
#include <endian.h>
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline uint32_t rol32(uint32_t x, int r) {
|
||||
static_assert(sizeof(uint32_t) == sizeof(unsigned int), "this code assumes 32-bit integers");
|
||||
return _rotl(x, r);
|
||||
}
|
||||
|
||||
static inline uint64_t rol64(uint64_t x, int r) {
|
||||
return _rotl64(x, r);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static inline uint32_t rol32(uint32_t x, int r) {
|
||||
return (x << (r & 31)) | (x >> (-r & 31));
|
||||
}
|
||||
|
||||
static inline uint64_t rol64(uint64_t x, int r) {
|
||||
return (x << (r & 63)) | (x >> (-r & 63));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static inline uint64_t hi_dword(uint64_t val) {
|
||||
return val >> 32;
|
||||
}
|
||||
|
||||
static inline uint64_t lo_dword(uint64_t val) {
|
||||
return val & 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t* product_hi) {
|
||||
// multiplier = ab = a * 2^32 + b
|
||||
// multiplicand = cd = c * 2^32 + d
|
||||
// ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d
|
||||
uint64_t a = hi_dword(multiplier);
|
||||
uint64_t b = lo_dword(multiplier);
|
||||
uint64_t c = hi_dword(multiplicand);
|
||||
uint64_t d = lo_dword(multiplicand);
|
||||
|
||||
uint64_t ac = a * c;
|
||||
uint64_t ad = a * d;
|
||||
uint64_t bc = b * c;
|
||||
uint64_t bd = b * d;
|
||||
|
||||
uint64_t adbc = ad + bc;
|
||||
uint64_t adbc_carry = adbc < ad ? 1 : 0;
|
||||
|
||||
// multiplier * multiplicand = product_hi * 2^64 + product_lo
|
||||
uint64_t product_lo = bd + (adbc << 32);
|
||||
uint64_t product_lo_carry = product_lo < bd ? 1 : 0;
|
||||
*product_hi = ac + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry;
|
||||
assert(ac <= *product_hi);
|
||||
|
||||
return product_lo;
|
||||
}
|
||||
|
||||
static inline uint64_t div_with_reminder(uint64_t dividend, uint32_t divisor, uint32_t* remainder) {
|
||||
dividend |= ((uint64_t)*remainder) << 32;
|
||||
*remainder = dividend % divisor;
|
||||
return dividend / divisor;
|
||||
}
|
||||
|
||||
// Long division with 2^32 base
|
||||
static inline uint32_t div128_32(uint64_t dividend_hi, uint64_t dividend_lo, uint32_t divisor, uint64_t* quotient_hi, uint64_t* quotient_lo) {
|
||||
uint64_t dividend_dwords[4];
|
||||
uint32_t remainder = 0;
|
||||
|
||||
dividend_dwords[3] = hi_dword(dividend_hi);
|
||||
dividend_dwords[2] = lo_dword(dividend_hi);
|
||||
dividend_dwords[1] = hi_dword(dividend_lo);
|
||||
dividend_dwords[0] = lo_dword(dividend_lo);
|
||||
|
||||
*quotient_hi = div_with_reminder(dividend_dwords[3], divisor, &remainder) << 32;
|
||||
*quotient_hi |= div_with_reminder(dividend_dwords[2], divisor, &remainder);
|
||||
*quotient_lo = div_with_reminder(dividend_dwords[1], divisor, &remainder) << 32;
|
||||
*quotient_lo |= div_with_reminder(dividend_dwords[0], divisor, &remainder);
|
||||
|
||||
return remainder;
|
||||
}
|
||||
|
||||
// Long divisor with 2^64 base
|
||||
void div128_64(uint64_t dividend_hi, uint64_t dividend_lo, uint64_t divisor, uint64_t* quotient_hi, uint64_t *quotient_lo, uint64_t *remainder_hi, uint64_t *remainder_lo);
|
||||
|
||||
static inline void add64clamp(uint64_t *value, uint64_t add)
|
||||
{
|
||||
static const uint64_t maxval = (uint64_t)-1;
|
||||
if (*value > maxval - add)
|
||||
*value = maxval;
|
||||
else
|
||||
*value += add;
|
||||
}
|
||||
|
||||
static inline void sub64clamp(uint64_t *value, uint64_t sub)
|
||||
{
|
||||
if (*value < sub)
|
||||
*value = 0;
|
||||
else
|
||||
*value -= sub;
|
||||
}
|
||||
|
||||
#define IDENT16(x) ((uint16_t) (x))
|
||||
#define IDENT32(x) ((uint32_t) (x))
|
||||
#define IDENT64(x) ((uint64_t) (x))
|
||||
|
||||
#define SWAP16(x) ((((uint16_t) (x) & 0x00ff) << 8) | \
|
||||
(((uint16_t) (x) & 0xff00) >> 8))
|
||||
#define SWAP32(x) ((((uint32_t) (x) & 0x000000ff) << 24) | \
|
||||
(((uint32_t) (x) & 0x0000ff00) << 8) | \
|
||||
(((uint32_t) (x) & 0x00ff0000) >> 8) | \
|
||||
(((uint32_t) (x) & 0xff000000) >> 24))
|
||||
#define SWAP64(x) ((((uint64_t) (x) & 0x00000000000000ff) << 56) | \
|
||||
(((uint64_t) (x) & 0x000000000000ff00) << 40) | \
|
||||
(((uint64_t) (x) & 0x0000000000ff0000) << 24) | \
|
||||
(((uint64_t) (x) & 0x00000000ff000000) << 8) | \
|
||||
(((uint64_t) (x) & 0x000000ff00000000) >> 8) | \
|
||||
(((uint64_t) (x) & 0x0000ff0000000000) >> 24) | \
|
||||
(((uint64_t) (x) & 0x00ff000000000000) >> 40) | \
|
||||
(((uint64_t) (x) & 0xff00000000000000) >> 56))
|
||||
|
||||
static inline uint16_t ident16(uint16_t x) { return x; }
|
||||
static inline uint32_t ident32(uint32_t x) { return x; }
|
||||
static inline uint64_t ident64(uint64_t x) { return x; }
|
||||
|
||||
#ifndef __OpenBSD__
|
||||
# if defined(__ANDROID__) && defined(__swap16) && !defined(swap16)
|
||||
# define swap16 __swap16
|
||||
# elif !defined(swap16)
|
||||
static inline uint16_t swap16(uint16_t x) {
|
||||
return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
|
||||
}
|
||||
# endif
|
||||
# if defined(__ANDROID__) && defined(__swap32) && !defined(swap32)
|
||||
# define swap32 __swap32
|
||||
# elif !defined(swap32)
|
||||
static inline uint32_t swap32(uint32_t x) {
|
||||
x = ((x & 0x00ff00ff) << 8) | ((x & 0xff00ff00) >> 8);
|
||||
return (x << 16) | (x >> 16);
|
||||
}
|
||||
# endif
|
||||
# if defined(__ANDROID__) && defined(__swap64) && !defined(swap64)
|
||||
# define swap64 __swap64
|
||||
# elif !defined(swap64)
|
||||
static inline uint64_t swap64(uint64_t x) {
|
||||
x = ((x & 0x00ff00ff00ff00ff) << 8) | ((x & 0xff00ff00ff00ff00) >> 8);
|
||||
x = ((x & 0x0000ffff0000ffff) << 16) | ((x & 0xffff0000ffff0000) >> 16);
|
||||
return (x << 32) | (x >> 32);
|
||||
}
|
||||
# endif
|
||||
#endif /* __OpenBSD__ */
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#define UNUSED __attribute__((unused))
|
||||
#else
|
||||
#define UNUSED
|
||||
#endif
|
||||
static inline void mem_inplace_ident(void *mem UNUSED, size_t n UNUSED) { }
|
||||
#undef UNUSED
|
||||
|
||||
static inline void mem_inplace_swap16(void *mem, size_t n) {
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++) {
|
||||
((uint16_t *) mem)[i] = swap16(((const uint16_t *) mem)[i]);
|
||||
}
|
||||
}
|
||||
static inline void mem_inplace_swap32(void *mem, size_t n) {
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++) {
|
||||
((uint32_t *) mem)[i] = swap32(((const uint32_t *) mem)[i]);
|
||||
}
|
||||
}
|
||||
static inline void mem_inplace_swap64(void *mem, size_t n) {
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++) {
|
||||
((uint64_t *) mem)[i] = swap64(((const uint64_t *) mem)[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void memcpy_ident16(void *dst, const void *src, size_t n) {
|
||||
memcpy(dst, src, 2 * n);
|
||||
}
|
||||
static inline void memcpy_ident32(void *dst, const void *src, size_t n) {
|
||||
memcpy(dst, src, 4 * n);
|
||||
}
|
||||
static inline void memcpy_ident64(void *dst, const void *src, size_t n) {
|
||||
memcpy(dst, src, 8 * n);
|
||||
}
|
||||
|
||||
static inline void memcpy_swap16(void *dst, const void *src, size_t n) {
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++) {
|
||||
((uint16_t *) dst)[i] = swap16(((const uint16_t *) src)[i]);
|
||||
}
|
||||
}
|
||||
static inline void memcpy_swap32(void *dst, const void *src, size_t n) {
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++) {
|
||||
((uint32_t *) dst)[i] = swap32(((const uint32_t *) src)[i]);
|
||||
}
|
||||
}
|
||||
static inline void memcpy_swap64(void *dst, const void *src, size_t n) {
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++) {
|
||||
((uint64_t *) dst)[i] = swap64(((const uint64_t *) src)[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# define LITTLE_ENDIAN 1234
|
||||
# define BIG_ENDIAN 4321
|
||||
# define BYTE_ORDER LITTLE_ENDIAN
|
||||
#endif
|
||||
|
||||
#if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN)
|
||||
static_assert(false, "BYTE_ORDER is undefined. Perhaps, GNU extensions are not enabled");
|
||||
#endif
|
||||
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
#define SWAP16LE IDENT16
|
||||
#define SWAP16BE SWAP16
|
||||
#define swap16le ident16
|
||||
#define swap16be swap16
|
||||
#define mem_inplace_swap16le mem_inplace_ident
|
||||
#define mem_inplace_swap16be mem_inplace_swap16
|
||||
#define memcpy_swap16le memcpy_ident16
|
||||
#define memcpy_swap16be memcpy_swap16
|
||||
#define SWAP32LE IDENT32
|
||||
#define SWAP32BE SWAP32
|
||||
#define swap32le ident32
|
||||
#define swap32be swap32
|
||||
#define mem_inplace_swap32le mem_inplace_ident
|
||||
#define mem_inplace_swap32be mem_inplace_swap32
|
||||
#define memcpy_swap32le memcpy_ident32
|
||||
#define memcpy_swap32be memcpy_swap32
|
||||
#define SWAP64LE IDENT64
|
||||
#define SWAP64BE SWAP64
|
||||
#define swap64le ident64
|
||||
#define swap64be swap64
|
||||
#define mem_inplace_swap64le mem_inplace_ident
|
||||
#define mem_inplace_swap64be mem_inplace_swap64
|
||||
#define memcpy_swap64le memcpy_ident64
|
||||
#define memcpy_swap64be memcpy_swap64
|
||||
#endif
|
||||
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
#define SWAP16BE IDENT16
|
||||
#define SWAP16LE SWAP16
|
||||
#define swap16be ident16
|
||||
#define swap16le swap16
|
||||
#define mem_inplace_swap16be mem_inplace_ident
|
||||
#define mem_inplace_swap16le mem_inplace_swap16
|
||||
#define memcpy_swap16be memcpy_ident16
|
||||
#define memcpy_swap16le memcpy_swap16
|
||||
#define SWAP32BE IDENT32
|
||||
#define SWAP32LE SWAP32
|
||||
#define swap32be ident32
|
||||
#define swap32le swap32
|
||||
#define mem_inplace_swap32be mem_inplace_ident
|
||||
#define mem_inplace_swap32le mem_inplace_swap32
|
||||
#define memcpy_swap32be memcpy_ident32
|
||||
#define memcpy_swap32le memcpy_swap32
|
||||
#define SWAP64BE IDENT64
|
||||
#define SWAP64LE SWAP64
|
||||
#define swap64be ident64
|
||||
#define swap64le swap64
|
||||
#define mem_inplace_swap64be mem_inplace_ident
|
||||
#define mem_inplace_swap64le mem_inplace_swap64
|
||||
#define memcpy_swap64be memcpy_ident64
|
||||
#define memcpy_swap64le memcpy_swap64
|
||||
#endif
|
@ -1,40 +0,0 @@
|
||||
// keccak.h
|
||||
// 19-Nov-11 Markku-Juhani O. Saarinen <mjos@iki.fi>
|
||||
|
||||
#ifndef KECCAK_H
|
||||
#define KECCAK_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef KECCAK_ROUNDS
|
||||
#define KECCAK_ROUNDS 24
|
||||
#endif
|
||||
|
||||
#ifndef ROTL64
|
||||
#define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y))))
|
||||
#endif
|
||||
|
||||
// SHA3 Algorithm context.
|
||||
typedef struct KECCAK_CTX
|
||||
{
|
||||
// 1600 bits algorithm hashing state
|
||||
uint64_t hash[25];
|
||||
// 1088-bit buffer for leftovers, block size = 136 B for 256-bit keccak
|
||||
uint64_t message[17];
|
||||
// count of bytes in the message[] buffer
|
||||
size_t rest;
|
||||
} KECCAK_CTX;
|
||||
|
||||
// compute a keccak hash (md) of given byte length from "in"
|
||||
void keccak(const uint8_t *in, size_t inlen, uint8_t *md, int mdlen);
|
||||
|
||||
// update the state
|
||||
void keccakf(uint64_t st[25], int norounds);
|
||||
|
||||
void keccak1600(const uint8_t *in, size_t inlen, uint8_t *md);
|
||||
|
||||
void keccak_init(KECCAK_CTX * ctx);
|
||||
void keccak_update(KECCAK_CTX * ctx, const uint8_t *in, size_t inlen);
|
||||
void keccak_finish(KECCAK_CTX * ctx, uint8_t *md);
|
||||
#endif
|
@ -1,30 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
|
||||
#define PUSH_WARNINGS __pragma(warning(push))
|
||||
#define POP_WARNINGS __pragma(warning(pop))
|
||||
#define DISABLE_VS_WARNINGS(w) __pragma(warning(disable: w))
|
||||
#define DISABLE_GCC_WARNING(w)
|
||||
#define DISABLE_CLANG_WARNING(w)
|
||||
#define DISABLE_GCC_AND_CLANG_WARNING(w)
|
||||
|
||||
#else
|
||||
|
||||
#include <boost/preprocessor/stringize.hpp>
|
||||
|
||||
#define PUSH_WARNINGS _Pragma("GCC diagnostic push")
|
||||
#define POP_WARNINGS _Pragma("GCC diagnostic pop")
|
||||
#define DISABLE_VS_WARNINGS(w)
|
||||
|
||||
#if defined(__clang__)
|
||||
#define DISABLE_GCC_WARNING(w)
|
||||
#define DISABLE_CLANG_WARNING DISABLE_GCC_AND_CLANG_WARNING
|
||||
#else
|
||||
#define DISABLE_GCC_WARNING DISABLE_GCC_AND_CLANG_WARNING
|
||||
#define DISABLE_CLANG_WARNING(w)
|
||||
#endif
|
||||
|
||||
#define DISABLE_GCC_AND_CLANG_WARNING(w) _Pragma(BOOST_PP_STRINGIZE(GCC diagnostic ignored BOOST_PP_STRINGIZE(-W##w)))
|
||||
|
||||
#endif
|
@ -1,210 +0,0 @@
|
||||
// keccak.c
|
||||
// 19-Nov-11 Markku-Juhani O. Saarinen <mjos@iki.fi>
|
||||
// A baseline Keccak (3rd round) implementation.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "include/int-util.h"
|
||||
#include "include/hash-ops.h"
|
||||
#include "include/keccak.h"
|
||||
|
||||
static void local_abort(const char *msg)
|
||||
{
|
||||
fprintf(stderr, "%s\n", msg);
|
||||
#ifdef NDEBUG
|
||||
_exit(1);
|
||||
#else
|
||||
abort();
|
||||
#endif
|
||||
}
|
||||
|
||||
const uint64_t keccakf_rndc[24] =
|
||||
{
|
||||
0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
|
||||
0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
|
||||
0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
|
||||
0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
|
||||
0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
|
||||
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
|
||||
0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
|
||||
0x8000000000008080, 0x0000000080000001, 0x8000000080008008
|
||||
};
|
||||
|
||||
const int keccakf_rotc[24] =
|
||||
{
|
||||
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
|
||||
27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
|
||||
};
|
||||
|
||||
const int keccakf_piln[24] =
|
||||
{
|
||||
10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
|
||||
15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
|
||||
};
|
||||
|
||||
// update the state with given number of rounds
|
||||
|
||||
void keccakf(uint64_t st[25], int rounds)
|
||||
{
|
||||
int i, j, round;
|
||||
uint64_t t, bc[5];
|
||||
|
||||
for (round = 0; round < rounds; round++) {
|
||||
|
||||
// Theta
|
||||
for (i = 0; i < 5; i++)
|
||||
bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20];
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1);
|
||||
for (j = 0; j < 25; j += 5)
|
||||
st[j + i] ^= t;
|
||||
}
|
||||
|
||||
// Rho Pi
|
||||
t = st[1];
|
||||
for (i = 0; i < 24; i++) {
|
||||
j = keccakf_piln[i];
|
||||
bc[0] = st[j];
|
||||
st[j] = ROTL64(t, keccakf_rotc[i]);
|
||||
t = bc[0];
|
||||
}
|
||||
|
||||
// Chi
|
||||
for (j = 0; j < 25; j += 5) {
|
||||
for (i = 0; i < 5; i++)
|
||||
bc[i] = st[j + i];
|
||||
for (i = 0; i < 5; i++)
|
||||
st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5];
|
||||
}
|
||||
|
||||
// Iota
|
||||
st[0] ^= keccakf_rndc[round];
|
||||
}
|
||||
}
|
||||
|
||||
// compute a keccak hash (md) of given byte length from "in"
|
||||
typedef uint64_t state_t[25];
|
||||
|
||||
void keccak(const uint8_t *in, size_t inlen, uint8_t *md, int mdlen)
|
||||
{
|
||||
state_t st;
|
||||
uint8_t temp[144];
|
||||
size_t i, rsiz, rsizw;
|
||||
|
||||
static_assert(HASH_DATA_AREA <= sizeof(temp), "Bad keccak preconditions");
|
||||
if (mdlen <= 0 || (mdlen > 100 && sizeof(st) != (size_t)mdlen))
|
||||
{
|
||||
local_abort("Bad keccak use");
|
||||
}
|
||||
|
||||
rsiz = sizeof(state_t) == mdlen ? HASH_DATA_AREA : 200 - 2 * mdlen;
|
||||
rsizw = rsiz / 8;
|
||||
|
||||
memset(st, 0, sizeof(st));
|
||||
|
||||
for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz) {
|
||||
for (i = 0; i < rsizw; i++) {
|
||||
uint64_t ina;
|
||||
memcpy(&ina, in + i * 8, 8);
|
||||
st[i] ^= swap64le(ina);
|
||||
}
|
||||
keccakf(st, KECCAK_ROUNDS);
|
||||
}
|
||||
|
||||
// last block and padding
|
||||
if (inlen + 1 >= sizeof(temp) || inlen > rsiz || rsiz - inlen + inlen + 1 >= sizeof(temp) || rsiz == 0 || rsiz - 1 >= sizeof(temp) || rsizw * 8 > sizeof(temp))
|
||||
{
|
||||
local_abort("Bad keccak use");
|
||||
}
|
||||
|
||||
if (inlen > 0)
|
||||
memcpy(temp, in, inlen);
|
||||
temp[inlen++] = 1;
|
||||
memset(temp + inlen, 0, rsiz - inlen);
|
||||
temp[rsiz - 1] |= 0x80;
|
||||
|
||||
for (i = 0; i < rsizw; i++)
|
||||
st[i] ^= swap64le(((uint64_t *) temp)[i]);
|
||||
|
||||
keccakf(st, KECCAK_ROUNDS);
|
||||
|
||||
if (((size_t)mdlen % sizeof(uint64_t)) != 0)
|
||||
{
|
||||
local_abort("Bad keccak use");
|
||||
}
|
||||
memcpy_swap64le(md, st, mdlen/sizeof(uint64_t));
|
||||
}
|
||||
|
||||
void keccak1600(const uint8_t *in, size_t inlen, uint8_t *md)
|
||||
{
|
||||
keccak(in, inlen, md, sizeof(state_t));
|
||||
}
|
||||
|
||||
#define KECCAK_FINALIZED 0x80000000
|
||||
#define KECCAK_BLOCKLEN 136
|
||||
#define KECCAK_WORDS 17
|
||||
#define KECCAK_DIGESTSIZE 32
|
||||
#define KECCAK_PROCESS_BLOCK(st, block) { \
|
||||
for (int i_ = 0; i_ < KECCAK_WORDS; i_++){ \
|
||||
((st))[i_] ^= swap64le(((block))[i_]); \
|
||||
}; \
|
||||
keccakf(st, KECCAK_ROUNDS); }
|
||||
|
||||
|
||||
void keccak_init(KECCAK_CTX * ctx){
|
||||
memset(ctx, 0, sizeof(KECCAK_CTX));
|
||||
}
|
||||
|
||||
void keccak_update(KECCAK_CTX * ctx, const uint8_t *in, size_t inlen){
|
||||
if (ctx->rest & KECCAK_FINALIZED) {
|
||||
local_abort("Bad keccak use");
|
||||
}
|
||||
|
||||
const size_t idx = ctx->rest;
|
||||
ctx->rest = (ctx->rest + inlen) % KECCAK_BLOCKLEN;
|
||||
|
||||
// fill partial block
|
||||
if (idx) {
|
||||
size_t left = KECCAK_BLOCKLEN - idx;
|
||||
memcpy((char*)ctx->message + idx, in, (inlen < left ? inlen : left));
|
||||
if (inlen < left) return;
|
||||
|
||||
KECCAK_PROCESS_BLOCK(ctx->hash, ctx->message);
|
||||
|
||||
in += left;
|
||||
inlen -= left;
|
||||
}
|
||||
|
||||
while (inlen >= KECCAK_BLOCKLEN) {
|
||||
memcpy(ctx->message, in, KECCAK_BLOCKLEN);
|
||||
|
||||
KECCAK_PROCESS_BLOCK(ctx->hash, ctx->message);
|
||||
in += KECCAK_BLOCKLEN;
|
||||
inlen -= KECCAK_BLOCKLEN;
|
||||
}
|
||||
if (inlen) {
|
||||
memcpy(ctx->message, in, inlen);
|
||||
}
|
||||
}
|
||||
|
||||
void keccak_finish(KECCAK_CTX * ctx, uint8_t *md){
|
||||
if (!(ctx->rest & KECCAK_FINALIZED))
|
||||
{
|
||||
// clear the rest of the data queue
|
||||
memset((char*)ctx->message + ctx->rest, 0, KECCAK_BLOCKLEN - ctx->rest);
|
||||
((char*)ctx->message)[ctx->rest] |= 0x01;
|
||||
((char*)ctx->message)[KECCAK_BLOCKLEN - 1] |= 0x80;
|
||||
|
||||
// process final block
|
||||
KECCAK_PROCESS_BLOCK(ctx->hash, ctx->message);
|
||||
ctx->rest = KECCAK_FINALIZED; // mark context as finalized
|
||||
}
|
||||
|
||||
static_assert(KECCAK_BLOCKLEN > KECCAK_DIGESTSIZE, "");
|
||||
static_assert(KECCAK_DIGESTSIZE % sizeof(uint64_t) == 0, "");
|
||||
if (md) {
|
||||
memcpy_swap64le(md, ctx->hash, KECCAK_DIGESTSIZE / sizeof(uint64_t));
|
||||
}
|
||||
}
|
@ -2,15 +2,11 @@
|
||||
#![allow(non_upper_case_globals)]
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
extern "C" {
|
||||
fn hash_to_p3(hash: *const u8, p3: *mut ge_p3);
|
||||
fn ge_p3_tobytes(bytes: *mut u8, hash8_p3: *const ge_p3);
|
||||
}
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
use anyhow::{bail, Result};
|
||||
use curve25519_dalek::constants::ED25519_BASEPOINT_POINT;
|
||||
use curve25519_dalek::edwards::{CompressedEdwardsY, EdwardsPoint};
|
||||
use curve25519_dalek::edwards::EdwardsPoint;
|
||||
use curve25519_dalek::scalar::Scalar;
|
||||
use hash_edwards_to_edwards::hash_point_to_point;
|
||||
use rand::rngs::OsRng;
|
||||
use std::convert::TryInto;
|
||||
use tiny_keccak::{Hasher, Keccak};
|
||||
@ -18,37 +14,6 @@ use tiny_keccak::{Hasher, Keccak};
|
||||
const RING_SIZE: usize = 11;
|
||||
const DOMAIN_TAG: &str = "CSLAG_c";
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
struct ge_p3 {
|
||||
X: [i32; 10],
|
||||
Y: [i32; 10],
|
||||
Z: [i32; 10],
|
||||
T: [i32; 10],
|
||||
}
|
||||
|
||||
pub fn hash_point_to_point(point: EdwardsPoint) -> Result<EdwardsPoint> {
|
||||
let bytes = point.compress();
|
||||
|
||||
let mut compressed = [0u8; 32];
|
||||
unsafe {
|
||||
let mut p3 = ge_p3 {
|
||||
X: [0; 10],
|
||||
Y: [0; 10],
|
||||
Z: [0; 10],
|
||||
T: [0; 10],
|
||||
};
|
||||
|
||||
hash_to_p3(bytes.as_bytes().as_ptr() as *const u8, &mut p3);
|
||||
ge_p3_tobytes(&mut compressed as *mut u8, &p3);
|
||||
};
|
||||
|
||||
let compressed = CompressedEdwardsY::from_slice(&compressed);
|
||||
let point = compressed.decompress().context("not y-coordinate")?;
|
||||
|
||||
Ok(point)
|
||||
}
|
||||
|
||||
fn challenge(
|
||||
s_i: Scalar,
|
||||
pk_i: EdwardsPoint,
|
||||
@ -58,7 +23,7 @@ fn challenge(
|
||||
) -> Result<Scalar> {
|
||||
let L_i = s_i * ED25519_BASEPOINT_POINT + h_prev * pk_i;
|
||||
|
||||
let H_p_pk_i = hash_point_to_point(pk_i)?;
|
||||
let H_p_pk_i = hash_point_to_point(pk_i);
|
||||
|
||||
let R_i = s_i * H_p_pk_i + h_prev * I;
|
||||
|
||||
@ -121,6 +86,7 @@ fn final_challenge(
|
||||
.enumerate()
|
||||
.fold(h_0, |h_prev, (i, s_i)| {
|
||||
let pk_i = ring[i + 1];
|
||||
// TODO: Do not unwrap here
|
||||
challenge(*s_i, pk_i, h_prev, I, keccak.clone()).unwrap()
|
||||
});
|
||||
|
||||
@ -222,7 +188,7 @@ impl Alice0 {
|
||||
let alpha_a = Scalar::random(&mut OsRng);
|
||||
|
||||
let p_k = ring[0];
|
||||
let H_p_pk = hash_point_to_point(p_k)?;
|
||||
let H_p_pk = hash_point_to_point(p_k);
|
||||
|
||||
let I_a = s_prime_a * H_p_pk;
|
||||
let I_hat_a = alpha_a * H_p_pk;
|
||||
@ -350,8 +316,8 @@ impl Bob0 {
|
||||
) -> Result<Self> {
|
||||
let alpha_b = Scalar::random(&mut OsRng);
|
||||
|
||||
let p_k = ring.first().unwrap();
|
||||
let H_p_pk = hash_point_to_point(*p_k)?;
|
||||
let p_k = ring[0];
|
||||
let H_p_pk = hash_point_to_point(p_k);
|
||||
|
||||
let I_b = s_b * H_p_pk;
|
||||
let I_hat_b = alpha_b * H_p_pk;
|
||||
@ -651,7 +617,7 @@ mod tests {
|
||||
let r_a = Scalar::random(&mut OsRng);
|
||||
let R_a = r_a * ED25519_BASEPOINT_POINT;
|
||||
|
||||
let pk_hashed_to_point = hash_point_to_point(pk).unwrap();
|
||||
let pk_hashed_to_point = hash_point_to_point(pk);
|
||||
|
||||
let R_prime_a = r_a * pk_hashed_to_point;
|
||||
|
||||
@ -686,19 +652,4 @@ mod tests {
|
||||
|
||||
assert!(sig.verify(ring, msg_to_sign).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hash_point_to_point() {
|
||||
let slice = hex::decode("a7fbdeeccb597c2d5fdaf2ea2e10cbfcd26b5740903e7f6d46bcbf9a90384fc6")
|
||||
.unwrap();
|
||||
let point = CompressedEdwardsY::from_slice(&slice).decompress().unwrap();
|
||||
|
||||
let actual = hash_point_to_point(point).unwrap();
|
||||
|
||||
let slice = hex::decode("f055ba2d0d9828ce2e203d9896bfda494d7830e7e3a27fa27d5eaa825a79a19c")
|
||||
.unwrap();
|
||||
let expected = CompressedEdwardsY::from_slice(&slice).decompress().unwrap();
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user